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("realloc failed (size = %zu), 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 * \return A pointer to the allocated and zeroed-out memory, which is suitably
90 * aligned 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 \p exit(EXIT_FAILURE) on errors, i.e.
108 * there 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 * cut trailing newline
229 * \param buf the string to be chopped.
231 * Replace the last character in \p buf by zero if it is euqal to
232 * the newline character.
239 if (buf
[n
- 1] == '\n')
244 * get a random filename
246 * This is by no means a secure way to create temporary files in a hostile
247 * direcory like \p /tmp. However, it is OK to use for temp files, fifos,
248 * sockets that are created in ~/.paraslash. Result must be freed by the
251 * \return a pointer to a random filename.
253 __must_check __malloc
char *para_tmpname(void)
258 gettimeofday(&now
, NULL
);
261 return make_message("%08i", rand());
265 * create unique temporary file
267 * \param template the template to be passed to mkstemp()
268 * \param mode the desired mode of the tempfile
270 * This wrapper for mkstemp additionally uses fchmod() to
271 * set the given mode of the tempfile if mkstemp() returned success.
273 * \return The file descriptor of the temp file just created on success.
274 * On errors, -E_MKSTEMP or -E_FCHMOD is returned.
276 __must_check
int para_mkstemp(char *template, mode_t mode
)
278 int tmp
, fd
= mkstemp(template);
282 tmp
= fchmod(fd
, mode
);
291 * get the logname of the current user
293 * \return A dynammically allocated string that must be freed by the caller. On
294 * errors, the string "unknown user" is returned, i.e. this function never
299 __must_check __malloc
char *para_logname(void)
301 struct passwd
*pw
= getpwuid(getuid());
302 return para_strdup(pw
? pw
->pw_name
: "unknown_user");
306 * get the home directory of the current user
308 * \return A dynammically allocated string that must be freed by the caller. If
309 * the home directory could not be found, this function returns "/tmp".
311 __must_check __malloc
char *para_homedir(void)
313 struct passwd
*pw
= getpwuid(getuid());
314 return para_strdup(pw
? pw
->pw_dir
: "/tmp");
318 * split string and return pointers to its parts.
320 * \param args the string to be split
321 * \param argv_ptr pointer to the list of substrings
322 * \param delim delimiter
324 * This function modifies \a args by replacing each occurance of \a delim by
325 * zero. A \p NULL-terminated array of pointers to char* is allocated dynamically
326 * and these pointers are initialized to point to the broken-up substrings
327 * within \a args. A pointer to this array is returned via \a argv_ptr. It's OK
328 * to call this function with \a args \a == \p NULL.
330 * \return The number of substrings found in \a args.
332 __must_check
unsigned split_args(char *args
, char ***argv_ptr
, const char *delim
)
338 p
= args
+ strspn(args
, delim
);
340 i
= strcspn(p
, delim
);
345 p
+= strspn(p
, delim
);
347 *argv_ptr
= para_malloc((n
+ 1) * sizeof(char *));
350 p
= args
+ strspn(args
, delim
);
353 j
= strcspn(p
, delim
);
356 p
+= strcspn(p
, delim
);
360 p
+= strspn(p
, delim
);
369 * ensure that file descriptors 0, 1, and 2 are valid
371 * Common approach that opens /dev/null until it gets a file descriptor greater
374 * \sa okir's Black Hats Manual.
376 void valid_fd_012(void)
381 fd
= open("/dev/null", O_RDWR
);
392 * get the own hostname
394 * \return A dynammically allocated string containing the hostname.
398 __malloc
char *para_hostname(void)
403 return para_strdup(u
.nodename
);