13 #include "gcc-compat.h"
19 * Write a message to a dynamically allocated string.
21 * \param fmt Usual format string.
22 * \param p Result pointer.
25 #define VSPRINTF(fmt, p) \
29 p = dss_malloc(size); \
32 /* Try to print in the allocated space. */ \
34 n = vsnprintf(p, size, fmt, ap); \
36 /* If that worked, return the string. */ \
37 if (n > -1 && n < size) \
39 /* Else try again with more space. */ \
40 if (n > -1) /* glibc 2.1 */ \
41 size = n + 1; /* precisely what is needed */ \
42 else /* glibc 2.0 */ \
43 size *= 2; /* twice the old size */ \
44 p = dss_realloc(p, size); \
49 * dss' version of realloc().
51 * \param p Pointer to the memory block, may be \p NULL.
52 * \param size The desired new size.
54 * A wrapper for realloc(3). It calls \p exit(\p EXIT_FAILURE) on errors,
55 * i.e. there is no need to check the return value in the caller.
57 * \return A pointer to the newly allocated memory, which is suitably aligned
58 * for any kind of variable and may be different from \a p.
62 __must_check __malloc void *dss_realloc(void *p, size_t size)
65 * No need to check for NULL pointers: If p is NULL, the call
66 * to realloc is equivalent to malloc(size)
69 if (!(p = realloc(p, size))) {
70 DSS_EMERG_LOG("realloc failed (size = %zu), aborting\n",
78 * dss' version of malloc().
80 * \param size The desired new size.
82 * A wrapper for malloc(3) which exits on errors.
84 * \return A pointer to the allocated memory, which is suitably aligned for any
89 __must_check __malloc void *dss_malloc(size_t size)
92 void *p = malloc(size);
95 DSS_EMERG_LOG("malloc failed (size = %zu), aborting\n",
103 * dss' version of calloc().
105 * \param size The desired new size.
107 * A wrapper for calloc(3) which exits on errors.
109 * \return A pointer to the allocated and zeroed-out memory, which is suitably
110 * aligned for any kind of variable.
114 __must_check __malloc void *dss_calloc(size_t size)
116 void *ret = dss_malloc(size);
118 memset(ret, 0, size);
123 * dss' version of strdup().
125 * \param s The string to be duplicated.
127 * A wrapper for strdup(3). It calls \p exit(EXIT_FAILURE) on errors, i.e.
128 * there is no need to check the return value in the caller.
130 * \return A pointer to the duplicated string. If \p s was the NULL pointer,
131 * an pointer to an empty string is returned.
136 __must_check __malloc char *dss_strdup(const char *s)
140 if ((ret = strdup(s? s: "")))
142 DSS_EMERG_LOG("strdup failed, aborting\n");
147 * Allocate a sufficiently large string and print into it.
149 * \param fmt A usual format string.
151 * Produce output according to \p fmt. No artificial bound on the length of the
152 * resulting string is imposed.
154 * \return This function either returns a pointer to a string that must be
155 * freed by the caller or aborts without returning.
159 __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
168 * Get the home directory of the current user.
170 * \return A dynammically allocated string that must be freed by the caller. If
171 * the home directory could not be found, this function returns "/tmp".
173 __must_check __malloc char *get_homedir(void)
175 struct passwd *pw = getpwuid(getuid());
176 return dss_strdup(pw? pw->pw_dir : "/tmp");
180 * Convert a string to a 64-bit signed integer value.
182 * \param str The string to be converted.
183 * \param value Result pointer.
187 * \sa strtol(3), atoi(3).
189 int dss_atoi64(const char *str, int64_t *value)
194 errno = 0; /* To distinguish success/failure after call */
195 tmp = strtoll(str, &endptr, 10);
196 if (errno == ERANGE && (tmp == LLONG_MAX || tmp == LLONG_MIN))
197 return -E_ATOI_OVERFLOW;
198 if (errno != 0 && tmp == 0) /* other error */
201 return -E_ATOI_NO_DIGITS;
202 if (*endptr != '\0') /* Further characters after number */
203 return -E_ATOI_JUNK_AT_END;
209 * Get the logname of the current user.
211 * \return A dynammically allocated string that must be freed by the caller. On
212 * errors, the string "unknown user" is returned, i.e. this function never
217 __must_check __malloc char *dss_logname(void)
219 struct passwd *pw = getpwuid(getuid());
220 return dss_strdup(pw? pw->pw_name : "unknown_user");
224 * Split string and return pointers to its parts.
226 * \param args The string to be split.
227 * \param argv_ptr Pointer to the list of substrings.
228 * \param delim Delimiter.
230 * This function modifies \a args by replacing each occurance of \a delim by
231 * zero. A \p NULL-terminated array of pointers to char* is allocated dynamically
232 * and these pointers are initialized to point to the broken-up substrings
233 * within \a args. A pointer to this array is returned via \a argv_ptr.
235 * \return The number of substrings found in \a args.
237 __must_check unsigned split_args(char *args, char *** const argv_ptr, const char *delim)
243 p = args + strspn(args, delim);
245 i = strcspn(p, delim);
250 p += strspn(p, delim);
252 *argv_ptr = dss_malloc((n + 1) * sizeof(char *));
255 p = args + strspn(args, delim);
258 j = strcspn(p, delim);
261 p += strcspn(p, delim);
265 p += strspn(p, delim);