1 /* SPDX-License-Identifier: GPL-2.0 */
10 #include <sys/types.h>
14 #include "gcc-compat.h"
20 * dss' version of realloc().
22 * \param p Pointer to the memory block, may be \p NULL.
23 * \param size The desired new size.
25 * A wrapper for realloc(3). It calls \p exit(\p EXIT_FAILURE) on errors,
26 * i.e. there is no need to check the return value in the caller.
28 * \return A pointer to the newly allocated memory, which is suitably aligned
29 * for any kind of variable and may be different from \a p.
33 __must_check __malloc void *dss_realloc(void *p, size_t size)
36 * No need to check for NULL pointers: If p is NULL, the call
37 * to realloc is equivalent to malloc(size)
40 if (!(p = realloc(p, size))) {
41 DSS_EMERG_LOG(("realloc failed (size = %zu), aborting\n",
49 * dss' version of malloc().
51 * \param size The desired new size.
53 * A wrapper for malloc(3) which exits on errors.
55 * \return A pointer to the allocated memory, which is suitably aligned for any
60 __must_check __malloc void *dss_malloc(size_t size)
67 DSS_EMERG_LOG(("malloc failed (size = %zu), aborting\n",
75 * dss' version of calloc().
77 * \param size The desired new size.
79 * A wrapper for calloc(3) which exits on errors.
81 * \return A pointer to the allocated and zeroed-out memory, which is suitably
82 * aligned for any kind of variable.
86 __must_check __malloc void *dss_calloc(size_t size)
88 void *ret = dss_malloc(size);
95 * dss' version of strdup().
97 * \param s The string to be duplicated.
99 * A wrapper for strdup(3). It calls \p exit(EXIT_FAILURE) on errors, i.e.
100 * there is no need to check the return value in the caller.
102 * \return A pointer to the duplicated string. If \p s was the NULL pointer,
103 * an pointer to an empty string is returned.
108 __must_check __malloc char *dss_strdup(const char *s)
112 if ((ret = strdup(s? s: "")))
114 DSS_EMERG_LOG(("strdup failed, aborting\n"));
119 * Allocate a sufficiently large string and print into it.
121 * \param fmt A usual format string.
123 * Produce output according to \p fmt. No artificial bound on the length of the
124 * resulting string is imposed.
126 * \return This function either returns a pointer to a string that must be
127 * freed by the caller or aborts without returning.
131 __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
136 msg = dss_malloc(size);
141 /* Try to print in the allocated space. */
143 n = vsnprintf(msg, size, fmt, ap);
145 /* If that worked, return the string. */
148 /* Else try again with more space. */
149 size = n + 1; /* precisely what is needed */
150 msg = dss_realloc(msg, size);
155 * Get the home directory of the current user.
157 * \return A dynamically allocated string that must be freed by the caller. If
158 * the home directory could not be found, this function returns "/tmp".
160 __must_check __malloc char *get_homedir(void)
162 struct passwd *pw = getpwuid(getuid());
163 return dss_strdup(pw? pw->pw_dir : "/tmp");
167 * Convert a string to a 64-bit signed integer value.
169 * \param str The string to be converted.
170 * \param value Result pointer.
174 * \sa strtol(3), atoi(3).
176 int dss_atoi64(const char *str, int64_t *value)
181 errno = 0; /* To distinguish success/failure after call */
182 tmp = strtoll(str, &endptr, 10);
183 if (errno == ERANGE && (tmp == LLONG_MAX || tmp == LLONG_MIN))
184 return -E_ATOI_OVERFLOW;
185 if (errno != 0 && tmp == 0) /* other error */
188 return -E_ATOI_NO_DIGITS;
189 if (*endptr != '\0') /* Further characters after number */
190 return -E_ATOI_JUNK_AT_END;
196 * Get the logname of the current user.
198 * \return A dynamically allocated string that must be freed by the caller. On
199 * errors, the string "unknown user" is returned, i.e. this function never
204 __must_check __malloc char *dss_logname(void)
206 struct passwd *pw = getpwuid(getuid());
207 return dss_strdup(pw? pw->pw_name : "unknown_user");
211 * Split string and return pointers to its parts.
213 * \param args The string to be split.
214 * \param argv_ptr Pointer to the list of substrings.
215 * \param delim Delimiter.
217 * This function modifies \a args by replacing each occurrence of \a delim by
218 * zero. A \p NULL-terminated array of pointers to char* is allocated dynamically
219 * and these pointers are initialized to point to the broken-up substrings
220 * within \a args. A pointer to this array is returned via \a argv_ptr.
222 * \return The number of substrings found in \a args.
224 unsigned split_args(char *args, char *** const argv_ptr, const char *delim)
230 p = args + strspn(args, delim);
232 i = strcspn(p, delim);
237 p += strspn(p, delim);
239 *argv_ptr = dss_malloc((n + 1) * sizeof(char *));
242 p = args + strspn(args, delim);
245 j = strcspn(p, delim);
248 p += strcspn(p, delim);
252 p += strspn(p, delim);