2 * Copyright (C) 2004-2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file string.c memory allocation and string handling functions */
12 #include <sys/time.h> /* gettimeofday */
14 #include <sys/utsname.h> /* uname() */
20 * paraslash's version of realloc()
22 * \param p pointer to the memory block, may be NULL
23 * \param size 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 \p p.
33 __must_check __malloc void *para_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 PARA_EMERG_LOG("realloc failed (size = %zu), aborting\n",
49 * paraslash's version of malloc()
51 * \param size 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 *para_malloc(size_t size)
63 void *p = malloc(size);
66 PARA_EMERG_LOG("%s", "malloc failed, aborting\n");
73 * paraslash's version of calloc()
75 * \param size desired new size
77 * A wrapper for calloc(3) which exits on errors.
79 * \return A pointer to the allocated and zeroed-out memory, which is suitably
80 * aligned for any kind of variable.
84 __must_check __malloc void *para_calloc(size_t size)
86 void *ret = para_malloc(size);
93 * paraslash's version of strdup()
95 * \param s string to be duplicated
97 * A wrapper for strdup(3). It calls \p exit(EXIT_FAILURE) on errors, i.e.
98 * there is no need to check the return value in the caller.
100 * \return A pointer to the duplicated string. If \p s was the NULL pointer,
101 * an pointer to an empty string is returned.
105 __must_check __malloc char *para_strdup(const char *s)
109 if ((ret = strdup(s? s: "")))
111 PARA_EMERG_LOG("%s", "strdup failed, aborting\n");
116 * allocate a sufficiently large string and print into it
118 * \param fmt usual format string
120 * Produce output according to \p fmt. No artificial bound on the length of the
121 * resulting string is imposed.
123 * \return This function either returns a pointer to a string that must be
124 * freed by the caller or aborts without returning.
128 __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
132 PARA_VSPRINTF(fmt, msg);
137 * paraslash's version of strcat()
139 * \param a string to be appended to
140 * \param b string to append
142 * Append \p b to \p a.
144 * \return If \p a is NULL, return a pointer to a copy of \p b, i.e.
145 * para_strcat(NULL, b) is equivalent to para_strdup(b). If \p b is NULL,
146 * return \p a without making a copy of \p a. Otherwise, construct the
147 * concatenation \p c, free \p a (but not \p b) and return \p c.
151 __must_check __malloc char *para_strcat(char *a, const char *b)
156 return para_strdup(b);
159 tmp = make_message("%s%s", a, b);
165 * paraslash's version of dirname()
167 * \param name pointer to the full path
169 * Compute the directory component of \p name
171 * \return If \p name is \รพ NULL or the empty string, return \p NULL.
172 * Otherwise, Make a copy of \p name and return its directory component. Caller
173 * is responsible to free the result.
175 __must_check __malloc char *para_dirname(const char *name)
181 ret = para_strdup(name);
182 p = strrchr(ret, '/');
191 * paraslash's version of basename()
193 * \param name Pointer to the full path
195 * Compute the filename component of \p name
197 * \return If \p name is \p NULL or the empty string, return \p NULL,
198 * Otherwise, make a copy of \p name and return its filename component. Caller
199 * is responsible to free the result.
201 __must_check __malloc char *para_basename(const char *name)
207 p = strrchr(name, '/');
209 return para_strdup(name);
213 return para_strdup(p);
217 * cut trailing newline
219 * \param buf the string to be chopped.
221 * Replace the last character in \p buf by zero if it is euqal to
222 * the newline character.
229 if (buf[n - 1] == '\n')
234 * get a random filename
236 * This is by no means a secure way to create temporary files in a hostile
237 * direcory like \p /tmp. However, it is OK to use for temp files, fifos,
238 * sockets that are created in ~/.paraslash. Result must be freed by the
241 * \return a pointer to a random filename.
243 __must_check __malloc char *para_tmpname(void)
248 gettimeofday(&now, NULL);
251 return make_message("%08i", rand());
255 * create unique temporary file
257 * \param template the template to be passed to mkstemp()
258 * \param mode the desired mode of the tempfile
260 * This wrapper for mkstemp additionally uses fchmod() to
261 * set the given mode of the tempfile if mkstemp() returned success.
263 * \return The file descriptor of the temp file just created on success.
264 * On errors, -E_MKSTEMP or -E_FCHMOD is returned.
266 __must_check int para_mkstemp(char *template, mode_t mode)
268 int tmp, fd = mkstemp(template);
272 tmp = fchmod(fd, mode);
281 * get the logname of the current user
283 * \return A dynammically allocated string that must be freed by the caller. On
284 * errors, the string "unknown user" is returned, i.e. this function never
289 __must_check __malloc char *para_logname(void)
291 struct passwd *pw = getpwuid(getuid());
292 return para_strdup(pw? pw->pw_name : "unknown_user");
296 * get the home directory of the current user
298 * \return A dynammically allocated string that must be freed by the caller. If
299 * the home directory could not be found, this function returns "/tmp".
301 __must_check __malloc char *para_homedir(void)
303 struct passwd *pw = getpwuid(getuid());
304 return para_strdup(pw? pw->pw_dir : "/tmp");
308 * split string and return pointers to its parts.
310 * \param args the string to be split
311 * \param argv_ptr pointer to the list of substrings
312 * \param delim delimiter
314 * This function modifies \a args by replacing each occurance of \a delim by
315 * zero. A \p NULL-terminated array of pointers to char* is allocated dynamically
316 * and these pointers are initialized to point to the broken-up substrings
317 * within \a args. A pointer to this array is returned via \a argv_ptr. It's OK
318 * to call this function with \a args \a == \p NULL.
320 * \return The number of substrings found in \a args.
322 __must_check unsigned split_args(char *args, char ***argv_ptr, const char *delim)
328 p = args + strspn(args, delim);
330 i = strcspn(p, delim);
335 p += strspn(p, delim);
337 *argv_ptr = para_malloc((n + 1) * sizeof(char *));
340 p = args + strspn(args, delim);
343 j = strcspn(p, delim);
346 p += strcspn(p, delim);
350 p += strspn(p, delim);
359 * ensure that file descriptors 0, 1, and 2 are valid
361 * Common approach that opens /dev/null until it gets a file descriptor greater
364 * \sa okir's Black Hats Manual.
366 void valid_fd_012(void)
371 fd = open("/dev/null", O_RDWR);
382 * get the own hostname
384 * \return A dynammically allocated string containing the hostname.
388 __malloc char *para_hostname(void)
393 return para_strdup(u.nodename);