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> |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 6 | * Copyright (C) 2006 Rob Landley |
| 7 | * Copyright (C) 2006 Denis Vlasenko |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 8 | * |
Rob Landley | 4e9deec | 2006-02-20 02:44:30 +0000 | [diff] [blame] | 9 | * 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] | 10 | */ |
| 11 | |
Rob Landley | 552b56d | 2006-05-04 21:22:27 +0000 | [diff] [blame] | 12 | #include "busybox.h" |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 13 | |
| 14 | #ifndef DMALLOC |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 15 | #ifdef L_xmalloc |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 16 | void *xmalloc(size_t size) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 17 | { |
| 18 | void *ptr = malloc(size); |
Matt Kraai | a99b194 | 2002-02-26 15:28:22 +0000 | [diff] [blame] | 19 | if (ptr == NULL && size != 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 20 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 21 | return ptr; |
| 22 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 23 | #endif |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 24 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 25 | #ifdef L_xrealloc |
Rob Landley | dfba741 | 2006-03-06 20:47:33 +0000 | [diff] [blame] | 26 | void *xrealloc(void *ptr, size_t size) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 27 | { |
Matt Kraai | a99b194 | 2002-02-26 15:28:22 +0000 | [diff] [blame] | 28 | ptr = realloc(ptr, size); |
| 29 | if (ptr == NULL && size != 0) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 30 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 31 | return ptr; |
| 32 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 33 | #endif |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 34 | |
Rob Landley | 80b8ff0 | 2006-05-19 20:36:49 +0000 | [diff] [blame] | 35 | #ifdef L_xzalloc |
| 36 | void *xzalloc(size_t size) |
| 37 | { |
| 38 | void *ptr = xmalloc(size); |
| 39 | memset(ptr, 0, size); |
| 40 | return ptr; |
| 41 | } |
| 42 | #endif |
| 43 | |
Eric Andersen | 9f894f4 | 2003-07-05 22:15:43 +0000 | [diff] [blame] | 44 | #endif /* DMALLOC */ |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 45 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 46 | #ifdef L_xstrdup |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 47 | char * xstrdup (const char *s) |
Rob Landley | 31642d7 | 2006-03-14 21:45:38 +0000 | [diff] [blame] | 48 | { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 49 | char *t; |
| 50 | |
| 51 | if (s == NULL) |
| 52 | return NULL; |
| 53 | |
| 54 | t = strdup (s); |
| 55 | |
| 56 | if (t == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 57 | bb_error_msg_and_die(bb_msg_memory_exhausted); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 58 | |
| 59 | return t; |
| 60 | } |
| 61 | #endif |
| 62 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 63 | #ifdef L_xstrndup |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 64 | char * xstrndup (const char *s, int n) |
Rob Landley | 31642d7 | 2006-03-14 21:45:38 +0000 | [diff] [blame] | 65 | { |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 66 | char *t; |
| 67 | |
Rob Landley | 8bbdb87 | 2006-06-30 16:36:56 +0000 | [diff] [blame] | 68 | if (ENABLE_DEBUG && s == NULL) |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 69 | bb_error_msg_and_die("xstrndup bug"); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 70 | |
| 71 | t = xmalloc(++n); |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 72 | |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 73 | return safe_strncpy(t,s,n); |
| 74 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 75 | #endif |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 76 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 77 | #ifdef L_xfopen |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 78 | FILE *xfopen(const char *path, const char *mode) |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 79 | { |
| 80 | FILE *fp; |
| 81 | if ((fp = fopen(path, mode)) == NULL) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 82 | bb_perror_msg_and_die("%s", path); |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 83 | return fp; |
| 84 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 85 | #endif |
Eric Andersen | aad1a88 | 2001-03-16 22:47:14 +0000 | [diff] [blame] | 86 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 87 | #ifdef L_xopen |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 88 | int xopen(const char *pathname, int flags) |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 89 | { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 90 | return xopen3(pathname, flags, 0777); |
Bernhard Reutner-Fischer | c2cb0f3 | 2006-04-13 12:45:04 +0000 | [diff] [blame] | 91 | } |
| 92 | #endif |
| 93 | |
| 94 | #ifdef L_xopen3 |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 95 | int xopen3(const char *pathname, int flags, int mode) |
Bernhard Reutner-Fischer | c2cb0f3 | 2006-04-13 12:45:04 +0000 | [diff] [blame] | 96 | { |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 97 | int ret; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 98 | |
Bernhard Reutner-Fischer | c2cb0f3 | 2006-04-13 12:45:04 +0000 | [diff] [blame] | 99 | ret = open(pathname, flags, mode); |
| 100 | if (ret < 0) { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 101 | bb_perror_msg_and_die("%s", pathname); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 102 | } |
| 103 | return ret; |
| 104 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 105 | #endif |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 106 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 107 | #ifdef L_xread |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 108 | |
| 109 | // Die with an error message if we can't read the entire buffer. |
| 110 | |
| 111 | void xread(int fd, void *buf, size_t count) |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 112 | { |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 113 | while (count) { |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 114 | ssize_t size; |
| 115 | |
| 116 | if ((size = safe_read(fd, buf, count)) < 1) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 117 | bb_error_msg_and_die("Short read"); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 118 | count -= size; |
Manuel Novoa III | 948d490 | 2004-03-08 05:44:30 +0000 | [diff] [blame] | 119 | buf = ((char *) buf) + size; |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 120 | } |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 121 | } |
| 122 | #endif |
| 123 | |
| 124 | #ifdef L_xwrite |
| 125 | |
| 126 | // Die with an error message if we can't write the entire buffer. |
| 127 | |
| 128 | void xwrite(int fd, void *buf, size_t count) |
| 129 | { |
| 130 | while (count) { |
| 131 | ssize_t size; |
| 132 | |
| 133 | if ((size = safe_write(fd, buf, count)) < 1) |
| 134 | bb_error_msg_and_die("Short write"); |
| 135 | count -= size; |
| 136 | buf = ((char *) buf) + size; |
| 137 | } |
| 138 | } |
| 139 | #endif |
| 140 | |
| 141 | #ifdef L_xlseek |
| 142 | |
| 143 | // Die if we can't lseek to the right spot. |
| 144 | |
| 145 | void xlseek(int fd, off_t offset, int whence) |
| 146 | { |
Rob Landley | 2c55fca | 2006-08-04 05:24:58 +0000 | [diff] [blame^] | 147 | if (offset != lseek(fd, offset, whence)) bb_error_msg_and_die("lseek"); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 148 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 149 | #endif |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 150 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 151 | #ifdef L_xread_char |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 152 | unsigned char xread_char(int fd) |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 153 | { |
| 154 | char tmp; |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 155 | |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 156 | xread(fd, &tmp, 1); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 157 | |
Eric Andersen | c7bda1c | 2004-03-15 08:29:22 +0000 | [diff] [blame] | 158 | return(tmp); |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 159 | } |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 160 | #endif |
Glenn L McGrath | 7ca04f3 | 2002-09-25 02:47:48 +0000 | [diff] [blame] | 161 | |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 162 | #ifdef L_xferror |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 163 | void xferror(FILE *fp, const char *fn) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 164 | { |
| 165 | if (ferror(fp)) { |
| 166 | bb_error_msg_and_die("%s", fn); |
| 167 | } |
| 168 | } |
| 169 | #endif |
| 170 | |
| 171 | #ifdef L_xferror_stdout |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 172 | void xferror_stdout(void) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 173 | { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 174 | xferror(stdout, bb_msg_standard_output); |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 175 | } |
| 176 | #endif |
| 177 | |
| 178 | #ifdef L_xfflush_stdout |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 179 | void xfflush_stdout(void) |
Manuel Novoa III | cad5364 | 2003-03-19 09:13:01 +0000 | [diff] [blame] | 180 | { |
| 181 | if (fflush(stdout)) { |
| 182 | bb_perror_msg_and_die(bb_msg_standard_output); |
| 183 | } |
| 184 | } |
| 185 | #endif |
Rob Landley | 399d2b5 | 2006-05-25 23:02:40 +0000 | [diff] [blame] | 186 | |
| 187 | #ifdef L_spawn |
| 188 | // This does a fork/exec in one call, using vfork(). |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 189 | pid_t spawn(char **argv) |
Rob Landley | 399d2b5 | 2006-05-25 23:02:40 +0000 | [diff] [blame] | 190 | { |
| 191 | static int failed; |
| 192 | pid_t pid; |
Rob Landley | c7ddefc | 2006-06-14 01:24:33 +0000 | [diff] [blame] | 193 | void *app = find_applet_by_name(argv[0]); |
Rob Landley | 399d2b5 | 2006-05-25 23:02:40 +0000 | [diff] [blame] | 194 | |
| 195 | // Be nice to nommu machines. |
| 196 | failed = 0; |
| 197 | pid = vfork(); |
| 198 | if (pid < 0) return pid; |
| 199 | if (!pid) { |
Rob Landley | c7ddefc | 2006-06-14 01:24:33 +0000 | [diff] [blame] | 200 | execvp(app ? CONFIG_BUSYBOX_EXEC_PATH : *argv, argv); |
Rob Landley | 399d2b5 | 2006-05-25 23:02:40 +0000 | [diff] [blame] | 201 | |
| 202 | // We're sharing a stack with blocked parent, let parent know we failed |
| 203 | // and then exit to unblock parent (but don't run atexit() stuff, which |
| 204 | // would screw up parent.) |
| 205 | |
| 206 | failed = -1; |
| 207 | _exit(0); |
| 208 | } |
| 209 | return failed ? failed : pid; |
| 210 | } |
| 211 | #endif |
| 212 | |
| 213 | #ifdef L_xspawn |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 214 | pid_t xspawn(char **argv) |
Rob Landley | 399d2b5 | 2006-05-25 23:02:40 +0000 | [diff] [blame] | 215 | { |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 216 | pid_t pid = spawn(argv); |
Rob Landley | 399d2b5 | 2006-05-25 23:02:40 +0000 | [diff] [blame] | 217 | if (pid < 0) bb_perror_msg_and_die("%s", *argv); |
| 218 | return pid; |
| 219 | } |
| 220 | #endif |
Rob Landley | c7ddefc | 2006-06-14 01:24:33 +0000 | [diff] [blame] | 221 | |
| 222 | #ifdef L_wait4 |
| 223 | int wait4pid(int pid) |
| 224 | { |
| 225 | int status; |
| 226 | |
| 227 | if (pid == -1 || waitpid(pid, &status, 0) == -1) return -1; |
| 228 | if (WIFEXITED(status)) return WEXITSTATUS(status); |
| 229 | if (WIFSIGNALED(status)) return WTERMSIG(status); |
| 230 | return 0; |
| 231 | } |
Rob Landley | 5b88a38 | 2006-07-10 07:41:34 +0000 | [diff] [blame] | 232 | #endif |
| 233 | |
| 234 | #ifdef L_itoa |
| 235 | // Largest 32 bit integer is -2 billion plus null terminator. |
| 236 | // Int should always be 32 bits on a Unix-oid system, see |
| 237 | // http://www.unix.org/whitepapers/64bit.html |
| 238 | static char local_buf[12]; |
| 239 | |
Rob Landley | 22d3958 | 2006-07-11 00:44:36 +0000 | [diff] [blame] | 240 | void utoa_to_buf(unsigned n, char *buf, unsigned buflen) |
Rob Landley | 5b88a38 | 2006-07-10 07:41:34 +0000 | [diff] [blame] | 241 | { |
| 242 | int i, out = 0; |
Rob Landley | 22d3958 | 2006-07-11 00:44:36 +0000 | [diff] [blame] | 243 | if (buflen) { |
| 244 | for (i=1000000000; i; i/=10) { |
| 245 | int res = n/i; |
Rob Landley | 5b88a38 | 2006-07-10 07:41:34 +0000 | [diff] [blame] | 246 | |
Rob Landley | 22d3958 | 2006-07-11 00:44:36 +0000 | [diff] [blame] | 247 | if ((res || out || i == 1) && --buflen>0) { |
| 248 | out++; |
| 249 | n -= res*i; |
| 250 | *buf++ = '0' + res; |
| 251 | } |
Rob Landley | 5b88a38 | 2006-07-10 07:41:34 +0000 | [diff] [blame] | 252 | } |
Rob Landley | 22d3958 | 2006-07-11 00:44:36 +0000 | [diff] [blame] | 253 | *buf = 0; |
Rob Landley | 5b88a38 | 2006-07-10 07:41:34 +0000 | [diff] [blame] | 254 | } |
Rob Landley | 5b88a38 | 2006-07-10 07:41:34 +0000 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | // Note: uses static buffer, calling it twice in a row will overwrite. |
| 258 | |
| 259 | char *utoa(unsigned n) |
| 260 | { |
| 261 | utoa_to_buf(n, local_buf, sizeof(local_buf)); |
| 262 | |
| 263 | return local_buf; |
| 264 | } |
| 265 | |
Rob Landley | 22d3958 | 2006-07-11 00:44:36 +0000 | [diff] [blame] | 266 | void itoa_to_buf(int n, char *buf, unsigned buflen) |
Rob Landley | 5b88a38 | 2006-07-10 07:41:34 +0000 | [diff] [blame] | 267 | { |
Rob Landley | 22d3958 | 2006-07-11 00:44:36 +0000 | [diff] [blame] | 268 | if (buflen && n<0) { |
Rob Landley | 5b88a38 | 2006-07-10 07:41:34 +0000 | [diff] [blame] | 269 | n = -n; |
| 270 | *buf++ = '-'; |
Rob Landley | 22d3958 | 2006-07-11 00:44:36 +0000 | [diff] [blame] | 271 | buflen--; |
Rob Landley | 5b88a38 | 2006-07-10 07:41:34 +0000 | [diff] [blame] | 272 | } |
| 273 | utoa_to_buf((unsigned)n, buf, buflen); |
| 274 | } |
| 275 | |
| 276 | // Note: uses static buffer, calling it twice in a row will overwrite. |
| 277 | |
| 278 | char *itoa(int n) |
| 279 | { |
| 280 | itoa_to_buf(n, local_buf, sizeof(local_buf)); |
| 281 | |
| 282 | return local_buf; |
| 283 | } |
| 284 | #endif |
Rob Landley | df822f2 | 2006-07-15 23:00:46 +0000 | [diff] [blame] | 285 | |
| 286 | #ifdef L_setuid |
| 287 | void xsetgid(gid_t gid) |
| 288 | { |
| 289 | if (setgid(gid)) bb_error_msg_and_die("setgid"); |
| 290 | } |
| 291 | |
| 292 | void xsetuid(uid_t uid) |
| 293 | { |
| 294 | if (setuid(uid)) bb_error_msg_and_die("setuid"); |
| 295 | } |
| 296 | #endif |
Rob Landley | 5343747 | 2006-07-16 08:14:35 +0000 | [diff] [blame] | 297 | |
| 298 | #ifdef L_fdlength |
| 299 | off_t fdlength(int fd) |
| 300 | { |
| 301 | off_t bottom = 0, top = 0, pos; |
| 302 | long size; |
| 303 | |
| 304 | // If the ioctl works for this, return it. |
| 305 | |
| 306 | if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512; |
| 307 | |
| 308 | // If not, do a binary search for the last location we can read. |
| 309 | |
| 310 | do { |
| 311 | char temp; |
| 312 | |
| 313 | pos = bottom + (top - bottom) / 2;; |
| 314 | |
| 315 | // If we can read from the current location, it's bigger. |
| 316 | |
| 317 | if (lseek(fd, pos, 0)>=0 && safe_read(fd, &temp, 1)==1) { |
| 318 | if (bottom == top) bottom = top = (top+1) * 2; |
| 319 | else bottom = pos; |
| 320 | |
| 321 | // If we can't, it's smaller. |
| 322 | |
| 323 | } else { |
| 324 | if (bottom == top) { |
| 325 | if (!top) return 0; |
| 326 | bottom = top/2; |
| 327 | } |
| 328 | else top = pos; |
| 329 | } |
| 330 | } while (bottom + 1 != top); |
| 331 | |
| 332 | return pos + 1; |
| 333 | } |
| 334 | #endif |
Rob Landley | d921b2e | 2006-08-03 15:41:12 +0000 | [diff] [blame] | 335 | |
| 336 | #ifdef L_xasprintf |
| 337 | char *xasprintf(const char *format, ...) |
| 338 | { |
| 339 | va_list p; |
| 340 | int r; |
| 341 | char *string_ptr; |
| 342 | |
| 343 | #if 1 |
| 344 | // GNU extension |
| 345 | va_start(p, format); |
| 346 | r = vasprintf(&string_ptr, format, p); |
| 347 | va_end(p); |
| 348 | #else |
| 349 | // Bloat for systems that haven't got the GNU extension. |
| 350 | va_start(p, format); |
| 351 | r = vsnprintf(NULL, 0, format, p); |
| 352 | va_end(p); |
| 353 | string_ptr = xmalloc(r+1); |
| 354 | va_start(p, format); |
| 355 | r = vsnprintf(string_ptr, r+1, format, p); |
| 356 | va_end(p); |
| 357 | #endif |
| 358 | |
| 359 | if (r < 0) bb_perror_msg_and_die("xasprintf"); |
| 360 | return string_ptr; |
| 361 | } |
| 362 | #endif |
| 363 | |
| 364 | #ifdef L_xprint_and_close_file |
| 365 | void xprint_and_close_file(FILE *file) |
| 366 | { |
| 367 | // copyfd outputs error messages for us. |
| 368 | if (bb_copyfd_eof(fileno(file), 1) == -1) exit(bb_default_error_retval); |
| 369 | |
| 370 | fclose(file); |
| 371 | } |
| 372 | #endif |
| 373 | |
| 374 | #ifdef L_xchdir |
| 375 | void xchdir(const char *path) |
| 376 | { |
| 377 | if (chdir(path)) |
| 378 | bb_perror_msg_and_die("chdir(%s)", path); |
| 379 | } |
| 380 | #endif |
| 381 | |
| 382 | #ifdef L_warn_opendir |
| 383 | DIR *warn_opendir(const char *path) |
| 384 | { |
| 385 | DIR *dp; |
| 386 | |
| 387 | if ((dp = opendir(path)) == NULL) { |
| 388 | bb_perror_msg("unable to open `%s'", path); |
| 389 | return NULL; |
| 390 | } |
| 391 | return dp; |
| 392 | } |
| 393 | #endif |
| 394 | |
| 395 | #ifdef L_xopendir |
| 396 | DIR *xopendir(const char *path) |
| 397 | { |
| 398 | DIR *dp; |
| 399 | |
| 400 | if ((dp = opendir(path)) == NULL) |
| 401 | bb_perror_msg_and_die("unable to open `%s'", path); |
| 402 | return dp; |
| 403 | } |
| 404 | #endif |
| 405 | |
| 406 | #ifdef L_xdaemon |
| 407 | #ifndef BB_NOMMU |
| 408 | void xdaemon(int nochdir, int noclose) |
| 409 | { |
| 410 | if (daemon(nochdir, noclose)) bb_perror_msg_and_die("daemon"); |
| 411 | } |
| 412 | #endif |
| 413 | #endif |
| 414 | |
| 415 | #ifdef L_xbind |
| 416 | void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen) |
| 417 | { |
| 418 | if (bind(sockfd, my_addr, addrlen)) bb_perror_msg_and_die("bind"); |
| 419 | } |
| 420 | #endif |
| 421 | |
| 422 | #ifdef L_xsocket |
| 423 | int xsocket(int domain, int type, int protocol) |
| 424 | { |
| 425 | int r = socket(domain, type, protocol); |
| 426 | |
| 427 | if (r < 0) bb_perror_msg_and_die("socket"); |
| 428 | |
| 429 | return r; |
| 430 | } |
| 431 | #endif |
| 432 | |
| 433 | #ifdef L_xlisten |
| 434 | void xlisten(int s, int backlog) |
| 435 | { |
| 436 | if (listen(s, backlog)) bb_perror_msg_and_die("listen"); |
| 437 | } |
| 438 | #endif |