1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
7 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
10 #include <sys/types.h>
22 void *xmalloc(size_t size)
24 void *ptr = malloc(size);
25 if (ptr == NULL && size != 0)
26 bb_error_msg_and_die(bb_msg_memory_exhausted);
32 void *xrealloc(void *ptr, size_t size)
34 ptr = realloc(ptr, size);
35 if (ptr == NULL && size != 0)
36 bb_error_msg_and_die(bb_msg_memory_exhausted);
42 void *xzalloc(size_t size)
44 void *ptr = xmalloc(size);
51 void *xcalloc(size_t nmemb, size_t size)
53 void *ptr = calloc(nmemb, size);
54 if (ptr == NULL && nmemb != 0 && size != 0)
55 bb_error_msg_and_die(bb_msg_memory_exhausted);
62 char * bb_xstrdup (const char *s)
72 bb_error_msg_and_die(bb_msg_memory_exhausted);
79 char * bb_xstrndup (const char *s, int n)
83 if (ENABLE_DEBUG && s == NULL)
84 bb_error_msg_and_die("bb_xstrndup bug");
88 return safe_strncpy(t,s,n);
93 FILE *bb_xfopen(const char *path, const char *mode)
96 if ((fp = fopen(path, mode)) == NULL)
97 bb_perror_msg_and_die("%s", path);
103 int bb_xopen(const char *pathname, int flags)
105 return bb_xopen3(pathname, flags, 0777);
110 int bb_xopen3(const char *pathname, int flags, int mode)
114 ret = open(pathname, flags, mode);
116 bb_perror_msg_and_die("%s", pathname);
124 // Die with an error message if we can't read the entire buffer.
126 void xread(int fd, void *buf, size_t count)
131 if ((size = safe_read(fd, buf, count)) < 1)
132 bb_error_msg_and_die("Short read");
134 buf = ((char *) buf) + size;
141 // Die with an error message if we can't write the entire buffer.
143 void xwrite(int fd, void *buf, size_t count)
148 if ((size = safe_write(fd, buf, count)) < 1)
149 bb_error_msg_and_die("Short write");
151 buf = ((char *) buf) + size;
158 // Die if we can't lseek to the right spot.
160 void xlseek(int fd, off_t offset, int whence)
162 if (whence != lseek(fd, offset, whence)) bb_error_msg_and_die("lseek");
167 unsigned char xread_char(int fd)
178 void bb_xferror(FILE *fp, const char *fn)
181 bb_error_msg_and_die("%s", fn);
186 #ifdef L_xferror_stdout
187 void bb_xferror_stdout(void)
189 bb_xferror(stdout, bb_msg_standard_output);
193 #ifdef L_xfflush_stdout
194 void bb_xfflush_stdout(void)
196 if (fflush(stdout)) {
197 bb_perror_msg_and_die(bb_msg_standard_output);
203 // This does a fork/exec in one call, using vfork().
204 pid_t bb_spawn(char **argv)
208 void *app = find_applet_by_name(argv[0]);
210 // Be nice to nommu machines.
213 if (pid < 0) return pid;
215 execvp(app ? CONFIG_BUSYBOX_EXEC_PATH : *argv, argv);
217 // We're sharing a stack with blocked parent, let parent know we failed
218 // and then exit to unblock parent (but don't run atexit() stuff, which
219 // would screw up parent.)
224 return failed ? failed : pid;
229 pid_t bb_xspawn(char **argv)
231 pid_t pid = bb_spawn(argv);
232 if (pid < 0) bb_perror_msg_and_die("%s", *argv);
238 int wait4pid(int pid)
242 if (pid == -1 || waitpid(pid, &status, 0) == -1) return -1;
243 if (WIFEXITED(status)) return WEXITSTATUS(status);
244 if (WIFSIGNALED(status)) return WTERMSIG(status);
250 // Largest 32 bit integer is -2 billion plus null terminator.
251 // Int should always be 32 bits on a Unix-oid system, see
252 // http://www.unix.org/whitepapers/64bit.html
253 static char local_buf[12];
255 void utoa_to_buf(unsigned n, char *buf, unsigned buflen)
259 for (i=1000000000; i; i/=10) {
262 if ((res || out || i == 1) && --buflen>0) {
272 // Note: uses static buffer, calling it twice in a row will overwrite.
274 char *utoa(unsigned n)
276 utoa_to_buf(n, local_buf, sizeof(local_buf));
281 void itoa_to_buf(int n, char *buf, unsigned buflen)
288 utoa_to_buf((unsigned)n, buf, buflen);
291 // Note: uses static buffer, calling it twice in a row will overwrite.
295 itoa_to_buf(n, local_buf, sizeof(local_buf));
302 void xsetgid(gid_t gid)
304 if (setgid(gid)) bb_error_msg_and_die("setgid");
307 void xsetuid(uid_t uid)
309 if (setuid(uid)) bb_error_msg_and_die("setuid");
314 off_t fdlength(int fd)
316 off_t bottom = 0, top = 0, pos;
319 // If the ioctl works for this, return it.
321 if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512;
323 // If not, do a binary search for the last location we can read.
328 pos = bottom + (top - bottom) / 2;;
330 // If we can read from the current location, it's bigger.
332 if (lseek(fd, pos, 0)>=0 && safe_read(fd, &temp, 1)==1) {
333 if (bottom == top) bottom = top = (top+1) * 2;
336 // If we can't, it's smaller.
345 } while (bottom + 1 != top);