find: support -HLP
[oweals/busybox.git] / shell / shell_common.c
index 86a6493ed6febe10e54f7edb8e77ea277044414a..0051f21d9eb08d878ff72d6c6dda984b265a12d2 100644 (file)
@@ -18,6 +18,7 @@
  */
 #include "libbb.h"
 #include "shell_common.h"
+#include <sys/resource.h> /* getrlimit */
 
 const char defifsvar[] ALIGN1 = "IFS= \t\n";
 
@@ -36,6 +37,10 @@ int FAST_FUNC is_well_formed_var_name(const char *s, char terminator)
 
 /* read builtin */
 
+/* Needs to be interruptible: shell mush handle traps and shell-special signals
+ * while inside read. To implement this, be sure to not loop on EINTR
+ * and return errno == EINTR reliably.
+ */
 //TODO: use more efficient setvar() which takes a pointer to malloced "VAR=VAL"
 //string. hush naturally has it, and ash has setvareq().
 //Here we can simply store "VAR=" at buffer start and store read data directly
@@ -51,6 +56,7 @@ shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val),
        const char *opt_u
 )
 {
+       unsigned err;
        unsigned end_ms; /* -t TIMEOUT */
        int fd; /* -u FD */
        int nchars; /* -n NUM */
@@ -62,6 +68,8 @@ shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val),
        int startword;
        smallint backslash;
 
+       errno = err = 0;
+
        pp = argv;
        while (*pp) {
                if (!is_well_formed_var_name(*pp, '\0')) {
@@ -131,7 +139,13 @@ shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val),
                old_tty = tty;
                if (nchars) {
                        tty.c_lflag &= ~ICANON;
-                       tty.c_cc[VMIN] = nchars < 256 ? nchars : 255;
+                       // Setting it to more than 1 breaks poll():
+                       // it blocks even if there's data. !??
+                       //tty.c_cc[VMIN] = nchars < 256 ? nchars : 255;
+                       /* reads would block only if < 1 char is available */
+                       tty.c_cc[VMIN] = 1;
+                       /* no timeout (reads block forever) */
+                       tty.c_cc[VTIME] = 0;
                }
                if (read_flags & BUILTIN_READ_SILENT) {
                        tty.c_lflag &= ~(ECHO | ECHOK | ECHONL);
@@ -152,28 +166,40 @@ shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val),
        bufpos = 0;
        do {
                char c;
+               struct pollfd pfd[1];
+               int timeout;
 
-               if (end_ms) {
-                       int timeout;
-                       struct pollfd pfd[1];
+               if ((bufpos & 0xff) == 0)
+                       buffer = xrealloc(buffer, bufpos + 0x101);
 
-                       pfd[0].fd = fd;
-                       pfd[0].events = POLLIN;
+               timeout = -1;
+               if (end_ms) {
                        timeout = end_ms - (unsigned)monotonic_ms();
-                       if (timeout <= 0 /* already late? */
-                        || safe_poll(pfd, 1, timeout) != 1 /* no? wait... */
-                       ) { /* timed out! */
+                       if (timeout <= 0) { /* already late? */
                                retval = (const char *)(uintptr_t)1;
                                goto ret;
                        }
                }
 
-               if ((bufpos & 0xff) == 0)
-                       buffer = xrealloc(buffer, bufpos + 0x100);
-               if (nonblock_immune_read(fd, &buffer[bufpos], 1) != 1) {
+               /* We must poll even if timeout is -1:
+                * we want to be interrupted if signal arrives,
+                * regardless of SA_RESTART-ness of that signal!
+                */
+               errno = 0;
+               pfd[0].fd = fd;
+               pfd[0].events = POLLIN;
+               if (poll(pfd, 1, timeout) != 1) {
+                       /* timed out, or EINTR */
+                       err = errno;
+                       retval = (const char *)(uintptr_t)1;
+                       goto ret;
+               }
+               if (read(fd, &buffer[bufpos], 1) != 1) {
+                       err = errno;
                        retval = (const char *)(uintptr_t)1;
                        break;
                }
+
                c = buffer[bufpos];
                if (c == '\0')
                        continue;
@@ -240,6 +266,8 @@ shell_builtin_read(void FAST_FUNC (*setvar)(const char *name, const char *val),
        free(buffer);
        if (read_flags & BUILTIN_READ_SILENT)
                tcsetattr(fd, TCSANOW, &old_tty);
+
+       errno = err;
        return retval;
 }