Remove #ifdef __STDC__ junk. We don't do K&R round these parts,
[oweals/busybox.git] / coreutils / tail.c
index 156f6368b3943f6ee0df55c0ab7b1fbf00ecf6d0..a9da95462efc2743cbdffa3b59cd648f3683719b 100644 (file)
 
  */
 #include <sys/types.h>
-#include <sys/stat.h>
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
 #include <string.h>
 #include <getopt.h>
-#include "internal.h"
+#include "busybox.h"
 
 #define STDIN "standard input"
 #define LINES 0
@@ -62,13 +61,13 @@ static char verbose = 0;
 
 static off_t units=0;
 
-int tail_stream(int fd)
+static int tail_stream(int fd)
 {
        ssize_t startpoint;
        ssize_t endpoint=0;
        ssize_t count=0;
        ssize_t filesize=0;
-       char direction=1;
+       int direction=1;
 
        filelocation=0;
        startpoint=bs=BUFSIZ;
@@ -88,12 +87,12 @@ int tail_stream(int fd)
                        ssize_t f_size=0;
 
                        bs=BUFSIZ;
-                       line=malloc(bs);
+                       line=xmalloc(bs);
                        while(1) {
                                bytes_read=read(fd,line,bs);
                                if(bytes_read<=0)
                                        break;
-                               buffer=realloc(buffer,f_size+bytes_read);
+                               buffer=xrealloc(buffer,f_size+bytes_read);
                                memcpy(&buffer[f_size],line,bytes_read);
                                filelocation=f_size+=bytes_read;
                        }
@@ -151,44 +150,26 @@ int tail_stream(int fd)
 void add_file(char *name)
 {
        ++n_files;
-       files = realloc(files, n_files);
-       files[n_files - 1] = (char *) malloc(strlen(name) + 1);
+       files = xrealloc(files, n_files);
+       files[n_files - 1] = (char *) xmalloc(strlen(name) + 1);
        strcpy(files[n_files - 1], name);
 }
 
-
 int tail_main(int argc, char **argv)
 {
        int show_headers = 1;
        int test;
        int opt;
-       int optc=0;
-       char **optv=NULL;
        char follow=0;
        int sleep_int=1;
        int *fd;
 
        opterr = 0;
        
-       for(opt=0;opt<argc;opt++){
-               test=atoi(argv[opt]);
-               if(test){
-                       units=test;
-                       if(units<0)
-                               units=units-1;
-               }else{
-                       optc++;
-                       optv = realloc(optv, optc);
-                       optv[optc - 1] = (char *) malloc(strlen(argv[opt]) + 1);
-                       strcpy(optv[optc - 1], argv[opt]);
-               }
-       }
-       while ((opt=getopt(optc,optv,"c:fhn:s:q:v")) >0) {
+       while ((opt=getopt(argc,argv,"c:fhn:s:q:v")) >0) {
 
                switch (opt) {
-
 #ifndef BB_FEATURE_SIMPLE_TAIL
-
                case 'c':
                        unit_type = BYTES;
                        test = atoi(optarg);
@@ -248,17 +229,18 @@ int tail_main(int argc, char **argv)
                        usage(tail_usage);
                }
        }
-       while (optind <= optc) {
-               if(optind==optc) {
+       while (optind <= argc) {
+               if(optind==argc) {
                        if (n_files==0)
                                add_file(STDIN);
                        else
                                break;
                }else {
-                       if (!strcmp(optv[optind], "-"))
+                       if (!strcmp(argv[optind], "-")) {
                                add_file(STDIN);
-                       else
-                               add_file(optv[optind]);
+                       } else {
+                               add_file(argv[optind]);
+                       }
                        optind++;
                }
        }
@@ -266,13 +248,13 @@ int tail_main(int argc, char **argv)
                units=-11;
        if(units>0)
                units--;
-       fd=malloc(sizeof(int)*n_files);
+       fd=xmalloc(sizeof(int)*n_files);
        if (n_files == 1)
 #ifndef BB_FEATURE_SIMPLE_TAIL
                if (!verbose)
 #endif
                        show_headers = 0;
-       buffer=malloc(BUFSIZ);
+       buffer=xmalloc(BUFSIZ);
        for (test = 0; test < n_files; test++) {
                if (show_headers)
                        printf("==> %s <==\n", files[test]);
@@ -325,8 +307,6 @@ int tail_main(int argc, char **argv)
                free(buffer);
        if(files)
                free(files);
-       if(optv)
-               free(optv);
        return 0;
 }