home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_02_06 / 2n06064a < prev    next >
Text File  |  1991-05-01  |  3KB  |  130 lines

  1. /*********************************************************/
  2. /*  note:           This program is intended to be a demo
  3.                     for the DOS to Windows interface code
  4. **********************************************************/
  5.  
  6. #include <stdio.h>
  7. #include <stdlib.h>
  8. #include <malloc.h>
  9. #include <fcntl.h>
  10. #include <io.h>
  11. #include <string.h>
  12. #include <graph.h>
  13.  
  14. static  char FileBuffer[4097];;
  15.  
  16. #ifdef WINDOWS
  17. #include <windows.h>
  18. char *argv[50];
  19.  
  20. main()
  21. {
  22.     int argc;
  23.  
  24. #else
  25. /* DOS version */
  26. main(argc,argv)
  27. int argc;
  28. char **argv;
  29. {
  30. #endif
  31.  
  32.     char *linestart, *linestop;
  33.     char srchstr[80],outputbuffer[512];
  34.     long filesize;
  35.     int lineno,offset;
  36.     short fd,bytesread,readsize;
  37. #ifdef WINDOWS
  38.     memset(argv,0,sizeof(argv));
  39.     argc = CommandLine(argv);
  40.     if(argc < 2) {
  41.         printf("grep: Invalid arguments.");
  42.         return;
  43.     }
  44. #endif
  45.  
  46.  
  47. while(argc > 2)
  48.     {
  49.     argc--;
  50.     if((fd = open(argv[argc], O_RDONLY | O_BINARY ))
  51.         == -1) {
  52.         printf("grep: I/O error, cannot open %s\n",
  53.             argv[argc]);
  54.         continue;
  55.         }
  56.     filesize = filelength(fd);
  57.     lineno = offset = 0;
  58.     /* lower case search string */
  59.     strncpy(srchstr, argv[1],sizeof(srchstr) -1);
  60.     strlwr(srchstr);
  61.  
  62.     readsize = sizeof(FileBuffer) -1;
  63.     while((filesize-offset) > 0)
  64.         {
  65.         if((long)readsize > filesize)
  66.             readsize = (short)filesize;
  67.         if((bytesread = read(fd,FileBuffer + offset,
  68.             readsize - offset)) != readsize - offset) {
  69.             printf("grep: I/O error");
  70.             close(fd);
  71.             continue;
  72.             }
  73.         FileBuffer[sizeof(FileBuffer) -1] = 0;
  74.  
  75.         strlwr(FileBuffer);        /* lower case file */
  76.  
  77.         linestop = FileBuffer;
  78.  
  79.         while(*linestop && (linestop <
  80.                 FileBuffer + filesize))
  81.             {
  82.             linestart = strstr(linestop,srchstr);
  83.             if(!linestart) break;
  84.             /* if we found the string,
  85.                     back up to a line start */
  86.             while((linestart > FileBuffer) &&
  87.                 ((*linestart != 0x0a) &&
  88.                     (*linestart != 0x0d)))
  89.                     linestart--;
  90.             linestart++;
  91.             /* find the end of the line */
  92.             while(linestop < linestart)
  93.                 {
  94.                 linestop = strchr(linestop,0x0d);
  95.                 if(!linestop) break;
  96.                 linestop++;
  97.                 lineno++;
  98.                 }
  99.             if(!linestop) linestop = strchr(linestart,0x0a);
  100.             if(!linestop) linestop = linestart +
  101.                 strlen(linestart);
  102.             while((*linestop == 0x0a) ||
  103.                 (*linestop == 0x0d))
  104.                 linestop--;
  105.             /* increment to the start of the next line */
  106.             linestop++;
  107.             if(*linestop) *linestop++ = 0;
  108.             strcpy(outputbuffer,argv[argc]);
  109.             strcat(outputbuffer,":");
  110.             itoa(lineno,
  111.                 &outputbuffer[strlen(outputbuffer)],10);
  112.             strcat(outputbuffer,":  ");
  113.             strcat(outputbuffer,linestart);
  114.             puts(outputbuffer);
  115.             }
  116.         filesize -= readsize - offset;
  117.         /* re-search the last characters in case the
  118.             search pattern fell across the last few
  119.             bytes of the file */
  120.         offset = strlen(srchstr);
  121.         memmove(FileBuffer,FileBuffer + sizeof(FileBuffer)
  122.             - offset,offset);
  123.         while(!FileBuffer[offset-1] && offset) offset--;
  124.         }
  125.  
  126.     close(fd);
  127.   }/* while(argc > 2) */
  128.  
  129. }
  130.