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 */
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.
42 __must_check __malloc
void *para_realloc(void *p
, size_t size
)
45 * No need to check for NULL pointers: If p is NULL, the call
46 * to realloc is equivalent to malloc(size)
48 if (!(p
= realloc(p
, size
))) {
49 PARA_EMERG_LOG("%s", "realloc failed, aborting\n");
56 * paraslash's version of malloc()
58 * \param size desired new size
60 * A wrapper for malloc(3) which exits on errors.
63 __must_check __malloc
void *para_malloc(size_t size
)
65 void *p
= malloc(size
);
68 PARA_EMERG_LOG("%s", "malloc failed, aborting\n");
75 * paraslash's version of calloc()
77 * \param size desired new size
79 * A wrapper for calloc(3) which exits on errors.
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 exit(EXIT_FAILURE) on
96 * errors, i.e. there is no need to check the return value in the caller.
97 * Moreover, this wrapper checks for \a s being NULL and returns an empty
98 * string in this case.
102 __must_check __malloc
char *para_strdup(const char *s
)
106 if ((ret
= strdup(s
? s
: "")))
108 PARA_EMERG_LOG("%s", "strdup failed, aborting\n");
113 * allocate a sufficiently large string and print into it
115 * \param fmt usual format string
117 * Produce output according to \a fmt. No artificial bound on the length of the
118 * resulting string is imposed. This function either returns a pointer to a
119 * string that must be freed by the caller or aborts without returning.
123 __must_check __printf_1_2 __malloc
char *make_message(const char *fmt
, ...)
127 PARA_VSPRINTF(fmt
, msg
);
132 * paraslash's version of strcat()
134 * \param a string to be appended to
135 * \param b string to append
137 * Append \a b to \a a. If \a a is NULL, return a copy of \a b, i.e.
138 * para_strcat(NULL, b) is equivalent to para_strdup(b). If \a b is NULL,
139 * return \a a without making a copy of \a a. Otherwise, construct the
140 * concatenation \a c, free \a a (but not \a b) and return \a c.
144 __must_check __malloc
char *para_strcat(char *a
, const char *b
)
149 return para_strdup(b
);
152 tmp
= make_message("%s%s", a
, b
);
158 * paraslash's version of dirname()
160 * \param name pointer to The full path
162 * If \a name is \รพ NULL or the empty string, return \p NULL, Otherwise, Make a
163 * copy of \a name and return its directory component. Caller is responsible to
166 __must_check __malloc
char *para_dirname(const char *name
)
172 ret
= para_strdup(name
);
173 p
= strrchr(ret
, '/');
182 * paraslash's version of basename()
184 * \param name Pointer to the full path
186 * If \a name is \p NULL or the empty string, return \p NULL, Otherwise, make a
187 * copy of \a name and return its filename component. Caller is responsible to
190 __must_check __malloc
char *para_basename(const char *name
)
196 p
= strrchr(name
, '/');
198 return para_strdup(name
);
202 return para_strdup(p
);
206 * simple search and replace routine
208 * \param src source string
209 * \param macro_name the name of the macro
210 * \param replacement the replacement format string
212 * Replace \a macro_name(arg) by \a replacement. \a replacement is a format
213 * string which may contain a single string conversion specifier which gets
216 * \return A string in which all matches in \a src are replaced, or NULL if an
217 * syntax error was encountered. Caller must free the result.
221 __must_check __malloc
char *s_a_r(const char *src
, const char* macro_name
,
222 const char *replacement
)
226 regmatch_t pmatch
[1];
229 const char *bufptr
= src
;
231 if (!macro_name
|| !replacement
|| !src
)
232 return para_strdup(src
);
233 regcomp(&preg
, macro_name
, 0);
234 while (regexec(&preg
, bufptr
, nmatch
, pmatch
, eflags
)
236 char *tmp
, *arg
, *o_bracket
, *c_bracket
;
238 o_bracket
= strchr(bufptr
+ pmatch
[0].rm_so
, '(');
239 c_bracket
= o_bracket
? strchr(o_bracket
, ')') : NULL
;
242 tmp
= para_strdup(bufptr
);
243 tmp
[pmatch
[0].rm_so
] = '\0';
244 dest
= para_strcat(dest
, tmp
);
247 arg
= para_strdup(o_bracket
+ 1);
248 arg
[c_bracket
- o_bracket
- 1] = '\0';
249 tmp
= make_message(replacement
, arg
);
251 dest
= para_strcat(dest
, tmp
);
256 dest
= para_strcat(dest
, bufptr
);
257 // PARA_DEBUG_LOG("%s: returning %s\n", __func__, dest);
264 * replace a string according to a list of macros
266 * \param macro_list the array containing a macro/replacement pairs.
267 * \param src the source string
269 * This function just calls s_a_r() for each element of \a macro_list.
271 __must_check __malloc
char *s_a_r_list(struct para_macro
*macro_list
, char *src
)
273 struct para_macro
*mp
= macro_list
;
274 char *ret
= NULL
, *tmp
= para_strdup(src
);
277 ret
= s_a_r(tmp
, mp
->name
, mp
->replacement
);
279 if (!ret
) /* syntax error */
284 //PARA_DEBUG_LOG("%s: returning %s\n", __func__, dest);
289 * cut trailing newline
291 * \param buf the string to be chopped.
293 * Replace the last character in \a buf by zero if it is euqal to
294 * the newline character.
301 if (buf
[n
- 1] == '\n')
306 * get a random filename
308 * This is by no means a secure way to create temporary files in a hostile
309 * direcory like /tmp. However, it is OK to use for temp files, fifos, sockets
310 * that are created in ~/.paraslash. Result must be freed by the caller.
312 __must_check __malloc
char *para_tmpname(void)
315 gettimeofday(&now
, NULL
);
317 return make_message("%08i", rand());
321 * create unique temporary file
323 * \param template the template to be passed to mkstemp()
324 * \param mode the desired mode of the tempfile
326 * This wrapper for mkstemp additionally uses fchmod() to
327 * set the given mode of the tempfile if mkstemp() returned success.
328 * Return value: The file descriptor of the temp file just created on success.
329 * On errors, -E_MKSTEMP or -E_FCHMOD is returned.
331 __must_check
int para_mkstemp(char *template, mode_t mode
)
333 int tmp
, fd
= mkstemp(template);
337 tmp
= fchmod(fd
, mode
);
346 * get the logname of the current user
348 * \return A dynammically allocated string that must be freed by the caller. On
349 * errors, the string "unknown user" is returned, i.e. this function never
352 __must_check __malloc
char *para_logname(void)
354 struct passwd
*pw
= getpwuid(getuid());
355 return para_strdup(pw
? pw
->pw_name
: "unknown_user");
359 * get the home directory of the current user
361 * \return A dynammically allocated string that must be freed by the caller. If
362 * the home directory could not be found, this function returns "/tmp".
364 __must_check __malloc
char *para_homedir(void)
366 struct passwd
*pw
= getpwuid(getuid());
367 return para_strdup(pw
? pw
->pw_dir
: "/tmp");
371 * split string and return pointers to its parts.
373 * \param args the string to be split
374 * \param argv_ptr pointer to the list of substrings
375 * \param delim delimiter
377 * This function modifies \a args by replacing each occurance of \a delim by
378 * zero. A NULL-terminated array of pointers to char* is allocated dynamically
379 * and these pointers are initialized to point to the broken-up substrings
380 * within \a args. A pointer to this array is returned via \a argv_ptr. It's OK
381 * to call this function with \a args == NULL.
383 * \return The number of substrings found in \a args.
385 __must_check
unsigned split_args(char *args
, char ***argv_ptr
, int delim
)
391 while (p
&& (p
= strchr(p
, delim
))) {
395 *argv_ptr
= para_calloc((n
+ 3) * sizeof(char *));
399 // printf("split_args: a:%s\n", p);
402 p
= strchr(p
, delim
);
404 // printf("a:%s\n", p);
415 * ensure that file descriptors 0, 1, and 2 are valid
417 * Common approach that opens /dev/null until it gets a file descriptor greater
420 * \sa okir's Black Hats Manual.
422 void valid_fd_012(void)
427 fd
= open("/dev/null", O_RDWR
);
438 * get the own hostname
440 * \return A dynammically allocated string containing the hostname.
444 __malloc
char *para_hostname(void)
449 return para_strdup(u
.nodename
);