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