diff options
Diffstat (limited to 'libbb/xfuncs.c')
-rw-r--r-- | libbb/xfuncs.c | 142 |
1 files changed, 120 insertions, 22 deletions
diff --git a/libbb/xfuncs.c b/libbb/xfuncs.c index 8562a4fcb..699d09c67 100644 --- a/libbb/xfuncs.c +++ b/libbb/xfuncs.c | |||
@@ -3,18 +3,12 @@ | |||
3 | * Utility routines. | 3 | * Utility routines. |
4 | * | 4 | * |
5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
6 | * Copyright (C) 2006 Rob Landley | ||
7 | * Copyright (C) 2006 Denis Vlasenko | ||
6 | * | 8 | * |
7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. | 9 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
8 | */ | 10 | */ |
9 | 11 | ||
10 | #include <sys/types.h> | ||
11 | #include <sys/stat.h> | ||
12 | #include <sys/wait.h> | ||
13 | #include <stdio.h> | ||
14 | #include <string.h> | ||
15 | #include <stdlib.h> | ||
16 | #include <unistd.h> | ||
17 | #include <fcntl.h> | ||
18 | #include "busybox.h" | 12 | #include "busybox.h" |
19 | 13 | ||
20 | #ifndef DMALLOC | 14 | #ifndef DMALLOC |
@@ -59,7 +53,7 @@ void *xcalloc(size_t nmemb, size_t size) | |||
59 | #endif /* DMALLOC */ | 53 | #endif /* DMALLOC */ |
60 | 54 | ||
61 | #ifdef L_xstrdup | 55 | #ifdef L_xstrdup |
62 | char * bb_xstrdup (const char *s) | 56 | char * xstrdup (const char *s) |
63 | { | 57 | { |
64 | char *t; | 58 | char *t; |
65 | 59 | ||
@@ -76,12 +70,12 @@ char * bb_xstrdup (const char *s) | |||
76 | #endif | 70 | #endif |
77 | 71 | ||
78 | #ifdef L_xstrndup | 72 | #ifdef L_xstrndup |
79 | char * bb_xstrndup (const char *s, int n) | 73 | char * xstrndup (const char *s, int n) |
80 | { | 74 | { |
81 | char *t; | 75 | char *t; |
82 | 76 | ||
83 | if (ENABLE_DEBUG && s == NULL) | 77 | if (ENABLE_DEBUG && s == NULL) |
84 | bb_error_msg_and_die("bb_xstrndup bug"); | 78 | bb_error_msg_and_die("xstrndup bug"); |
85 | 79 | ||
86 | t = xmalloc(++n); | 80 | t = xmalloc(++n); |
87 | 81 | ||
@@ -90,7 +84,7 @@ char * bb_xstrndup (const char *s, int n) | |||
90 | #endif | 84 | #endif |
91 | 85 | ||
92 | #ifdef L_xfopen | 86 | #ifdef L_xfopen |
93 | FILE *bb_xfopen(const char *path, const char *mode) | 87 | FILE *xfopen(const char *path, const char *mode) |
94 | { | 88 | { |
95 | FILE *fp; | 89 | FILE *fp; |
96 | if ((fp = fopen(path, mode)) == NULL) | 90 | if ((fp = fopen(path, mode)) == NULL) |
@@ -100,14 +94,14 @@ FILE *bb_xfopen(const char *path, const char *mode) | |||
100 | #endif | 94 | #endif |
101 | 95 | ||
102 | #ifdef L_xopen | 96 | #ifdef L_xopen |
103 | int bb_xopen(const char *pathname, int flags) | 97 | int xopen(const char *pathname, int flags) |
104 | { | 98 | { |
105 | return bb_xopen3(pathname, flags, 0777); | 99 | return xopen3(pathname, flags, 0777); |
106 | } | 100 | } |
107 | #endif | 101 | #endif |
108 | 102 | ||
109 | #ifdef L_xopen3 | 103 | #ifdef L_xopen3 |
110 | int bb_xopen3(const char *pathname, int flags, int mode) | 104 | int xopen3(const char *pathname, int flags, int mode) |
111 | { | 105 | { |
112 | int ret; | 106 | int ret; |
113 | 107 | ||
@@ -175,7 +169,7 @@ unsigned char xread_char(int fd) | |||
175 | #endif | 169 | #endif |
176 | 170 | ||
177 | #ifdef L_xferror | 171 | #ifdef L_xferror |
178 | void bb_xferror(FILE *fp, const char *fn) | 172 | void xferror(FILE *fp, const char *fn) |
179 | { | 173 | { |
180 | if (ferror(fp)) { | 174 | if (ferror(fp)) { |
181 | bb_error_msg_and_die("%s", fn); | 175 | bb_error_msg_and_die("%s", fn); |
@@ -184,14 +178,14 @@ void bb_xferror(FILE *fp, const char *fn) | |||
184 | #endif | 178 | #endif |
185 | 179 | ||
186 | #ifdef L_xferror_stdout | 180 | #ifdef L_xferror_stdout |
187 | void bb_xferror_stdout(void) | 181 | void xferror_stdout(void) |
188 | { | 182 | { |
189 | bb_xferror(stdout, bb_msg_standard_output); | 183 | xferror(stdout, bb_msg_standard_output); |
190 | } | 184 | } |
191 | #endif | 185 | #endif |
192 | 186 | ||
193 | #ifdef L_xfflush_stdout | 187 | #ifdef L_xfflush_stdout |
194 | void bb_xfflush_stdout(void) | 188 | void xfflush_stdout(void) |
195 | { | 189 | { |
196 | if (fflush(stdout)) { | 190 | if (fflush(stdout)) { |
197 | bb_perror_msg_and_die(bb_msg_standard_output); | 191 | bb_perror_msg_and_die(bb_msg_standard_output); |
@@ -201,7 +195,7 @@ void bb_xfflush_stdout(void) | |||
201 | 195 | ||
202 | #ifdef L_spawn | 196 | #ifdef L_spawn |
203 | // This does a fork/exec in one call, using vfork(). | 197 | // This does a fork/exec in one call, using vfork(). |
204 | pid_t bb_spawn(char **argv) | 198 | pid_t spawn(char **argv) |
205 | { | 199 | { |
206 | static int failed; | 200 | static int failed; |
207 | pid_t pid; | 201 | pid_t pid; |
@@ -226,9 +220,9 @@ pid_t bb_spawn(char **argv) | |||
226 | #endif | 220 | #endif |
227 | 221 | ||
228 | #ifdef L_xspawn | 222 | #ifdef L_xspawn |
229 | pid_t bb_xspawn(char **argv) | 223 | pid_t xspawn(char **argv) |
230 | { | 224 | { |
231 | pid_t pid = bb_spawn(argv); | 225 | pid_t pid = spawn(argv); |
232 | if (pid < 0) bb_perror_msg_and_die("%s", *argv); | 226 | if (pid < 0) bb_perror_msg_and_die("%s", *argv); |
233 | return pid; | 227 | return pid; |
234 | } | 228 | } |
@@ -347,3 +341,107 @@ off_t fdlength(int fd) | |||
347 | return pos + 1; | 341 | return pos + 1; |
348 | } | 342 | } |
349 | #endif | 343 | #endif |
344 | |||
345 | #ifdef L_xasprintf | ||
346 | char *xasprintf(const char *format, ...) | ||
347 | { | ||
348 | va_list p; | ||
349 | int r; | ||
350 | char *string_ptr; | ||
351 | |||
352 | #if 1 | ||
353 | // GNU extension | ||
354 | va_start(p, format); | ||
355 | r = vasprintf(&string_ptr, format, p); | ||
356 | va_end(p); | ||
357 | #else | ||
358 | // Bloat for systems that haven't got the GNU extension. | ||
359 | va_start(p, format); | ||
360 | r = vsnprintf(NULL, 0, format, p); | ||
361 | va_end(p); | ||
362 | string_ptr = xmalloc(r+1); | ||
363 | va_start(p, format); | ||
364 | r = vsnprintf(string_ptr, r+1, format, p); | ||
365 | va_end(p); | ||
366 | #endif | ||
367 | |||
368 | if (r < 0) bb_perror_msg_and_die("xasprintf"); | ||
369 | return string_ptr; | ||
370 | } | ||
371 | #endif | ||
372 | |||
373 | #ifdef L_xprint_and_close_file | ||
374 | void xprint_and_close_file(FILE *file) | ||
375 | { | ||
376 | // copyfd outputs error messages for us. | ||
377 | if (bb_copyfd_eof(fileno(file), 1) == -1) exit(bb_default_error_retval); | ||
378 | |||
379 | fclose(file); | ||
380 | } | ||
381 | #endif | ||
382 | |||
383 | #ifdef L_xchdir | ||
384 | void xchdir(const char *path) | ||
385 | { | ||
386 | if (chdir(path)) | ||
387 | bb_perror_msg_and_die("chdir(%s)", path); | ||
388 | } | ||
389 | #endif | ||
390 | |||
391 | #ifdef L_warn_opendir | ||
392 | DIR *warn_opendir(const char *path) | ||
393 | { | ||
394 | DIR *dp; | ||
395 | |||
396 | if ((dp = opendir(path)) == NULL) { | ||
397 | bb_perror_msg("unable to open `%s'", path); | ||
398 | return NULL; | ||
399 | } | ||
400 | return dp; | ||
401 | } | ||
402 | #endif | ||
403 | |||
404 | #ifdef L_xopendir | ||
405 | DIR *xopendir(const char *path) | ||
406 | { | ||
407 | DIR *dp; | ||
408 | |||
409 | if ((dp = opendir(path)) == NULL) | ||
410 | bb_perror_msg_and_die("unable to open `%s'", path); | ||
411 | return dp; | ||
412 | } | ||
413 | #endif | ||
414 | |||
415 | #ifdef L_xdaemon | ||
416 | #ifndef BB_NOMMU | ||
417 | void xdaemon(int nochdir, int noclose) | ||
418 | { | ||
419 | if (daemon(nochdir, noclose)) bb_perror_msg_and_die("daemon"); | ||
420 | } | ||
421 | #endif | ||
422 | #endif | ||
423 | |||
424 | #ifdef L_xbind | ||
425 | void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen) | ||
426 | { | ||
427 | if (bind(sockfd, my_addr, addrlen)) bb_perror_msg_and_die("bind"); | ||
428 | } | ||
429 | #endif | ||
430 | |||
431 | #ifdef L_xsocket | ||
432 | int xsocket(int domain, int type, int protocol) | ||
433 | { | ||
434 | int r = socket(domain, type, protocol); | ||
435 | |||
436 | if (r < 0) bb_perror_msg_and_die("socket"); | ||
437 | |||
438 | return r; | ||
439 | } | ||
440 | #endif | ||
441 | |||
442 | #ifdef L_xlisten | ||
443 | void xlisten(int s, int backlog) | ||
444 | { | ||
445 | if (listen(s, backlog)) bb_perror_msg_and_die("listen"); | ||
446 | } | ||
447 | #endif | ||