home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC / SRC15B27.ZIP / UUMAIN.C < prev    next >
C/C++ Source or Header  |  1997-04-23  |  15KB  |  605 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 <time.h>
  12. #include <errno.h>
  13. #include "uu.h"
  14. #include "net.h"
  15. #include "vardec.h"
  16. #include "version.h"
  17.  
  18. unsigned _stklen = 16384;
  19.  
  20. #define MAX_BUF 1024
  21.  
  22. #define SHARE_LEVEL 10
  23. #define WAIT_TIME 10
  24. #define TRIES 100
  25. #define MT_DESQVIEW 0x01
  26. #define MT_WINDOWS  0x02
  27. #define MT_OS2      0x04
  28. #define MT_NB       0x40
  29.  
  30.  
  31. #define LAST(s) s[strlen(s)-1]
  32.  
  33. char buf[MAX_BUF];
  34. int multitasker = 0;
  35. char *version = "Freeware UU Encode/Decode " VERSION;
  36.  
  37. void dv_pause(void)
  38. {
  39.   __emit__(0xb8, 0x1a, 0x10, 0xcd, 0x15);
  40.   __emit__(0xb8, 0x00, 0x10, 0xcd, 0x15);
  41.   __emit__(0xb8, 0x25, 0x10, 0xcd, 0x15);
  42. }
  43.  
  44. void win_pause(void)
  45. {
  46.   __emit__(0x55, 0xb8, 0x80, 0x16, 0xcd, 0x2f, 0x5d);
  47. }
  48.  
  49. int get_dos_version(void)
  50. {
  51.   _AX = 0x3000;
  52.   geninterrupt(0x21);
  53.   if (_AX % 256 >= 10) {
  54.     multitasker |= MT_OS2;
  55.   }
  56.   return (_AX);
  57. }
  58.  
  59. int get_dv_version(void)
  60. {
  61.   int v;
  62.  
  63.   if (multitasker & MT_OS2)
  64.     return 0;
  65.   _AX = 0x2b01;
  66.   _CX = 0x4445;
  67.   _DX = 0x5351;
  68.   geninterrupt(0x21);
  69.   if (_AL == 0xff) {
  70.     return 0;
  71.   } else {
  72.     v = _BX;
  73.     multitasker |= MT_DESQVIEW;
  74.     return v;
  75.   }
  76. }
  77.  
  78. int get_win_version(void)
  79. {
  80.   int v = 0;
  81.  
  82.   __emit__(0x55, 0x06, 0x53);
  83.   _AX = 0x352f;
  84.   geninterrupt(0x21);
  85.   _AX = _ES;
  86.   if (_AX | _BX) {
  87.     _AX = 0x1600;
  88.     geninterrupt(0x2f);
  89.     v = _AX;
  90.     if (v % 256 <= 1)
  91.       v = 0;
  92.   }
  93.   __emit__(0x5b, 0x07, 0x5d);
  94.   if (v != 0)
  95.     multitasker |= MT_WINDOWS;
  96.   return (v);
  97. }
  98.  
  99. int get_nb_version(void)
  100. {
  101.   _AX = 0;
  102.   geninterrupt(0x2A);
  103.   return (_AH);
  104. }
  105.  
  106. void detect_multitask(void)
  107. {
  108.   get_dos_version();
  109.   get_win_version();
  110.   get_dv_version();
  111. }
  112.  
  113. void giveup_timeslice(void)
  114. {
  115.   if (multitasker) {
  116.     switch (multitasker) {
  117.  case 1: 
  118.  case 3: 
  119.         dv_pause();
  120.         break;
  121.       case 2:
  122.       case 4:
  123.       case 5:
  124.       case 6:
  125.       case 7:
  126.         win_pause();
  127.         break;
  128.       default:
  129.         break;
  130.     }
  131.   }
  132. }
  133.  
  134. int sh_read(int handle, void *buf, unsigned len)
  135. {
  136.   if (handle == -1) {
  137.     return (-1);
  138.   }
  139.   return (read(handle, buf, len));
  140. }
  141.  
  142. long sh_lseek(int handle, long offset, int fromwhere)
  143. {
  144.   if (handle == -1) {
  145.     return (-1L);
  146.   }
  147.   return (lseek(handle, offset, fromwhere));
  148. }
  149.  
  150. int sh_write(int handle, void *buffer, unsigned long len)
  151. {
  152.   if (handle == -1) {
  153.     return (-1);
  154.   }
  155.   return (write(handle, buffer, (unsigned) len));
  156. }
  157.  
  158. int sh_close(int f)
  159. {
  160.   if (f != -1)
  161.     close(f);
  162.   return (-1);
  163. }
  164.  
  165. int sh_open(char *path, int file_access, unsigned fmode)
  166. {
  167.   int handle, count, share;
  168.   char drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE], ext[MAXEXT];
  169.  
  170.   if ((file_access & O_RDWR) || (file_access & O_WRONLY) || (fmode & S_IWRITE)) {
  171.     share = SH_DENYRW;
  172.   } else {
  173.     share = SH_DENYWR;
  174.   }
  175.   handle = open(path, file_access | share, fmode);
  176.   if (handle < 0) {
  177.     count = 1;
  178.     fnsplit(path, drive, dir, file, ext);
  179.     if (access(path, 0) != -1) {
  180.       delay(WAIT_TIME);
  181.       handle = open(path, file_access | share, fmode);
  182.       while (((handle < 0) && (errno == EACCES)) && (count < TRIES)) {
  183.         if (count % 2)
  184.           delay(WAIT_TIME);
  185.         else
  186.           giveup_timeslice();
  187.         count++;
  188.         handle = open(path, file_access | share, fmode);
  189.       }
  190.     }
  191.   }
  192.   return (handle);
  193. }
  194.  
  195. int sh_open1(char *path, int access)
  196. {
  197.   unsigned fmode;
  198.  
  199.   fmode = 0;
  200.   if ((access & O_RDWR) || (access & O_WRONLY))
  201.     fmode |= S_IWRITE;
  202.   if ((access & O_RDWR) || (access & O_RDONLY))
  203.     fmode |= S_IREAD;
  204.   return (sh_open(path, access, fmode));
  205. }
  206.  
  207. scanfor(char *token, FILE * in)
  208. {
  209.   long pos;
  210.  
  211.   pos = ftell(in);
  212.   while (fgets(buf, MAX_BUF, in) && strncmpi(buf, token, strlen(token))) {
  213.     pos = ftell(in);
  214.   }
  215.   rewind(in);
  216.   fseek(in, pos, 0);
  217.   pos = ftell(in);
  218.   return (!strncmpi(buf, "begin ", 6));
  219. }
  220.  
  221. scanfornet(FILE * in, char *net_name)
  222. {
  223.   char *ss;
  224.   int found = 0;
  225.  
  226.   while ((fgets(buf, MAX_BUF, in)) && !found) {
  227.     if (strncmpi(buf, "subject:", strlen("subject:"))) {
  228.       ss = strtok(buf, " ");
  229.       ss = strtok(NULL, " ");
  230.       if (ss[0] == '@') {
  231.         found = 1;
  232.         strcpy(net_name, ss);
  233.       }
  234.     }
  235.   }
  236.   rewind(in);
  237.   return found;
  238. }
  239.  
  240. void cd_to(char *s)
  241. {
  242.   char s1[81];
  243.   int i, db;
  244.  
  245.   strcpy(s1, s);
  246.   i = strlen(s1) - 1;
  247.   db = (s1[i] == '\\');
  248.   if (i == 0)
  249.     db = 0;
  250.   if ((i == 2) && (s1[1] == ':'))
  251.     db = 0;
  252.   if (db)
  253.     s1[i] = 0;
  254.   chdir(s1);
  255.   if (s[1] == ':') {
  256.     setdisk(s[0] - 'A');
  257.     if (s[2] == 0)
  258.       chdir("\\");
  259.   }
  260. }
  261.  
  262. void get_dir(char *s, int be)
  263. {
  264.   strcpy(s, "X:\\");
  265.   s[0] = 'A' + getdisk();
  266.   getcurdir(0, &(s[3]));
  267.   if (be) {
  268.     if (s[strlen(s) - 1] != '\\')
  269.       strcat(s, "\\");
  270.   }
  271. }
  272.  
  273. int exist(char *s)
  274. {
  275.   int i;
  276.   struct ffblk ff;
  277.  
  278.   i = findfirst(s, &ff, FA_HIDDEN);
  279.   if (i)
  280.     return (0);
  281.   else
  282.     return (1);
  283. }
  284.  
  285. int copyfile(char *input, char *output)
  286. {
  287.   int f1, f2, i;
  288.   char *b, s[181], s1[21], s2[81], s3[81];
  289.   struct ftime ft;
  290.  
  291.   if ((strcmp(input, output) != 0) && (exist(input))) {
  292.     if (exist(output)) {
  293.       fnsplit(output, s3, s2, s1, NULL);
  294.       i = 0;
  295.       sprintf(s, "%s%s%s.%03d", s3, s2, s1, i);
  296.       while (exist(s))
  297.         sprintf(s, "%s%s%s.%03d", s3, s2, s1, ++i);
  298.       strcpy(output, s);
  299.     }
  300.     fprintf(stderr, "\n ■ Creating File   : %s", output);
  301.     if ((b = (char *) farmalloc(16400)) == NULL)
  302.       return 0;
  303.     f1 = open(input, O_RDONLY | O_BINARY, S_IREAD | S_IWRITE);
  304.     if (!f1) {
  305.       farfree(b);
  306.       return 0;
  307.     }
  308.     getftime(f1, &ft);
  309.     f2 = open(output, O_RDWR | O_BINARY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
  310.     if (!f2) {
  311.       farfree(b);
  312.       close(f1);
  313.       return 0;
  314.     }
  315.     i = read(f1, (void *) b, 16384);
  316.     while (i > 0) {
  317.       write(f2, (void *) b, i);
  318.       i = read(f1, (void *) b, 16384);
  319.     }
  320.     f1 = close(f1);
  321.     setftime(f2, &ft);
  322.     f2 = close(f2);
  323.     farfree(b);
  324.   }
  325.   return 1;
  326. }
  327.  
  328. char *stripfn(char *fn)
  329. {
  330.   static char ofn[15];
  331.   int i, i1;
  332.   char s[81];
  333.  
  334.   i1 = -1;
  335.   for (i = 0; i < strlen(fn); i++)
  336.     if ((fn[i] == '\\') || (fn[i] == ':') || (fn[i] == '/'))
  337.       i1 = i;
  338.   if (i1 != -1)
  339.     strcpy(s, &(fn[i1 + 1]));
  340.   else
  341.     strcpy(s, fn);
  342.   for (i = 0; i < strlen(s); i++)
  343.     if ((s[i] >= 'A') && (s[i] <= 'Z'))
  344.       s[i] = s[i] - 'A' + 'a';
  345.   i = 0;
  346.   while (s[i] != 0) {
  347.     if (s[i] == 32)
  348.       strcpy(&s[i], &s[i + 1]);
  349.     else
  350.       ++i;
  351.   }
  352.   strcpy(ofn, s);
  353.   return (ofn);
  354. }
  355.  
  356. void ssm(char *s)
  357. {
  358.   int f, i, i1;
  359.   char s1[161];
  360.   shortmsgrec sm;
  361.   configrec syscfg;
  362.  
  363.   sprintf(s1, "CONFIG.DAT");
  364.   f = sh_open1(s1, O_RDWR | O_BINARY);
  365.   if (f < 0)
  366.     return;
  367.   sh_read(f, (void *) (&syscfg), sizeof(configrec));
  368.   sh_close(f);
  369.   sprintf(s1, "%sSMW.DAT", syscfg.datadir);
  370.   f = sh_open(s1, O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
  371.   if (f < 0)
  372.     return;
  373.   i = (int) (filelength(f) / sizeof(shortmsgrec));
  374.   i1 = i - 1;
  375.   if (i1 >= 0) {
  376.     sh_lseek(f, ((long) (i1)) * sizeof(shortmsgrec), SEEK_SET);
  377.     sh_read(f, (void *) &sm, sizeof(shortmsgrec));
  378.     while ((sm.tosys == 0) && (sm.touser == 0) && (i1 > 0)) {
  379.       --i1;
  380.       sh_lseek(f, ((long) (i1)) * sizeof(shortmsgrec), SEEK_SET);
  381.       sh_read(f, (void *) &sm, sizeof(shortmsgrec));
  382.     }
  383.     if ((sm.tosys) || (sm.touser))
  384.       ++i1;
  385.   } else
  386.     i1 = 0;
  387.   sm.tosys = 0;
  388.   sm.touser = 1;
  389.   strncpy(sm.message, s, 80);
  390.   sm.message[80] = 0;
  391.   sh_lseek(f, ((long) (i1)) * sizeof(shortmsgrec), SEEK_SET);
  392.   sh_write(f, (void *) &sm, sizeof(shortmsgrec));
  393.   sh_close(f);
  394. }
  395.  
  396. main(int argc, char *argv[])
  397. {
  398.   FILE *out = NULL, *in = NULL;
  399.   char outname[181], net_name[21];
  400.   char cur_dir[181], temp_dir[181], s1[181], s2[181], s3[21], s4[21], s5[21];
  401.   int i;
  402.  
  403.   if (get_nb_version())
  404.     multitasker = 8;
  405.   else
  406.     detect_multitask();
  407.   switch (multitasker) {
  408.     case 1:
  409.       get_dv_version();
  410.       break;
  411.     case 2:
  412.       get_win_version();
  413.       break;
  414.     case 3:
  415.       get_dv_version();
  416.       break;
  417.     case 4:
  418.     case 5:
  419.     case 6:
  420.     case 7:
  421.       break;
  422.     case 8:
  423.       multitasker = 1;
  424.       break;
  425.     default:
  426.       break;
  427.   }
  428.   if (argc == 4 || argc == 5) {
  429.     if (strcmpi(argv[1], "-encode") == 0 && argc == 4) {
  430.       if (((in = fopen(argv[2], "r")) != NULL) && ((out = fopen(argv[3], "at+")) != NULL)) {
  431.         fprintf(stderr, " %ld bytes.", uuencode(in, out, argv[2]));
  432.         if (in)
  433.           fclose(in);
  434.         if (out)
  435.           fclose(out);
  436.         exit(EXIT_SUCCESS);
  437.       } else {
  438.         exit(EXIT_FAILURE);
  439.       }
  440.     } else
  441.       if (strcmpi(argv[1], "-decode") == 0 && argc == 5) {
  442.       get_dir(cur_dir, 1);
  443.       strcpy(temp_dir, argv[3]);
  444.       if (LAST(temp_dir) == '\\')
  445.         LAST(temp_dir) = 0;
  446.       cd_to(temp_dir);
  447.       if ((in = fopen(argv[2], "r")) != NULL) {
  448.         if (!scanfornet(in, net_name))
  449.           net_name[0] = 0;
  450.         rewind(in);
  451.         scanfor("begin", in);
  452.         switch (uudecode(in, argv[3], outname)) {
  453.           case UU_NO_MEM:
  454.             fprintf(stderr, "\n ■ Insufficient memory to decode... moved to CHECKNET.");
  455.             sprintf(s1, "%s\\%s", temp_dir, argv[2]);
  456.             sprintf(s2, "%sCHECKNET\\%s", argv[4], argv[2]);
  457.             if (copyfile(s1, s2))
  458.               unlink(s1);
  459.             cd_to(cur_dir);
  460.             if (in)
  461.               fclose(in);
  462.             sprintf(s1, "Out of memory decoding %s - moved to CHECKNET!", argv[2]);
  463.             ssm(s1);
  464.             exit(EXIT_FAILURE);
  465.           case UU_BAD_BEGIN:
  466.             fprintf(stderr, "\n ■ Appears to be a standard message... moving to SPOOL");
  467.             sprintf(s1, "%s\\%s", temp_dir, argv[2]);
  468.             while (LAST(argv[2]) != '.')
  469.               LAST(argv[2]) = 0;
  470.             strcat(argv[2], "MSG");
  471.             sprintf(s2, "%sSPOOL\\%s", argv[4], argv[2]);
  472.             if (copyfile(s1, s2))
  473.               unlink(s1);
  474.             cd_to(cur_dir);
  475.             if (in)
  476.               fclose(in);
  477.             exit(EXIT_FAILURE);
  478.           case UU_CANT_OPEN:
  479.             fprintf(stderr, "\n ■ Cannot open %s%s", argv[4], outname);
  480.             cd_to(cur_dir);
  481.             if (in)
  482.               fclose(in);
  483.             exit(EXIT_FAILURE);
  484.           case UU_CHECKSUM:
  485.             fprintf(stderr, "\n ■ Bad checksum... moving packet to CHECKNET");
  486.             sprintf(s1, "%s\\%s", temp_dir, argv[2]);
  487.             while (LAST(argv[2]) != '.')
  488.               LAST(argv[2]) = 0;
  489.             strcat(argv[2], "MSG");
  490.             sprintf(s2, "%sCHECKNET\\%s", argv[4], argv[2]);
  491.             if (copyfile(s1, s2))
  492.               unlink(s1);
  493.             cd_to(cur_dir);
  494.             if (in)
  495.               fclose(in);
  496.             sprintf(s1, "Bad checksum decoding %s - moved to CHECKNET!", argv[2]);
  497.             ssm(s1);
  498.             exit(EXIT_FAILURE);
  499.           case UU_BAD_END:
  500.             fprintf(stderr, "\n ■ \'end\' not found in message... moving to CHECKNET");
  501.             sprintf(s1, "%s\\%s", temp_dir, argv[2]);
  502.             while (LAST(argv[2]) != '.')
  503.               LAST(argv[2]) = 0;
  504.             strcat(argv[2], "MSG");
  505.             sprintf(s2, "%sCHECKNET\\%s", argv[4], argv[2]);
  506.             if (copyfile(s1, s2))
  507.               unlink(s1);
  508.             cd_to(cur_dir);
  509.             if (in)
  510.               fclose(in);
  511.             sprintf(s1, "No \'end\' found in %s - moved to CHECKNET!", argv[2]);
  512.             ssm(s1);
  513.             exit(EXIT_FAILURE);
  514.           case UU_SUCCESS:
  515.             fprintf(stderr, "\n ■ Successfully decoded : %s", stripfn(outname));
  516.             fnsplit(outname, NULL, NULL, s4, s3);
  517.             sprintf(s5, "%s%s", s4, s3);
  518.             if ((strcmpi(s3, ".NET") == NULL) && (strncmpi(argv[2], "PKT", 3) == NULL)) {
  519.               unlink(argv[2]);
  520.               i = 0;
  521.               sprintf(s2, "%sP1-%03d.NET", argv[4], i);
  522.               while (exist(s2))
  523.                 sprintf(s2, "%sP1-%03d.NET", argv[4], ++i);
  524.               sprintf(s1, "%s\\%s", temp_dir, stripfn(outname));
  525.               if (!copyfile(s1, s2)) {
  526.                 fprintf(stderr, "\n ■ Error creating %s", s2);
  527.                 cd_to(cur_dir);
  528.                 if (in)
  529.                   fclose(in);
  530.                 exit(EXIT_FAILURE);
  531.               } else
  532.                 unlink(s1);
  533.             } else {
  534.               sprintf(s1, "Received transferred file : %s.", s5);
  535.               ssm(s1);
  536.               sprintf(s1, "%s\\%s", temp_dir, stripfn(outname));
  537.               sprintf(s2, "%sCHECKNET\\%s", argv[4], stripfn(outname));
  538.               if (exist(s2)) {
  539.                 s2[strlen(s2 - 1)] = 0;
  540.                 i = 0;
  541.                 sprintf(s2, "%sCHECKNET\\%s%d", argv[4], stripfn(outname), i);
  542.                 while (exist(s2)) {
  543.                   sprintf(s2, "%sCHECKNET\\%s%d", argv[4], stripfn(outname), ++i);
  544.                   if (i > 9)
  545.                     break;
  546.                 }
  547.               }
  548.               if (!copyfile(s1, s2)) {
  549.                 fprintf(stderr, "\n ■ Error creating %s", s2);
  550.                 cd_to(cur_dir);
  551.                 if (in)
  552.                   fclose(in);
  553.                 exit(EXIT_FAILURE);
  554.               } else {
  555.                 unlink(s1);
  556. /* Added */
  557.                 sprintf(s1, "%s\\%s", temp_dir, argv[2]);
  558.                 unlink(s1);
  559.               }
  560. /*
  561.               sprintf(s1, "%s\\%s", temp_dir, argv[2]);
  562.               while (LAST(argv[2]) != '.')
  563.                 LAST(argv[2]) = 0;
  564.               strcat(argv[2], "MSG");
  565.               sprintf(s2, "%sCHECKNET\\%s", argv[4], argv[2]);
  566.               if (copyfile(s1, s2))
  567.                 unlink(s1);
  568. */
  569.             }
  570.             cd_to(cur_dir);
  571.             if (in)
  572.               fclose(in);
  573.             exit(EXIT_SUCCESS);
  574.           default:
  575.             fprintf(stderr, "\n ■ Unknown error... moving packet to CHECKNET");
  576.             sprintf(s1, "%s\\%s", temp_dir, argv[2]);
  577.             while (LAST(argv[2]) != '.')
  578.               LAST(argv[2]) = 0;
  579.             strcat(argv[2], "MSG");
  580.             sprintf(s2, "%sCHECKNET\\%s", argv[4], argv[2]);
  581.             if (copyfile(s1, s2))
  582.               unlink(s1);
  583.             cd_to(cur_dir);
  584.             if (in)
  585.               fclose(in);
  586.             sprintf(s1, "Unknown error processing %s - moved to CHECKNET!", argv[2]);
  587.             ssm(s1);
  588.             exit(EXIT_FAILURE);
  589.         }
  590.       } else {
  591.         cd_to(cur_dir);
  592.         fprintf(stderr, "\n ■ Input file %s not found.", argv[2]);
  593.         if (in)
  594.           fclose(in);
  595.         exit(EXIT_FAILURE);
  596.       }
  597.       }
  598.   } else
  599.     fprintf(stderr, "\n ■ %s\n", version);
  600.   if (in)
  601.     fclose(in);
  602.   exit(EXIT_SUCCESS);
  603.   return 0;
  604. }
  605.