2 * Copyright (C) 2004-2008 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file string.c \brief Memory allocation and string handling functions. */
15 * Adu's version of realloc().
17 * \param p Pointer to the memory block, may be \p NULL.
18 * \param size The desired new size.
20 * A wrapper for realloc(3). It calls \p exit(\p EXIT_FAILURE) on errors,
21 * i.e. there is no need to check the return value in the caller.
23 * \return A pointer to the newly allocated memory, which is suitably aligned
24 * for any kind of variable and may be different from \a p.
28 __must_check __malloc void *adu_realloc(void *p, size_t size)
31 * No need to check for NULL pointers: If p is NULL, the call
32 * to realloc is equivalent to malloc(size)
35 if (!(p = realloc(p, size))) {
36 EMERG_LOG("realloc failed (size = %zu), aborting\n",
44 * Adu's version of malloc().
46 * \param size The desired new size.
48 * A wrapper for malloc(3) which exits on errors.
50 * \return A pointer to the allocated memory, which is suitably aligned for any
55 __must_check __malloc void *adu_malloc(size_t size)
58 void *p = malloc(size);
61 EMERG_LOG("malloc failed (size = %zu), aborting\n",
69 * Adu's version of calloc().
71 * \param size The desired new size.
73 * A wrapper for calloc(3) which exits on errors.
75 * \return A pointer to the allocated and zeroed-out memory, which is suitably
76 * aligned for any kind of variable.
80 __must_check __malloc void *adu_calloc(size_t size)
82 void *ret = adu_malloc(size);
89 * Adu's version of strdup().
91 * \param s The string to be duplicated.
93 * A wrapper for strdup(3). It calls \p exit(EXIT_FAILURE) on errors, i.e.
94 * there is no need to check the return value in the caller.
96 * \return A pointer to the duplicated string. If \p s was the NULL pointer,
97 * an pointer to an empty string is returned.
101 __must_check __malloc char *adu_strdup(const char *s)
105 if ((ret = strdup(s? s: "")))
107 EMERG_LOG("strdup failed, aborting\n");
112 * Allocate a sufficiently large string and print into it.
114 * \param fmt A usual format string.
116 * Produce output according to \p fmt. No artificial bound on the length of the
117 * resulting string is imposed.
119 * \return This function either returns a pointer to a string that must be
120 * freed by the caller or aborts without returning.
124 __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
130 p = adu_malloc(size);
133 /* Try to print in the allocated space. */
135 n = vsnprintf(p, size, fmt, ap);
137 /* If that worked, return the string. */
138 if (n > -1 && n < size)
140 /* Else try again with more space. */
141 if (n > -1) /* glibc 2.1 */
142 size = n + 1; /* precisely what is needed */
144 size *= 2; /* twice the old size */
145 p = adu_realloc(p, size);
151 * adu's version of strcat().
153 * \param a String to be appended to.
154 * \param b String to append.
156 * Append \p b to \p a.
158 * \return If \a a is \p NULL, return a pointer to a copy of \a b, i.e.
159 * adu_strcat(NULL, b) is equivalent to adu_strdup(b). If \a b is \p NULL,
160 * return \a a without making a copy of \a a. Otherwise, construct the
161 * concatenation \a c, free \a a (but not \a b) and return \a c.
165 __must_check __malloc char *adu_strcat(char *a, const char *b)
170 return adu_strdup(b);
173 tmp = make_message("%s%s", a, b);
178 /** \cond LLONG_MAX and LLONG_LIN might not be defined. */
180 #define LLONG_MAX (1 << (sizeof(long) - 1))
183 #define LLONG_MIN (-LLONG_MAX - 1LL)
188 * Convert a string to a 64-bit signed integer value.
190 * \param str The string to be converted.
191 * \param result Result pointer.
195 * \sa strtol(3), atoi(3).
197 __must_check int atoi64(const char *str, int64_t *result)
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 return -E_ATOI_OVERFLOW;
206 if (errno != 0 && tmp == 0) /* other error */
209 return -E_ATOI_NO_DIGITS;
210 if (*endptr != '\0') /* Further characters after number */
211 return -E_ATOI_JUNK_AT_END;
217 * Split a string and return pointers to its parts.
219 * \param args The string to be split.
220 * \param argv_ptr Pointer to the list of substrings.
221 * \param delim Delimiter.
223 * This function modifies \a args by replacing each occurrence of \a delim by
224 * zero. A \p NULL terminated array of pointers to char* is allocated dynamically
225 * and these pointers are initialized to point to the broken-up substrings
226 * within \a args. A pointer to this array is returned via \a argv_ptr.
228 * \return The number of substrings found in \a args.
230 unsigned split_args(char *args, char *** const argv_ptr, const char *delim)
236 p = args + strspn(args, delim);
238 i = strcspn(p, delim);
243 p += strspn(p, delim);
245 *argv_ptr = adu_malloc((n + 1) * sizeof(char *));
248 p = args + strspn(args, delim);
251 j = strcspn(p, delim);
254 p += strcspn(p, delim);
258 p += strspn(p, delim);
267 static int get_next_word(const char *line, char **word)
269 enum line_state_flags {LSF_HAVE_WORD = 1, LSF_BACKSLASH = 2,
275 out = adu_malloc(strlen(line) + 1);
278 for (in = line; *in; in++) {
281 if (state & LSF_BACKSLASH) /* \\ */
283 state |= LSF_BACKSLASH;
284 state |= LSF_HAVE_WORD;
288 if (state & LSF_BACKSLASH) { /* \n or \t */
289 *out++ = (*in == 'n')? '\n' : '\t';
290 state &= ~LSF_BACKSLASH;
295 if (state & LSF_BACKSLASH) /* \" */
297 if (state & LSF_QUOTE) {
301 state |= LSF_HAVE_WORD;
307 if (state & LSF_BACKSLASH)
309 if (state & LSF_QUOTE)
311 if (state & LSF_HAVE_WORD)
313 /* ignore space at the beginning */
317 state |= LSF_HAVE_WORD;
319 state &= ~LSF_BACKSLASH;
322 if (!(state & LSF_HAVE_WORD))
324 ret = -ERRNO_TO_ERROR(EINVAL);
325 if (state & LSF_BACKSLASH) {
326 ERROR_LOG("trailing backslash\n");
329 if (state & LSF_QUOTE) {
330 ERROR_LOG("unmatched quote character\n");
343 * Free an array of words created by create_argv().
345 * \param argv A pointer previously obtained by \ref create_argv().
347 void free_argv(char **argv)
351 for (i = 0; argv[i]; i++)
357 * Split a line into words which are separated by whitespace.
359 * In contrast to gengetopt's string parser, double quotes, backslash-escaped
360 * characters and special characters like \p \\n are honored. The result
361 * contains pointers to copies of the words contained in \a line and has to be
362 * freed by using \ref free_argv().
364 * \param line The line to be split.
365 * \param result The array of words is returned here.
367 * \return Number of words in \a line, negative on errors.
369 int create_argv(const char *line, char ***result)
371 char *word, **argv = adu_malloc(2 * sizeof(char *));
375 argv[0] = adu_strdup(line);
376 for (p = line, num_words = 1; ; p += ret, num_words++) {
377 ret = get_next_word(p, &word);
382 argv = adu_realloc(argv, (num_words + 2) * sizeof(char*));
383 argv[num_words] = word;
385 argv[num_words] = NULL;
389 while (num_words > 0)
390 free(argv[--num_words]);
395 char *absolute_path(const char *path)
400 if (!path || !path[0])
403 return adu_strdup(path);
409 * The result of pathconf(3) may be huge and unsuitable for mallocing
410 * memory. OTOH pathconf(3) may return -1 to signify that PATH_MAX is
413 path_max = pathconf(name, _PC_PATH_MAX);
414 if (path_max <= 0 || path_max >= 4096)
417 cwd = adu_malloc(path_max);
418 if (!getcwd(cwd, path_max)) {
422 ap = make_message("%s/%s", cwd, path);