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 */
27 #include <sys/utsname.h> /* uname() */
33 * paraslash's version of realloc()
35 * \param p pointer to the memory block, may be NULL
36 * \param size desired new size
38 * A wrapper for realloc(3). It calls \p exit(\p EXIT_FAILURE) on errors,
39 * i.e. there is no need to check the return value in the caller.
41 * \return A pointer to the newly allocated memory, which is suitably aligned
42 * for any kind of variable and may be different from \p p.
46 __must_check __malloc
void *para_realloc(void *p
, size_t size
)
49 * No need to check for NULL pointers: If p is NULL, the call
50 * to realloc is equivalent to malloc(size)
52 if (!(p
= realloc(p
, size
))) {
53 PARA_EMERG_LOG("%s", "realloc failed, aborting\n");
60 * paraslash's version of malloc()
62 * \param size desired new size
64 * A wrapper for malloc(3) which exits on errors.
66 * \return A pointer to the allocated memory, which is suitably aligned for any
71 __must_check __malloc
void *para_malloc(size_t size
)
73 void *p
= malloc(size
);
76 PARA_EMERG_LOG("%s", "malloc failed, aborting\n");
83 * paraslash's version of calloc()
85 * \param size desired new size
87 * A wrapper for calloc(3) which exits on errors.
89 * A pointer to the allocated and zeroed-out memory, which is suitably aligned
90 * for any kind of variable.
94 __must_check __malloc
void *para_calloc(size_t size
)
96 void *ret
= para_malloc(size
);
103 * paraslash's version of strdup()
105 * \param s string to be duplicated
107 * A wrapper for strdup(3). It calls exit(EXIT_FAILURE) on errors, i.e. there
108 * is no need to check the return value in the caller.
110 * \return A pointer to the duplicated string. If \p s was the NULL pointer,
111 * an pointer to an empty string is returned.
115 __must_check __malloc
char *para_strdup(const char *s
)
119 if ((ret
= strdup(s
? s
: "")))
121 PARA_EMERG_LOG("%s", "strdup failed, aborting\n");
126 * allocate a sufficiently large string and print into it
128 * \param fmt usual format string
130 * Produce output according to \p fmt. No artificial bound on the length of the
131 * resulting string is imposed.
133 * \return This function either returns a pointer to a string that must be
134 * freed by the caller or aborts without returning.
138 __must_check __printf_1_2 __malloc
char *make_message(const char *fmt
, ...)
142 PARA_VSPRINTF(fmt
, msg
);
147 * paraslash's version of strcat()
149 * \param a string to be appended to
150 * \param b string to append
152 * Append \p b to \p a.
154 * \return If \p a is NULL, return a pointer to a copy of \p b, i.e.
155 * para_strcat(NULL, b) is equivalent to para_strdup(b). If \p b is NULL,
156 * return \p a without making a copy of \p a. Otherwise, construct the
157 * concatenation \p c, free \p a (but not \p b) and return \p c.
161 __must_check __malloc
char *para_strcat(char *a
, const char *b
)
166 return para_strdup(b
);
169 tmp
= make_message("%s%s", a
, b
);
175 * paraslash's version of dirname()
177 * \param name pointer to the full path
179 * Compute the directory component of \p name
181 * \return If \p name is \รพ NULL or the empty string, return \p NULL.
182 * Otherwise, Make a copy of \p name and return its directory component. Caller
183 * is responsible to free the result.
185 __must_check __malloc
char *para_dirname(const char *name
)
191 ret
= para_strdup(name
);
192 p
= strrchr(ret
, '/');
201 * paraslash's version of basename()
203 * \param name Pointer to the full path
205 * Compute the filename component of \p name
207 * \return If \p name is \p NULL or the empty string, return \p NULL,
208 * Otherwise, make a copy of \p name and return its filename component. Caller
209 * is responsible to free the result.
211 __must_check __malloc
char *para_basename(const char *name
)
217 p
= strrchr(name
, '/');
219 return para_strdup(name
);
223 return para_strdup(p
);
227 * simple search and replace routine
229 * \param src source string
230 * \param macro_name the name of the macro
231 * \param replacement the replacement format string
233 * In \p src, replace each occurence of \p macro_name(arg) by the string
234 * determined by the \p replacement format string. \p replacement may (but
235 * needs not) contain a single string conversion specifier (%s) which gets
236 * replaced by \p arg.
238 * \return A string in which all matches in \p src are replaced, or \p NULL if
239 * an syntax error was encountered. Caller must free the result.
243 __must_check __malloc
char *s_a_r(const char *src
, const char* macro_name
,
244 const char *replacement
)
248 regmatch_t pmatch
[1];
251 const char *bufptr
= src
;
253 if (!macro_name
|| !replacement
|| !src
)
254 return para_strdup(src
);
255 regcomp(&preg
, macro_name
, 0);
256 while (regexec(&preg
, bufptr
, nmatch
, pmatch
, eflags
)
258 char *tmp
, *arg
, *o_bracket
, *c_bracket
;
260 o_bracket
= strchr(bufptr
+ pmatch
[0].rm_so
, '(');
261 c_bracket
= o_bracket
? strchr(o_bracket
, ')') : NULL
;
264 tmp
= para_strdup(bufptr
);
265 tmp
[pmatch
[0].rm_so
] = '\0';
266 dest
= para_strcat(dest
, tmp
);
269 arg
= para_strdup(o_bracket
+ 1);
270 arg
[c_bracket
- o_bracket
- 1] = '\0';
271 tmp
= make_message(replacement
, arg
);
273 dest
= para_strcat(dest
, tmp
);
278 dest
= para_strcat(dest
, bufptr
);
279 // PARA_DEBUG_LOG("%s: returning %s\n", __func__, dest);
286 * replace a string according to a list of macros
288 * \param macro_list the array containing a macro/replacement pairs.
289 * \param src the source string
291 * This function just calls s_a_r() for each element of \p macro_list.
293 * \return \p NULL if one of the underlying calls to \p s_a_r returned \p NULL.
294 * Otherwise the completely expanded version of \p src is returned.
296 __must_check __malloc
char *s_a_r_list(struct para_macro
*macro_list
, char *src
)
298 struct para_macro
*mp
= macro_list
;
299 char *ret
= NULL
, *tmp
= para_strdup(src
);
302 ret
= s_a_r(tmp
, mp
->name
, mp
->replacement
);
304 if (!ret
) /* syntax error */
309 //PARA_DEBUG_LOG("%s: returning %s\n", __func__, dest);
314 * cut trailing newline
316 * \param buf the string to be chopped.
318 * Replace the last character in \p buf by zero if it is euqal to
319 * the newline character.
326 if (buf
[n
- 1] == '\n')
331 * get a random filename
333 * This is by no means a secure way to create temporary files in a hostile
334 * direcory like \p /tmp. However, it is OK to use for temp files, fifos,
335 * sockets that are created in ~/.paraslash. Result must be freed by the
338 * \return a pointer to a random filename.
340 __must_check __malloc
char *para_tmpname(void)
343 gettimeofday(&now
, NULL
);
345 return make_message("%08i", rand());
349 * create unique temporary file
351 * \param template the template to be passed to mkstemp()
352 * \param mode the desired mode of the tempfile
354 * This wrapper for mkstemp additionally uses fchmod() to
355 * set the given mode of the tempfile if mkstemp() returned success.
357 * \return The file descriptor of the temp file just created on success.
358 * On errors, -E_MKSTEMP or -E_FCHMOD is returned.
360 __must_check
int para_mkstemp(char *template, mode_t mode
)
362 int tmp
, fd
= mkstemp(template);
366 tmp
= fchmod(fd
, mode
);
375 * get the logname of the current user
377 * \return A dynammically allocated string that must be freed by the caller. On
378 * errors, the string "unknown user" is returned, i.e. this function never
383 __must_check __malloc
char *para_logname(void)
385 struct passwd
*pw
= getpwuid(getuid());
386 return para_strdup(pw
? pw
->pw_name
: "unknown_user");
390 * get the home directory of the current user
392 * \return A dynammically allocated string that must be freed by the caller. If
393 * the home directory could not be found, this function returns "/tmp".
395 __must_check __malloc
char *para_homedir(void)
397 struct passwd
*pw
= getpwuid(getuid());
398 return para_strdup(pw
? pw
->pw_dir
: "/tmp");
402 * split string and return pointers to its parts.
404 * \param args the string to be split
405 * \param argv_ptr pointer to the list of substrings
406 * \param delim delimiter
408 * This function modifies \p args by replacing each occurance of \p delim by
409 * zero. A NULL-terminated array of pointers to char* is allocated dynamically
410 * and these pointers are initialized to point to the broken-up substrings
411 * within \p args. A pointer to this array is returned via \p argv_ptr. It's OK
412 * to call this function with \p args \p == \p NULL.
414 * \return The number of substrings found in \p args.
416 __must_check
unsigned split_args(char *args
, char ***argv_ptr
, const char *delim
)
422 p
= args
+ strspn(args
, delim
);
424 i
= strcspn(p
, delim
);
429 p
+= strspn(p
, delim
);
431 *argv_ptr
= para_malloc((n
+ 1) * sizeof(char *));
434 p
= args
+ strspn(args
, delim
);
437 j
= strcspn(p
, delim
);
440 p
+= strcspn(p
, delim
);
444 p
+= strspn(p
, delim
);
453 * ensure that file descriptors 0, 1, and 2 are valid
455 * Common approach that opens /dev/null until it gets a file descriptor greater
458 * \sa okir's Black Hats Manual.
460 void valid_fd_012(void)
465 fd
= open("/dev/null", O_RDWR
);
476 * get the own hostname
478 * \return A dynammically allocated string containing the hostname.
482 __malloc
char *para_hostname(void)
487 return para_strdup(u
.nodename
);