-
-// Die with an error message if we can't malloc() enough space and do an
-// sprintf() into that space.
-char *xasprintf(const char *format, ...)
-{
- va_list p;
- int r;
- char *string_ptr;
-
-#if 1
- // GNU extension
- va_start(p, format);
- r = vasprintf(&string_ptr, format, p);
- va_end(p);
-#else
- // Bloat for systems that haven't got the GNU extension.
- va_start(p, format);
- r = vsnprintf(NULL, 0, format, p);
- va_end(p);
- string_ptr = xmalloc(r+1);
- va_start(p, format);
- r = vsnprintf(string_ptr, r+1, format, p);
- va_end(p);
-#endif
-
- if (r < 0) bb_error_msg_and_die(bb_msg_memory_exhausted);
- return string_ptr;
-}
-
-#if 0 /* If we will ever meet a libc which hasn't [f]dprintf... */
-int fdprintf(int fd, const char *format, ...)
-{
- va_list p;
- int r;
- char *string_ptr;
-
-#if 1
- // GNU extension
- va_start(p, format);
- r = vasprintf(&string_ptr, format, p);
- va_end(p);
-#else
- // Bloat for systems that haven't got the GNU extension.
- va_start(p, format);
- r = vsnprintf(NULL, 0, format, p);
- va_end(p);
- string_ptr = xmalloc(r+1);
- va_start(p, format);
- r = vsnprintf(string_ptr, r+1, format, p);
- va_end(p);
-#endif
-
- if (r >= 0) {
- full_write(fd, string_ptr, r);
- free(string_ptr);
- }
- return r;
-}
-#endif
-
-// Die with an error message if we can't copy an entire FILE * to stdout, then
-// close that file.
-void xprint_and_close_file(FILE *file)
-{
- fflush(stdout);
- // copyfd outputs error messages for us.
- if (bb_copyfd_eof(fileno(file), 1) == -1)
- exit(xfunc_error_retval);
-
- fclose(file);
-}
-
-// Die if we can't chdir to a new path.
-void xchdir(const char *path)
-{
- if (chdir(path))
- bb_perror_msg_and_die("chdir(%s)", path);
-}
-
-// Print a warning message if opendir() fails, but don't die.
-DIR *warn_opendir(const char *path)
-{
- DIR *dp;
-
- if ((dp = opendir(path)) == NULL) {
- bb_perror_msg("cannot open '%s'", path);
- return NULL;
- }
- return dp;
-}
-
-// Die with an error message if opendir() fails.
-DIR *xopendir(const char *path)
-{
- DIR *dp;
-
- if ((dp = opendir(path)) == NULL)
- bb_perror_msg_and_die("cannot open '%s'", path);
- return dp;
-}
-
-#ifndef BB_NOMMU
-// Die with an error message if we can't daemonize.
-void xdaemon(int nochdir, int noclose)
-{
- if (daemon(nochdir, noclose))
- bb_perror_msg_and_die("daemon");
-}