Self-implemented Simple tail command under Linux

Source: Internet
Author: User

/*     Browse individual files, by row or by byte      format:  tail filename c/n num, No other formats currently supported */#include  <stdio.h> #include  <stdlib.h> #include  <fcntl.h> #include  <unistd.h>/*     error Handling */void oops (CHAR&NBSP;*S1,&NBSP;CHAR*&NBSP;S2) {     fprintf (stderr,  "error:%s",  s1);     perror (S2);     exit (1);} /*     output by byte, name open file name, command line byte number */void print_c (Char* name, int num) {     char buf[200];    int fd, n;    long  size;        fd = open (Name, O_RDONLY);     if  (fd == -1)         oops ("Can ' t  open ",  name);     size = lseek (fd, 0, seek_end);     if  (num < 0)          if  (-num  >= size)             lseek (fd, 0,  seek_set);        else             lseek (fd, num, seek_end);    else if  ( num >= size)         lseek (Fd, 0, SEEK_END);     else        lseek (Fd, num, SEEK_SET) ;    while  (( n = read (fd, buf, 200))  > 0)         if  (Write (stdout_fileno, buf, n)  != n )             oops ("write failed",  " StdOut ");     if  (n == -1)         oops ("read failed",  " Input ");     close (FD);} /*     output by row, name file name, num command line line      line output, the method is less efficient, first traverse the file statistics so the number of rows, and then determine the output location */void  print_n (Char *name, int num) {    char buf[200];      file *fp;    int n = 0;    fp  = fopen (name,  "R");    while  (fgets (BUF,&NBSP;200,&NBSP;FP)  !=  null)         n++;    rewind (FP);     if  (num < 0)         num = n  + num;    if  (num < 0)          num = 0;    while (num--)      &Nbsp;  fgets (BUF,&NBSP;200,&NBSP;FP);    while  (1)  {         if  (fgets (BUF,&NBSP;200,&NBSP;FP)  == null)              break;        if   (fputs (buf, stdout)  == eof)              oops ("write failed",  "stdout");     }    fclose ( FP);} Int main (int argc, char* argv[]) {    int num;     FILE *fp;    char cmd;    if  (argc <  3)  {        fprintf (stderr,  "tail01: missing  operand ");         exit (1);    }     if  (argv[2][0] ==  ' C '  | |  argv[2][0] ==  ' n ')         cmd = argv[2][0];     else {        fprintf (stderr,  " Operand error ");         exit (1);    }     num = atoi (Argv[3]);    if  (argv[3][0] !=  ' + ') &&argv[3][0] !=  '-')         num = -num;     switch (cmd)  {        case  ' C ':             print_c (Argv[1], num);             break;         case  ' n ':             print_n (ARGV[1],&NBsp;num);            break;                     default:             fprintf (stderr,  "Switch eror ");             exit (1);     }}

Summarize:

No other features are currently implemented

Self-implemented Simple tail command under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.