tar: support -T - and -X -
[oweals/busybox.git] / libbb / xfuncs.c
index 1cd8d7c016374974864bb5937022dd558153b02c..23f27516f633c2178f2b00b24e13aec5c9c2d357 100644 (file)
@@ -6,7 +6,7 @@
  * Copyright (C) 2006 Rob Landley
  * Copyright (C) 2006 Denys Vlasenko
  *
- * Licensed under GPL version 2, see file LICENSE in this tarball for details.
+ * Licensed under GPLv2, see file LICENSE in this source tree.
  */
 
 /* We need to have separate xfuncs.c and xfuncs_printf.c because
 #include "libbb.h"
 
 /* Turn on nonblocking I/O on a fd */
-int FAST_FUNC ndelay_on(int fd)
+void FAST_FUNC ndelay_on(int fd)
 {
-       return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) | O_NONBLOCK);
+       int flags = fcntl(fd, F_GETFL);
+       if (flags & O_NONBLOCK)
+               return;
+       fcntl(fd, F_SETFL, flags | O_NONBLOCK);
 }
 
-int FAST_FUNC ndelay_off(int fd)
+void FAST_FUNC ndelay_off(int fd)
 {
-       return fcntl(fd, F_SETFL, fcntl(fd, F_GETFL) & ~O_NONBLOCK);
+       int flags = fcntl(fd, F_GETFL);
+       if (!(flags & O_NONBLOCK))
+               return;
+       fcntl(fd, F_SETFL, flags & ~O_NONBLOCK);
 }
 
-int FAST_FUNC close_on_exec_on(int fd)
+void FAST_FUNC close_on_exec_on(int fd)
 {
-       return fcntl(fd, F_SETFD, FD_CLOEXEC);
+       fcntl(fd, F_SETFD, FD_CLOEXEC);
 }
 
 char* FAST_FUNC strncpy_IFNAMSIZ(char *dst, const char *src)
@@ -49,23 +55,35 @@ char* FAST_FUNC strncpy_IFNAMSIZ(char *dst, const char *src)
 }
 
 
-// Convert unsigned integer to ascii, writing into supplied buffer.
-// A truncated result contains the first few digits of the result ala strncpy.
-// Returns a pointer past last generated digit, does _not_ store NUL.
-void BUG_sizeof_unsigned_not_4(void);
+/* Convert unsigned integer to ascii, writing into supplied buffer.
+ * A truncated result contains the first few digits of the result ala strncpy.
+ * Returns a pointer past last generated digit, does _not_ store NUL.
+ */
+void BUG_sizeof(void);
 char* FAST_FUNC utoa_to_buf(unsigned n, char *buf, unsigned buflen)
 {
        unsigned i, out, res;
-       if (sizeof(unsigned) != 4)
-               BUG_sizeof_unsigned_not_4();
+
        if (buflen) {
                out = 0;
-               for (i = 1000000000; i; i /= 10) {
+               if (sizeof(n) == 4)
+               // 2^32-1 = 4294967295
+                       i = 1000000000;
+#if UINT_MAX > 4294967295 /* prevents warning about "const too large" */
+               else
+               if (sizeof(n) == 8)
+               // 2^64-1 = 18446744073709551615
+                       i = 10000000000000000000;
+#endif
+               else
+                       BUG_sizeof();
+               for (; i; i /= 10) {
                        res = n / i;
+                       n = n % i;
                        if (res || out || i == 1) {
-                               if (!--buflen) break;
+                               if (--buflen == 0)
+                                       break;
                                out++;
-                               n -= res*i;
                                *buf++ = '0' + res;
                        }
                }
@@ -76,7 +94,9 @@ char* FAST_FUNC utoa_to_buf(unsigned n, char *buf, unsigned buflen)
 /* Convert signed integer to ascii, like utoa_to_buf() */
 char* FAST_FUNC itoa_to_buf(int n, char *buf, unsigned buflen)
 {
-       if (buflen && n < 0) {
+       if (!buflen)
+               return buf;
+       if (n < 0) {
                n = -n;
                *buf++ = '-';
                buflen--;
@@ -87,16 +107,16 @@ char* FAST_FUNC itoa_to_buf(int n, char *buf, unsigned buflen)
 // The following two functions use a static buffer, so calling either one a
 // second time will overwrite previous results.
 //
-// The largest 32 bit integer is -2 billion plus null terminator, or 12 bytes.
-// It so happens that sizeof(int) * 3 is enough for 32+ bits.
+// The largest 32 bit integer is -2 billion plus NUL, or 1+10+1=12 bytes.
+// It so happens that sizeof(int) * 3 is enough for 32+ bit ints.
 // (sizeof(int) * 3 + 2 is correct for any width, even 8-bit)
 
 static char local_buf[sizeof(int) * 3];
 
-// Convert unsigned integer to ascii using a static buffer (returned).
+/* Convert unsigned integer to ascii using a static buffer (returned). */
 char* FAST_FUNC utoa(unsigned n)
 {
-       *(utoa_to_buf(n, local_buf, sizeof(local_buf))) = '\0';
+       *(utoa_to_buf(n, local_buf, sizeof(local_buf) - 1)) = '\0';
 
        return local_buf;
 }
@@ -104,7 +124,7 @@ char* FAST_FUNC utoa(unsigned n)
 /* Convert signed integer to ascii using a static buffer (returned). */
 char* FAST_FUNC itoa(int n)
 {
-       *(itoa_to_buf(n, local_buf, sizeof(local_buf))) = '\0';
+       *(itoa_to_buf(n, local_buf, sizeof(local_buf) - 1)) = '\0';
 
        return local_buf;
 }
@@ -220,7 +240,7 @@ static int wh_helper(int value, int def_val, const char *env_name, int *err)
                char *s = getenv(env_name);
                if (s) {
                        value = atoi(s);
-                       /* If LINES/COLUMNS are set, pretent that there is
+                       /* If LINES/COLUMNS are set, pretend that there is
                         * no error getting w/h, this prevents some ugly
                         * cursor tricks by our callers */
                        *err = 0;
@@ -254,3 +274,37 @@ int FAST_FUNC tcsetattr_stdin_TCSANOW(const struct termios *tp)
 {
        return tcsetattr(STDIN_FILENO, TCSANOW, tp);
 }
+
+pid_t FAST_FUNC safe_waitpid(pid_t pid, int *wstat, int options)
+{
+       pid_t r;
+
+       do
+               r = waitpid(pid, wstat, options);
+       while ((r == -1) && (errno == EINTR));
+       return r;
+}
+
+pid_t FAST_FUNC wait_any_nohang(int *wstat)
+{
+       return safe_waitpid(-1, wstat, WNOHANG);
+}
+
+// Wait for the specified child PID to exit, returning child's error return.
+int FAST_FUNC wait4pid(pid_t pid)
+{
+       int status;
+
+       if (pid <= 0) {
+               /*errno = ECHILD; -- wrong. */
+               /* we expect errno to be already set from failed [v]fork/exec */
+               return -1;
+       }
+       if (safe_waitpid(pid, &status, 0) == -1)
+               return -1;
+       if (WIFEXITED(status))
+               return WEXITSTATUS(status);
+       if (WIFSIGNALED(status))
+               return WTERMSIG(status) + 0x180;
+       return 0;
+}