home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 11 Util / 11-Util.zip / RCUTILS.ZIP / ASCII.C next >
C/C++ Source or Header  |  1992-03-14  |  4KB  |  122 lines

  1. /********************************************************/
  2. /*                                                      */
  3. /*  Function: ascii                                     */
  4. /*                                                      */
  5. /*   Purpose: Search for text in any file.              */
  6. /*                                                      */
  7. /*   To build: CL /Ox /Lp /Fb /F 2000 ascii.c           */
  8. /*                                                      */
  9. /*   To use:  ascii [/nn] filename                      */
  10. /*  Defaults:         4    NONE                         */
  11. /*                                                      */
  12. /*      File: ascii.c                                   */
  13. /*                                                      */
  14. /********************************************************/
  15.  
  16.         /* Include system calls/library routines/macros */
  17. #include <stdio.h>
  18. #include <ctype.h>
  19.  
  20.         /* Define constants */
  21. #define true -1
  22. #define false 0
  23. #define check if (ret!=SS$_NORMAL) exit(ret)
  24. #define SAVEARSIZ 65
  25. #define MAXCOL 78
  26.  
  27.         /* Define general variables */
  28. char inp_buf[16384];
  29. int i, k, numasc, column, retval;
  30. long offset;
  31. int minstr=4;           /* Default min size string to print */
  32. char savar[SAVEARSIZ];          /* Array that holds ascii strings before print */
  33. int ch;
  34. char *usage_string = "\n\
  35. Correct usage is ascii [-nn] input-file(s)\n\
  36.              Defaults:    4   NONE\n\
  37.   -nn switch determines # of ascii characters required before printing\n";
  38.  
  39.         /* Define file access structures */
  40. FILE *inp_file;
  41.  
  42. main(argc, argv)
  43. int argc;
  44. char *argv[];
  45. {
  46.   if (argc < 2)
  47.     errex(usage_string);
  48.  
  49.   for (i=1; i<argc; i++)
  50.   {
  51.     if ( *argv[i] == '/' || *argv[i] == '-' ) {
  52.       minstr = atoi(++argv[i]);
  53.       if (minstr > SAVEARSIZ) {
  54.         printf("\n%d characters is more than I can handle!\n", minstr);
  55.         exit(2);
  56.       }
  57.       if (minstr < 1) {
  58.         errex(usage_string);
  59.       }
  60.     }
  61.     else {
  62.       inp_file = fopen(argv[i], "rb");
  63.       if (inp_file == NULL) {
  64.         printf("Error opening %s\n", argv[i]);
  65.         errex("\n\nGiving up the ghost");
  66.       }
  67.       retval=setvbuf(inp_file, inp_buf, _IOFBF, sizeof(inp_buf) );
  68.       if (retval!=0) errex("Unable to set input buffer\n");
  69.       offset = numasc = 0;
  70.       for (; !feof(inp_file); offset++ ) {
  71.         ch = fgetc(inp_file);
  72.         if (numasc >= minstr) {         /* If we are printing now */
  73.           if ( isprint(ch) ) {          /* And this is printable */
  74.             if (column++ > MAXCOL) {    /* If we've run out of column */
  75.               printf("\n          ");   /* Start a new line */
  76.               column = 11;
  77.             }
  78.             putchar(ch);                /* Output the character */
  79.           }
  80.           else {
  81.             numasc = 0;                 /* Clear the output flag/counter */
  82.             putchar('\n');              /* and terminate the line */
  83.           }
  84.         }
  85.         else {                          /* Not yet outputting */
  86.           if ( !isprint(ch) )
  87.             numasc = 0;
  88.           else {                        /* Log the char & check for output now */
  89.             savar[numasc++] = ch;
  90.             if (numasc >= minstr) {
  91.               printf("%08lX: ",offset-minstr+1); /* Start a new line */
  92.               for (k=0; k<numasc; k++) {
  93.                 putchar(savar[k]);      /* Output the character */
  94.               }
  95.               column = minstr+10;
  96.             }   /* Start printing now */
  97.           }     /* Saving characters */
  98.         }       /* Examining characters */
  99.       }         /* Reading a file */
  100.       putchar('\n');    /* Mark end of file */
  101.       fclose(inp_file);
  102.     }   /* Not a switch */
  103.   }     /* Reading parameters */
  104. }       /* of main */
  105.  
  106. /********************************************************/
  107. /*                                                      */
  108. /*  Function: errex                                     */
  109. /*                                                      */
  110. /*   Purpose: Prints an error message to the screen and */
  111. /*              exits.                                  */
  112. /*                                                      */
  113. /********************************************************/
  114.  
  115. errex(mess_ptr)
  116. char *mess_ptr;
  117. {
  118.  
  119. perror(mess_ptr);
  120. exit();
  121. }
  122.