1 /* vi: set sw=4 ts=4: */
5 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
6 * Copyright (C) 2006 Rob Landley
7 * Copyright (C) 2006 Denis Vlasenko
9 * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
16 void *xmalloc(size_t size)
18 void *ptr = malloc(size);
19 if (ptr == NULL && size != 0)
20 bb_error_msg_and_die(bb_msg_memory_exhausted);
26 void *xrealloc(void *ptr, size_t size)
28 ptr = realloc(ptr, size);
29 if (ptr == NULL && size != 0)
30 bb_error_msg_and_die(bb_msg_memory_exhausted);
36 void *xzalloc(size_t size)
38 void *ptr = xmalloc(size);
47 char * xstrdup (const char *s)
57 bb_error_msg_and_die(bb_msg_memory_exhausted);
64 char * xstrndup (const char *s, int n)
68 if (ENABLE_DEBUG && s == NULL)
69 bb_error_msg_and_die("xstrndup bug");
73 return safe_strncpy(t,s,n);
78 FILE *xfopen(const char *path, const char *mode)
81 if ((fp = fopen(path, mode)) == NULL)
82 bb_perror_msg_and_die("%s", path);
88 int xopen(const char *pathname, int flags)
90 return xopen3(pathname, flags, 0777);
95 int xopen3(const char *pathname, int flags, int mode)
99 ret = open(pathname, flags, mode);
101 bb_perror_msg_and_die("%s", pathname);
109 // Die with an error message if we can't read the entire buffer.
111 void xread(int fd, void *buf, size_t count)
116 if ((size = safe_read(fd, buf, count)) < 1)
117 bb_error_msg_and_die("Short read");
119 buf = ((char *) buf) + size;
126 // Die with an error message if we can't write the entire buffer.
128 void xwrite(int fd, void *buf, size_t count)
133 if ((size = safe_write(fd, buf, count)) < 1)
134 bb_error_msg_and_die("Short write");
136 buf = ((char *) buf) + size;
143 // Die if we can't lseek to the right spot.
145 void xlseek(int fd, off_t offset, int whence)
147 if (whence != lseek(fd, offset, whence)) bb_error_msg_and_die("lseek");
152 unsigned char xread_char(int fd)
163 void xferror(FILE *fp, const char *fn)
166 bb_error_msg_and_die("%s", fn);
171 #ifdef L_xferror_stdout
172 void xferror_stdout(void)
174 xferror(stdout, bb_msg_standard_output);
178 #ifdef L_xfflush_stdout
179 void xfflush_stdout(void)
181 if (fflush(stdout)) {
182 bb_perror_msg_and_die(bb_msg_standard_output);
188 // This does a fork/exec in one call, using vfork().
189 pid_t spawn(char **argv)
193 void *app = find_applet_by_name(argv[0]);
195 // Be nice to nommu machines.
198 if (pid < 0) return pid;
200 execvp(app ? CONFIG_BUSYBOX_EXEC_PATH : *argv, argv);
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.)
209 return failed ? failed : pid;
214 pid_t xspawn(char **argv)
216 pid_t pid = spawn(argv);
217 if (pid < 0) bb_perror_msg_and_die("%s", *argv);
223 int wait4pid(int pid)
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);
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];
240 void utoa_to_buf(unsigned n, char *buf, unsigned buflen)
244 for (i=1000000000; i; i/=10) {
247 if ((res || out || i == 1) && --buflen>0) {
257 // Note: uses static buffer, calling it twice in a row will overwrite.
259 char *utoa(unsigned n)
261 utoa_to_buf(n, local_buf, sizeof(local_buf));
266 void itoa_to_buf(int n, char *buf, unsigned buflen)
273 utoa_to_buf((unsigned)n, buf, buflen);
276 // Note: uses static buffer, calling it twice in a row will overwrite.
280 itoa_to_buf(n, local_buf, sizeof(local_buf));
287 void xsetgid(gid_t gid)
289 if (setgid(gid)) bb_error_msg_and_die("setgid");
292 void xsetuid(uid_t uid)
294 if (setuid(uid)) bb_error_msg_and_die("setuid");
299 off_t fdlength(int fd)
301 off_t bottom = 0, top = 0, pos;
304 // If the ioctl works for this, return it.
306 if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512;
308 // If not, do a binary search for the last location we can read.
313 pos = bottom + (top - bottom) / 2;;
315 // If we can read from the current location, it's bigger.
317 if (lseek(fd, pos, 0)>=0 && safe_read(fd, &temp, 1)==1) {
318 if (bottom == top) bottom = top = (top+1) * 2;
321 // If we can't, it's smaller.
330 } while (bottom + 1 != top);
337 char *xasprintf(const char *format, ...)
346 r = vasprintf(&string_ptr, format, p);
349 // Bloat for systems that haven't got the GNU extension.
351 r = vsnprintf(NULL, 0, format, p);
353 string_ptr = xmalloc(r+1);
355 r = vsnprintf(string_ptr, r+1, format, p);
359 if (r < 0) bb_perror_msg_and_die("xasprintf");
364 #ifdef L_xprint_and_close_file
365 void xprint_and_close_file(FILE *file)
367 // copyfd outputs error messages for us.
368 if (bb_copyfd_eof(fileno(file), 1) == -1) exit(bb_default_error_retval);
375 void xchdir(const char *path)
378 bb_perror_msg_and_die("chdir(%s)", path);
382 #ifdef L_warn_opendir
383 DIR *warn_opendir(const char *path)
387 if ((dp = opendir(path)) == NULL) {
388 bb_perror_msg("unable to open `%s'", path);
396 DIR *xopendir(const char *path)
400 if ((dp = opendir(path)) == NULL)
401 bb_perror_msg_and_die("unable to open `%s'", path);
408 void xdaemon(int nochdir, int noclose)
410 if (daemon(nochdir, noclose)) bb_perror_msg_and_die("daemon");
416 void xbind(int sockfd, struct sockaddr *my_addr, socklen_t addrlen)
418 if (bind(sockfd, my_addr, addrlen)) bb_perror_msg_and_die("bind");
423 int xsocket(int domain, int type, int protocol)
425 int r = socket(domain, type, protocol);
427 if (r < 0) bb_perror_msg_and_die("socket");
434 void xlisten(int s, int backlog)
436 if (listen(s, backlog)) bb_perror_msg_and_die("listen");