Check args before initializing signal handlers.
[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 }