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