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)
39 if (!(p = realloc(p, size))) {
40 PARA_EMERG_LOG("realloc failed (size = %zu), aborting\n",
48 * paraslash's version of malloc()
50 * \param size desired new size
52 * A wrapper for malloc(3) which exits on errors.
54 * \return A pointer to the allocated memory, which is suitably aligned for any
59 __must_check __malloc void *para_malloc(size_t size)
61 void *p = malloc(size);
64 PARA_EMERG_LOG("%s", "malloc failed, aborting\n");
71 * paraslash's version of calloc()
73 * \param size desired new size
75 * A wrapper for calloc(3) which exits on errors.
77 * \return A pointer to the allocated and zeroed-out memory, which is suitably
78 * aligned for any kind of variable.
82 __must_check __malloc void *para_calloc(size_t size)
84 void *ret = para_malloc(size);
91 * paraslash's version of strdup()
93 * \param s string to be duplicated
95 * A wrapper for strdup(3). It calls \p exit(EXIT_FAILURE) on errors, i.e.
96 * there is no need to check the return value in the caller.
98 * \return A pointer to the duplicated string. If \p s was the NULL pointer,
99 * an pointer to an empty string is returned.
103 __must_check __malloc char *para_strdup(const char *s)
107 if ((ret = strdup(s? s: "")))
109 PARA_EMERG_LOG("%s", "strdup failed, aborting\n");
114 * allocate a sufficiently large string and print into it
116 * \param fmt usual format string
118 * Produce output according to \p fmt. No artificial bound on the length of the
119 * resulting string is imposed.
121 * \return This function either returns a pointer to a string that must be
122 * freed by the caller or aborts without returning.
126 __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
130 PARA_VSPRINTF(fmt, msg);
135 * paraslash's version of strcat()
137 * \param a string to be appended to
138 * \param b string to append
140 * Append \p b to \p a.
142 * \return If \p a is NULL, return a pointer to a copy of \p b, i.e.
143 * para_strcat(NULL, b) is equivalent to para_strdup(b). If \p b is NULL,
144 * return \p a without making a copy of \p a. Otherwise, construct the
145 * concatenation \p c, free \p a (but not \p b) and return \p c.
149 __must_check __malloc char *para_strcat(char *a, const char *b)
154 return para_strdup(b);
157 tmp = make_message("%s%s", a, b);
163 * paraslash's version of dirname()
165 * \param name pointer to the full path
167 * Compute the directory component of \p name
169 * \return If \p name is \รพ NULL or the empty string, return \p NULL.
170 * Otherwise, Make a copy of \p name and return its directory component. Caller
171 * is responsible to free the result.
173 __must_check __malloc char *para_dirname(const char *name)
179 ret = para_strdup(name);
180 p = strrchr(ret, '/');
189 * paraslash's version of basename()
191 * \param name Pointer to the full path
193 * Compute the filename component of \p name
195 * \return If \p name is \p NULL or the empty string, return \p NULL,
196 * Otherwise, make a copy of \p name and return its filename component. Caller
197 * is responsible to free the result.
199 __must_check __malloc char *para_basename(const char *name)
205 p = strrchr(name, '/');
207 return para_strdup(name);
211 return para_strdup(p);
215 * cut trailing newline
217 * \param buf the string to be chopped.
219 * Replace the last character in \p buf by zero if it is euqal to
220 * the newline character.
227 if (buf[n - 1] == '\n')
232 * get a random filename
234 * This is by no means a secure way to create temporary files in a hostile
235 * direcory like \p /tmp. However, it is OK to use for temp files, fifos,
236 * sockets that are created in ~/.paraslash. Result must be freed by the
239 * \return a pointer to a random filename.
241 __must_check __malloc char *para_tmpname(void)
246 gettimeofday(&now, NULL);
249 return make_message("%08i", rand());
253 * create unique temporary file
255 * \param template the template to be passed to mkstemp()
256 * \param mode the desired mode of the tempfile
258 * This wrapper for mkstemp additionally uses fchmod() to
259 * set the given mode of the tempfile if mkstemp() returned success.
261 * \return The file descriptor of the temp file just created on success.
262 * On errors, -E_MKSTEMP or -E_FCHMOD is returned.
264 __must_check int para_mkstemp(char *template, mode_t mode)
266 int tmp, fd = mkstemp(template);
270 tmp = fchmod(fd, mode);
279 * get the logname of the current user
281 * \return A dynammically allocated string that must be freed by the caller. On
282 * errors, the string "unknown user" is returned, i.e. this function never
287 __must_check __malloc char *para_logname(void)
289 struct passwd *pw = getpwuid(getuid());
290 return para_strdup(pw? pw->pw_name : "unknown_user");
294 * get the home directory of the current user
296 * \return A dynammically allocated string that must be freed by the caller. If
297 * the home directory could not be found, this function returns "/tmp".
299 __must_check __malloc char *para_homedir(void)
301 struct passwd *pw = getpwuid(getuid());
302 return para_strdup(pw? pw->pw_dir : "/tmp");
306 * split string and return pointers to its parts.
308 * \param args the string to be split
309 * \param argv_ptr pointer to the list of substrings
310 * \param delim delimiter
312 * This function modifies \a args by replacing each occurance of \a delim by
313 * zero. A \p NULL-terminated array of pointers to char* is allocated dynamically
314 * and these pointers are initialized to point to the broken-up substrings
315 * within \a args. A pointer to this array is returned via \a argv_ptr. It's OK
316 * to call this function with \a args \a == \p NULL.
318 * \return The number of substrings found in \a args.
320 __must_check unsigned split_args(char *args, char ***argv_ptr, const char *delim)
326 p = args + strspn(args, delim);
328 i = strcspn(p, delim);
333 p += strspn(p, delim);
335 *argv_ptr = para_malloc((n + 1) * sizeof(char *));
338 p = args + strspn(args, delim);
341 j = strcspn(p, delim);
344 p += strcspn(p, delim);
348 p += strspn(p, delim);
357 * ensure that file descriptors 0, 1, and 2 are valid
359 * Common approach that opens /dev/null until it gets a file descriptor greater
362 * \sa okir's Black Hats Manual.
364 void valid_fd_012(void)
369 fd = open("/dev/null", O_RDWR);
380 * get the own hostname
382 * \return A dynammically allocated string containing the hostname.
386 __malloc char *para_hostname(void)
391 return para_strdup(u.nodename);