home *** CD-ROM | disk | FTP | other *** search
/ Garbo / Garbo.cdr / mac / unix / comb.c < prev    next >
C/C++ Source or Header  |  1989-06-03  |  5KB  |  192 lines

  1. #include <stdio.h>
  2. #define min(a,b)    (a<b?a:b)
  3.  
  4. #ifdef DEBUG
  5. #define EBUG
  6. #endif
  7.  
  8. #ifdef EBUG
  9. #define debug(x)    x
  10. #else
  11. #define debug(x)    /**/
  12. #endif
  13.  
  14. int verbose=1;
  15. int mailfile = 0;    /* file contains multiple parts */
  16. int gotend = 0;        /* found apparent end of binhex data */
  17. int eof = 0;        /* found at least 1 eof (only used if mailfile) */
  18.  
  19. main(argc,argv)
  20. char **argv;
  21. {
  22.     int l,i,fnum, curline;
  23.     char inline[256];
  24.     char inline2[256];
  25.     FILE *fd;
  26.  
  27.     if (argc <= 1) {
  28.     fprintf(stderr,"Usage: %s [-q] [-m] file1 [..fileN]\n",argv[0]);
  29.     exit(2);
  30.     }
  31.  
  32.     fnum=1;
  33.     /* get flags */
  34.     while (argv[fnum][0]=='-') {
  35.         switch (argv[fnum][1]) {
  36.         case 'm':
  37.             mailfile=1;
  38.             break;
  39.         case 'q':
  40.             verbose=0;
  41.             break;
  42.         case 'v':
  43.             verbose=1;
  44.             break;
  45.         default:
  46.             fprintf(stderr,"Unknown option -%c ignored\n",argv[fnum][1]);
  47.             break;
  48.             }
  49.         fnum++;
  50.         }
  51.  
  52.     fd = fopen(argv[fnum],"r"); 
  53.     if (fd==0) {
  54.     perror(argv[fnum]);
  55.     exit(3);
  56.     }
  57.     argc--;
  58.     fnum++;
  59.     for ( ;; ) {
  60.         if (fgets(inline,80,fd)<=0) break;
  61.         if (strncmp(inline,"(This file",10)==0) {
  62.             printf("%s",inline);
  63.  
  64.             fgets(inline,80,fd);
  65.             printf("%s",inline);
  66.  
  67.             fgets(inline,80,fd);
  68.             printf("%s",inline);
  69.             l = strlen(inline);
  70.             for ( ; (fgets(inline,80,fd)>0) && (strlen(inline)==l); ) 
  71.                 printf("%s",inline);
  72.             break;
  73.             }
  74.         else {
  75.             if (verbose) fprintf(stderr,"%s",inline);
  76.             }
  77.         }
  78.     if (!mailfile) {
  79.         /* throw away rest of file */
  80.         while (verbose && (fgets(inline,80,fd)>0)) {
  81.             fprintf(stderr,"%s",inline);
  82.             }
  83.         }
  84.  
  85.     for ( ; (mailfile && !eof) || (argc-->1);  ) {
  86.         if (!mailfile) fd = freopen(argv[fnum++],"r",fd);
  87.  
  88.     /* hack, hack.  jump here when in mailfile mode */
  89.     /* instead of opening a new file        */
  90. fakenew:
  91.         /* only consider first 500 lines of each file for data start */
  92.         for (curline=1;curline<500;curline++) {
  93.             if (fgets(inline,90,fd)<=0) {
  94.                 debug(printf("eof (argc=%d)\n",argc));
  95.                 eof = 1;
  96.                 break;
  97.                 }
  98.  
  99.     /* jump here if we already read 2 lines thinking the first was
  100.        the start of the binhex data, but that hypothesis failed.
  101.        We make the extra line look like we just read it, & jump here.
  102.     */
  103. got1:
  104.  
  105.         /* is the line the right length? */
  106.             if (strlen(inline)!=l) {
  107.                 if (verbose) fprintf(stderr,"%s",inline);
  108.                 debug(printf("Bad length %d != %d\n",strlen(inline),l));
  109.                 continue;
  110.                 }
  111.  
  112.         /* we don't expect binhex to contain these lines, just in case
  113.         we fluked out on line length 
  114.         */
  115.             if ((substr(inline,"here"))  ||
  116.                 (substr(inline,"From")) ||
  117.                 (substr(inline,"CUT")) ||
  118.                 (substr(inline,"end")) ||
  119.                 (substr(inline,"Path")) ||
  120.                 (substr(inline,"cut")) ) {
  121.                 if (verbose) fprintf(stderr,"%s",inline);
  122.                 debug(printf("Has English\n"));
  123.                 continue;
  124.                 }
  125.  
  126.             /* get another line, see if lengths match */
  127.             if (fgets(inline2,80,fd)<=0) {
  128.                 eof = 1;
  129.                 break;
  130.                 }
  131.         /* check it for the keywords too */
  132.             if ((strlen(inline)==strlen(inline2)) &&
  133.                 ( ! ((substr(inline2,"here"))  ||
  134.                 (substr(inline2,"From")) ||
  135.                 (substr(inline2,"CUT")) ||
  136.                 (substr(inline2,"end")) ||
  137.                 (substr(inline2,"Path")) ||
  138.                 (substr(inline2,"cut")) ))) {
  139.  
  140.                 /* Okay, we're convinced     */
  141.         /* spit the 2 lines        */
  142.                 printf("%s",inline);
  143.                 printf("%s",inline2);
  144.         /* Take any more lines of the right length */
  145.                 for ( ; ; ) {
  146.                     if (fgets(inline,80,fd)<=0) {
  147.                         eof = 1;
  148.                         break;
  149.                         }
  150.                     if (strlen(inline)!=l) {
  151.                         i = strlen(inline);
  152.                         while (inline[i-1]=='\n') i--;
  153.                         if (inline[i-1]==':') {
  154.                             /* end of stuff */
  155.                             gotend = 1;
  156.                             printf("%s",inline);
  157.                             }
  158.                         break;
  159.                         }
  160.                     printf("%s",inline);
  161.                     }
  162.                 }
  163.             else {
  164.                 debug(printf("Second line is bad\n"));
  165.                 strcpy(inline,inline2);
  166.                 goto got1;
  167.                 }
  168.             break;
  169.             }
  170.         if (mailfile && !eof) goto fakenew;
  171.         while (verbose & (fgets(inline,80,fd)>0)) {
  172.             fprintf(stderr,"%s",inline);
  173.             }
  174.         }
  175.     if (!gotend) {
  176.     fprintf(stderr,"File didn't seem to end properly\n");
  177.     exit(1);
  178.     }
  179.     exit(0);
  180.     }
  181.  
  182. substr(s,t)
  183. char *s, *t;
  184. {
  185.     extern char *index();
  186.     while ((s=index(s,*t))!=0) {
  187.         if (strncmp(s,t,(min(strlen(s),strlen(t))))==0) return(1);
  188.         s++;
  189.         }
  190.     return(0);
  191.     }
  192.