* 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)
}
-// 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;
}
}
/* 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--;
// 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;
}
/* 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;
}
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;
{
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;
+}