blob: 4790aa140e8e46c4226ea9d52163d8860d275ff8 [file] [log] [blame]
Eric Andersenaad1a882001-03-16 22:47:14 +00001/* vi: set sw=4 ts=4: */
2/*
3 * Utility routines.
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Rob Landleyd921b2e2006-08-03 15:41:12 +00006 * Copyright (C) 2006 Rob Landley
7 * Copyright (C) 2006 Denis Vlasenko
Eric Andersenaad1a882001-03-16 22:47:14 +00008 *
Rob Landleyfbdf1212006-09-20 22:06:01 +00009 * Licensed under GPL version 2, see file LICENSE in this tarball for details.
Eric Andersenaad1a882001-03-16 22:47:14 +000010 */
11
Rob Landley552b56d2006-05-04 21:22:27 +000012#include "busybox.h"
Eric Andersenaad1a882001-03-16 22:47:14 +000013
Rob Landley23b61be2006-08-04 20:20:03 +000014/* All the functions starting with "x" call bb_error_msg_and_die() if they
15 * fail, so callers never need to check for errors. If it returned, it
16 * succeeded. */
17
Eric Andersenaad1a882001-03-16 22:47:14 +000018#ifndef DMALLOC
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000019/* dmalloc provides variants of these that do abort() on failure.
20 * Since dmalloc's prototypes overwrite the impls here as they are
21 * included after these prototypes in libbb.h, all is well.
22 */
Rob Landleyda9d1d02006-09-14 19:52:07 +000023// Die if we can't allocate size bytes of memory.
Rob Landleydfba7412006-03-06 20:47:33 +000024void *xmalloc(size_t size)
Eric Andersenaad1a882001-03-16 22:47:14 +000025{
26 void *ptr = malloc(size);
Matt Kraaia99b1942002-02-26 15:28:22 +000027 if (ptr == NULL && size != 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000028 bb_error_msg_and_die(bb_msg_memory_exhausted);
Eric Andersenaad1a882001-03-16 22:47:14 +000029 return ptr;
30}
31
Rob Landleyda9d1d02006-09-14 19:52:07 +000032// Die if we can't resize previously allocated memory. (This returns a pointer
33// to the new memory, which may or may not be the same as the old memory.
34// It'll copy the contents to a new chunk and free the old one if necessary.)
Rob Landleydfba7412006-03-06 20:47:33 +000035void *xrealloc(void *ptr, size_t size)
Eric Andersenaad1a882001-03-16 22:47:14 +000036{
Matt Kraaia99b1942002-02-26 15:28:22 +000037 ptr = realloc(ptr, size);
38 if (ptr == NULL && size != 0)
Manuel Novoa III cad53642003-03-19 09:13:01 +000039 bb_error_msg_and_die(bb_msg_memory_exhausted);
Eric Andersenaad1a882001-03-16 22:47:14 +000040 return ptr;
41}
Bernhard Reutner-Fischer73561cc2006-08-28 23:31:54 +000042#endif /* DMALLOC */
43
Rob Landleyda9d1d02006-09-14 19:52:07 +000044// Die if we can't allocate and zero size bytes of memory.
Rob Landley80b8ff02006-05-19 20:36:49 +000045void *xzalloc(size_t size)
46{
47 void *ptr = xmalloc(size);
48 memset(ptr, 0, size);
49 return ptr;
50}
Rob Landley80b8ff02006-05-19 20:36:49 +000051
Rob Landleyda9d1d02006-09-14 19:52:07 +000052// Die if we can't copy a string to freshly allocated memory.
Rob Landley23b61be2006-08-04 20:20:03 +000053char * xstrdup(const char *s)
Rob Landley31642d72006-03-14 21:45:38 +000054{
Eric Andersenaad1a882001-03-16 22:47:14 +000055 char *t;
56
57 if (s == NULL)
58 return NULL;
59
Denis Vlasenkocf749bc2006-11-26 15:45:17 +000060 t = strdup(s);
Eric Andersenaad1a882001-03-16 22:47:14 +000061
62 if (t == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +000063 bb_error_msg_and_die(bb_msg_memory_exhausted);
Eric Andersenaad1a882001-03-16 22:47:14 +000064
65 return t;
66}
Eric Andersenaad1a882001-03-16 22:47:14 +000067
Rob Landleyda9d1d02006-09-14 19:52:07 +000068// Die if we can't allocate n+1 bytes (space for the null terminator) and copy
69// the (possibly truncated to length n) string into it.
Rob Landley23b61be2006-08-04 20:20:03 +000070char * xstrndup(const char *s, int n)
Rob Landley31642d72006-03-14 21:45:38 +000071{
Denis Vlasenkocf749bc2006-11-26 15:45:17 +000072 int m;
Eric Andersenaad1a882001-03-16 22:47:14 +000073 char *t;
74
Rob Landley8bbdb872006-06-30 16:36:56 +000075 if (ENABLE_DEBUG && s == NULL)
Rob Landleyd921b2e2006-08-03 15:41:12 +000076 bb_error_msg_and_die("xstrndup bug");
Eric Andersenaad1a882001-03-16 22:47:14 +000077
Denis Vlasenkocf749bc2006-11-26 15:45:17 +000078 /* We can just xmalloc(n+1) and strncpy into it, */
79 /* but think about xstrndup("abc", 10000) wastage! */
80 m = n;
81 t = (char*) s;
82 while (m) {
83 if (!*t) break;
84 m--; t++;
85 }
86 n = n - m;
87 t = xmalloc(n + 1);
88 t[n] = '\0';
Eric Andersenc7bda1c2004-03-15 08:29:22 +000089
Denis Vlasenkocf749bc2006-11-26 15:45:17 +000090 return memcpy(t,s,n);
Eric Andersenaad1a882001-03-16 22:47:14 +000091}
92
Rob Landleyda9d1d02006-09-14 19:52:07 +000093// Die if we can't open a file and return a FILE * to it.
94// Notice we haven't got xfread(), This is for use with fscanf() and friends.
Rob Landleyd921b2e2006-08-03 15:41:12 +000095FILE *xfopen(const char *path, const char *mode)
Eric Andersenaad1a882001-03-16 22:47:14 +000096{
Denis Vlasenkocf749bc2006-11-26 15:45:17 +000097 FILE *fp = fopen(path, mode);
98 if (fp == NULL)
Manuel Novoa III cad53642003-03-19 09:13:01 +000099 bb_perror_msg_and_die("%s", path);
Eric Andersenaad1a882001-03-16 22:47:14 +0000100 return fp;
101}
102
Rob Landleyda9d1d02006-09-14 19:52:07 +0000103// Die if we can't open an existing file and return an fd.
Rob Landleyd921b2e2006-08-03 15:41:12 +0000104int xopen(const char *pathname, int flags)
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000105{
Denis Vlasenkocf749bc2006-11-26 15:45:17 +0000106 //if (ENABLE_DEBUG && (flags & O_CREAT))
107 // bb_error_msg_and_die("xopen() with O_CREAT");
Rob Landley23b61be2006-08-04 20:20:03 +0000108
Denis Vlasenkoea620772006-10-14 02:23:43 +0000109 return xopen3(pathname, flags, 0666);
Bernhard Reutner-Fischerc2cb0f32006-04-13 12:45:04 +0000110}
Bernhard Reutner-Fischerc2cb0f32006-04-13 12:45:04 +0000111
Rob Landleyda9d1d02006-09-14 19:52:07 +0000112// Die if we can't open a new file and return an fd.
Rob Landleyd921b2e2006-08-03 15:41:12 +0000113int xopen3(const char *pathname, int flags, int mode)
Bernhard Reutner-Fischerc2cb0f32006-04-13 12:45:04 +0000114{
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000115 int ret;
Eric Andersenc7bda1c2004-03-15 08:29:22 +0000116
Bernhard Reutner-Fischerc2cb0f32006-04-13 12:45:04 +0000117 ret = open(pathname, flags, mode);
118 if (ret < 0) {
Manuel Novoa III cad53642003-03-19 09:13:01 +0000119 bb_perror_msg_and_die("%s", pathname);
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000120 }
121 return ret;
122}
123
Denis Vlasenko75f8d082006-11-22 15:54:52 +0000124/*
125int ndelay_off(int fd)
126{
127 return fcntl(fd,F_SETFL,fcntl(fd,F_GETFL,0) & ~O_NONBLOCK);
128}
129*/
130// Turn on nonblocking I/O on a fd
131int ndelay_on(int fd)
132{
133 return fcntl(fd,F_SETFL,fcntl(fd,F_GETFL,0) | O_NONBLOCK);
134}
135
Rob Landleyda9d1d02006-09-14 19:52:07 +0000136// Die with an error message if we can't write the entire buffer.
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +0000137void xwrite(int fd, const void *buf, size_t count)
Rob Landley53437472006-07-16 08:14:35 +0000138{
Denis Vlasenko88ca0672006-10-12 22:44:13 +0000139 if (count) {
140 ssize_t size = full_write(fd, buf, count);
141 if (size != count)
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000142 bb_error_msg_and_die("short write");
Rob Landley53437472006-07-16 08:14:35 +0000143 }
144}
Rob Landley53437472006-07-16 08:14:35 +0000145
Rob Landleyda9d1d02006-09-14 19:52:07 +0000146// Die with an error message if we can't lseek to the right spot.
Denis Vlasenkoea620772006-10-14 02:23:43 +0000147off_t xlseek(int fd, off_t offset, int whence)
Rob Landley53437472006-07-16 08:14:35 +0000148{
Denis Vlasenkoea620772006-10-14 02:23:43 +0000149 off_t off = lseek(fd, offset, whence);
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000150 if (off == (off_t)-1) {
151 if (whence == SEEK_SET)
152 bb_perror_msg_and_die("lseek(%"OFF_FMT"u)", offset);
Denis Vlasenkoea620772006-10-14 02:23:43 +0000153 bb_perror_msg_and_die("lseek");
Denis Vlasenkoc6ce8732006-11-29 18:15:52 +0000154 }
Denis Vlasenkoea620772006-10-14 02:23:43 +0000155 return off;
Glenn L McGrath7ca04f32002-09-25 02:47:48 +0000156}
157
Denis Vlasenkocf749bc2006-11-26 15:45:17 +0000158// Die with supplied filename if this FILE * has ferror set.
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000159void die_if_ferror(FILE *fp, const char *fn)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000160{
161 if (ferror(fp)) {
Denis Vlasenko2d27e4c2006-11-25 23:50:28 +0000162 bb_error_msg_and_die("%s: I/O error", fn);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000163 }
164}
Manuel Novoa III cad53642003-03-19 09:13:01 +0000165
Rob Landleyda9d1d02006-09-14 19:52:07 +0000166// Die with an error message if stdout has ferror set.
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000167void die_if_ferror_stdout(void)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000168{
Denis Vlasenkof0ed3762006-10-26 23:21:47 +0000169 die_if_ferror(stdout, bb_msg_standard_output);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000170}
Manuel Novoa III cad53642003-03-19 09:13:01 +0000171
Rob Landleyda9d1d02006-09-14 19:52:07 +0000172// Die with an error message if we have trouble flushing stdout.
Rob Landleyd921b2e2006-08-03 15:41:12 +0000173void xfflush_stdout(void)
Manuel Novoa III cad53642003-03-19 09:13:01 +0000174{
175 if (fflush(stdout)) {
176 bb_perror_msg_and_die(bb_msg_standard_output);
177 }
178}
Rob Landley399d2b52006-05-25 23:02:40 +0000179
Rob Landleyda9d1d02006-09-14 19:52:07 +0000180// This does a fork/exec in one call, using vfork(). Return PID of new child,
181// -1 for failure. Runs argv[0], searching path if that has no / in it.
Rob Landleyd921b2e2006-08-03 15:41:12 +0000182pid_t spawn(char **argv)
Rob Landley399d2b52006-05-25 23:02:40 +0000183{
184 static int failed;
185 pid_t pid;
Rob Landley519d7df2006-08-09 20:56:23 +0000186 void *app = ENABLE_FEATURE_SH_STANDALONE_SHELL ? find_applet_by_name(argv[0]) : 0;
Rob Landley399d2b52006-05-25 23:02:40 +0000187
Rob Landleyda9d1d02006-09-14 19:52:07 +0000188 // Be nice to nommu machines.
Rob Landley399d2b52006-05-25 23:02:40 +0000189 failed = 0;
190 pid = vfork();
191 if (pid < 0) return pid;
192 if (!pid) {
Rob Landleyc7ddefc2006-06-14 01:24:33 +0000193 execvp(app ? CONFIG_BUSYBOX_EXEC_PATH : *argv, argv);
Rob Landley399d2b52006-05-25 23:02:40 +0000194
Rob Landleyda9d1d02006-09-14 19:52:07 +0000195 // We're sharing a stack with blocked parent, let parent know we failed
196 // and then exit to unblock parent (but don't run atexit() stuff, which
197 // would screw up parent.)
Rob Landley399d2b52006-05-25 23:02:40 +0000198
199 failed = -1;
200 _exit(0);
201 }
202 return failed ? failed : pid;
203}
Rob Landley399d2b52006-05-25 23:02:40 +0000204
Rob Landleyda9d1d02006-09-14 19:52:07 +0000205// Die with an error message if we can't spawn a child process.
Rob Landleyd921b2e2006-08-03 15:41:12 +0000206pid_t xspawn(char **argv)
Rob Landley399d2b52006-05-25 23:02:40 +0000207{
Rob Landleyd921b2e2006-08-03 15:41:12 +0000208 pid_t pid = spawn(argv);
Rob Landley399d2b52006-05-25 23:02:40 +0000209 if (pid < 0) bb_perror_msg_and_die("%s", *argv);
210 return pid;
211}
Rob Landleyc7ddefc2006-06-14 01:24:33 +0000212
Rob Landleyda9d1d02006-09-14 19:52:07 +0000213// Wait for the specified child PID to exit, returning child's error return.
Rob Landleyc7ddefc2006-06-14 01:24:33 +0000214int wait4pid(int pid)
215{
216 int status;
217
218 if (pid == -1 || waitpid(pid, &status, 0) == -1) return -1;
219 if (WIFEXITED(status)) return WEXITSTATUS(status);
220 if (WIFSIGNALED(status)) return WTERMSIG(status);
221 return 0;
222}
Rob Landley5b88a382006-07-10 07:41:34 +0000223
Denis Vlasenkofe544582006-10-03 15:57:40 +0000224void xsetenv(const char *key, const char *value)
225{
Denis Vlasenko1db39b22006-10-11 20:59:02 +0000226 if (setenv(key, value, 1))
Denis Vlasenkofe544582006-10-03 15:57:40 +0000227 bb_error_msg_and_die(bb_msg_memory_exhausted);
228}
Denis Vlasenkofe544582006-10-03 15:57:40 +0000229
Denis Vlasenkoaae03112006-11-05 00:44:39 +0000230// Converts unsigned long long value into compact 4-char
231// representation. Examples: "1234", "1.2k", " 27M", "123T"
232// Fifth char is always '\0'
233void smart_ulltoa5(unsigned long long ul, char buf[5])
234{
235 char *fmt;
236 char c;
237 unsigned v,idx = 0;
238 ul *= 10;
239 if (ul > 9999*10) { // do not scale if 9999 or less
240 while (ul >= 10000) {
241 ul /= 1024;
242 idx++;
243 }
244 }
245 v = ul; // ullong divisions are expensive, avoid them
246
247 fmt = " 123456789";
248 if (!idx) { // 9999 or less: use 1234 format
249 c = buf[0] = " 123456789"[v/10000];
250 if (c!=' ') fmt = "0123456789";
251 c = buf[1] = fmt[v/1000%10];
252 if (c!=' ') fmt = "0123456789";
253 buf[2] = fmt[v/100%10];
254 buf[3] = "0123456789"[v/10%10];
255 } else {
256 if (v>=10*10) { // scaled value is >=10: use 123M format
257 c = buf[0] = " 123456789"[v/1000];
258 if (c!=' ') fmt = "0123456789";
259 buf[1] = fmt[v/100%10];
260 buf[2] = "0123456789"[v/10%10];
261 } else { // scaled value is <10: use 1.2M format
262 buf[0] = "0123456789"[v/10];
263 buf[1] = '.';
264 buf[2] = "0123456789"[v%10];
265 }
266 // see http://en.wikipedia.org/wiki/Tera
267 buf[3] = " kMGTPEZY"[idx];
268 }
269 buf[4] = '\0';
270}
271
Rob Landleyda9d1d02006-09-14 19:52:07 +0000272// Convert unsigned integer to ascii, writing into supplied buffer. A
273// truncated result is always null terminated (unless buflen is 0), and
274// contains the first few digits of the result ala strncpy.
Denis Vlasenkoaae03112006-11-05 00:44:39 +0000275void BUG_sizeof_unsigned_not_4(void);
Rob Landley22d39582006-07-11 00:44:36 +0000276void utoa_to_buf(unsigned n, char *buf, unsigned buflen)
Rob Landley5b88a382006-07-10 07:41:34 +0000277{
Denis Vlasenkoaae03112006-11-05 00:44:39 +0000278 unsigned i, out, res;
279 if (sizeof(unsigned) != 4)
280 BUG_sizeof_unsigned_not_4();
Rob Landley22d39582006-07-11 00:44:36 +0000281 if (buflen) {
Denis Vlasenkoaae03112006-11-05 00:44:39 +0000282 out = 0;
283 for (i = 1000000000; i; i /= 10) {
284 res = n / i;
285 if (res || out || i == 1) {
286 if (!--buflen) break;
Rob Landley22d39582006-07-11 00:44:36 +0000287 out++;
288 n -= res*i;
289 *buf++ = '0' + res;
290 }
Rob Landley5b88a382006-07-10 07:41:34 +0000291 }
Denis Vlasenkoaae03112006-11-05 00:44:39 +0000292 *buf = '\0';
Rob Landley5b88a382006-07-10 07:41:34 +0000293 }
Rob Landley5b88a382006-07-10 07:41:34 +0000294}
295
Rob Landleyda9d1d02006-09-14 19:52:07 +0000296// Convert signed integer to ascii, like utoa_to_buf()
Rob Landley22d39582006-07-11 00:44:36 +0000297void itoa_to_buf(int n, char *buf, unsigned buflen)
Rob Landley5b88a382006-07-10 07:41:34 +0000298{
Rob Landley22d39582006-07-11 00:44:36 +0000299 if (buflen && n<0) {
Rob Landley5b88a382006-07-10 07:41:34 +0000300 n = -n;
301 *buf++ = '-';
Rob Landley22d39582006-07-11 00:44:36 +0000302 buflen--;
Rob Landley5b88a382006-07-10 07:41:34 +0000303 }
304 utoa_to_buf((unsigned)n, buf, buflen);
305}
306
Rob Landleyda9d1d02006-09-14 19:52:07 +0000307// The following two functions use a static buffer, so calling either one a
308// second time will overwrite previous results.
309//
310// The largest 32 bit integer is -2 billion plus null terminator, or 12 bytes.
311// Int should always be 32 bits on any remotely Unix-like system, see
312// http://www.unix.org/whitepapers/64bit.html for the reasons why.
313
Rob Landley23b61be2006-08-04 20:20:03 +0000314static char local_buf[12];
315
Rob Landleyda9d1d02006-09-14 19:52:07 +0000316// Convert unsigned integer to ascii using a static buffer (returned).
Rob Landley23b61be2006-08-04 20:20:03 +0000317char *utoa(unsigned n)
318{
319 utoa_to_buf(n, local_buf, sizeof(local_buf));
320
321 return local_buf;
322}
323
Rob Landleyda9d1d02006-09-14 19:52:07 +0000324// Convert signed integer to ascii using a static buffer (returned).
Rob Landley5b88a382006-07-10 07:41:34 +0000325char *itoa(int n)
326{
327 itoa_to_buf(n, local_buf, sizeof(local_buf));
328
329 return local_buf;
330}
Rob Landleydf822f22006-07-15 23:00:46 +0000331
Rob Landleyda9d1d02006-09-14 19:52:07 +0000332// Die with an error message if we can't set gid. (Because resource limits may
333// limit this user to a given number of processes, and if that fills up the
334// setgid() will fail and we'll _still_be_root_, which is bad.)
Rob Landleydf822f22006-07-15 23:00:46 +0000335void xsetgid(gid_t gid)
336{
337 if (setgid(gid)) bb_error_msg_and_die("setgid");
338}
339
Denis Vlasenkoe1a0d482006-10-20 13:28:22 +0000340// Die with an error message if we can't set uid. (See xsetgid() for why.)
Rob Landleydf822f22006-07-15 23:00:46 +0000341void xsetuid(uid_t uid)
342{
343 if (setuid(uid)) bb_error_msg_and_die("setuid");
344}
Rob Landley53437472006-07-16 08:14:35 +0000345
Rob Landleyda9d1d02006-09-14 19:52:07 +0000346// Return how long the file at fd is, if there's any way to determine it.
Rob Landley53437472006-07-16 08:14:35 +0000347off_t fdlength(int fd)
348{
349 off_t bottom = 0, top = 0, pos;
350 long size;
351
Rob Landleyda9d1d02006-09-14 19:52:07 +0000352 // If the ioctl works for this, return it.
Rob Landley53437472006-07-16 08:14:35 +0000353
354 if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512;
355
Denis Vlasenko621204b2006-10-27 09:03:24 +0000356 // FIXME: explain why lseek(SEEK_END) is not used here!
357
Rob Landleyda9d1d02006-09-14 19:52:07 +0000358 // If not, do a binary search for the last location we can read. (Some
359 // block devices don't do BLKGETSIZE right.)
Rob Landley53437472006-07-16 08:14:35 +0000360
361 do {
362 char temp;
363
Denis Vlasenkod25a2642006-09-05 09:36:19 +0000364 pos = bottom + (top - bottom) / 2;
Rob Landley53437472006-07-16 08:14:35 +0000365
Rob Landleyda9d1d02006-09-14 19:52:07 +0000366 // If we can read from the current location, it's bigger.
Rob Landley53437472006-07-16 08:14:35 +0000367
Denis Vlasenkoea620772006-10-14 02:23:43 +0000368 if (lseek(fd, pos, SEEK_SET)>=0 && safe_read(fd, &temp, 1)==1) {
Rob Landley53437472006-07-16 08:14:35 +0000369 if (bottom == top) bottom = top = (top+1) * 2;
370 else bottom = pos;
371
Rob Landleyda9d1d02006-09-14 19:52:07 +0000372 // If we can't, it's smaller.
Rob Landley53437472006-07-16 08:14:35 +0000373
374 } else {
375 if (bottom == top) {
376 if (!top) return 0;
377 bottom = top/2;
378 }
379 else top = pos;
380 }
381 } while (bottom + 1 != top);
382
383 return pos + 1;
384}
Rob Landleyd921b2e2006-08-03 15:41:12 +0000385
Rob Landleyda9d1d02006-09-14 19:52:07 +0000386// Die with an error message if we can't malloc() enough space and do an
387// sprintf() into that space.
Rob Landleyd921b2e2006-08-03 15:41:12 +0000388char *xasprintf(const char *format, ...)
389{
390 va_list p;
391 int r;
392 char *string_ptr;
393
394#if 1
395 // GNU extension
396 va_start(p, format);
397 r = vasprintf(&string_ptr, format, p);
398 va_end(p);
399#else
400 // Bloat for systems that haven't got the GNU extension.
401 va_start(p, format);
402 r = vsnprintf(NULL, 0, format, p);
403 va_end(p);
404 string_ptr = xmalloc(r+1);
405 va_start(p, format);
406 r = vsnprintf(string_ptr, r+1, format, p);
407 va_end(p);
408#endif
409
Denis Vlasenko3538b9a2006-09-06 18:36:50 +0000410 if (r < 0) bb_error_msg_and_die(bb_msg_memory_exhausted);
Rob Landleyd921b2e2006-08-03 15:41:12 +0000411 return string_ptr;
412}
Rob Landleyd921b2e2006-08-03 15:41:12 +0000413
Denis Vlasenkoc8e6e352006-12-18 22:10:24 +0000414#ifdef __dietlibc__
415int dprintf(int fd, const char *format, ...)
416{
417 va_list p;
418 int r;
419 char *string_ptr;
420
421#if 1
422 // GNU extension
423 va_start(p, format);
424 r = vasprintf(&string_ptr, format, p);
425 va_end(p);
426#else
427 // Bloat for systems that haven't got the GNU extension.
428 va_start(p, format);
429 r = vsnprintf(NULL, 0, format, p);
430 va_end(p);
431 string_ptr = xmalloc(r+1);
432 va_start(p, format);
433 r = vsnprintf(string_ptr, r+1, format, p);
434 va_end(p);
435#endif
436
437 if (r >= 0) {
438 full_write(fd, string_ptr, r);
439 free(string_ptr);
440 }
441 return r;
442}
443#endif
444
Rob Landleyda9d1d02006-09-14 19:52:07 +0000445// Die with an error message if we can't copy an entire FILE * to stdout, then
446// close that file.
Rob Landleyd921b2e2006-08-03 15:41:12 +0000447void xprint_and_close_file(FILE *file)
448{
Denis Vlasenkoddec5af2006-10-26 23:25:17 +0000449 fflush(stdout);
Rob Landleyd921b2e2006-08-03 15:41:12 +0000450 // copyfd outputs error messages for us.
Denis Vlasenkoa9801652006-09-07 16:20:03 +0000451 if (bb_copyfd_eof(fileno(file), 1) == -1)
Denis Vlasenko40920822006-10-03 20:28:06 +0000452 exit(xfunc_error_retval);
Rob Landleyd921b2e2006-08-03 15:41:12 +0000453
454 fclose(file);
455}
Rob Landleyd921b2e2006-08-03 15:41:12 +0000456
Rob Landleyda9d1d02006-09-14 19:52:07 +0000457// Die if we can't chdir to a new path.
Rob Landleyd921b2e2006-08-03 15:41:12 +0000458void xchdir(const char *path)
459{
460 if (chdir(path))
461 bb_perror_msg_and_die("chdir(%s)", path);
462}
Rob Landleyd921b2e2006-08-03 15:41:12 +0000463
Rob Landleyda9d1d02006-09-14 19:52:07 +0000464// Print a warning message if opendir() fails, but don't die.
Rob Landleyd921b2e2006-08-03 15:41:12 +0000465DIR *warn_opendir(const char *path)
466{
467 DIR *dp;
468
469 if ((dp = opendir(path)) == NULL) {
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000470 bb_perror_msg("cannot open '%s'", path);
Rob Landleyd921b2e2006-08-03 15:41:12 +0000471 return NULL;
472 }
473 return dp;
474}
Rob Landleyd921b2e2006-08-03 15:41:12 +0000475
Rob Landleyda9d1d02006-09-14 19:52:07 +0000476// Die with an error message if opendir() fails.
Rob Landleyd921b2e2006-08-03 15:41:12 +0000477DIR *xopendir(const char *path)
478{
479 DIR *dp;
480
481 if ((dp = opendir(path)) == NULL)
Denis Vlasenko89f0b342006-11-18 22:04:09 +0000482 bb_perror_msg_and_die("cannot open '%s'", path);
Rob Landleyd921b2e2006-08-03 15:41:12 +0000483 return dp;
484}
Rob Landleyd921b2e2006-08-03 15:41:12 +0000485
Rob Landleyd921b2e2006-08-03 15:41:12 +0000486#ifndef BB_NOMMU
Rob Landleyda9d1d02006-09-14 19:52:07 +0000487// Die with an error message if we can't daemonize.
Rob Landleyd921b2e2006-08-03 15:41:12 +0000488void xdaemon(int nochdir, int noclose)
489{
Denis Vlasenko621204b2006-10-27 09:03:24 +0000490 if (daemon(nochdir, noclose))
491 bb_perror_msg_and_die("daemon");
Rob Landleyd921b2e2006-08-03 15:41:12 +0000492}
493#endif
Rob Landleyd921b2e2006-08-03 15:41:12 +0000494
Rob Landleyda9d1d02006-09-14 19:52:07 +0000495// Die with an error message if we can't open a new socket.
Rob Landleyd921b2e2006-08-03 15:41:12 +0000496int xsocket(int domain, int type, int protocol)
497{
498 int r = socket(domain, type, protocol);
499
500 if (r < 0) bb_perror_msg_and_die("socket");
501
502 return r;
503}
Rob Landleyd921b2e2006-08-03 15:41:12 +0000504
Rob Landleyda9d1d02006-09-14 19:52:07 +0000505// Die with an error message if we can't bind a socket to an address.
Rob Landley23b61be2006-08-04 20:20:03 +0000506void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen)
507{
508 if (bind(sockfd, my_addr, addrlen)) bb_perror_msg_and_die("bind");
509}
Rob Landley23b61be2006-08-04 20:20:03 +0000510
Rob Landleyda9d1d02006-09-14 19:52:07 +0000511// Die with an error message if we can't listen for connections on a socket.
Rob Landleyd921b2e2006-08-03 15:41:12 +0000512void xlisten(int s, int backlog)
513{
514 if (listen(s, backlog)) bb_perror_msg_and_die("listen");
515}
Bernhard Reutner-Fischer57b56672006-09-11 09:18:09 +0000516
Rob Landleyda9d1d02006-09-14 19:52:07 +0000517// xstat() - a stat() which dies on failure with meaningful error message
Rob Landley20cc6d52006-09-12 21:42:17 +0000518void xstat(char *name, struct stat *stat_buf)
Bernhard Reutner-Fischer57b56672006-09-11 09:18:09 +0000519{
Denis Vlasenko9213a9e2006-09-17 16:28:10 +0000520 if (stat(name, stat_buf))
Denis Vlasenkob6332242006-10-03 19:57:50 +0000521 bb_perror_msg_and_die("can't stat '%s'", name);
Bernhard Reutner-Fischer57b56672006-09-11 09:18:09 +0000522}
Bernhard Reutner-Fischer57b56672006-09-11 09:18:09 +0000523
Rob Landleyfbdf1212006-09-20 22:06:01 +0000524/* It is perfectly ok to pass in a NULL for either width or for
Denis Vlasenko621204b2006-10-27 09:03:24 +0000525 * height, in which case that value will not be set. */
Rob Landleyfbdf1212006-09-20 22:06:01 +0000526int get_terminal_width_height(int fd, int *width, int *height)
527{
528 struct winsize win = { 0, 0, 0, 0 };
529 int ret = ioctl(fd, TIOCGWINSZ, &win);
Denis Vlasenko621204b2006-10-27 09:03:24 +0000530
531 if (height) {
532 if (!win.ws_row) {
533 char *s = getenv("LINES");
534 if (s) win.ws_row = atoi(s);
535 }
536 if (win.ws_row <= 1 || win.ws_row >= 30000)
537 win.ws_row = 24;
538 *height = (int) win.ws_row;
Rob Landleyfbdf1212006-09-20 22:06:01 +0000539 }
Denis Vlasenko621204b2006-10-27 09:03:24 +0000540
541 if (width) {
542 if (!win.ws_col) {
543 char *s = getenv("COLUMNS");
544 if (s) win.ws_col = atoi(s);
545 }
546 if (win.ws_col <= 1 || win.ws_col >= 30000)
547 win.ws_col = 80;
548 *width = (int) win.ws_col;
Rob Landleyfbdf1212006-09-20 22:06:01 +0000549 }
Rob Landleyfbdf1212006-09-20 22:06:01 +0000550
551 return ret;
552}