Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 1 | /* vi: set sw=4 ts=4: */ |
| 2 | /* |
| 3 | * Utility routines. |
| 4 | * |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 5 | * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 6 | * |
Rob Landley | 4e9deec | 2006-02-20 02:44:30 +0000 | [diff] [blame] | 7 | * Licensed under GPLv2 or later, see file LICENSE in this tarball for details. |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 10 | #include <sys/types.h> |
| 11 | #include <sys/stat.h> |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 12 | #include <stdio.h> |
| 13 | #include <string.h> |
| 14 | #include <stdlib.h> |
| 15 | #include <unistd.h> |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 16 | #include <fcntl.h> |
Rob Landley | 552b56d | 2006-05-04 21:22:27 +0000 | [diff] [blame] | 17 | #include "busybox.h" |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 18 | |
| 19 | #ifndef DMALLOC |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 20 | #ifdef L_xmalloc |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 21 | void *xmalloc(size_t size) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 22 | { |
| 23 | void *ptr = malloc(size); |
Matt Kraai | a99b194 | 2002-02-26 15:28:22 +0000 | [diff] [blame] | 24 | if (ptr == NULL && size != 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 25 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 26 | return ptr; |
| 27 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 28 | #endif |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 29 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 30 | #ifdef L_xrealloc |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 31 | void *xrealloc(void *ptr, size_t size) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 32 | { |
Matt Kraai | a99b194 | 2002-02-26 15:28:22 +0000 | [diff] [blame] | 33 | ptr = realloc(ptr, size); |
| 34 | if (ptr == NULL && size != 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 35 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 36 | return ptr; |
| 37 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 38 | #endif |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 39 | |
Rob Landley | 80b8ff0 | 2006-05-19 20:36:49 +0000 | [diff] [blame] | 40 | #ifdef L_xzalloc |
| 41 | void *xzalloc(size_t size) |
| 42 | { |
| 43 | void *ptr = xmalloc(size); |
| 44 | memset(ptr, 0, size); |
| 45 | return ptr; |
| 46 | } |
| 47 | #endif |
| 48 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 49 | #ifdef L_xcalloc |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 50 | void *xcalloc(size_t nmemb, size_t size) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 51 | { |
| 52 | void *ptr = calloc(nmemb, size); |
Matt Kraai | a99b194 | 2002-02-26 15:28:22 +0000 | [diff] [blame] | 53 | if (ptr == NULL && nmemb != 0 && size != 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 54 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 55 | return ptr; |
| 56 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 57 | #endif |
Eric Andersen | 9f894f4 | 2003-07-05 22:15:43 +0000 | [diff] [blame] | 58 | #endif /* DMALLOC */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 59 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 60 | #ifdef L_xstrdup |
Rob Landley | 31642d7 | 2006-03-14 21:45:38 +0000 | [diff] [blame] | 61 | char * bb_xstrdup (const char *s) |
| 62 | { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 63 | char *t; |
| 64 | |
| 65 | if (s == NULL) |
| 66 | return NULL; |
| 67 | |
| 68 | t = strdup (s); |
| 69 | |
| 70 | if (t == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 71 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 72 | |
| 73 | return t; |
| 74 | } |
| 75 | #endif |
| 76 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 77 | #ifdef L_xstrndup |
Rob Landley | 31642d7 | 2006-03-14 21:45:38 +0000 | [diff] [blame] | 78 | char * bb_xstrndup (const char *s, int n) |
| 79 | { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 80 | char *t; |
| 81 | |
| 82 | if (s == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 83 | bb_error_msg_and_die("bb_xstrndup bug"); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 84 | |
| 85 | t = xmalloc(++n); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 86 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 87 | return safe_strncpy(t,s,n); |
| 88 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 89 | #endif |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 90 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 91 | #ifdef L_xfopen |
| 92 | FILE *bb_xfopen(const char *path, const char *mode) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 93 | { |
| 94 | FILE *fp; |
| 95 | if ((fp = fopen(path, mode)) == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 96 | bb_perror_msg_and_die("%s", path); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 97 | return fp; |
| 98 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 99 | #endif |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 100 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 101 | #ifdef L_xopen |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 102 | int bb_xopen(const char *pathname, int flags) |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 103 | { |
Bernhard Reutner-Fischer | c2cb0f3 | 2006-04-13 12:45:04 +0000 | [diff] [blame] | 104 | return bb_xopen3(pathname, flags, 0777); |
| 105 | } |
| 106 | #endif |
| 107 | |
| 108 | #ifdef L_xopen3 |
| 109 | int bb_xopen3(const char *pathname, int flags, int mode) |
| 110 | { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 111 | int ret; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 112 | |
Bernhard Reutner-Fischer | c2cb0f3 | 2006-04-13 12:45:04 +0000 | [diff] [blame] | 113 | ret = open(pathname, flags, mode); |
| 114 | if (ret < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 115 | bb_perror_msg_and_die("%s", pathname); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 116 | } |
| 117 | return ret; |
| 118 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 119 | #endif |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 120 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 121 | #ifdef L_xread |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 122 | ssize_t bb_xread(int fd, void *buf, size_t count) |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 123 | { |
| 124 | ssize_t size; |
| 125 | |
| 126 | size = read(fd, buf, count); |
Bernhard Reutner-Fischer | c2cb0f3 | 2006-04-13 12:45:04 +0000 | [diff] [blame] | 127 | if (size < 0) { |
"Vladimir N. Oleynik" | 4eb2fd6 | 2005-11-25 11:36:36 +0000 | [diff] [blame] | 128 | bb_perror_msg_and_die(bb_msg_read_error); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 129 | } |
| 130 | return(size); |
| 131 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 132 | #endif |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 133 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 134 | #ifdef L_xread_all |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 135 | void bb_xread_all(int fd, void *buf, size_t count) |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 136 | { |
| 137 | ssize_t size; |
| 138 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 139 | while (count) { |
| 140 | if ((size = bb_xread(fd, buf, count)) == 0) { /* EOF */ |
| 141 | bb_error_msg_and_die("Short read"); |
| 142 | } |
| 143 | count -= size; |
Manuel Novoa III | 948d490 | 2004-03-08 05:44:30 +0000 | [diff] [blame] | 144 | buf = ((char *) buf) + size; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 145 | } |
| 146 | return; |
| 147 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 148 | #endif |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 149 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 150 | #ifdef L_xread_char |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 151 | unsigned char bb_xread_char(int fd) |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 152 | { |
| 153 | char tmp; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 154 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 155 | bb_xread_all(fd, &tmp, 1); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 156 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 157 | return(tmp); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 158 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 159 | #endif |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 160 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 161 | #ifdef L_xferror |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 162 | void bb_xferror(FILE *fp, const char *fn) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 163 | { |
| 164 | if (ferror(fp)) { |
| 165 | bb_error_msg_and_die("%s", fn); |
| 166 | } |
| 167 | } |
| 168 | #endif |
| 169 | |
| 170 | #ifdef L_xferror_stdout |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 171 | void bb_xferror_stdout(void) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 172 | { |
| 173 | bb_xferror(stdout, bb_msg_standard_output); |
| 174 | } |
| 175 | #endif |
| 176 | |
| 177 | #ifdef L_xfflush_stdout |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 178 | void bb_xfflush_stdout(void) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 179 | { |
| 180 | if (fflush(stdout)) { |
| 181 | bb_perror_msg_and_die(bb_msg_standard_output); |
| 182 | } |
| 183 | } |
| 184 | #endif |
Rob Landley | 399d2b5 | 2006-05-25 23:02:40 +0000 | [diff] [blame] | 185 | |
| 186 | #ifdef L_spawn |
| 187 | // This does a fork/exec in one call, using vfork(). |
| 188 | pid_t bb_spawn(char **argv) |
| 189 | { |
| 190 | static int failed; |
| 191 | pid_t pid; |
| 192 | |
| 193 | // Be nice to nommu machines. |
| 194 | failed = 0; |
| 195 | pid = vfork(); |
| 196 | if (pid < 0) return pid; |
| 197 | if (!pid) { |
| 198 | execvp(*argv, argv); |
| 199 | |
| 200 | // We're sharing a stack with blocked parent, let parent know we failed |
| 201 | // and then exit to unblock parent (but don't run atexit() stuff, which |
| 202 | // would screw up parent.) |
| 203 | |
| 204 | failed = -1; |
| 205 | _exit(0); |
| 206 | } |
| 207 | return failed ? failed : pid; |
| 208 | } |
| 209 | #endif |
| 210 | |
| 211 | #ifdef L_xspawn |
| 212 | pid_t bb_xspawn(char **argv) |
| 213 | { |
| 214 | pid_t pid = bb_spawn(argv); |
| 215 | if (pid < 0) bb_perror_msg_and_die("%s", *argv); |
| 216 | return pid; |
| 217 | } |
| 218 | #endif |