home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / mail / qwkrep.zip / REP.C < prev    next >
C/C++ Source or Header  |  1992-09-17  |  6KB  |  263 lines

  1. /*
  2.  * rep.c - main module for rep: convert .REP packet back to text
  3.  */
  4.  
  5. #include    <stdio.h>
  6. #include    <time.h>
  7. #include    <malloc.h>
  8. #include    <clib.h>
  9.  
  10. FILE *ifp;
  11.  
  12. char subject[26];
  13. char from[26];
  14. char to[26];
  15.  
  16. #pragma pack(1)
  17.  
  18. /*
  19.  * this matches the header format in a .REP packet
  20.  */
  21. struct _header_
  22.  {
  23.     char _status;
  24.     char _msgnum[7];
  25.     char _date[8];
  26.     char _time[5];
  27.     char _to[25];
  28.     char _from[25];
  29.     char _subject[25];
  30.     char _passwd[12];
  31.     char _refer[8];
  32.     char _size[6];
  33.     char _alive;
  34.     int _conf;
  35.     char _filler[3];
  36.  } header;
  37.  
  38. char line[] = "%s\n";
  39.  
  40. /*
  41.  * copy src to dest, up to len characters, and then remove trailing white
  42.  * space
  43.  */
  44. stripcpy(dest, src, len)
  45. char *dest, *src;
  46.  {
  47.     strncpy(dest, src, len);
  48.     dest[len] = 0;
  49.     strip(dest);
  50.  }
  51.  
  52. /*
  53.  * copy src to dest, up to len characters, and pad with spaces if needed
  54.  */
  55. padcpy(dest, src, len)
  56. char *dest, *src;
  57.  {
  58.     while (*src && len)
  59.      {
  60.         *dest++ = *src++;
  61.         len--;
  62.      }
  63.     while (len--)
  64.       *dest++ = ' ';
  65.  }
  66.  
  67. /*
  68.  * process a rep file
  69.  */
  70. handlerep(s)
  71. char *s;
  72.  {
  73.     int ch;
  74.     int i;
  75.     int j;
  76.     int l;
  77.     int type;
  78.     long pos;
  79.     long cpos;
  80.     FILE *cxfp;
  81.     FILE *dfp;
  82.     char t[256];
  83.     char drive[8];
  84.     char path[256];
  85.     char name[10];
  86.     char ext[8];
  87.     char cxdat[20];
  88.     char data[20];
  89.  
  90.     /*
  91.      * get a local copy
  92.      */
  93.     strcpy(t, s);
  94.     /*
  95.      * and chop it up
  96.      */
  97.     _splitpath(t, drive, path, name, ext);
  98.     /*
  99.      * figure what PKUNZIP will do, and then do it
  100.      */
  101.     sprintf(path, "pkunzip %s", s);
  102.     system(path);
  103.     /*
  104.      * generate local filename
  105.      */
  106.     sprintf(path, "%s.MSG", name);
  107.     sprintf(cxdat, "%s.INF", name);
  108.     sprintf(data, "%s.TXT", name);
  109.     /*
  110.      * open inf file: this maps conf numbers to conf names
  111.      */
  112.     if ((cxfp = fopen(cxdat, "r")) == (FILE *) NULL)
  113.      {
  114.         printf("Can't find %s\n", cxdat);
  115.         return;
  116.      }
  117.     /*
  118.      * open .MSG file
  119.      */
  120.     if ((ifp = fopen(path, "rb")) == NULL)
  121.      {
  122.         printf("Can't find %s, check ZIP file integrity\n", path);
  123.         fclose(cxfp);
  124.         return;
  125.      }
  126.     /*
  127.      * and output file
  128.      */
  129.     if ((dfp = fopen(data, "w")) == (FILE *) NULL)
  130.      {
  131.         printf("Can't create %s\n", data);
  132.         fclose(cxfp);
  133.         fclose(ifp);
  134.         return;
  135.      }
  136.     /*
  137.      * toss the first header
  138.      */
  139.     fread(&header, 1, 128, ifp);
  140.     /*
  141.      * while there's another message to process
  142.      */
  143.     while (fread(&header, 1, 128, ifp) == 128)
  144.      {
  145.         /*
  146.          * get the conference number
  147.          */
  148.         stripcpy(data, header._msgnum, 7);
  149.         i = atoi(data);
  150.         /*
  151.          * rewind inf file
  152.          */
  153.         fseek(cxfp, 0L, 0);
  154.         /*
  155.          * read to the appropriate line
  156.          */
  157.         while (fgets(data, 18, cxfp) != (char *) NULL && i--)
  158.           ;
  159.         strip(data);
  160.         /*
  161.          * output confname first
  162.          */
  163.         fprintf(dfp, line, data);
  164.         /*
  165.          * then from
  166.          */
  167.         stripcpy(from, header._from, 25);
  168.         fprintf(dfp, line, from);
  169.         /*
  170.          * to
  171.          */
  172.         stripcpy(to, header._to, 25);
  173.         fprintf(dfp, line, to);
  174.         /*
  175.          * subject
  176.          */
  177.         stripcpy(subject, header._subject, 25);
  178.         fprintf(dfp, line, subject);
  179.         /*
  180.          * get the message size
  181.          */
  182.         stripcpy(data, header._size, 6);
  183.         i = atoi(data);
  184.         /*
  185.          * save our place
  186.          */
  187.         pos = ftell(ifp);
  188.         l = 0;
  189.         /*
  190.          * now count the lines
  191.          */
  192.         while (--i > 0 && fread(t, 1, 128, ifp) == 128)
  193.           for (j = 0; j < 128; j++)
  194.             if ((t[j] & 0xff) == 0xe3)
  195.               l++;
  196.         /*
  197.          * output the numbner of lines
  198.          */
  199.         fprintf(dfp, "%d\n", l);
  200.         /*
  201.          * back to where the data starts
  202.          */
  203.         fseek(ifp, pos, 0);
  204.         i = atoi(data);
  205.         /*
  206.          * now read and transfer the message
  207.          */
  208.         while (--i > 0 && fread(t, 1, 128, ifp) == 128)
  209.          {
  210.             for (j = 0; j < 128; j++)
  211.              {
  212.                 if (l)
  213.                  {
  214.                     if ((ch = t[j] & 0xff) == 0xe3)
  215.                      {
  216.                         /*
  217.                          * 0xe3's become newlines
  218.                          */
  219.                         fputc('\n', dfp);
  220.                         l--;
  221.                      }
  222.                     /*
  223.                      * plus some other character conversion
  224.                      */
  225.                     /*
  226.                      * XXX this is where we ought to implement 7 or 8 bit
  227.                      * conversions
  228.                      */
  229.                     else if ((ch >= ' ' && ch <= '~') /* XXX || type == 8 */)
  230.                       fputc(ch, dfp);
  231.                     else if (ch == 254)
  232.                       fputc('+', dfp);
  233.                     else
  234.                       fputc('.', dfp);
  235.                  }
  236.              }
  237.          }
  238.      }
  239.     /*
  240.      * all done, close the files, and clean up
  241.      */
  242.     fclose(ifp);
  243.     fclose(cxfp);
  244.     fclose(dfp);
  245.     unlink(s);
  246.     unlink(path);
  247.  }
  248.  
  249. main()
  250.  {
  251.     /*
  252.      * use scnwld to crunch all the rep files
  253.      * XXX do we want to only process those given to us, i.e.
  254.      *   REP
  255.      * with no args crunches everything, while
  256.      *   REP ABC.REP
  257.      * crunches abc.rep only
  258.      */
  259.     if (scnwld("*.rep", handlerep, 17) == 0)
  260.       printf("Warning: no .REP files found\n");
  261.     return(0);
  262.  }
  263.