summaryrefslogtreecommitdiff
path: root/str.c
blob: f507379021dade0e8ba401d631563d5a0dec2d85 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/* SPDX-License-Identifier: GPL-2.0 */

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h>
#include <assert.h>
#include <limits.h>
#include <errno.h>
#include <pwd.h>
#include <sys/types.h>
#include <unistd.h>


#include "gcc-compat.h"
#include "log.h"
#include "err.h"
#include "str.h"

/*
 * A wrapper for realloc(3) which exits on errors so that there is no need to
 * check the return value in the caller.
 */
__must_check __malloc void *dss_realloc(void *p, size_t size)
{
	/*
	 * No need to check for NULL pointers: If p is NULL, the  call
	 * to realloc is equivalent to malloc(size).
	 */
	assert(size);
	if (!(p = realloc(p, size))) {
		DSS_EMERG_LOG("realloc failed (size = %zu), aborting\n",
			size);
		exit(EXIT_FAILURE);
	}
	return p;
}

/* A wrapper for malloc(3) which exits on errors. */
__must_check __malloc void *dss_malloc(size_t size)
{
	void *p;
	assert(size);
	p = malloc(size);

	if (!p) {
		DSS_EMERG_LOG("malloc failed (size = %zu),  aborting\n",
			size);
		exit(EXIT_FAILURE);
	}
	return p;
}

/* Allocate zeroed-out memory. */
__must_check __malloc void *dss_calloc(size_t size)
{
	void *ret = dss_malloc(size);

	memset(ret, 0, size);
	return ret;
}

/*
 * A wrapper for strdup(3) which exits on errors. If NULL is passed as the
 * string to duplicate, a pointer to an empty string is returned.
 */
__must_check __malloc char *dss_strdup(const char *s)
{
	char *ret;

	if ((ret = strdup(s? s: "")))
		return ret;
	DSS_EMERG_LOG("strdup failed, aborting\n");
	exit(EXIT_FAILURE);
}

/*
 * Allocate a sufficiently large string and print into it. No artificial bound
 * on the length of the resulting string is imposed. Either returns a pointer
 * to a string which must be freed by the caller, or aborts without returning.
 */
__must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
{
	char *msg;
	size_t size = 100;

	msg = dss_malloc(size);
	while (1) {
		int n;
		va_list ap;

		/* Try to print in the allocated space. */
		va_start(ap, fmt);
		n = vsnprintf(msg, size, fmt, ap);
		va_end(ap);
		/* If that worked, return the string. */
		if (n < size)
			return msg;
		/* Else try again with more space. */
		size = n + 1; /* precisely what is needed */
		msg = dss_realloc(msg, size);
	}
}

/*
 * Get the home directory of the current user.
 *
 * Returns a dynamically allocated string that must be freed by the caller. If
 * the HOME environment variable is unset or empty, the function aborts.
 */
__must_check __malloc char *get_homedir(void)
{
	const char *home = getenv("HOME");
	if (home && *home)
		return dss_strdup(home);
	DSS_EMERG_LOG("fatal: HOME is unset or empty\n");
	exit(EXIT_FAILURE);
}

/* Convert a string to a 64-bit signed integer value. */
int dss_atoi64(const char *str, int64_t *value)
{
	char *endptr;
	long long tmp;

	errno = 0; /* To distinguish success/failure after call */
	tmp = strtoll(str, &endptr, 10);
	if (errno == ERANGE && (tmp == LLONG_MAX || tmp == LLONG_MIN))
		return -E_ATOI_OVERFLOW;
	if (errno != 0 && tmp == 0) /* other error */
		return -E_STRTOLL;
	if (endptr == str)
		return -E_ATOI_NO_DIGITS;
	if (*endptr != '\0') /* Further characters after number */
		return -E_ATOI_JUNK_AT_END;
	*value = tmp;
	return 1;
}

/*
 * Get the logname of the current user.
 *
 * Returns a dynamically allocated string that must be freed by the caller. On
 * errors, the string "unknown" is returned. This function never returns NULL.
 */
__must_check __malloc char *dss_logname(void)
{
	const char *logname = getenv("LOGNAME");
	if (!logname && !*logname)
		logname = "unknown";
	return dss_strdup(logname);
}

/*
 * Split a string and return pointers to its parts.
 *
 * This function modifies the string given by the first argument by replacing
 * all occurrences of space and '\t' characters by '\0'. A NULL-terminated
 * array of pointers to char * is allocated dynamically, and these pointers are
 * initialized to point to the broken-up substrings. A pointer to this array
 * is returned via the last argument.
 *
 * Returns the number of substrings found.
 */
unsigned split_args(char *args, char *** const argv_ptr)
{
	char *p;
	char **argv;
	size_t n = 0, i, j;
	const char delim[] = " \t";

	p = args + strspn(args, delim);
	for (;;) {
		i = strcspn(p, delim);
		if (!i)
			break;
		p += i;
		n++;
		p += strspn(p, delim);
	}
	*argv_ptr = dss_malloc((n + 1) * sizeof(char *));
	argv = *argv_ptr;
	i = 0;
	p = args + strspn(args, delim);
	while (p) {
		argv[i] = p;
		j = strcspn(p, delim);
		if (!j)
			break;
		p += strcspn(p, delim);
		if (*p) {
			*p = '\0';
			p++;
			p += strspn(p, delim);
		}
		i++;
	}
	argv[n] = NULL;
	return n;
}