home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC15B17.ZIP / UUMAIN.C < prev    next >
Text File  |  1997-03-01  |  9KB  |  335 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <dir.h>
  5. #include <dos.h>
  6. #include <share.h>
  7. #include <sys\stat.h>
  8. #include <alloc.h>
  9. #include <io.h>
  10. #include <fcntl.h>
  11. #include "uu.h"
  12.  
  13. unsigned _stklen = 16384;
  14.  
  15. #define MAX_BUF 1024
  16.  
  17. #define LAST(s) s[strlen(s)-1]
  18.  
  19. char buf[MAX_BUF];
  20.  
  21. scanfor(char *token, FILE * in)
  22. {
  23.   long pos;
  24.  
  25.   pos = ftell(in);
  26.   while (fgets(buf, MAX_BUF, in) && strncmpi(buf, token, strlen(token))) {
  27.     pos = ftell(in);
  28.   }
  29.   rewind(in);
  30.   fseek(in, pos, 0);
  31.   pos = ftell(in);
  32.   return (!strncmpi(buf, "begin ", 6));
  33. }
  34.  
  35. scanfornet(FILE * in, char *net_name)
  36. {
  37.   char *ss;
  38.   int found = 0;
  39.  
  40.   while ((fgets(buf, MAX_BUF, in)) && !found) {
  41.     if (strncmpi(buf, "subject:", strlen("subject:"))) {
  42.       ss = strtok(buf, " ");
  43.       ss = strtok(NULL, " ");
  44.       if (ss[0] == '@') {
  45.         found = 1;
  46.         strcpy(net_name, ss);
  47.       }
  48.     }
  49.   }
  50.   rewind(in);
  51.   return found;
  52. }
  53.  
  54. void cd_to(char *s)
  55. {
  56.   char s1[81];
  57.   int i, db;
  58.  
  59.   strcpy(s1, s);
  60.   i = strlen(s1) - 1;
  61.   db = (s1[i] == '\\');
  62.   if (i == 0)
  63.     db = 0;
  64.   if ((i == 2) && (s1[1] == ':'))
  65.     db = 0;
  66.   if (db)
  67.     s1[i] = 0;
  68.   chdir(s1);
  69.   if (s[1] == ':') {
  70.     setdisk(s[0] - 'A');
  71.     if (s[2] == 0)
  72.       chdir("\\");
  73.   }
  74. }
  75.  
  76. void get_dir(char *s, int be)
  77. {
  78.   strcpy(s, "X:\\");
  79.   s[0] = 'A' + getdisk();
  80.   getcurdir(0, &(s[3]));
  81.   if (be) {
  82.     if (s[strlen(s) - 1] != '\\')
  83.       strcat(s, "\\");
  84.   }
  85. }
  86.  
  87. int exist(char *s)
  88. {
  89.   int i;
  90.   struct ffblk    ff;
  91.  
  92.   i = findfirst(s, &ff, FA_HIDDEN);
  93.   if (i)
  94.     return (0);
  95.   else
  96.     return (1);
  97. }
  98.  
  99. int copyfile(char *input, char *output)
  100. {
  101.   int f1, f2, i;
  102.   char *b, s[181], s1[21], s2[81], s3[81];
  103.   struct ftime ft;
  104.  
  105.   if ((strcmp(input, output) != 0) && (exist(input))) {
  106.     if (exist(output)) {
  107.       fnsplit(output, s3, s2, s1, NULL);
  108.       i = 0;
  109.       sprintf(s, "%s%s%s.%03d", s3, s2, s1, i);
  110.       while (exist(s))
  111.         sprintf(s, "%s%s%s.%03d", s3, s2, s1, ++i);
  112.       strcpy(output, s);
  113.     }
  114.     fprintf(stderr, "\n ■ Creating File   : %s", output);
  115.     if ((b = (char *) farmalloc(16400)) == NULL)
  116.       return 0;
  117.     f1 = open(input, O_RDONLY | O_BINARY, S_IREAD | S_IWRITE);
  118.     if (!f1) {
  119.       farfree(b);
  120.       return 0;
  121.     }
  122.     getftime(f1, &ft);
  123.     f2 = open(output, O_RDWR | O_BINARY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
  124.     if (!f2) {
  125.       farfree(b);
  126.       close(f1);
  127.       return 0;
  128.     }
  129.     i = read(f1, (void *) b, 16384);
  130.     while (i > 0) {
  131.       write(f2, (void *) b, i);
  132.       i = read(f1, (void *) b, 16384);
  133.     }
  134.     f1 = close(f1);
  135.     setftime(f2, &ft);
  136.     f2 = close(f2);
  137.     farfree(b);
  138.   }
  139.   return 1;
  140. }
  141.  
  142. char *stripfn(char *fn)
  143. {
  144.   static char ofn[15];
  145.   int i, i1;
  146.   char s[81];
  147.  
  148.   i1 = -1;
  149.   for (i = 0; i < strlen(fn); i++)
  150.     if ((fn[i] == '\\') || (fn[i] == ':') || (fn[i] == '/'))
  151.   i1 = i;
  152.   if (i1 != -1)
  153.     strcpy(s, &(fn[i1 + 1]));
  154.   else
  155.     strcpy(s, fn);
  156.   for (i = 0; i < strlen(s); i++)
  157.     if ((s[i] >= 'A') && (s[i] <= 'Z'))
  158.       s[i] = s[i] - 'A' + 'a';
  159.   i = 0;
  160.   while (s[i] != 0) {
  161.     if (s[i] == 32)
  162.       strcpy(&s[i], &s[i + 1]);
  163.     else
  164.       ++i;
  165.   }
  166.   strcpy(ofn, s);
  167.   return (ofn);
  168. }
  169.  
  170. main(int argc, char *argv[])
  171. {
  172.   FILE *out=NULL, *in=NULL;
  173.   char outname[181], net_name[21];
  174.   char cur_dir[181], temp_dir[181], s1[181], s2[181], s3[21], s4[21], s5[21];
  175.   int i;
  176.  
  177.   if (argc == 4 || argc == 5) {
  178.     if (strcmpi(argv[1], "-encode") == 0 && argc == 4) {
  179.       if (((in = fopen(argv[2], "r")) != NULL) && ((out = fopen(argv[3], "at+")) != NULL)) {
  180.         fprintf(stderr, " %ld bytes.", uuencode(in, out, argv[2]));
  181.         if (in)
  182.           fclose(in);
  183.         if (out)
  184.           fclose(out);
  185.         exit(EXIT_SUCCESS);
  186.       } else {
  187.         exit(EXIT_FAILURE);
  188.       }
  189.     } else if (strcmpi(argv[1], "-decode") == 0 && argc == 5) {
  190.       get_dir(cur_dir, 1);
  191.       strcpy(temp_dir, argv[3]);
  192.       if (LAST(temp_dir) == '\\')
  193.         LAST(temp_dir) = 0;
  194.       cd_to(temp_dir);
  195.       if ((in = fopen(argv[2], "r")) != NULL) {
  196.         if (!scanfornet(in, net_name))
  197.           net_name[0] = 0;
  198.         rewind(in);
  199.         scanfor("begin", in);
  200.         switch (uudecode(in, argv[3], outname)) {
  201.           case UU_NO_MEM:
  202.             fprintf(stderr, "\n ■ Insufficient memory to decode... moving to CHECKNET");
  203.             sprintf(s1, "%s\\%s", temp_dir, argv[2]);
  204.             sprintf(s2, "%sCHECKNET\\%s", argv[4], argv[2]);
  205.             if (copyfile(s1, s2))
  206.               unlink(s1);
  207.             cd_to(cur_dir);
  208.             if (in)
  209.               fclose(in);
  210.             exit(EXIT_FAILURE);
  211.           case UU_BAD_BEGIN:
  212.             fprintf(stderr, "\n ■ Appears to be a standard message... moving to SPOOL");
  213.             sprintf(s1, "%s\\%s", temp_dir, argv[2]);
  214.             while (LAST(argv[2]) != '.')
  215.               LAST(argv[2]) = 0;
  216.             strcat(argv[2], "MSG");
  217.             sprintf(s2, "%sSPOOL\\%s", argv[4], argv[2]);
  218.             if (copyfile(s1, s2))
  219.               unlink(s1);
  220.             cd_to(cur_dir);
  221.             if (in)
  222.               fclose(in);
  223.             exit(EXIT_FAILURE);
  224.           case UU_CANT_OPEN:
  225.             fprintf(stderr, "\n ■ Cannot open %s%s", argv[4], outname);
  226.             cd_to(cur_dir);
  227.             if (in)
  228.               fclose(in);
  229.             exit(EXIT_FAILURE);
  230.           case UU_CHECKSUM:
  231.             fprintf(stderr, "\n ■ Bad checksum... moving packet to CHECKNET");
  232.             sprintf(s1, "%s\\%s", temp_dir, argv[2]);
  233.             while (LAST(argv[2]) != '.')
  234.               LAST(argv[2]) = 0;
  235.             strcat(argv[2], "MSG");
  236.             sprintf(s2, "%sCHECKNET\\%s", argv[4], argv[2]);
  237.             if (copyfile(s1, s2))
  238.               unlink(s1);
  239.             cd_to(cur_dir);
  240.             if (in)
  241.               fclose(in);
  242.             exit(EXIT_FAILURE);
  243.           case UU_BAD_END:
  244.             fprintf(stderr, "\n ■ \'end\' not found in message... moving to CHECKNET");
  245.             sprintf(s1, "%s\\%s", temp_dir, argv[2]);
  246.             while (LAST(argv[2]) != '.')
  247.               LAST(argv[2]) = 0;
  248.             strcat(argv[2], "MSG");
  249.             sprintf(s2, "%sCHECKNET\\%s", argv[4], argv[2]);
  250.             if (copyfile(s1, s2))
  251.               unlink(s1);
  252.             cd_to(cur_dir);
  253.             if (in)
  254.               fclose(in);
  255.             exit(EXIT_FAILURE);
  256.           case UU_SUCCESS:
  257.             fprintf(stderr, "\n ■ Successfully decoded %s", stripfn(outname));
  258.             fnsplit(outname, NULL, NULL, s4, s3);
  259.             sprintf(s5, "%s%s", s4, s3);
  260.             if (strcmpi(s3, ".NET") == NULL) {
  261.               unlink(argv[2]);
  262.               i = 0;
  263.               sprintf(s2, "%sP1-%03d.NET", argv[4], i);
  264.               while (exist(s2))
  265.                 sprintf(s2, "%sP1-%03d.NET", argv[4], ++i);
  266.               sprintf(s1, "%s\\%s", temp_dir, stripfn(outname));
  267.               if (!copyfile(s1, s2)) {
  268.                 fprintf(stderr, "\n ■ Error creating %s", s2);
  269.                 cd_to(cur_dir);
  270.                 if (in)
  271.                   fclose(in);
  272.                 exit(EXIT_FAILURE);
  273.               } else
  274.                 unlink(s1);
  275.             } else {
  276.               sprintf(s1, "%s\\%s", temp_dir, stripfn(outname));
  277.               sprintf(s2, "%sCHECKNET\\%s", argv[4], stripfn(outname));
  278.               if (exist(s2)) {
  279.                 s2[strlen(s2-1)] = 0;
  280.                 i = 0;
  281.                 sprintf(s2, "%sCHECKNET\\%s%d", argv[4], stripfn(outname), i);
  282.                 while (exist(s2)) {
  283.                   sprintf(s2, "%sCHECKNET\\%s%d", argv[4], stripfn(outname), ++i);
  284.                   if (i > 9)
  285.                     break;
  286.                 }
  287.               }
  288.               if (!copyfile(s1, s2)) {
  289.                 fprintf(stderr, "\n ■ Error creating %s", s2);
  290.                 cd_to(cur_dir);
  291.                 if (in)
  292.                   fclose(in);
  293.                 exit(EXIT_FAILURE);
  294.               }
  295.               sprintf(s1, "%s\\%s", temp_dir, argv[2]);
  296.               while (LAST(argv[2]) != '.')
  297.                 LAST(argv[2]) = 0;
  298.               strcat(argv[2], "MSG");
  299.               sprintf(s2, "%sCHECKNET\\%s", argv[4], argv[2]);
  300.               if (copyfile(s1, s2))
  301.                 unlink(s1);
  302.             }
  303.             cd_to(cur_dir);
  304.             if (in)
  305.               fclose(in);
  306.             exit(EXIT_SUCCESS);
  307.           default:
  308.             fprintf(stderr, "\n ■ Unknown error... moving packet to CHECKNET");
  309.             sprintf(s1, "%s\\%s", temp_dir, argv[2]);
  310.             while (LAST(argv[2]) != '.')
  311.               LAST(argv[2]) = 0;
  312.             strcat(argv[2], "MSG");
  313.               sprintf(s2, "%sCHECKNET\\%s", argv[4], argv[2]);
  314.             if (copyfile(s1, s2))
  315.               unlink(s1);
  316.             cd_to(cur_dir);
  317.             if (in)
  318.               fclose(in);
  319.             exit(EXIT_FAILURE);
  320.         }
  321.       } else {
  322.         cd_to(cur_dir);
  323.         fprintf(stderr, "\n ■ Input file %s not found.", argv[2]);
  324.         if (in)
  325.           fclose(in);
  326.         exit(EXIT_FAILURE);
  327.       }
  328.     }
  329.   }
  330.   if (in)
  331.     fclose(in);
  332.   exit(EXIT_SUCCESS);
  333.   return 0;
  334. }
  335.