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 * dss' version of realloc().
26 * \param p Pointer to the memory block, may be \p NULL.
27 * \param size The desired new size.
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.
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.
37 __must_check __malloc void *dss_realloc(void *p, size_t size)
40 * No need to check for NULL pointers: If p is NULL, the call
41 * to realloc is equivalent to malloc(size)
44 if (!(p = realloc(p, size))) {
45 DSS_EMERG_LOG(("realloc failed (size = %zu), aborting\n",
53 * dss' version of malloc().
55 * \param size The desired new size.
57 * A wrapper for malloc(3) which exits on errors.
59 * \return A pointer to the allocated memory, which is suitably aligned for any
64 __must_check __malloc void *dss_malloc(size_t size)
71 DSS_EMERG_LOG(("malloc failed (size = %zu), aborting\n",
79 * dss' version of calloc().
81 * \param size The desired new size.
83 * A wrapper for calloc(3) which exits on errors.
85 * \return A pointer to the allocated and zeroed-out memory, which is suitably
86 * aligned for any kind of variable.
90 __must_check __malloc void *dss_calloc(size_t size)
92 void *ret = dss_malloc(size);
99 * dss' version of strdup().
101 * \param s The string to be duplicated.
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.
106 * \return A pointer to the duplicated string. If \p s was the NULL pointer,
107 * an pointer to an empty string is returned.
112 __must_check __malloc char *dss_strdup(const char *s)
116 if ((ret = strdup(s? s: "")))
118 DSS_EMERG_LOG(("strdup failed, aborting\n"));
123 * Allocate a sufficiently large string and print into it.
125 * \param fmt A usual format string.
127 * Produce output according to \p fmt. No artificial bound on the length of the
128 * resulting string is imposed.
130 * \return This function either returns a pointer to a string that must be
131 * freed by the caller or aborts without returning.
135 __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
140 msg = dss_malloc(size);
145 /* Try to print in the allocated space. */
147 n = vsnprintf(msg, size, fmt, ap);
149 /* If that worked, return the string. */
152 /* Else try again with more space. */
153 size = n + 1; /* precisely what is needed */
154 msg = dss_realloc(msg, size);
159 * Get the home directory of the current user.
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".
164 __must_check __malloc char *get_homedir(void)
166 struct passwd *pw = getpwuid(getuid());
167 return dss_strdup(pw? pw->pw_dir : "/tmp");
171 * Convert a string to a 64-bit signed integer value.
173 * \param str The string to be converted.
174 * \param value Result pointer.
178 * \sa strtol(3), atoi(3).
180 int dss_atoi64(const char *str, int64_t *value)
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 */
192 return -E_ATOI_NO_DIGITS;
193 if (*endptr != '\0') /* Further characters after number */
194 return -E_ATOI_JUNK_AT_END;
200 * Get the logname of the current user.
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
208 __must_check __malloc char *dss_logname(void)
210 struct passwd *pw = getpwuid(getuid());
211 return dss_strdup(pw? pw->pw_name : "unknown_user");
215 * Split string and return pointers to its parts.
217 * \param args The string to be split.
218 * \param argv_ptr Pointer to the list of substrings.
219 * \param delim Delimiter.
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.
226 * \return The number of substrings found in \a args.
228 unsigned split_args(char *args, char *** const argv_ptr, const char *delim)
234 p = args + strspn(args, delim);
236 i = strcspn(p, delim);
241 p += strspn(p, delim);
243 *argv_ptr = dss_malloc((n + 1) * sizeof(char *));
246 p = args + strspn(args, delim);
249 j = strcspn(p, delim);
252 p += strcspn(p, delim);
256 p += strspn(p, delim);