Advertisement
Guest User

Untitled

a guest
Jan 17th, 2014
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.48 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #define DEFAULT_STRING_NUM 10
  5. #define MAXLEN 1000
  6. #define MAXLINES 5000
  7.  
  8. char *lineptr[MAXLINES];
  9.  
  10. int getline(char *, int);
  11. char *smartalloc(int n, char *begin);
  12. int readlines(char *linestack[], int tail_num);
  13. void writelines(char *lineptr[], int tail_num, int nlines);
  14. void writespecific(char *lineptr[]);
  15.  
  16. int main(int argc, char *argv[])
  17. {
  18.     int num, nlines;
  19.  
  20.     if (argc == 3 && strcmp(*(argv+1),"-n") == 0) {
  21.         num = atoi(*(argv+2));
  22.         if (num < 1) {
  23.             printf("usage: 'tail -n COUNT_LINES' \n");
  24.             return -1;
  25.         }
  26.         else if (num > MAXLINES) {
  27.             printf("n must be in {1..%d}\n",MAXLINES);
  28.             return -1;         
  29.         }
  30.     }
  31.     else if (argc == 1)
  32.         num = DEFAULT_STRING_NUM;
  33.     else {
  34.         printf("usage: 'tail -n COUNT_LINES' \n");
  35.         return -1;
  36.     }
  37.     if ((nlines=readlines(lineptr, num)) >= 0) {
  38.         num = (num > nlines) ? nlines : num ; /* if in stdio lines less, than user want (num) */
  39.         writelines(lineptr, num, nlines);
  40.         return 0;
  41.     } else {
  42.         printf("error: input too big to tail\n");
  43.         return -1;
  44.     }
  45. }
  46.  
  47. /* readlines: */
  48. int readlines(char *lineptr[], int tail_num)
  49. {
  50.     int len;                   /* length string getted from getline */
  51.     int nlines = 0;            /* total count strings ( <= tail_num )*/
  52.     char *p;                   /* pointer to current free position (returned by smartalloc) */
  53.     char *leftpos = NULL;      /* pointer to beginning interest block (use in smartalloc) */
  54.     char *rightpos = NULL;     /* pointer to end interest block (calculate as p+len)  */
  55.     char line[MAXLEN];         /* current string */
  56.  
  57.     while ((len = getline(line, MAXLEN)) > 0) {
  58.         if ((p = smartalloc(len+1, leftpos)) == NULL) { /* if buffer overfull */
  59.             return -1;
  60.         }
  61.         else {
  62.             /* check is `memory moved`? if next pointer not "next" */
  63.             if (rightpos+1 > p) {
  64.                 /* moving interest pointers value */
  65.                 for (int i = 0; i < tail_num; i++)
  66.                     lineptr[nlines-i-1] -= rightpos - p + 1;
  67.                     /* (rightpos - p + 1) - moving 'diff' (in pointers) */
  68.             }
  69.             /* copying string */
  70.             line[len] = '\0'; /* delete \n in line */
  71.             strcpy(p, line);
  72.             lineptr[nlines++] = p;
  73.             if (nlines <= tail_num)
  74.                 leftpos = lineptr[0]; /* first element */
  75.             else
  76.                 leftpos = lineptr[nlines-tail_num]; /* first interest element */
  77.             rightpos = p + len;
  78.         }
  79.     }
  80.     return nlines;
  81. }
  82.  
  83. void writelines(char *lineptr[], int tail_num, int nlines)
  84. {
  85.     for (int i = 0; i < tail_num; i++)
  86.         printf("%s\n",lineptr[nlines-tail_num+i]);
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement