home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC / SRC15B33.ZIP / UUMAIN.C < prev    next >
C/C++ Source or Header  |  1997-07-20  |  15KB  |  589 lines

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