rodata cleanup. "unable to" == "cannot". -300 bytes
[oweals/busybox.git] / coreutils / tail.c
index ef00383853831a7f183a5ba506ae73d1b4e05bc7..82c0d99bcc929d21b69ef58b188dbeedfa5e418c 100644 (file)
@@ -4,20 +4,7 @@
  *
  * Copyright (C) 2001 by Matt Kraai <kraai@alumni.carnegiemellon.edu>
  *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
- * General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
- *
+ * Licensed under GPLv2 or later, see file LICENSE in this tarball for details.
  */
 
 /* BB_AUDIT SUSv3 compliant (need fancy for -c) */
@@ -53,36 +40,41 @@ static const struct suffix_mult tail_suffixes[] = {
        { NULL, 0 }
 };
 
-static int status
-#if EXIT_SUCCESS != 0
-       = EXIT_SUCCESS  /* If it is 0 (paranoid check), let bss initialize it. */
-#endif
-       ;
+static int status;
 
-static void tail_xprint_header(const char *fmt, const char *filename)
+static void tail_xbb_full_write(const char *buf, size_t len)
 {
-       /* If we get an output error, there is really no sense in continuing. */
-       if (printf(fmt, filename) < 0 ||
-                       fflush(stdout) < 0) {
+       /* If we get a write error, there is really no sense in continuing. */
+       if (full_write(STDOUT_FILENO, buf, len) < 0)
                bb_perror_nomsg_and_die();
-       }
 }
 
-/* len should probably be size_t */
-static void tail_xbb_full_write(const char *buf, size_t len)
+static void tail_xprint_header(const char *fmt, const char *filename)
 {
-       /* If we get a write error, there is really no sense in continuing. */
-       if (bb_full_write(STDOUT_FILENO, buf, len) < 0) {
+#if defined __GLIBC__
+       if (dprintf(STDOUT_FILENO, fmt, filename) < 0) {
                bb_perror_nomsg_and_die();
        }
+#else
+       int hdr_len = strlen(fmt) + strlen(filename);
+       char *hdr = xzalloc(hdr_len);
+       sprintf(hdr, filename, filename);
+       tail_xbb_full_write(hdr, hdr_len);
+#endif
 }
 
 static ssize_t tail_read(int fd, char *buf, size_t count)
 {
        ssize_t r;
+       off_t current,end;
+       struct stat sbuf;
 
+       end = current = lseek(fd, 0, SEEK_CUR);
+       if (!fstat(fd, &sbuf))
+               end = sbuf.st_size;
+       lseek(fd, end < current ? 0 : current, SEEK_SET);
        if ((r = safe_read(fd, buf, count)) < 0) {
-               bb_perror_msg("read");
+               bb_perror_msg(bb_msg_read_error);
                status = EXIT_FAILURE;
        }
 
@@ -91,7 +83,7 @@ static ssize_t tail_read(int fd, char *buf, size_t count)
 
 static const char tail_opts[] =
        "fn:c:"
-#ifdef CONFIG_FEATURE_FANCY_TAIL
+#if ENABLE_FEATURE_FANCY_TAIL
        "qs:v"
 #endif
        ;
@@ -116,6 +108,7 @@ int tail_main(int argc, char **argv)
        char *s, *buf;
        const char *fmt;
 
+#if !ENABLE_DEBUG_YANK_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]) */
@@ -125,6 +118,7 @@ int tail_main(int argc, char **argv)
                optarg = argv[1];
                goto GET_COUNT;
        }
+#endif
 
        while ((opt = getopt(argc, argv, tail_opts)) > 0) {
                switch (opt) {
@@ -135,8 +129,10 @@ int tail_main(int argc, char **argv)
                                count_bytes = 1;
                                /* FALLS THROUGH */
                        case 'n':
+#if !ENABLE_DEBUG_YANK_SUSv2 || ENABLE_FEATURE_FANCY_TAIL
                        GET_COUNT:
-                               count = bb_xgetlarg10_sfx(optarg, tail_suffixes);
+#endif
+                               count = xatol_sfx(optarg, tail_suffixes);
                                /* Note: Leading whitespace is an error trapped above. */
                                if (*optarg == '+') {
                                        from_top = 1;
@@ -147,12 +143,12 @@ int tail_main(int argc, char **argv)
                                        count = -count;
                                }
                                break;
-#ifdef CONFIG_FEATURE_FANCY_TAIL
+#if ENABLE_FEATURE_FANCY_TAIL
                        case 'q':
                                header_threshhold = INT_MAX;
                                break;
                        case 's':
-                               sleep_period =bb_xgetularg10_bnd(optarg, 0, UINT_MAX);
+                               sleep_period = xatou(optarg);
                                break;
                        case 'v':
                                header_threshhold = 0;