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