2 * Copyright (C) 2004-2006 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 */
21 #include <sys/time.h> /* gettimeofday */
25 #include <sys/utsname.h> /* uname() */
30 * paraslash's version of realloc()
32 * \param p pointer to the memory block, may be NULL
33 * \param size desired new size
35 * A wrapper for realloc(3). It calls \p exit(\p EXIT_FAILURE) on errors,
36 * i.e. there is no need to check the return value in the caller.
39 __must_check __malloc void *para_realloc(void *p, size_t size)
42 * No need to check for NULL pointers: If p is NULL, the call
43 * to realloc is equivalent to malloc(size)
45 if (!(p = realloc(p, size))) {
46 PARA_EMERG_LOG("%s", "realloc failed, aborting\n");
53 * paraslash's version of malloc()
55 * \param size desired new size
57 * A wrapper for malloc(3) which exits on errors.
60 __must_check __malloc void *para_malloc(size_t size)
62 void *p = malloc(size);
65 PARA_EMERG_LOG("%s", "malloc failed, aborting\n");
72 * paraslash's version of calloc()
74 * \param size desired new size
76 * A wrapper for calloc(3) which exits on errors.
79 __must_check __malloc void *para_calloc(size_t size)
81 void *ret = para_malloc(size);
88 * paraslash's version of strdup()
90 * \param s: string to be duplicated
92 * A wrapper for strdup(3). It calls exit(EXIT_FAILURE) on
93 * errors, i.e. there is no need to check the return value in the caller.
94 * Moreover, this wrapper checks for \a s being NULL and returns an empty
95 * string in this case.
99 __must_check __malloc char *para_strdup(const char *s)
103 if ((ret = strdup(s? s: "")))
105 PARA_EMERG_LOG("%s", "strdup failed, aborting\n");
110 * allocate a sufficiently large string and print into it
112 * \param fmt usual format string
114 * Produce output according to \a fmt. No artificial bound on the length of the
115 * resulting string is imposed. This function either returns a pointer to a
116 * string that must be freed by the caller or aborts without returning.
120 __must_check __printf_1_2 __malloc char *make_message(const char *fmt, ...)
124 PARA_VSPRINTF(fmt, msg);
129 * paraslash's version of strcat()
131 * \param a string to be appended to
132 * \param b string to append
134 * Append \a b to \a a. If \a a is NULL, return a copy of \a b, i.e.
135 * para_strcat(NULL, b) is equivalent to para_strdup(b). If \a b is NULL,
136 * return \a a without making a copy of \a a. Otherwise, construct the
137 * concatenation \a c, free \a a (but not \a b) and return \a c.
141 __must_check __malloc char *para_strcat(char *a, const char *b)
146 return para_strdup(b);
149 tmp = make_message("%s%s", a, b);
155 * paraslash's version of dirname()
157 * \param name pointer to The full path
159 * If \a name is \รพ NULL or the empty string, return \p NULL, Otherwise, Make a
160 * copy of \a name and return its directory component. Caller is responsible to
163 __must_check __malloc char *para_dirname(const char *name)
169 ret = para_strdup(name);
170 p = strrchr(ret, '/');
179 * paraslash's version of basename()
181 * \param name Pointer to the full path
183 * If \a name is \p NULL or the empty string, return \p NULL, Otherwise, make a
184 * copy of \a name and return its filename component. Caller is responsible to
187 __must_check __malloc char *para_basename(const char *name)
193 p = strrchr(name, '/');
195 return para_strdup(name);
199 return para_strdup(p);
203 * simple search and replace routine
205 * \param src source string
206 * \param macro_name the name of the macro
207 * \param replacement the replacement format string
209 * Replace \a macro_name(arg) by \a replacement. \a replacement is a format
210 * string which may contain a single string conversion specifier which gets
213 * \return A string in which all matches in \a src are replaced, or NULL if \a
214 * macro_name was not found in \a src. Caller must free the result.
218 __must_check __malloc char *s_a_r(const char *src, const char* macro_name,
219 const char *replacement)
223 regmatch_t pmatch[1];
226 const char *bufptr = src;
228 if (!macro_name || !replacement || !src)
229 return para_strdup(src);
230 regcomp(&preg, macro_name, 0);
231 while (regexec(&preg, bufptr, nmatch, pmatch, eflags)
233 char *tmp, *arg, *o_bracket, *c_bracket;
235 o_bracket = strchr(bufptr + pmatch[0].rm_so, '(');
236 c_bracket = o_bracket? strchr(o_bracket, ')') : NULL;
239 tmp = para_strdup(bufptr);
240 tmp[pmatch[0].rm_so] = '\0';
241 dest = para_strcat(dest, tmp);
244 arg = para_strdup(o_bracket + 1);
245 arg[c_bracket - o_bracket - 1] = '\0';
246 tmp = make_message(replacement, arg);
248 dest = para_strcat(dest, tmp);
253 dest = para_strcat(dest, bufptr);
254 // PARA_DEBUG_LOG("%s: returning %s\n", __func__, dest);
261 * replace a string according to a list of macros
263 * \param macro_list the array containing a macro/replacement pairs.
264 * \param src the source string
266 * This function just calls s_a_r() for each element of \a macro_list.
268 __must_check __malloc char *s_a_r_list(struct para_macro *macro_list, char *src)
270 struct para_macro *mp = macro_list;
271 char *ret = NULL, *tmp = para_strdup(src);
274 ret = s_a_r(tmp, mp->name, mp->replacement);
277 return src; /* FIXME: shouldn't we continue here? */
281 //PARA_DEBUG_LOG("%s: returning %s\n", __func__, dest);
286 * cut trailing newline
288 * \param buf the string to be chopped.
290 * Replace the last character in \a buf by zero if it is euqal to
291 * the newline character.
298 if (buf[n - 1] == '\n')
303 * get a random filename
305 * This is by no means a secure way to create temporary files in a hostile
306 * direcory like /tmp. However, it is OK to use for temp files, fifos, sockets
307 * that are created in ~/.paraslash. Result must be freed by the caller.
309 __must_check __malloc char *para_tmpname(void)
312 gettimeofday(&now, NULL);
314 return make_message("%08i", rand());
318 * create unique temporary file
320 * \param template the template to be passed to mkstemp()
321 * \param mode the desired mode of the tempfile
323 * This wrapper for mkstemp additionally uses fchmod() to
324 * set the given mode of the tempfile if mkstemp() returned success.
325 * Return value: The file descriptor of the temp file just created on success.
326 * On errors, -E_MKSTEMP or -E_FCHMOD is returned.
328 __must_check int para_mkstemp(char *template, mode_t mode)
330 int tmp, fd = mkstemp(template);
334 tmp = fchmod(fd, mode);
343 * get the logname of the current user
345 * \return A dynammically allocated string that must be freed by the caller. On
346 * errors, the string "unknown user" is returned, i.e. this function never
349 __must_check __malloc char *para_logname(void)
351 struct passwd *pw = getpwuid(getuid());
352 return para_strdup(pw? pw->pw_name : "unknown_user");
356 * get the home directory of the current user
358 * \return A dynammically allocated string that must be freed by the caller. If
359 * the home directory could not be found, this function returns "/tmp".
361 __must_check __malloc char *para_homedir(void)
363 struct passwd *pw = getpwuid(getuid());
364 return para_strdup(pw? pw->pw_dir : "/tmp");
368 * split string and return pointers to its parts.
370 * \param args the string to be split
371 * \param argv_ptr pointer to the list of substrings
372 * \param delim delimiter
374 * This function modifies \a args by replacing each occurance of \a delim by
375 * zero. A NULL-terminated array of pointers to char* is allocated dynamically
376 * and these pointers are initialized to point to the broken-up substrings
377 * within \a args. A pointer to this array is returned via \a argv_ptr.
379 * \return The number of substrings found in \a args.
381 __must_check unsigned split_args(char *args, char ***argv_ptr, int delim)
387 while ((p = strchr(p, delim))) {
391 *argv_ptr = para_malloc((n + 3) * sizeof(char *));
395 // printf("split_args: a:%s\n", p);
398 p = strchr(p, delim);
400 // printf("a:%s\n", p);
412 * ensure that file descriptors 0, 1, and 2 are valid
414 * Common approach that opens /dev/null until it gets a file descriptor greater
417 * \sa okir's Black Hats Manual.
419 void valid_fd_012(void)
424 fd = open("/dev/null", O_RDWR);
435 * get the own hostname
437 * \return A dynammically allocated string containing the hostname.
441 __malloc char *para_hostname(void)
446 return para_strdup(u.nodename);