lolamontes69

K+R Exercise5_13

Sep 25th, 2014
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.20 KB | None | 0 0
  1. /* Write the program tail, which prints the last n lines of its input. By default,
  2.  * n is set to 10, let us say, but it can be changed by an optional arguement
  3.  * so that
  4.  *     tail -n
  5.  * prints the last n lines. The program should behave rationally no matter how
  6.  * unreasonable the input or the value of n. Write the program so that is makes
  7.  * the best use of available storage; lines should be stored as in the sorting
  8.  * program of section 5.6, not in a two-dimensional array of fixed size.
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <string.h>
  13.  
  14. #define MAXTAIL 100
  15. #define MAXLINES 1000
  16. #define MAXLEN 1000               /* max length of any input line */
  17. #define TAB 8
  18. #define ALLOCSIZE 10000           /* size of available space */
  19.  
  20. static char allocbuf[ALLOCSIZE];  /* storage of alloc   */
  21. static char *allocp = allocbuf;   /* next free position */
  22. char *lineptr[MAXLINES];          /* pointers to textlines */
  23.  
  24. void errormessage(void);
  25. int readlines(char *lineptr[], int maxlines, int tailsize);
  26. int getline(char *s, int lim);
  27. char *alloc(int n);
  28. void tailprint(int nlines, int tailsize);
  29.  
  30. main(int argc, char *argv[])
  31. {
  32.     int c, j=0, origargc=argc;
  33.     int nflagged=0;
  34.     int tailsize=10;
  35.  
  36.     if(argc>3 || argc==2) {
  37.         errormessage();
  38.         return 0;
  39.     }
  40.     while(--argc>0) {
  41.         if((*++argv)[0] == '-') {
  42.             while(c = *++argv[0])
  43.                 switch(c) {
  44.                 case 'n':
  45.                     nflagged++;
  46.                     break;
  47.                 default:
  48.                     errormessage();
  49.                     return 0;
  50.                 }
  51.                 c = *++argv[0];
  52.                 if(nflagged==1) {
  53.                     if(isdigit(**argv)) {
  54.                         while(isdigit(**argv)) {
  55.                             j = (j*10)+(**argv-'0');
  56.                             (argv[0]++);
  57.                         }
  58.                         c = *argv[0];
  59.                         if(c!=0) {
  60.                             errormessage();
  61.                             return 0;
  62.                         }
  63.                     }
  64.                     else {
  65.                         errormessage();
  66.                         return 0;
  67.                     }
  68.                 }
  69.                 else if(isdigit(**argv)) {
  70.                     errormessage();
  71.                     return 0;
  72.                 }
  73.             if(nflagged==1) {
  74.                 nflagged++;
  75.                 tailsize=j;
  76.             }
  77.             j=0;
  78.         }
  79.     }
  80.     if(tailsize>MAXTAIL) {
  81.         printf("Maximum value of n is %d\n",MAXTAIL);
  82.         return 0;
  83.     }
  84.     else
  85.         j = readlines(lineptr,MAXLINES,tailsize);
  86.     if(j==-1) {
  87.         puts("Maximum number of lines exceeded"); /* or just print last n lines */
  88.         tailprint(j,tailsize);
  89.     }
  90.     else tailprint(j,tailsize);
  91.     return 0;
  92. }
  93.  
  94. /* alloc: return pointer to n characters */
  95. char *alloc(int n)
  96. {
  97.     if(allocbuf + ALLOCSIZE - allocp >= n) {
  98.         allocp += n;
  99.         return allocp - n;
  100.     }
  101.     else return 0;
  102. }
  103.  
  104. int readlines(char *lineptr[], int maxlines, int tailsize)
  105. {
  106.     int len, nlines, i;
  107.     char *p, line[MAXLEN];   // atm not pointing to anything
  108.    
  109.     nlines = 0;
  110.     while((len=getline(line,MAXLEN))>0) {
  111.         if(nlines >= maxlines || (p = alloc(len+1)) == NULL)
  112.             return -1;
  113.         else {
  114.             line[len]='\0'; /* delete newline */
  115.             strcpy(p, line);
  116.             lineptr[nlines++] = p;
  117.         }
  118.     }
  119.     return nlines;
  120. }
  121.  
  122. /* getline: get a line of input into an array */
  123. int getline(char *s, int lim)
  124. {
  125.     int c, i;
  126.  
  127.     for(i=0; i<lim-1 && (c=getchar())!=EOF && c!='\n'; ++i) {
  128.         s[i]=c;
  129.     }
  130.     s[i]='\0';
  131.     return i;
  132. }
  133.  
  134. /* errormessage: print an error message */
  135. void errormessage(void)
  136. {
  137.     puts("Usage: tail [-n lines_to_print]");
  138. }
  139.  
  140. /* tailprint: print the last tailsize lines entered */
  141. void tailprint(int nlines, int tailsize)
  142. {
  143.     int i;
  144.  
  145.     printf("-------- Last %d lines --------\n",tailsize);
  146.     if(tailsize>=nlines)
  147.         for(i=0; i<nlines; i++)
  148.             printf("%s\n",lineptr[i]);
  149.     else
  150.         for(i=nlines-tailsize; i<nlines; i++)
  151.             printf("%s\n",lineptr[i]);
  152. }
Advertisement
Add Comment
Please, Sign In to add comment