Use only one global variable for snapshot creation pids.
[dss.git] / string.c
1 /*
2 * Copyright (C) 2004-2008 Andre Noll <maan@systemlinux.org>
3 *
4 * Licensed under the GPL v2. For licencing details see COPYING.
5 */
6 #include <string.h>
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <stdarg.h>
10 #include <assert.h>
11 #include <limits.h>
12 #include <errno.h>
13 #include <pwd.h>
14 #include <sys/types.h>
15 #include <unistd.h>
16
17
18 #include "gcc-compat.h"
19 #include "log.h"
20 #include "error.h"
21 #include "string.h"
22
23 /**
24 * Write a message to a dynamically allocated string.
25 *
26 * \param fmt Usual format string.
27 * \param p Result pointer.
28 *
29 * \sa printf(3). */
30 #define VSPRINTF(fmt, p) \
31 { \
32 int n; \
33 size_t size = 100; \
34 p = dss_malloc(size); \
35 while (1) { \
36 va_list ap; \
37 /* Try to print in the allocated space. */ \
38 va_start(ap, fmt); \
39 n = vsnprintf(p, size, fmt, ap); \
40 va_end(ap); \
41 /* If that worked, return the string. */ \
42 if (n > -1 && n < size) \
43 break; \
44 /* Else try again with more space. */ \
45 if (n > -1) /* glibc 2.1 */ \
46 size = n + 1; /* precisely what is needed */ \
47 else /* glibc 2.0 */ \
48 size *= 2; /* twice the old size */ \
49 p = dss_realloc(p, size); \
50 } \
51 }
52
53 /**
54 * dss' version of realloc().
55 *
56 * \param p Pointer to the memory block, may be \p NULL.
57 * \param size The desired new size.
58 *
59 * A wrapper for realloc(3). It calls \p exit(\p EXIT_FAILURE) on errors,
60 * i.e. there is no need to check the return value in the caller.
61 *
62 * \return A pointer to the newly allocated memory, which is suitably aligned
63 * for any kind of variable and may be different from \a p.
64 *
65 * \sa realloc(3).
66 */
67 __must_check __malloc void *dss_realloc(void *p, size_t size)
68 {
69 /*
70 * No need to check for NULL pointers: If p is NULL, the call
71 * to realloc is equivalent to malloc(size)
72 */
73 assert(size);
74 if (!(p = realloc(p, size))) {
75 DSS_EMERG_LOG("realloc failed (size = %zu), aborting\n",
76 size);
77 exit(EXIT_FAILURE);
78 }
79 return p;
80 }
81
82 /**
83 * dss' version of malloc().
84 *
85 * \param size The desired new size.
86 *
87 * A wrapper for malloc(3) which exits on errors.
88 *
89 * \return A pointer to the allocated memory, which is suitably aligned for any
90 * kind of variable.
91 *
92 * \sa malloc(3).
93 */
94 __must_check __malloc void *dss_malloc(size_t size)
95 {
96 assert(size);
97 void *p = malloc(size);
98
99 if (!p) {
100 DSS_EMERG_LOG("malloc failed (size = %zu), aborting\n",
101 size);
102 exit(EXIT_FAILURE);
103 }
104 return p;
105 }
106
107 /**
108 * dss' version of calloc().
109 *
110 * \param size The desired new size.
111 *
112 * A wrapper for calloc(3) which exits on errors.
113 *
114 * \return A pointer to the allocated and zeroed-out memory, which is suitably
115 * aligned for any kind of variable.
116 *
117 * \sa calloc(3)
118 */
119 __must_check __malloc void *dss_calloc(size_t size)
120 {
121 void *ret = dss_malloc(size);
122
123 memset(ret, 0, size);
124 return ret;
125 }
126
127 /**
128 * dss' version of strdup().
129 *
130 * \param s The string to be duplicated.
131 *
132 * A wrapper for strdup(3). It calls \p exit(EXIT_FAILURE) on errors, i.e.
133 * there is no need to check the return value in the caller.
134 *
135 * \return A pointer to the duplicated string. If \p s was the NULL pointer,
136 * an pointer to an empty string is returned.
137 *
138 * \sa strdup(3)
139 */
140
141 __must_check __malloc char *dss_strdup(const char *s)
142 {
143 char *ret;
144
145 if ((ret = strdup(s? s: "")))
146 return ret;
147 DSS_EMERG_LOG("strdup failed, aborting\n");
148 exit(EXIT_FAILURE);
149 }
150
151 /**
152 * Allocate a sufficiently large string and print into it.
153 *
154 * \param fmt A usual format string.
155 *
156 * Produce output according to \p fmt. No artificial bound on the length of the
157 * resulting string is imposed.
158 *
159 * \return This function either returns a pointer to a string that must be
160 * freed by the caller or aborts without returning.
161 *
162 * \sa printf(3).
163 */
164 __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
165 {
166 char *msg;
167
168 VSPRINTF(fmt, msg);
169 return msg;
170 }
171
172 /**
173 * Get the home directory of the current user.
174 *
175 * \return A dynammically allocated string that must be freed by the caller. If
176 * the home directory could not be found, this function returns "/tmp".
177 */
178 __must_check __malloc char *get_homedir(void)
179 {
180 struct passwd *pw = getpwuid(getuid());
181 return dss_strdup(pw? pw->pw_dir : "/tmp");
182 }
183
184 /**
185 * Convert a string to a 64-bit signed integer value.
186 *
187 * \param str The string to be converted.
188 * \param value Result pointer.
189 *
190 * \return Standard.
191 *
192 * \sa strtol(3), atoi(3).
193 */
194 int dss_atoi64(const char *str, int64_t *value)
195 {
196 char *endptr;
197 long long tmp;
198
199 errno = 0; /* To distinguish success/failure after call */
200 tmp = strtoll(str, &endptr, 10);
201 if (errno == ERANGE && (tmp == LLONG_MAX || tmp == LLONG_MIN))
202 return -E_ATOI_OVERFLOW;
203 if (errno != 0 && tmp == 0) /* other error */
204 return -E_STRTOLL;
205 if (endptr == str)
206 return -E_ATOI_NO_DIGITS;
207 if (*endptr != '\0') /* Further characters after number */
208 return -E_ATOI_JUNK_AT_END;
209 *value = tmp;
210 return 1;
211 }
212
213 /**
214 * Get the logname of the current user.
215 *
216 * \return A dynammically allocated string that must be freed by the caller. On
217 * errors, the string "unknown user" is returned, i.e. this function never
218 * returns \p NULL.
219 *
220 * \sa getpwuid(3).
221 */
222 __must_check __malloc char *dss_logname(void)
223 {
224 struct passwd *pw = getpwuid(getuid());
225 return dss_strdup(pw? pw->pw_name : "unknown_user");
226 }
227
228 /**
229 * Split string and return pointers to its parts.
230 *
231 * \param args The string to be split.
232 * \param argv_ptr Pointer to the list of substrings.
233 * \param delim Delimiter.
234 *
235 * This function modifies \a args by replacing each occurance of \a delim by
236 * zero. A \p NULL-terminated array of pointers to char* is allocated dynamically
237 * and these pointers are initialized to point to the broken-up substrings
238 * within \a args. A pointer to this array is returned via \a argv_ptr.
239 *
240 * \return The number of substrings found in \a args.
241 */
242 __must_check unsigned split_args(char *args, char *** const argv_ptr, const char *delim)
243 {
244 char *p = args;
245 char **argv;
246 size_t n = 0, i, j;
247
248 p = args + strspn(args, delim);
249 for (;;) {
250 i = strcspn(p, delim);
251 if (!i)
252 break;
253 p += i;
254 n++;
255 p += strspn(p, delim);
256 }
257 *argv_ptr = dss_malloc((n + 1) * sizeof(char *));
258 argv = *argv_ptr;
259 i = 0;
260 p = args + strspn(args, delim);
261 while (p) {
262 argv[i] = p;
263 j = strcspn(p, delim);
264 if (!j)
265 break;
266 p += strcspn(p, delim);
267 if (*p) {
268 *p = '\0';
269 p++;
270 p += strspn(p, delim);
271 }
272 i++;
273 }
274 argv[n] = NULL;
275 return n;
276 }