2 * Copyright (C) 2004-2008 Andre Noll <maan@systemlinux.org>
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, ...)
133 * adu's version of strcat().
135 * \param a String to be appended to.
136 * \param b String to append.
138 * Append \p b to \p a.
140 * \return If \a a is \p NULL, return a pointer to a copy of \a b, i.e.
141 * para_strcat(NULL, b) is equivalent to para_strdup(b). If \a b is \p NULL,
142 * return \a a without making a copy of \a a. Otherwise, construct the
143 * concatenation \a c, free \a a (but not \a b) and return \a c.
147 __must_check __malloc char *adu_strcat(char *a, const char *b)
152 return adu_strdup(b);
155 tmp = make_message("%s%s", a, b);
160 /** \cond LLONG_MAX and LLONG_LIN might not be defined. */
162 #define LLONG_MAX (1 << (sizeof(long) - 1))
165 #define LLONG_MIN (-LLONG_MAX - 1LL)
170 * Convert a string to a 64-bit signed integer value.
172 * \param str The string to be converted.
173 * \param result Result pointer.
177 * \sa strtol(3), atoi(3).
179 __must_check int atoi64(const char *str, int64_t *result)
184 errno = 0; /* To distinguish success/failure after call */
185 tmp = strtoll(str, &endptr, 10);
186 if (errno == ERANGE && (tmp == LLONG_MAX || tmp == LLONG_MIN))
187 return -E_ATOI_OVERFLOW;
188 if (errno != 0 && tmp == 0) /* other error */
191 return -E_ATOI_NO_DIGITS;
192 if (*endptr != '\0') /* Further characters after number */
193 return -E_ATOI_JUNK_AT_END;
199 * Split a string and return pointers to its parts.
201 * \param args The string to be split.
202 * \param argv_ptr Pointer to the list of substrings.
203 * \param delim Delimiter.
205 * This function modifies \a args by replacing each occurance of \a delim by
206 * zero. A \p NULL terminated array of pointers to char* is allocated dynamically
207 * and these pointers are initialized to point to the broken-up substrings
208 * within \a args. A pointer to this array is returned via \a argv_ptr.
210 * \return The number of substrings found in \a args.
212 __must_check unsigned split_args(char *args, char *** const argv_ptr, const char *delim)
218 p = args + strspn(args, delim);
220 i = strcspn(p, delim);
225 p += strspn(p, delim);
227 *argv_ptr = adu_malloc((n + 1) * sizeof(char *));
230 p = args + strspn(args, delim);
233 j = strcspn(p, delim);
236 p += strcspn(p, delim);
240 p += strspn(p, delim);
249 static int get_next_word(const char *line, char **word)
251 enum line_state_flags {LSF_HAVE_WORD = 1, LSF_BACKSLASH = 2,
257 out = adu_malloc(strlen(line) + 1);
260 for (in = line; *in; in++) {
263 if (state & LSF_BACKSLASH) /* \\ */
265 state |= LSF_BACKSLASH;
266 state |= LSF_HAVE_WORD;
270 if (state & LSF_BACKSLASH) { /* \n or \t */
271 *out++ = (*in == 'n')? '\n' : '\t';
272 state &= ~LSF_BACKSLASH;
277 if (state & LSF_BACKSLASH) /* \" */
279 if (state & LSF_QUOTE) {
283 state |= LSF_HAVE_WORD;
289 if (state & LSF_BACKSLASH)
291 if (state & LSF_QUOTE)
293 if (state & LSF_HAVE_WORD)
295 /* ignore space at the beginning */
299 state |= LSF_HAVE_WORD;
301 state &= ~LSF_BACKSLASH;
304 if (!(state & LSF_HAVE_WORD))
306 ret = -ERRNO_TO_ERROR(EINVAL);
307 if (state & LSF_BACKSLASH) {
308 ERROR_LOG("trailing backslash\n");
311 if (state & LSF_QUOTE) {
312 ERROR_LOG("unmatched quote character\n");
325 * Free an array of words created by create_argv().
327 * \param argv A pointer previously obtained by \ref create_argv().
329 void free_argv(char **argv)
333 for (i = 0; argv[i]; i++)
339 * Split a line into words which are separated by whitespace.
341 * In contrast to gengetopt's string parser, double quotes, backslash-escaped
342 * characters and special characters like \p \\n are honored. The result
343 * contains pointers to copies of the words contained in \a line and has to be
344 * freed by using \ref free_argv().
346 * \param line The line to be split.
347 * \param result The array of words is returned here.
349 * \return Number of words in \a line, negative on errors.
351 int create_argv(const char *line, char ***result)
353 char *word, **argv = adu_malloc(2 * sizeof(char *));
357 argv[0] = adu_strdup(line);
358 for (p = line, num_words = 1; ; p += ret, num_words++) {
359 ret = get_next_word(p, &word);
364 argv = adu_realloc(argv, (num_words + 2) * sizeof(char*));
365 argv[num_words] = word;
367 argv[num_words] = NULL;
371 while (num_words > 0)
372 free(argv[--num_words]);