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 Memory allocation and string handling functions. */
15 * Paraslash'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 * Paraslash'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 * Paraslash'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 * Paraslash'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 value 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 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);
248 static int check_uid_arg(const char *arg, uint32_t *uid)
250 const uint32_t max = ~0U;
252 * we need an 64-bit int for string -> uid conversion because strtoll()
253 * returns a signed value.
256 int ret = atoi64(arg, &val);
260 if (val < 0 || val > max)
261 return -ERRNO_TO_ERROR(EINVAL);
266 int parse_uid_range(const char *orig_arg, struct uid_range *ur)
269 char *arg = adu_strdup(orig_arg), *p = strchr(arg, '-');
271 if (!p || p == arg) { /* -42 or 42 */
272 ret = check_uid_arg(p? p + 1 : arg, &ur->high);
275 ur->low = p? 0 : ur->high;
282 ret = check_uid_arg(arg, &ur->low);
286 if (*p) { /* 42-4711 */
287 ret = check_uid_arg(p, &ur->high);
291 if (ur->low > ur->high)
292 ret = -ERRNO_TO_ERROR(EINVAL);
295 ERROR_LOG("bad uid option: %s\n", orig_arg);
297 INFO_LOG("admissible uid range: %u - %u\n", ur->low,
303 int parse_uid_arg(const char *orig_arg, struct uid_range **ur)
311 arg = adu_strdup(orig_arg);
312 n = split_args(arg, &argv, ",");
315 *ur = adu_malloc((n + 1) * sizeof(struct uid_range));
316 for (i = 0; i < n; i++) {
317 ret = parse_uid_range(argv[i], *ur + i);
326 /* an empty range indicates the end of the list */