2 * Copyright (C) 2004-2007 Andre Noll <maan@systemlinux.org>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 /** \file string.c memory allocation and string handling functions */
24 #include <sys/time.h> /* gettimeofday */
26 #include <sys/utsname.h> /* uname() */
32 * paraslash's version of realloc()
34 * \param p pointer to the memory block, may be NULL
35 * \param size desired new size
37 * A wrapper for realloc(3). It calls \p exit(\p EXIT_FAILURE) on errors,
38 * i.e. there is no need to check the return value in the caller.
40 * \return A pointer to the newly allocated memory, which is suitably aligned
41 * for any kind of variable and may be different from \p p.
45 __must_check __malloc void *para_realloc(void *p, size_t size)
48 * No need to check for NULL pointers: If p is NULL, the call
49 * to realloc is equivalent to malloc(size)
51 if (!(p = realloc(p, size))) {
52 PARA_EMERG_LOG("%s", "realloc failed, aborting\n");
59 * paraslash's version of malloc()
61 * \param size desired new size
63 * A wrapper for malloc(3) which exits on errors.
65 * \return A pointer to the allocated memory, which is suitably aligned for any
70 __must_check __malloc void *para_malloc(size_t size)
72 void *p = malloc(size);
75 PARA_EMERG_LOG("%s", "malloc failed, aborting\n");
82 * paraslash's version of calloc()
84 * \param size desired new size
86 * A wrapper for calloc(3) which exits on errors.
88 * \return A pointer to the allocated and zeroed-out memory, which is suitably
89 * aligned for any kind of variable.
93 __must_check __malloc void *para_calloc(size_t size)
95 void *ret = para_malloc(size);
102 * paraslash's version of strdup()
104 * \param s string to be duplicated
106 * A wrapper for strdup(3). It calls \p exit(EXIT_FAILURE) on errors, i.e.
107 * there is no need to check the return value in the caller.
109 * \return A pointer to the duplicated string. If \p s was the NULL pointer,
110 * an pointer to an empty string is returned.
114 __must_check __malloc char *para_strdup(const char *s)
118 if ((ret = strdup(s? s: "")))
120 PARA_EMERG_LOG("%s", "strdup failed, aborting\n");
125 * allocate a sufficiently large string and print into it
127 * \param fmt usual format string
129 * Produce output according to \p fmt. No artificial bound on the length of the
130 * resulting string is imposed.
132 * \return This function either returns a pointer to a string that must be
133 * freed by the caller or aborts without returning.
137 __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
141 PARA_VSPRINTF(fmt, msg);
146 * paraslash's version of strcat()
148 * \param a string to be appended to
149 * \param b string to append
151 * Append \p b to \p a.
153 * \return If \p a is NULL, return a pointer to a copy of \p b, i.e.
154 * para_strcat(NULL, b) is equivalent to para_strdup(b). If \p b is NULL,
155 * return \p a without making a copy of \p a. Otherwise, construct the
156 * concatenation \p c, free \p a (but not \p b) and return \p c.
160 __must_check __malloc char *para_strcat(char *a, const char *b)
165 return para_strdup(b);
168 tmp = make_message("%s%s", a, b);
174 * paraslash's version of dirname()
176 * \param name pointer to the full path
178 * Compute the directory component of \p name
180 * \return If \p name is \รพ NULL or the empty string, return \p NULL.
181 * Otherwise, Make a copy of \p name and return its directory component. Caller
182 * is responsible to free the result.
184 __must_check __malloc char *para_dirname(const char *name)
190 ret = para_strdup(name);
191 p = strrchr(ret, '/');
200 * paraslash's version of basename()
202 * \param name Pointer to the full path
204 * Compute the filename component of \p name
206 * \return If \p name is \p NULL or the empty string, return \p NULL,
207 * Otherwise, make a copy of \p name and return its filename component. Caller
208 * is responsible to free the result.
210 __must_check __malloc char *para_basename(const char *name)
216 p = strrchr(name, '/');
218 return para_strdup(name);
222 return para_strdup(p);
226 * cut trailing newline
228 * \param buf the string to be chopped.
230 * Replace the last character in \p buf by zero if it is euqal to
231 * the newline character.
238 if (buf[n - 1] == '\n')
243 * get a random filename
245 * This is by no means a secure way to create temporary files in a hostile
246 * direcory like \p /tmp. However, it is OK to use for temp files, fifos,
247 * sockets that are created in ~/.paraslash. Result must be freed by the
250 * \return a pointer to a random filename.
252 __must_check __malloc char *para_tmpname(void)
255 gettimeofday(&now, NULL);
257 return make_message("%08i", rand());
261 * create unique temporary file
263 * \param template the template to be passed to mkstemp()
264 * \param mode the desired mode of the tempfile
266 * This wrapper for mkstemp additionally uses fchmod() to
267 * set the given mode of the tempfile if mkstemp() returned success.
269 * \return The file descriptor of the temp file just created on success.
270 * On errors, -E_MKSTEMP or -E_FCHMOD is returned.
272 __must_check int para_mkstemp(char *template, mode_t mode)
274 int tmp, fd = mkstemp(template);
278 tmp = fchmod(fd, mode);
287 * get the logname of the current user
289 * \return A dynammically allocated string that must be freed by the caller. On
290 * errors, the string "unknown user" is returned, i.e. this function never
295 __must_check __malloc char *para_logname(void)
297 struct passwd *pw = getpwuid(getuid());
298 return para_strdup(pw? pw->pw_name : "unknown_user");
302 * get the home directory of the current user
304 * \return A dynammically allocated string that must be freed by the caller. If
305 * the home directory could not be found, this function returns "/tmp".
307 __must_check __malloc char *para_homedir(void)
309 struct passwd *pw = getpwuid(getuid());
310 return para_strdup(pw? pw->pw_dir : "/tmp");
314 * split string and return pointers to its parts.
316 * \param args the string to be split
317 * \param argv_ptr pointer to the list of substrings
318 * \param delim delimiter
320 * This function modifies \p args by replacing each occurance of \p delim by
321 * zero. A NULL-terminated array of pointers to char* is allocated dynamically
322 * and these pointers are initialized to point to the broken-up substrings
323 * within \p args. A pointer to this array is returned via \p argv_ptr. It's OK
324 * to call this function with \p args \p == \p NULL.
326 * \return The number of substrings found in \p args.
328 __must_check unsigned split_args(char *args, char ***argv_ptr, const char *delim)
334 p = args + strspn(args, delim);
336 i = strcspn(p, delim);
341 p += strspn(p, delim);
343 *argv_ptr = para_malloc((n + 1) * sizeof(char *));
346 p = args + strspn(args, delim);
349 j = strcspn(p, delim);
352 p += strcspn(p, delim);
356 p += strspn(p, delim);
365 * ensure that file descriptors 0, 1, and 2 are valid
367 * Common approach that opens /dev/null until it gets a file descriptor greater
370 * \sa okir's Black Hats Manual.
372 void valid_fd_012(void)
377 fd = open("/dev/null", O_RDWR);
388 * get the own hostname
390 * \return A dynammically allocated string containing the hostname.
394 __malloc char *para_hostname(void)
399 return para_strdup(u.nodename);