lolamontes69

K+R Exercise7_7

Sep 25th, 2014
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 4.23 KB | None | 0 0
  1. /* Modify the pattern finding program of Chapter 5 to take its input from a set
  2. of named files or, if no files are named as arguements, from the standard input.
  3. */
  4. #include <stdio.h>
  5. #include <stdlib.h>
  6. #include <string.h>
  7. #define MAXLINE 100
  8. #define MAXCHAR 100
  9.  
  10. int getline(char *line, int max);
  11. char *fgets1(char *s, int n, FILE *iop);
  12.  
  13.  
  14. main(int argc, char *argv[]) /* argv[n] doesnt overflow tried with a 1,000,000 char arg */
  15. {
  16.     char line[MAXLINE];
  17.     char *prog = argv[0];
  18.     char *pattern;
  19.     FILE *fp;
  20.     long lineno = 0;
  21.     int c, except = 0, number = 0, found = 0, fninfo = 0;
  22.     /* fninfo is a flag to show whether or not to print the filename  */
  23.     /* found is the number of matches found */
  24.     while(--argc > 0 && (*++argv)[0] == '-')
  25.         while(c = *++argv[0])
  26.             switch(c) {
  27.             case '-':  /* Because some people are used to '--flag' */
  28.                 break;
  29.             case 'x':
  30.                 except = 1;
  31.                 break;
  32.             case 'n':
  33.                 number = 1;
  34.                 break;
  35.             case 'i':
  36.                 fninfo = 1;
  37.                 break;
  38.             case 'h':
  39.                 puts("Usage: find [-xinh] <PATTERN> [FILE1 FILE2 ...]");
  40.                 puts("Search for PATTERN in each FILE or standard input.");
  41.                 puts("Example: find -inx frog frog.txt elephant.txt cheese.txt\n");
  42.                 puts("Options:");
  43.                 puts("       -x  lines without match");
  44.                 puts("       -i  include filename");
  45.                 puts("       -n  include line number");
  46.                 puts("       -h  print this help menu\n");
  47.                 puts("Report bugs to <[email protected]>.");
  48.                 exit(0);
  49.             default:
  50.                 printf("find: illegal option %c\n", c);
  51.                 argc = 0;
  52.                 found = -1;
  53.                 break;
  54.             }
  55.     if(argc < 1)
  56.         printf("Usage: find [-xinh] <pattern> [file1 file2 ...]\n");
  57.     else if(argc == 1) {
  58.         pattern = *argv;
  59.         while(getline1(line, MAXLINE) > 0) {
  60.             lineno++;
  61.             if((strstr(line, pattern) != NULL) != except) {
  62.                 if(number)
  63.                     printf("%ld:",lineno);
  64.                 printf("%s",line);
  65.                 found++;
  66.             }
  67.         }
  68.     }
  69.     else {
  70.         pattern = *argv++;      /* pattern is the pattern to match */
  71.         for( ; argc>1; argc--) {
  72.             if((fp = fopen(*argv, "r")) == NULL) {   /* first open the FILE object */
  73.                 fprintf(stderr, "%s: can't open %s\n", prog, *argv);
  74.                 exit(1);
  75.             } else {
  76.                 // fprintf(stderr, "%s: Examining %s\n", prog, *argv);
  77.                 lineno=0; /* initialize for each file */
  78.                 found = 0;
  79.                 while(fgets1(line, MAXCHAR, fp)) {
  80.                     lineno++;
  81.                     if((strstr(line, pattern) != NULL) != except) {
  82.                         if(fninfo)
  83.                             printf("%s: ",*argv);
  84.                         if(number)
  85.                             printf("line %ld:",lineno);
  86.                         printf("%s",line);
  87.                         found++;
  88.                     }
  89.                 }
  90.                 fclose(fp);
  91.                 if(found==0 && fninfo) fprintf(stderr, "%s: No matches found\n", *argv);
  92.                 // else fprintf(stderr, "%s: Matches found:%d\n", *argv, found);
  93.             } ++argv;
  94.         }
  95.     }
  96.     exit(0);
  97. }
  98.  
  99. /* getline: read a line, return length */
  100. int getline1(char *line, int max)
  101. {
  102.     if(fgets1(line, max, stdin) == NULL)  /* fgets gets a line of input        */
  103.         return 0;                         /* getline is prolly similar to gets */
  104.     else
  105.         return strlen(line);
  106. }
  107.  
  108. /* fgets1: get at most n chars from iop */
  109. char *fgets1(char *s, int n, FILE *iop)
  110. {
  111.     register int c;
  112.     register char *cs;
  113.  
  114.     cs = s;
  115.     while(--n > 0 && (c = getc(iop)) != EOF)   /* get up to n characters from one line of iop */
  116.         if((*cs++ = c) == '\n')                /* quit at '\n' cs is at the same address as s  */
  117.             break;
  118.     *cs = '\0';
  119.     return (c == EOF && cs == s) ? NULL : s;   /* ternary operation */
  120. }
Advertisement
Add Comment
Please, Sign In to add comment