]> git.tuebingen.mpg.de Git - adu.git/blob - string.c
Implement the interactive "run" command to run the select query.
[adu.git] / string.c
1 /*
2  * Copyright (C) 2004-2008 Andre Noll <maan@systemlinux.org>
3  *
4  * Licensed under the GPL v2. For licencing details see COPYING.
5  */
6
7 /** \file string.c Memory allocation and string handling functions. */
8
9 #include "adu.h"
10 #include "string.h"
11 #include <string.h>
12 #include "error.h"
13
14 /**
15  * Paraslash's version of realloc().
16  *
17  * \param p Pointer to the memory block, may be \p NULL.
18  * \param size The desired new size.
19  *
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.
22  *
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.
25  *
26  * \sa realloc(3).
27  */
28 __must_check __malloc void *adu_realloc(void *p, size_t size)
29 {
30         /*
31          * No need to check for NULL pointers: If p is NULL, the  call
32          * to realloc is equivalent to malloc(size)
33          */
34         assert(size);
35         if (!(p = realloc(p, size))) {
36                 EMERG_LOG("realloc failed (size = %zu), aborting\n",
37                         size);
38                 exit(EXIT_FAILURE);
39         }
40         return p;
41 }
42
43 /**
44  * Paraslash's version of malloc().
45  *
46  * \param size The desired new size.
47  *
48  * A wrapper for malloc(3) which exits on errors.
49  *
50  * \return A pointer to the allocated memory, which is suitably aligned for any
51  * kind of variable.
52  *
53  * \sa malloc(3).
54  */
55 __must_check __malloc void *adu_malloc(size_t size)
56 {
57         assert(size);
58         void *p = malloc(size);
59
60         if (!p) {
61                 EMERG_LOG("malloc failed (size = %zu),  aborting\n",
62                         size);
63                 exit(EXIT_FAILURE);
64         }
65         return p;
66 }
67
68 /**
69  * Paraslash's version of calloc().
70  *
71  * \param size The desired new size.
72  *
73  * A wrapper for calloc(3) which exits on errors.
74  *
75  * \return A pointer to the allocated and zeroed-out memory, which is suitably
76  * aligned for any kind of variable.
77  *
78  * \sa calloc(3)
79  */
80 __must_check __malloc void *adu_calloc(size_t size)
81 {
82         void *ret = adu_malloc(size);
83
84         memset(ret, 0, size);
85         return ret;
86 }
87
88 /**
89  * Paraslash's version of strdup().
90  *
91  * \param s The string to be duplicated.
92  *
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.
95  *
96  * \return A pointer to the duplicated string. If \p s was the NULL pointer,
97  * an pointer to an empty string is returned.
98  *
99  * \sa strdup(3)
100  */
101 __must_check __malloc char *adu_strdup(const char *s)
102 {
103         char *ret;
104
105         if ((ret = strdup(s? s: "")))
106                 return ret;
107         EMERG_LOG("strdup failed, aborting\n");
108         exit(EXIT_FAILURE);
109 }
110
111 /**
112  * Allocate a sufficiently large string and print into it.
113  *
114  * \param fmt A usual format string.
115  *
116  * Produce output according to \p fmt. No artificial bound on the length of the
117  * resulting string is imposed.
118  *
119  * \return This function either returns a pointer to a string that must be
120  * freed by the caller or aborts without returning.
121  *
122  * \sa printf(3).
123  */
124 __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
125 {
126         char *msg;
127
128         VSPRINTF(fmt, msg);
129         return msg;
130 }
131
132 /** \cond LLONG_MAX and LLONG_LIN might not be defined. */
133 #ifndef LLONG_MAX
134 #define LLONG_MAX (1 << (sizeof(long) - 1))
135 #endif
136 #ifndef LLONG_MIN
137 #define LLONG_MIN (-LLONG_MAX - 1LL)
138 #endif
139 /** \endcond */
140
141 /**
142  * Convert a string to a 64-bit signed integer value.
143  *
144  * \param str The string to be converted.
145  * \param value Result pointer.
146  *
147  * \return Standard.
148  *
149  * \sa strtol(3), atoi(3).
150  */
151 __must_check int atoi64(const char *str, int64_t *result)
152 {
153         char *endptr;
154         long long tmp;
155
156         errno = 0; /* To distinguish success/failure after call */
157         tmp = strtoll(str, &endptr, 10);
158         if (errno == ERANGE && (tmp == LLONG_MAX || tmp == LLONG_MIN))
159                 return -E_ATOI_OVERFLOW;
160         if (errno != 0 && tmp == 0) /* other error */
161                 return -E_STRTOLL;
162         if (endptr == str)
163                 return -E_ATOI_NO_DIGITS;
164         if (*endptr != '\0') /* Further characters after number */
165                 return -E_ATOI_JUNK_AT_END;
166         *result = tmp;
167         return 1;
168 }
169
170 /**
171  * Split string and return pointers to its parts.
172  *
173  * \param args The string to be split.
174  * \param argv_ptr Pointer to the list of substrings.
175  * \param delim Delimiter.
176  *
177  * This function modifies \a args by replacing each occurance of \a delim by
178  * zero. A \p NULL-terminated array of pointers to char* is allocated dynamically
179  * and these pointers are initialized to point to the broken-up substrings
180  * within \a args. A pointer to this array is returned via \a argv_ptr.
181  *
182  * \return The number of substrings found in \a args.
183  */
184 __must_check unsigned split_args(char *args, char *** const argv_ptr, const char *delim)
185 {
186         char *p = args;
187         char **argv;
188         size_t n = 0, i, j;
189
190         p = args + strspn(args, delim);
191         for (;;) {
192                 i = strcspn(p, delim);
193                 if (!i)
194                         break;
195                 p += i;
196                 n++;
197                 p += strspn(p, delim);
198         }
199         *argv_ptr = adu_malloc((n + 1) * sizeof(char *));
200         argv = *argv_ptr;
201         i = 0;
202         p = args + strspn(args, delim);
203         while (p) {
204                 argv[i] = p;
205                 j = strcspn(p, delim);
206                 if (!j)
207                         break;
208                 p += strcspn(p, delim);
209                 if (*p) {
210                         *p = '\0';
211                         p++;
212                         p += strspn(p, delim);
213                 }
214                 i++;
215         }
216         argv[n] = NULL;
217         return n;
218 }
219
220 static int check_uid_arg(const char *arg, uint32_t *uid)
221 {
222         const uint32_t max = ~0U;
223         /*
224          * we need an 64-bit int for string -> uid conversion because strtoll()
225          * returns a signed value.
226          */
227         int64_t val;
228         int ret = atoi64(arg, &val);
229
230         if (ret < 0)
231                 return ret;
232         if (val < 0 || val > max)
233                 return -ERRNO_TO_ERROR(EINVAL);
234         *uid = val;
235         return 1;
236 }
237
238 int parse_uid_range(const char *orig_arg, struct uid_range *ur)
239 {
240         int ret;
241         char *arg = adu_strdup(orig_arg), *p = strchr(arg, '-');
242
243         if (!p || p == arg) { /* -42 or 42 */
244                 ret = check_uid_arg(p? p + 1 : arg, &ur->high);
245                 if (ret < 0)
246                         goto out;
247                 ur->low = p? 0 : ur->high;
248                 ret = 1;
249                 goto out;
250         }
251         /* 42- or 42-4711 */
252         *p = '\0';
253         p++;
254         ret = check_uid_arg(arg, &ur->low);
255         if (ret < 0)
256                 goto out;
257         ur->high = ~0U;
258         if (*p) { /* 42-4711 */
259                 ret = check_uid_arg(p, &ur->high);
260                 if (ret < 0)
261                         goto out;
262         }
263         if (ur->low > ur->high)
264                 ret = -ERRNO_TO_ERROR(EINVAL);
265 out:
266         if (ret < 0)
267                 ERROR_LOG("bad uid option: %s\n", orig_arg);
268         else
269                 INFO_LOG("admissible uid range: %u - %u\n", ur->low,
270                         ur->high);
271         free(arg);
272         return ret;
273 }
274
275 int parse_uid_arg(const char *orig_arg, struct uid_range **ur)
276 {
277         char *arg, **argv;
278         unsigned n;
279         int i, ret = 1;
280
281         if (!orig_arg)
282                 return 0;
283         arg = adu_strdup(orig_arg);
284         n = split_args(arg, &argv, ",");
285         if (!n)
286                 return -E_SYNTAX;
287         *ur = adu_malloc((n + 1) * sizeof(struct uid_range));
288         for (i = 0; i < n; i++) {
289                 ret = parse_uid_range(argv[i], *ur + i);
290                 if (ret < 0)
291                         break;
292         }
293         free(arg);
294         if (ret < 0) {
295                 free(*ur);
296                 *ur = NULL;
297         }
298         /* an empty range indicates the end of the list */
299         (*ur)[n].low = 1;
300         (*ur)[n].high = 0;
301         return n;
302 }