Define XOPEN_SOURCE and __EXTENSIONS__ on Solaris.
[paraslash.git] / string.c
1 /*
2 * Copyright (C) 2004-2007 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 "para.h"
10 #include "string.h"
11
12 #include <sys/time.h> /* gettimeofday */
13 #include <pwd.h>
14 #include <sys/utsname.h> /* uname() */
15 #include <string.h>
16
17 #include "error.h"
18
19 /**
20 * paraslash's version of realloc()
21 *
22 * \param p pointer to the memory block, may be NULL
23 * \param size desired new size
24 *
25 * A wrapper for realloc(3). It calls \p exit(\p EXIT_FAILURE) on errors,
26 * i.e. there is no need to check the return value in the caller.
27 *
28 * \return A pointer to the newly allocated memory, which is suitably aligned
29 * for any kind of variable and may be different from \p p.
30 *
31 * \sa realloc(3)
32 */
33 __must_check __malloc void *para_realloc(void *p, size_t size)
34 {
35 /*
36 * No need to check for NULL pointers: If p is NULL, the call
37 * to realloc is equivalent to malloc(size)
38 */
39 assert(size);
40 if (!(p = realloc(p, size))) {
41 PARA_EMERG_LOG("realloc failed (size = %zu), aborting\n",
42 size);
43 exit(EXIT_FAILURE);
44 }
45 return p;
46 }
47
48 /**
49 * paraslash's version of malloc()
50 *
51 * \param size desired new size
52 *
53 * A wrapper for malloc(3) which exits on errors.
54 *
55 * \return A pointer to the allocated memory, which is suitably aligned for any
56 * kind of variable.
57 *
58 * \sa malloc(3)
59 */
60 __must_check __malloc void *para_malloc(size_t size)
61 {
62 assert(size);
63 void *p = malloc(size);
64
65 if (!p) {
66 PARA_EMERG_LOG("malloc failed (size = %zu), aborting\n",
67 size);
68 exit(EXIT_FAILURE);
69 }
70 return p;
71 }
72
73 /**
74 * paraslash's version of calloc()
75 *
76 * \param size desired new size
77 *
78 * A wrapper for calloc(3) which exits on errors.
79 *
80 * \return A pointer to the allocated and zeroed-out memory, which is suitably
81 * aligned for any kind of variable.
82 *
83 * \sa calloc(3)
84 */
85 __must_check __malloc void *para_calloc(size_t size)
86 {
87 void *ret = para_malloc(size);
88
89 memset(ret, 0, size);
90 return ret;
91 }
92
93 /**
94 * paraslash's version of strdup()
95 *
96 * \param s string to be duplicated
97 *
98 * A wrapper for strdup(3). It calls \p exit(EXIT_FAILURE) on errors, i.e.
99 * there is no need to check the return value in the caller.
100 *
101 * \return A pointer to the duplicated string. If \p s was the NULL pointer,
102 * an pointer to an empty string is returned.
103 *
104 * \sa strdup(3)
105 */
106 __must_check __malloc char *para_strdup(const char *s)
107 {
108 char *ret;
109
110 if ((ret = strdup(s? s: "")))
111 return ret;
112 PARA_EMERG_LOG("%s", "strdup failed, aborting\n");
113 exit(EXIT_FAILURE);
114 }
115
116 /**
117 * allocate a sufficiently large string and print into it
118 *
119 * \param fmt usual format string
120 *
121 * Produce output according to \p fmt. No artificial bound on the length of the
122 * resulting string is imposed.
123 *
124 * \return This function either returns a pointer to a string that must be
125 * freed by the caller or aborts without returning.
126 *
127 * \sa printf(3)
128 */
129 __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
130 {
131 char *msg;
132
133 PARA_VSPRINTF(fmt, msg);
134 return msg;
135 }
136
137 /**
138 * paraslash's version of strcat()
139 *
140 * \param a string to be appended to
141 * \param b string to append
142 *
143 * Append \p b to \p a.
144 *
145 * \return If \p a is NULL, return a pointer to a copy of \p b, i.e.
146 * para_strcat(NULL, b) is equivalent to para_strdup(b). If \p b is NULL,
147 * return \p a without making a copy of \p a. Otherwise, construct the
148 * concatenation \p c, free \p a (but not \p b) and return \p c.
149 *
150 * \sa strcat(3)
151 */
152 __must_check __malloc char *para_strcat(char *a, const char *b)
153 {
154 char *tmp;
155
156 if (!a)
157 return para_strdup(b);
158 if (!b)
159 return a;
160 tmp = make_message("%s%s", a, b);
161 free(a);
162 return tmp;
163 }
164
165 /**
166 * paraslash's version of dirname()
167 *
168 * \param name pointer to the full path
169 *
170 * Compute the directory component of \p name
171 *
172 * \return If \p name is \รพ NULL or the empty string, return \p NULL.
173 * Otherwise, Make a copy of \p name and return its directory component. Caller
174 * is responsible to free the result.
175 */
176 __must_check __malloc char *para_dirname(const char *name)
177 {
178 char *p, *ret;
179
180 if (!name || !*name)
181 return NULL;
182 ret = para_strdup(name);
183 p = strrchr(ret, '/');
184 if (!p)
185 *ret = '\0';
186 else
187 *p = '\0';
188 return ret;
189 }
190
191 /**
192 * paraslash's version of basename()
193 *
194 * \param name Pointer to the full path
195 *
196 * Compute the filename component of \p name
197 *
198 * \return If \p name is \p NULL or the empty string, return \p NULL,
199 * Otherwise, make a copy of \p name and return its filename component. Caller
200 * is responsible to free the result.
201 */
202 __must_check __malloc char *para_basename(const char *name)
203 {
204 char *p;
205
206 if (!name || !*name)
207 return NULL;
208 p = strrchr(name, '/');
209 if (!p)
210 return para_strdup(name);
211 p++;
212 if (!*p)
213 return NULL;
214 return para_strdup(p);
215 }
216
217 /**
218 * cut trailing newline
219 *
220 * \param buf the string to be chopped.
221 *
222 * Replace the last character in \p buf by zero if it is euqal to
223 * the newline character.
224 */
225 void chop(char *buf)
226 {
227 int n = strlen(buf);
228 if (!n)
229 return;
230 if (buf[n - 1] == '\n')
231 buf[n - 1] = '\0';
232 }
233
234 /**
235 * get a random filename
236 *
237 * This is by no means a secure way to create temporary files in a hostile
238 * direcory like \p /tmp. However, it is OK to use for temp files, fifos,
239 * sockets that are created in ~/.paraslash. Result must be freed by the
240 * caller.
241 *
242 * \return a pointer to a random filename.
243 */
244 __must_check __malloc char *para_tmpname(void)
245 {
246 struct timeval now;
247 unsigned int seed;
248
249 gettimeofday(&now, NULL);
250 seed = now.tv_usec;
251 srand(seed);
252 return make_message("%08i", rand());
253 }
254
255 /**
256 * create unique temporary file
257 *
258 * \param template the template to be passed to mkstemp()
259 * \param mode the desired mode of the tempfile
260 *
261 * This wrapper for mkstemp additionally uses fchmod() to
262 * set the given mode of the tempfile if mkstemp() returned success.
263 *
264 * \return The file descriptor of the temp file just created on success.
265 * On errors, -E_MKSTEMP or -E_FCHMOD is returned.
266 */
267 __must_check int para_mkstemp(char *template, mode_t mode)
268 {
269 int tmp, fd = mkstemp(template);
270
271 if (fd < 0)
272 return -E_MKSTEMP;
273 tmp = fchmod(fd, mode);
274 if (tmp >= 0)
275 return fd;
276 close(fd);
277 unlink(template);
278 return -E_FCHMOD;
279 }
280
281 /**
282 * get the logname of the current user
283 *
284 * \return A dynammically allocated string that must be freed by the caller. On
285 * errors, the string "unknown user" is returned, i.e. this function never
286 * returns NULL.
287 *
288 * \sa getpwuid(3)
289 */
290 __must_check __malloc char *para_logname(void)
291 {
292 struct passwd *pw = getpwuid(getuid());
293 return para_strdup(pw? pw->pw_name : "unknown_user");
294 }
295
296 /**
297 * get the home directory of the current user
298 *
299 * \return A dynammically allocated string that must be freed by the caller. If
300 * the home directory could not be found, this function returns "/tmp".
301 */
302 __must_check __malloc char *para_homedir(void)
303 {
304 struct passwd *pw = getpwuid(getuid());
305 return para_strdup(pw? pw->pw_dir : "/tmp");
306 }
307
308 /**
309 * split string and return pointers to its parts.
310 *
311 * \param args the string to be split
312 * \param argv_ptr pointer to the list of substrings
313 * \param delim delimiter
314 *
315 * This function modifies \a args by replacing each occurance of \a delim by
316 * zero. A \p NULL-terminated array of pointers to char* is allocated dynamically
317 * and these pointers are initialized to point to the broken-up substrings
318 * within \a args. A pointer to this array is returned via \a argv_ptr. It's OK
319 * to call this function with \a args \a == \p NULL.
320 *
321 * \return The number of substrings found in \a args.
322 */
323 __must_check unsigned split_args(char *args, char *** const argv_ptr, const char *delim)
324 {
325 char *p = args;
326 char **argv;
327 size_t n = 0, i, j;
328
329 p = args + strspn(args, delim);
330 for (;;) {
331 i = strcspn(p, delim);
332 if (!i)
333 break;
334 p += i;
335 n++;
336 p += strspn(p, delim);
337 }
338 *argv_ptr = para_malloc((n + 1) * sizeof(char *));
339 argv = *argv_ptr;
340 i = 0;
341 p = args + strspn(args, delim);
342 while (p) {
343 argv[i] = p;
344 j = strcspn(p, delim);
345 if (!j)
346 break;
347 p += strcspn(p, delim);
348 if (*p) {
349 *p = '\0';
350 p++;
351 p += strspn(p, delim);
352 }
353 i++;
354 }
355 argv[n] = NULL;
356 return n;
357 }
358
359 /**
360 * ensure that file descriptors 0, 1, and 2 are valid
361 *
362 * Common approach that opens /dev/null until it gets a file descriptor greater
363 * than two.
364 *
365 * \sa okir's Black Hats Manual.
366 */
367 void valid_fd_012(void)
368 {
369 while (1) {
370 int fd;
371
372 fd = open("/dev/null", O_RDWR);
373 if (fd < 0)
374 exit(EXIT_FAILURE);
375 if (fd > 2) {
376 close(fd);
377 break;
378 }
379 }
380 }
381
382 /**
383 * get the own hostname
384 *
385 * \return A dynammically allocated string containing the hostname.
386 *
387 * \sa uname(2)
388 */
389 __malloc char *para_hostname(void)
390 {
391 struct utsname u;
392
393 uname(&u);
394 return para_strdup(u.nodename);
395 }
396
397 /**
398 * Used to distinguish between read-only and read-write mode.
399 *
400 * \sa for_each_line(), for_each_line_ro().
401 */
402 enum for_each_line_modes{
403 /** Activate read-only mode. */
404 LINE_MODE_RO,
405 /** Activate read-write mode. */
406 LINE_MODE_RW
407 };
408
409 static int for_each_complete_line(enum for_each_line_modes mode, char *buf,
410 size_t size, line_handler_t *line_handler, void *private_data)
411 {
412 char *start = buf, *end;
413 int ret, i, num_lines = 0;
414
415 // PARA_NOTICE_LOG("buf: %s\n", buf);
416 while (start < buf + size) {
417 char *next_null;
418 char *next_cr;
419
420 next_cr = memchr(start, '\n', buf + size - start);
421 next_null = memchr(start, '\0', buf + size - start);
422 if (!next_cr && !next_null)
423 break;
424 if (next_cr && next_null) {
425 end = next_cr < next_null? next_cr : next_null;
426 } else if (next_null) {
427 end = next_null;
428 } else
429 end = next_cr;
430 num_lines++;
431 if (!line_handler) {
432 start = ++end;
433 continue;
434 }
435 if (mode == LINE_MODE_RO) {
436 size_t s = end - start;
437 char *b = para_malloc(s + 1);
438 memcpy(b, start, s);
439 b[s] = '\0';
440 // PARA_NOTICE_LOG("b: %s, start: %s\n", b, start);
441 ret = line_handler(b, private_data);
442 free(b);
443 } else {
444 *end = '\0';
445 ret = line_handler(start, private_data);
446 }
447 if (ret < 0)
448 return ret;
449 start = ++end;
450 }
451 if (!line_handler || mode == LINE_MODE_RO)
452 return num_lines;
453 i = buf + size - start;
454 if (i && i != size)
455 memmove(buf, start, i);
456 return i;
457 }
458
459 /**
460 * Call a custom function for each complete line.
461 *
462 * \param buf The buffer containing data seperated by newlines.
463 * \param size The number of bytes in \a buf.
464 * \param line_handler The custom function.
465 * \param private_data Pointer passed to \a line_handler.
466 *
467 * If \p line_handler is \p NULL, the function returns the number of complete
468 * lines in \p buf. Otherwise, \p line_handler is called for each complete
469 * line in \p buf. The first argument to \p line_handler is the current line,
470 * and \p private_data is passed as the second argument. The function returns
471 * if \p line_handler returns a negative value or no more lines are in the
472 * buffer. The rest of the buffer (last chunk containing an incomplete line)
473 * is moved to the beginning of the buffer.
474 *
475 * \return If \p line_handler is not \p NULL, this function returns the number
476 * of bytes not handled to \p line_handler on success, or the negative return
477 * value of the \p line_handler on errors.
478 *
479 * \sa for_each_line_ro().
480 */
481 int for_each_line(char *buf, size_t size, line_handler_t *line_handler,
482 void *private_data)
483 {
484 return for_each_complete_line(LINE_MODE_RW, buf, size, line_handler,
485 private_data);
486 }
487
488 /**
489 * Call a custom function for each complete line.
490 *
491 * \param buf Same meaning as in \p for_each_line().
492 * \param size Same meaning as in \p for_each_line().
493 * \param line_handler Same meaning as in \p for_each_line().
494 * \param private_data Same meaning as in \p for_each_line().
495 *
496 * This function behaves like \p for_each_line(), but \a buf is left unchanged.
497 *
498 * \return On success, the function returns the number of complete lines in \p
499 * buf, otherwise the (negative) return value of \p line_handler is returned.
500 *
501 * \sa for_each_line().
502 */
503 int for_each_line_ro(char *buf, size_t size, line_handler_t *line_handler,
504 void *private_data)
505 {
506 return for_each_complete_line(LINE_MODE_RO, buf, size, line_handler,
507 private_data);
508 }
509
510 /**
511 * Safely print into a buffer at a given offset
512 *
513 * \param b Determines the buffer, its size, and the offset.
514 * \param fmt The format string.
515 *
516 * This function prints into the buffer given by \a b at the offset which is
517 * also given by \a b. If there is not enough space to hold the result, the
518 * buffer size is doubled until the underlying call to vsnprintf() succeeds.
519 * Upon return, the offset of \a b is adjusted accordingly so that subsequent
520 * calls to this function append data to what is already contained in the
521 * buffer.
522 *
523 * It's OK to call this function with \p b->buf being \p NULL. In this case, an
524 * initial buffer is allocated.
525 *
526 * \return The number of bytes printed into the buffer (not including the
527 * therminating \p NULL byte).
528 *
529 * \sa make_message(), vsnprintf(3).
530 */
531 __printf_2_3 int para_printf(struct para_buffer *b, const char *fmt, ...)
532 {
533 int ret;
534
535 if (!b->buf) {
536 b->buf = para_malloc(128);
537 b->size = 128;
538 b->offset = 0;
539 } else if (b->size <= b->offset + 1) {
540 b->size *= 2;
541 b->buf = para_realloc(b->buf, b->size);
542 }
543 while (1) {
544 char *p = b->buf + b->offset;
545 size_t size = b->size - b->offset;
546 va_list ap;
547 va_start(ap, fmt);
548 ret = vsnprintf(p, size, fmt, ap);
549 va_end(ap);
550 if (ret > -1 && ret < size) { /* success */
551 b->offset += ret;
552 break;
553 }
554 /* try again with more space */
555 b->size *= 2;
556 b->buf = para_realloc(b->buf, b->size);
557 }
558 return ret;
559 }
560
561 /** \cond LLONG_MAX and LLONG_LIN might not be defined. */
562 #ifndef LLONG_MAX
563 #define LLONG_MAX (1 << (sizeof(long) - 1))
564 #endif
565 #ifndef LLONG_MIN
566 #define LLONG_MIN (-LLONG_MAX - 1LL)
567 #endif
568 /** \endcond */
569
570 /**
571 * Convert a string to a 64-bit signed integer value.
572 *
573 * \param str The string to be converted.
574 * \param value Result pointer.
575 *
576 * \return Positive on success, negative on errors.
577 *
578 * \sa para_atoi32(), strtol(3), atoi(3).
579 */
580 int para_atoi64(const char *str, int64_t *value)
581 {
582 char *endptr;
583 long long tmp;
584
585 errno = 0; /* To distinguish success/failure after call */
586 tmp = strtoll(str, &endptr, 10);
587 if (errno == ERANGE && (tmp == LLONG_MAX || tmp == LLONG_MIN))
588 return -E_ATOI_OVERFLOW;
589 if (errno != 0 && tmp == 0) /* other error */
590 return -E_STRTOLL;
591 if (endptr == str)
592 return -E_ATOI_NO_DIGITS;
593 if (*endptr != '\0') /* Further characters after number */
594 return -E_ATOI_JUNK_AT_END;
595 *value = tmp;
596 return 1;
597 }
598
599 /**
600 * Convert a string to a 32-bit signed integer value.
601 *
602 * \param str The string to be converted.
603 * \param value Result pointer.
604 *
605 * \return Positive on success, negative on errors.
606 *
607 * \sa para_atoi64().
608 */
609 int para_atoi32(const char *str, int32_t *value)
610 {
611 int64_t tmp;
612 int ret;
613 const int32_t max = 2147483647;
614
615 ret = para_atoi64(str, &tmp);
616 if (ret < 0)
617 return ret;
618 if (tmp > max || tmp < -max - 1)
619 return -E_ATOI_OVERFLOW;
620 *value = tmp;
621 return 1;
622 }