find: improve usage text (Natanael Copa <natanael.copa@gmail.com>)
[oweals/busybox.git] / coreutils / tail.c
index 98fbcc7c4cf8d195f3d25f8dc7ad533df224ba8e..67396ab1c52456c60f8aebf66d3fcf6a3cefb604 100644 (file)
@@ -33,7 +33,10 @@ static const struct suffix_mult tail_suffixes[] = {
        { NULL, 0 }
 };
 
-static int status;
+struct globals {
+       bool status;
+};
+#define G (*(struct globals*)&bb_common_bufsiz1)
 
 static void tail_xprint_header(const char *fmt, const char *filename)
 {
@@ -54,7 +57,7 @@ static ssize_t tail_read(int fd, char *buf, size_t count)
        r = safe_read(fd, buf, count);
        if (r < 0) {
                bb_perror_msg(bb_msg_read_error);
-               status = EXIT_FAILURE;
+               G.status = EXIT_FAILURE;
        }
 
        return r;
@@ -62,11 +65,18 @@ static ssize_t tail_read(int fd, char *buf, size_t count)
 
 static const char header_fmt[] = "\n==> %s <==\n";
 
+static unsigned eat_num(const char *p) {
+       if (*p == '-') p++;
+       else if (*p == '+') { p++; G.status = EXIT_FAILURE; }
+       return xatou_sfx(p, tail_suffixes);
+}
+
+int tail_main(int argc, char **argv);
 int tail_main(int argc, char **argv)
 {
        unsigned count = 10;
        unsigned sleep_period = 1;
-       int from_top = 0;
+       bool from_top;
        int header_threshhold = 1;
        const char *str_c, *str_n, *str_s;
 
@@ -80,19 +90,12 @@ int tail_main(int argc, char **argv)
        char *s, *buf;
        const char *fmt;
 
-       void eat_num(const char *p) {
-               if (*p == '-') p++;
-               else if (*p == '+') { p++; from_top = 1; }
-               count = xatou_sfx(p, tail_suffixes);
-       }
-
-
 #if ENABLE_INCLUDE_SUSv2 || ENABLE_FEATURE_FANCY_TAIL
        /* Allow legacy syntax of an initial numeric option without -n. */
        if (argc >= 2 && (argv[1][0] == '+' || argv[1][0] == '-')
         && isdigit(argv[1][1])
        ) {
-               argv[0] = "-n";
+               argv[0] = (char*)"-n";
                argv--;
                argc++;
        }
@@ -102,8 +105,8 @@ int tail_main(int argc, char **argv)
 #define FOLLOW (opt & 0x1)
 #define COUNT_BYTES (opt & 0x2)
        //if (opt & 0x1) // -f
-       if (opt & 0x2) eat_num(str_c); // -c
-       if (opt & 0x4) eat_num(str_n); // -n
+       if (opt & 0x2) count = eat_num(str_c); // -c
+       if (opt & 0x4) count = eat_num(str_n); // -n
 #if ENABLE_FEATURE_FANCY_TAIL
        if (opt & 0x8) header_threshhold = INT_MAX; // -q
        if (opt & 0x10) sleep_period = xatou(str_s); // -s
@@ -111,10 +114,12 @@ int tail_main(int argc, char **argv)
 #endif
        argc -= optind;
        argv += optind;
+       from_top = G.status;
 
        /* open all the files */
        fds = xmalloc(sizeof(int) * (argc + 1));
        nfiles = i = 0;
+       G.status = EXIT_SUCCESS;
        if (argc == 0) {
                struct stat statbuf;
 
@@ -122,23 +127,15 @@ int tail_main(int argc, char **argv)
                        opt &= ~1; /* clear FOLLOW */
                }
                *argv = (char *) bb_msg_standard_input;
-               goto DO_STDIN;
        }
-
        do {
-               if (NOT_LONE_DASH(argv[i])) {
-                       fds[nfiles] = open(argv[i], O_RDONLY);
-                       if (fds[nfiles] < 0) {
-                               bb_perror_msg("%s", argv[i]);
-                               status = EXIT_FAILURE;
-                               continue;
-                       }
-               } else {
- DO_STDIN:             /* "-" */
-                       fds[nfiles] = STDIN_FILENO;
+               FILE* fil = fopen_or_warn_stdin(argv[i]);
+               if (!fil) {
+                       G.status = EXIT_FAILURE;
+                       continue;
                }
-               argv[nfiles] = argv[i];
-               ++nfiles;
+               fds[nfiles] = fileno(fil);
+               argv[nfiles++] = argv[i];
        } while (++i < argc);
 
        if (!nfiles)
@@ -216,13 +213,11 @@ int tail_main(int argc, char **argv)
                                        if (newline + nbuf < count) {
                                                newline += nbuf;
                                                taillen += nread;
-
                                        } else {
                                                int extra = 0;
-                                               if (buf[nread-1] != '\n') {
-                                                       extra = 1;
-                                               }
 
+                                               if (buf[nread-1] != '\n')
+                                                       extra = 1;
                                                k = newline + nbuf + extra - count;
                                                s = tailbuf;
                                                while (k) {
@@ -231,7 +226,6 @@ int tail_main(int argc, char **argv)
                                                        }
                                                        ++s;
                                                }
-
                                                taillen += nread - (s - tailbuf);
                                                memmove(tailbuf, s, taillen);
                                                newline = count - extra;
@@ -272,6 +266,8 @@ int tail_main(int argc, char **argv)
                        }
                } while (++i < nfiles);
        }
-
-       return status;
+       if (ENABLE_FEATURE_CLEAN_UP) {
+               free(fds);
+       }
+       return G.status;
 }