2 * Copyright (C) 2004-2010 Andre Noll <maan@tuebingen.mpg.de>
4 * Licensed under the GPL v2. For licencing details see COPYING.
14 #include <sys/types.h>
18 #include "gcc-compat.h"
24 * Write a message to a dynamically allocated string.
26 * \param fmt Usual format string.
27 * \param p Result pointer.
30 #define VSPRINTF(fmt, p) \
34 p = dss_malloc(size); \
37 /* Try to print in the allocated space. */ \
39 n = vsnprintf(p, size, fmt, ap); \
41 /* If that worked, return the string. */ \
42 if (n > -1 && n < size) \
44 /* Else try again with more space. */ \
45 if (n > -1) /* glibc 2.1 */ \
46 size = n + 1; /* precisely what is needed */ \
47 else /* glibc 2.0 */ \
48 size *= 2; /* twice the old size */ \
49 p = dss_realloc(p, size); \
54 * dss' version of realloc().
56 * \param p Pointer to the memory block, may be \p NULL.
57 * \param size The desired new size.
59 * A wrapper for realloc(3). It calls \p exit(\p EXIT_FAILURE) on errors,
60 * i.e. there is no need to check the return value in the caller.
62 * \return A pointer to the newly allocated memory, which is suitably aligned
63 * for any kind of variable and may be different from \a p.
67 __must_check __malloc void *dss_realloc(void *p, size_t size)
70 * No need to check for NULL pointers: If p is NULL, the call
71 * to realloc is equivalent to malloc(size)
74 if (!(p = realloc(p, size))) {
75 DSS_EMERG_LOG(("realloc failed (size = %zu), aborting\n",
83 * dss' version of malloc().
85 * \param size The desired new size.
87 * A wrapper for malloc(3) which exits on errors.
89 * \return A pointer to the allocated memory, which is suitably aligned for any
94 __must_check __malloc void *dss_malloc(size_t size)
101 DSS_EMERG_LOG(("malloc failed (size = %zu), aborting\n",
109 * dss' version of calloc().
111 * \param size The desired new size.
113 * A wrapper for calloc(3) which exits on errors.
115 * \return A pointer to the allocated and zeroed-out memory, which is suitably
116 * aligned for any kind of variable.
120 __must_check __malloc void *dss_calloc(size_t size)
122 void *ret = dss_malloc(size);
124 memset(ret, 0, size);
129 * dss' version of strdup().
131 * \param s The string to be duplicated.
133 * A wrapper for strdup(3). It calls \p exit(EXIT_FAILURE) on errors, i.e.
134 * there is no need to check the return value in the caller.
136 * \return A pointer to the duplicated string. If \p s was the NULL pointer,
137 * an pointer to an empty string is returned.
142 __must_check __malloc char *dss_strdup(const char *s)
146 if ((ret = strdup(s? s: "")))
148 DSS_EMERG_LOG(("strdup failed, aborting\n"));
153 * Allocate a sufficiently large string and print into it.
155 * \param fmt A usual format string.
157 * Produce output according to \p fmt. No artificial bound on the length of the
158 * resulting string is imposed.
160 * \return This function either returns a pointer to a string that must be
161 * freed by the caller or aborts without returning.
165 __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
174 * Get the home directory of the current user.
176 * \return A dynamically allocated string that must be freed by the caller. If
177 * the home directory could not be found, this function returns "/tmp".
179 __must_check __malloc char *get_homedir(void)
181 struct passwd *pw = getpwuid(getuid());
182 return dss_strdup(pw? pw->pw_dir : "/tmp");
186 * Convert a string to a 64-bit signed integer value.
188 * \param str The string to be converted.
189 * \param value Result pointer.
193 * \sa strtol(3), atoi(3).
195 int dss_atoi64(const char *str, int64_t *value)
200 errno = 0; /* To distinguish success/failure after call */
201 tmp = strtoll(str, &endptr, 10);
202 if (errno == ERANGE && (tmp == LLONG_MAX || tmp == LLONG_MIN))
203 return -E_ATOI_OVERFLOW;
204 if (errno != 0 && tmp == 0) /* other error */
207 return -E_ATOI_NO_DIGITS;
208 if (*endptr != '\0') /* Further characters after number */
209 return -E_ATOI_JUNK_AT_END;
215 * Get the logname of the current user.
217 * \return A dynamically allocated string that must be freed by the caller. On
218 * errors, the string "unknown user" is returned, i.e. this function never
223 __must_check __malloc char *dss_logname(void)
225 struct passwd *pw = getpwuid(getuid());
226 return dss_strdup(pw? pw->pw_name : "unknown_user");
230 * Split string and return pointers to its parts.
232 * \param args The string to be split.
233 * \param argv_ptr Pointer to the list of substrings.
234 * \param delim Delimiter.
236 * This function modifies \a args by replacing each occurrence of \a delim by
237 * zero. A \p NULL-terminated array of pointers to char* is allocated dynamically
238 * and these pointers are initialized to point to the broken-up substrings
239 * within \a args. A pointer to this array is returned via \a argv_ptr.
241 * \return The number of substrings found in \a args.
243 unsigned split_args(char *args, char *** const argv_ptr, const char *delim)
249 p = args + strspn(args, delim);
251 i = strcspn(p, delim);
256 p += strspn(p, delim);
258 *argv_ptr = dss_malloc((n + 1) * sizeof(char *));
261 p = args + strspn(args, delim);
264 j = strcspn(p, delim);
267 p += strcspn(p, delim);
271 p += strspn(p, delim);