Fix prototype of com_create().
[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 static int check_uid_arg(const char *arg, uint32_t *uid)
171 {
172 const uint32_t max = ~0U;
173 /*
174 * we need an 64-bit int for string -> uid conversion because strtoll()
175 * returns a signed value.
176 */
177 int64_t val;
178 int ret = atoi64(arg, &val);
179
180 if (ret < 0)
181 return ret;
182 if (val < 0 || val > max)
183 return -ERRNO_TO_ERROR(EINVAL);
184 *uid = val;
185 return 1;
186 }
187
188 int parse_uid_range(const char *orig_arg, struct uid_range *ur)
189 {
190 int ret;
191 char *arg = adu_strdup(orig_arg), *p = strchr(arg, '-');
192
193 if (!p || p == arg) { /* -42 or 42 */
194 ret = check_uid_arg(p? p + 1 : arg, &ur->high);
195 if (ret < 0)
196 goto out;
197 ur->low = p? 0 : ur->high;
198 ret = 1;
199 goto out;
200 }
201 /* 42- or 42-4711 */
202 *p = '\0';
203 p++;
204 ret = check_uid_arg(arg, &ur->low);
205 if (ret < 0)
206 goto out;
207 ur->high = ~0U;
208 if (*p) { /* 42-4711 */
209 ret = check_uid_arg(p, &ur->high);
210 if (ret < 0)
211 goto out;
212 }
213 if (ur->low > ur->high)
214 ret = -ERRNO_TO_ERROR(EINVAL);
215 out:
216 if (ret < 0)
217 ERROR_LOG("bad uid option: %s\n", orig_arg);
218 else
219 INFO_LOG("admissible uid range: %u - %u\n", ur->low,
220 ur->high);
221 free(arg);
222 return ret;
223 }
224