13 #include "gcc-compat.h"
18 __noreturn
void clean_exit(int status
);
21 * Write a message to a dynamically allocated string.
23 * \param fmt Usual format string.
24 * \param p Result pointer.
27 #define VSPRINTF(fmt, p) \
31 p = dss_malloc(size); \
34 /* Try to print in the allocated space. */ \
36 n = vsnprintf(p, size, fmt, ap); \
38 /* If that worked, return the string. */ \
39 if (n > -1 && n < size) \
41 /* Else try again with more space. */ \
42 if (n > -1) /* glibc 2.1 */ \
43 size = n + 1; /* precisely what is needed */ \
44 else /* glibc 2.0 */ \
45 size *= 2; /* twice the old size */ \
46 p = dss_realloc(p, size); \
51 * dss' version of realloc().
53 * \param p Pointer to the memory block, may be \p NULL.
54 * \param size The desired new size.
56 * A wrapper for realloc(3). It calls \p exit(\p EXIT_FAILURE) on errors,
57 * i.e. there is no need to check the return value in the caller.
59 * \return A pointer to the newly allocated memory, which is suitably aligned
60 * for any kind of variable and may be different from \a p.
64 __must_check __malloc
void *dss_realloc(void *p
, size_t size
)
67 * No need to check for NULL pointers: If p is NULL, the call
68 * to realloc is equivalent to malloc(size)
71 if (!(p
= realloc(p
, size
))) {
72 DSS_EMERG_LOG("realloc failed (size = %zu), aborting\n",
74 clean_exit(EXIT_FAILURE
);
80 * dss' version of malloc().
82 * \param size The desired new size.
84 * A wrapper for malloc(3) which exits on errors.
86 * \return A pointer to the allocated memory, which is suitably aligned for any
91 __must_check __malloc
void *dss_malloc(size_t size
)
94 void *p
= malloc(size
);
97 DSS_EMERG_LOG("malloc failed (size = %zu), aborting\n",
99 clean_exit(EXIT_FAILURE
);
105 * dss' version of calloc().
107 * \param size The desired new size.
109 * A wrapper for calloc(3) which exits on errors.
111 * \return A pointer to the allocated and zeroed-out memory, which is suitably
112 * aligned for any kind of variable.
116 __must_check __malloc
void *dss_calloc(size_t size
)
118 void *ret
= dss_malloc(size
);
120 memset(ret
, 0, size
);
125 * dss' version of strdup().
127 * \param s The string to be duplicated.
129 * A wrapper for strdup(3). It calls \p exit(EXIT_FAILURE) on errors, i.e.
130 * there is no need to check the return value in the caller.
132 * \return A pointer to the duplicated string. If \p s was the NULL pointer,
133 * an pointer to an empty string is returned.
138 __must_check __malloc
char *dss_strdup(const char *s
)
142 if ((ret
= strdup(s
? s
: "")))
144 DSS_EMERG_LOG("strdup failed, aborting\n");
145 clean_exit(EXIT_FAILURE
);
149 * Allocate a sufficiently large string and print into it.
151 * \param fmt A usual format string.
153 * Produce output according to \p fmt. No artificial bound on the length of the
154 * resulting string is imposed.
156 * \return This function either returns a pointer to a string that must be
157 * freed by the caller or aborts without returning.
161 __must_check __printf_1_2 __malloc
char *make_message(const char *fmt
, ...)
169 __printf_1_2
void make_err_msg(const char* fmt
,...)
172 VSPRINTF(fmt
, dss_error_txt
);
176 * Get the home directory of the current user.
178 * \return A dynammically allocated string that must be freed by the caller. If
179 * the home directory could not be found, this function returns "/tmp".
181 __must_check __malloc
char *get_homedir(void)
183 struct passwd
*pw
= getpwuid(getuid());
184 return dss_strdup(pw
? pw
->pw_dir
: "/tmp");
188 * Convert a string to a 64-bit signed integer value.
190 * \param str The string to be converted.
191 * \param value Result pointer.
195 * \sa strtol(3), atoi(3).
197 int dss_atoi64(const char *str
, int64_t *value
)
202 errno
= 0; /* To distinguish success/failure after call */
203 tmp
= strtoll(str
, &endptr
, 10);
204 if (errno
== ERANGE
&& (tmp
== LLONG_MAX
|| tmp
== LLONG_MIN
)) {
205 make_err_msg("%s", str
);
206 return -E_ATOI_OVERFLOW
;
208 if (errno
!= 0 && tmp
== 0) { /* other error */
209 make_err_msg("%s", str
);
213 make_err_msg("%s", str
);
214 return -E_ATOI_NO_DIGITS
;
216 if (*endptr
!= '\0') { /* Further characters after number */
217 make_err_msg("%s", str
);
218 return -E_ATOI_JUNK_AT_END
;
225 * Get the logname of the current user.
227 * \return A dynammically allocated string that must be freed by the caller. On
228 * errors, the string "unknown user" is returned, i.e. this function never
233 __must_check __malloc
char *dss_logname(void)
235 struct passwd
*pw
= getpwuid(getuid());
236 return dss_strdup(pw
? pw
->pw_name
: "unknown_user");
240 * Split string and return pointers to its parts.
242 * \param args The string to be split.
243 * \param argv_ptr Pointer to the list of substrings.
244 * \param delim Delimiter.
246 * This function modifies \a args by replacing each occurance of \a delim by
247 * zero. A \p NULL-terminated array of pointers to char* is allocated dynamically
248 * and these pointers are initialized to point to the broken-up substrings
249 * within \a args. A pointer to this array is returned via \a argv_ptr.
251 * \return The number of substrings found in \a args.
253 __must_check
unsigned split_args(char *args
, char *** const argv_ptr
, const char *delim
)
259 p
= args
+ strspn(args
, delim
);
261 i
= strcspn(p
, delim
);
266 p
+= strspn(p
, delim
);
268 *argv_ptr
= dss_malloc((n
+ 1) * sizeof(char *));
271 p
= args
+ strspn(args
, delim
);
274 j
= strcspn(p
, delim
);
277 p
+= strcspn(p
, delim
);
281 p
+= strspn(p
, delim
);