index.html.in: Fix gitweb link.
[dss.git] / str.c
1 /*
2  * Copyright (C) 2004-2010 Andre Noll <maan@tuebingen.mpg.de>
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 "err.h"
21 #include "str.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         void *p;
97         assert(size);
98         p = malloc(size);
99
100         if (!p) {
101                 DSS_EMERG_LOG(("malloc failed (size = %zu),  aborting\n",
102                         size));
103                 exit(EXIT_FAILURE);
104         }
105         return p;
106 }
107
108 /**
109  * dss' version of calloc().
110  *
111  * \param size The desired new size.
112  *
113  * A wrapper for calloc(3) which exits on errors.
114  *
115  * \return A pointer to the allocated and zeroed-out memory, which is suitably
116  * aligned for any kind of variable.
117  *
118  * \sa calloc(3)
119  */
120 __must_check __malloc void *dss_calloc(size_t size)
121 {
122         void *ret = dss_malloc(size);
123
124         memset(ret, 0, size);
125         return ret;
126 }
127
128 /**
129  * dss' version of strdup().
130  *
131  * \param s The string to be duplicated.
132  *
133  * A wrapper for strdup(3). It calls \p exit(EXIT_FAILURE) on errors, i.e.
134  * there is no need to check the return value in the caller.
135  *
136  * \return A pointer to the duplicated string. If \p s was the NULL pointer,
137  * an pointer to an empty string is returned.
138  *
139  * \sa strdup(3)
140  */
141
142 __must_check __malloc char *dss_strdup(const char *s)
143 {
144         char *ret;
145
146         if ((ret = strdup(s? s: "")))
147                 return ret;
148         DSS_EMERG_LOG(("strdup failed, aborting\n"));
149         exit(EXIT_FAILURE);
150 }
151
152 /**
153  * Allocate a sufficiently large string and print into it.
154  *
155  * \param fmt A usual format string.
156  *
157  * Produce output according to \p fmt. No artificial bound on the length of the
158  * resulting string is imposed.
159  *
160  * \return This function either returns a pointer to a string that must be
161  * freed by the caller or aborts without returning.
162  *
163  * \sa printf(3).
164  */
165 __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
166 {
167         char *msg;
168
169         VSPRINTF(fmt, msg);
170         return msg;
171 }
172
173 /**
174  * Get the home directory of the current user.
175  *
176  * \return A dynammically allocated string that must be freed by the caller. If
177  * the home directory could not be found, this function returns "/tmp".
178  */
179 __must_check __malloc char *get_homedir(void)
180 {
181         struct passwd *pw = getpwuid(getuid());
182         return dss_strdup(pw? pw->pw_dir : "/tmp");
183 }
184
185 /**
186  * Convert a string to a 64-bit signed integer value.
187  *
188  * \param str The string to be converted.
189  * \param value Result pointer.
190  *
191  * \return Standard.
192  *
193  * \sa strtol(3), atoi(3).
194  */
195 int dss_atoi64(const char *str, int64_t *value)
196 {
197         char *endptr;
198         long long tmp;
199
200         errno = 0; /* To distinguish success/failure after call */
201         tmp = strtoll(str, &endptr, 10);
202         if (errno == ERANGE && (tmp == LLONG_MAX || tmp == LLONG_MIN))
203                 return -E_ATOI_OVERFLOW;
204         if (errno != 0 && tmp == 0) /* other error */
205                 return -E_STRTOLL;
206         if (endptr == str)
207                 return -E_ATOI_NO_DIGITS;
208         if (*endptr != '\0') /* Further characters after number */
209                 return -E_ATOI_JUNK_AT_END;
210         *value = tmp;
211         return 1;
212 }
213
214 /**
215  * Get the logname of the current user.
216  *
217  * \return A dynammically allocated string that must be freed by the caller. On
218  * errors, the string "unknown user" is returned, i.e. this function never
219  * returns \p NULL.
220  *
221  * \sa getpwuid(3).
222  */
223 __must_check __malloc char *dss_logname(void)
224 {
225         struct passwd *pw = getpwuid(getuid());
226         return dss_strdup(pw? pw->pw_name : "unknown_user");
227 }
228
229 /**
230  * Split string and return pointers to its parts.
231  *
232  * \param args The string to be split.
233  * \param argv_ptr Pointer to the list of substrings.
234  * \param delim Delimiter.
235  *
236  * This function modifies \a args by replacing each occurance of \a delim by
237  * zero. A \p NULL-terminated array of pointers to char* is allocated dynamically
238  * and these pointers are initialized to point to the broken-up substrings
239  * within \a args. A pointer to this array is returned via \a argv_ptr.
240  *
241  * \return The number of substrings found in \a args.
242  */
243 unsigned split_args(char *args, char *** const argv_ptr, const char *delim)
244 {
245         char *p = args;
246         char **argv;
247         size_t n = 0, i, j;
248
249         p = args + strspn(args, delim);
250         for (;;) {
251                 i = strcspn(p, delim);
252                 if (!i)
253                         break;
254                 p += i;
255                 n++;
256                 p += strspn(p, delim);
257         }
258         *argv_ptr = dss_malloc((n + 1) * sizeof(char *));
259         argv = *argv_ptr;
260         i = 0;
261         p = args + strspn(args, delim);
262         while (p) {
263                 argv[i] = p;
264                 j = strcspn(p, delim);
265                 if (!j)
266                         break;
267                 p += strcspn(p, delim);
268                 if (*p) {
269                         *p = '\0';
270                         p++;
271                         p += strspn(p, delim);
272                 }
273                 i++;
274         }
275         argv[n] = NULL;
276         return n;
277 }