]> git.tuebingen.mpg.de Git - dss.git/blob - str.c
Merge topic branch t/realpath into pu
[dss.git] / str.c
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #include <string.h>
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <stdarg.h>
6 #include <assert.h>
7 #include <limits.h>
8 #include <errno.h>
9 #include <pwd.h>
10 #include <sys/types.h>
11 #include <unistd.h>
12
13
14 #include "gcc-compat.h"
15 #include "log.h"
16 #include "err.h"
17 #include "str.h"
18
19 /**
20  * dss' version of realloc().
21  *
22  * \param p Pointer to the memory block, may be \p NULL.
23  * \param size The 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 \a p.
30  *
31  * \sa realloc(3).
32  */
33 __must_check __malloc void *dss_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                 DSS_EMERG_LOG(("realloc failed (size = %zu), aborting\n",
42                         size));
43                 exit(EXIT_FAILURE);
44         }
45         return p;
46 }
47
48 /**
49  * dss' version of malloc().
50  *
51  * \param size The 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 *dss_malloc(size_t size)
61 {
62         void *p;
63         assert(size);
64         p = malloc(size);
65
66         if (!p) {
67                 DSS_EMERG_LOG(("malloc failed (size = %zu),  aborting\n",
68                         size));
69                 exit(EXIT_FAILURE);
70         }
71         return p;
72 }
73
74 /**
75  * dss' version of calloc().
76  *
77  * \param size The desired new size.
78  *
79  * A wrapper for calloc(3) which exits on errors.
80  *
81  * \return A pointer to the allocated and zeroed-out memory, which is suitably
82  * aligned for any kind of variable.
83  *
84  * \sa calloc(3)
85  */
86 __must_check __malloc void *dss_calloc(size_t size)
87 {
88         void *ret = dss_malloc(size);
89
90         memset(ret, 0, size);
91         return ret;
92 }
93
94 /**
95  * dss' version of strdup().
96  *
97  * \param s The string to be duplicated.
98  *
99  * A wrapper for strdup(3). It calls \p exit(EXIT_FAILURE) on errors, i.e.
100  * there is no need to check the return value in the caller.
101  *
102  * \return A pointer to the duplicated string. If \p s was the NULL pointer,
103  * an pointer to an empty string is returned.
104  *
105  * \sa strdup(3)
106  */
107
108 __must_check __malloc char *dss_strdup(const char *s)
109 {
110         char *ret;
111
112         if ((ret = strdup(s? s: "")))
113                 return ret;
114         DSS_EMERG_LOG(("strdup failed, aborting\n"));
115         exit(EXIT_FAILURE);
116 }
117
118 /**
119  * Allocate a sufficiently large string and print into it.
120  *
121  * \param fmt A usual format string.
122  *
123  * Produce output according to \p fmt. No artificial bound on the length of the
124  * resulting string is imposed.
125  *
126  * \return This function either returns a pointer to a string that must be
127  * freed by the caller or aborts without returning.
128  *
129  * \sa printf(3).
130  */
131 __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
132 {
133         char *msg;
134         size_t size = 100;
135
136         msg = dss_malloc(size);
137         while (1) {
138                 int n;
139                 va_list ap;
140
141                 /* Try to print in the allocated space. */
142                 va_start(ap, fmt);
143                 n = vsnprintf(msg, size, fmt, ap);
144                 va_end(ap);
145                 /* If that worked, return the string. */
146                 if (n < size)
147                         return msg;
148                 /* Else try again with more space. */
149                 size = n + 1; /* precisely what is needed */
150                 msg = dss_realloc(msg, size);
151         }
152 }
153
154 /*
155  * Get the home directory of the current user.
156  *
157  * Returns a dynamically allocated string that must be freed by the caller. If
158  * the HOME environment variable is unset or empty, the function aborts.
159  */
160 __must_check __malloc char *get_homedir(void)
161 {
162         const char *home = getenv("HOME");
163         if (home && *home)
164                 return dss_strdup(home);
165         DSS_EMERG_LOG(("fatal: HOME is unset or empty\n"));
166         exit(EXIT_FAILURE);
167 }
168
169 /**
170  * Convert a string to a 64-bit signed integer value.
171  *
172  * \param str The string to be converted.
173  * \param value Result pointer.
174  *
175  * \return Standard.
176  *
177  * \sa strtol(3), atoi(3).
178  */
179 int dss_atoi64(const char *str, int64_t *value)
180 {
181         char *endptr;
182         long long tmp;
183
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 */
189                 return -E_STRTOLL;
190         if (endptr == str)
191                 return -E_ATOI_NO_DIGITS;
192         if (*endptr != '\0') /* Further characters after number */
193                 return -E_ATOI_JUNK_AT_END;
194         *value = tmp;
195         return 1;
196 }
197
198 /*
199  * Get the logname of the current user.
200  *
201  * Returns a dynamically allocated string that must be freed by the caller. On
202  * errors, the string "unknown" is returned. This function never returns NULL.
203  */
204 __must_check __malloc char *dss_logname(void)
205 {
206         const char *logname = getenv("LOGNAME");
207         if (!logname && !*logname)
208                 logname = "unknown";
209         return dss_strdup(logname);
210 }
211
212 /**
213  * Split string and return pointers to its parts.
214  *
215  * \param args The string to be split.
216  * \param argv_ptr Pointer to the list of substrings.
217  *
218  * This function modifies the string given by the first argument by replacing
219  * all occurrences of space and '\t' characters by '\0'. A NULL-terminated
220  * array of pointers to char * is allocated dynamically, and these pointers are
221  * initialized to point to the broken-up substrings.  A pointer to this array
222  * is returned via the last argument.
223  *
224  * \return The number of substrings found.
225  */
226 unsigned split_args(char *args, char *** const argv_ptr)
227 {
228         char *p;
229         char **argv;
230         size_t n = 0, i, j;
231         const char delim[] = " \t";
232
233         p = args + strspn(args, delim);
234         for (;;) {
235                 i = strcspn(p, delim);
236                 if (!i)
237                         break;
238                 p += i;
239                 n++;
240                 p += strspn(p, delim);
241         }
242         *argv_ptr = dss_malloc((n + 1) * sizeof(char *));
243         argv = *argv_ptr;
244         i = 0;
245         p = args + strspn(args, delim);
246         while (p) {
247                 argv[i] = p;
248                 j = strcspn(p, delim);
249                 if (!j)
250                         break;
251                 p += strcspn(p, delim);
252                 if (*p) {
253                         *p = '\0';
254                         p++;
255                         p += strspn(p, delim);
256                 }
257                 i++;
258         }
259         argv[n] = NULL;
260         return n;
261 }