Comment on kernel stuff
[oweals/busybox.git] / coreutils / tail.c
index 31705afa20fabf46272ae93e399907c24aa08013..3189d204f47c8c0061337d586099c8feda6d2d25 100644 (file)
@@ -1,5 +1,6 @@
 /* vi: set sw=4 ts=4: */
 #include "internal.h"
+
 /* This file contains _two_ implementations of tail.  One is
  * a bit more full featured, but costs 6k.  The other (i.e. the
  * SIMPLE_TAIL one) is less capable, but is good enough for about
 #include <sys/stat.h>
 #include <fcntl.h>
 #include <ctype.h>
+#define BB_DECLARE_EXTERN
+#define bb_need_help
+#include "messages.c"
 
 
 #define XWRITE(fd, buffer, n_bytes)                                    \
   do {                                                                 \
       if (n_bytes > 0 && fwrite ((buffer), 1, (n_bytes), stdout) == 0) \
-         error("write error");                                 \
+         errorMsg("write error");                                      \
   } while (0)
 
 /* Number of items to tail.  */
@@ -69,15 +73,18 @@ static int forever;
 static int print_headers;
 
 const char tail_usage[] =
-       "tail [OPTION] [FILE]...\n\n"
-       "Print last 10 lines of each FILE to standard output.\n"
+       "tail [OPTION] [FILE]...\n"
+#ifndef BB_FEATURE_TRIVIAL_HELP
+       "\nPrint last 10 lines of each FILE to standard output.\n"
        "With more than one FILE, precede each with a header giving the\n"
        "file name. With no FILE, or when FILE is -, read standard input.\n\n"
        "Options:\n"
        "\t-n NUM\t\tPrint last NUM lines instead of first 10\n"
 
        "\t-f\t\tOutput data as the file grows.  This version\n"
-       "\t\t\tof 'tail -f' supports only one file at a time.\n";
+       "\t\t\tof 'tail -f' supports only one file at a time.\n"
+#endif
+       ;
 
 
 static void write_header(const char *filename)
@@ -117,7 +124,7 @@ file_lines(const char *filename, int fd, long int n_lines, off_t pos)
        lseek(fd, pos, SEEK_SET);
        bytes_read = fullRead(fd, buffer, bytes_read);
        if (bytes_read == -1)
-               error("read error");
+               errorMsg("read error");
 
        /* Count the incomplete line on files that don't end with a newline.  */
        if (bytes_read && buffer[bytes_read - 1] != '\n')
@@ -147,7 +154,7 @@ file_lines(const char *filename, int fd, long int n_lines, off_t pos)
        }
        while ((bytes_read = fullRead(fd, buffer, BUFSIZ)) > 0);
        if (bytes_read == -1)
-               error("read error");
+               errorMsg("read error");
 
        return 0;
 }
@@ -209,7 +216,7 @@ static int pipe_lines(const char *filename, int fd, long int n_lines)
                }
        }
        if (tmp->nbytes == -1)
-               error("read error");
+               errorMsg("read error");
 
        free((char *) tmp);
 
@@ -272,7 +279,7 @@ static long dump_remainder(const char *filename, int fd)
                total += bytes_read;
        }
        if (bytes_read == -1)
-               error("read error");
+               errorMsg("read error");
        if (forever) {
                fflush(stdout);
                sleep(1);
@@ -294,7 +301,7 @@ static int tail_lines(const char *filename, int fd, long int n_lines)
                write_header(filename);
 
        if (fstat(fd, &stats))
-               error("fstat error");
+               errorMsg("fstat error");
 
        /* Use file_lines only if FD refers to a regular file with
           its file pointer positioned at beginning of file.  */
@@ -329,7 +336,7 @@ static int tail_file(const char *filename, off_t n_units)
                /* Not standard input.  */
                fd = open(filename, O_RDONLY);
                if (fd == -1)
-                       error("open error");
+                       perror(filename);
 
                errors = tail_lines(filename, fd, (long) n_units);
                close(fd);
@@ -367,8 +374,10 @@ extern int tail_main(int argc, char **argv)
                        case 'h':
                                usage(tail_usage);
                        default:
-                               fprintf(stderr, "tail: invalid option -- %c\n", opt);
-                               usage(tail_usage);
+                               if ((n_units = atoi(&argv[i][1])) < 1) {
+                                       fprintf(stderr, "tail: invalid option -- %c\n", opt);
+                                       usage(tail_usage);
+                               }
                        }
                } else {
                        break;
@@ -391,7 +400,7 @@ extern int tail_main(int argc, char **argv)
                        exit_status |= tail_file(argv[i], n_units);
        }
 
-       exit(exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
+       return(exit_status == 0 ? EXIT_SUCCESS : EXIT_FAILURE);
 }
 
 
@@ -443,6 +452,8 @@ extern int tail_main(int argc, char **argv)
 #define NDEBUG 1
 
 
+static void detailed_error(int i, int errnum, char *fmt, ...)
+                         __attribute__ ((format (printf, 3, 4)));
 static void detailed_error(int i, int errnum, char *fmt, ...)
 {
        va_list arguments;
@@ -507,9 +518,9 @@ char *program_name;
 static int have_read_stdin;
 
 
-static const char tail_usage[] = "tail [OPTION]... [FILE]...\n\
-\n\
-Print last 10 lines of each FILE to standard output.\n\
+static const char tail_usage[] = "tail [OPTION]... [FILE]...\n"
+#ifndef BB_FEATURE_TRIVIAL_HELP
+"\nPrint last 10 lines of each FILE to standard output.\n\
 With more than one FILE, precede each with a header giving the file name.\n\
 With no FILE, or when FILE is -, read standard input.\n\
 \n\
@@ -518,11 +529,12 @@ With no FILE, or when FILE is -, read standard input.\n\
   -n=N            output the last N lines, instead of last 10\n\
   -q              never output headers giving file names\n\
   -v              always output headers giving file names\n\
-  --help          display this help and exit\n\
 \n\
 If the first character of N (bytes or lines) is a `+', output begins with \n\
 the Nth item from the start of each file, otherwise, print the last N items\n\
-in the file.  N bytes may be suffixed by k (x1024), b (x512), or m (1024^2).\n\n";
+in the file.  N bytes may be suffixed by k (x1024), b (x512), or m (1024^2).\n"
+#endif
+;
 
 static void write_header(const char *filename, const char *comment)
 {