home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC / SRC15B29.ZIP / UUMAIN.C < prev    next >
C/C++ Source or Header  |  1997-06-29  |  14KB  |  574 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 = 8192;
  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.   if (multitasker < 2)
  112.     if (get_nb_version())
  113.       multitasker = MT_NB;
  114. }
  115.  
  116. void giveup_timeslice(void)
  117. {
  118.   if (multitasker) {
  119.     switch (multitasker) {
  120.  case 1: 
  121.  case 3: 
  122.         dv_pause();
  123.         break;
  124.       case 2:
  125.       case 4:
  126.       case 5:
  127.       case 6:
  128.       case 7:
  129.         win_pause();
  130.         break;
  131.       default:
  132.         break;
  133.     }
  134.   }
  135. }
  136.  
  137. int sh_read(int handle, void *buf, unsigned len)
  138. {
  139.   if (handle == -1) {
  140.     return (-1);
  141.   }
  142.   return (read(handle, buf, len));
  143. }
  144.  
  145. long sh_lseek(int handle, long offset, int fromwhere)
  146. {
  147.   if (handle == -1) {
  148.     return (-1L);
  149.   }
  150.   return (lseek(handle, offset, fromwhere));
  151. }
  152.  
  153. int sh_write(int handle, void *buffer, unsigned long len)
  154. {
  155.   if (handle == -1) {
  156.     return (-1);
  157.   }
  158.   return (write(handle, buffer, (unsigned) len));
  159. }
  160.  
  161. int sh_close(int f)
  162. {
  163.   if (f != -1)
  164.     close(f);
  165.   return (-1);
  166. }
  167.  
  168. int sh_open(char *path, int file_access, unsigned fmode)
  169. {
  170.   int handle, count, share;
  171.   char drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE], ext[MAXEXT];
  172.  
  173.   if ((file_access & O_RDWR) || (file_access & O_WRONLY) || (fmode & S_IWRITE)) {
  174.     share = SH_DENYRW;
  175.   } else {
  176.     share = SH_DENYWR;
  177.   }
  178.   handle = open(path, file_access | share, fmode);
  179.   if (handle < 0) {
  180.     count = 1;
  181.     fnsplit(path, drive, dir, file, ext);
  182.     if (access(path, 0) != -1) {
  183.       delay(WAIT_TIME);
  184.       handle = open(path, file_access | share, fmode);
  185.       while (((handle < 0) && (errno == EACCES)) && (count < TRIES)) {
  186.         if (count % 2)
  187.           delay(WAIT_TIME);
  188.         else
  189.           giveup_timeslice();
  190.         count++;
  191.         handle = open(path, file_access | share, fmode);
  192.       }
  193.     }
  194.   }
  195.   return (handle);
  196. }
  197.  
  198. int sh_open1(char *path, int access)
  199. {
  200.   unsigned fmode;
  201.  
  202.   fmode = 0;
  203.   if ((access & O_RDWR) || (access & O_WRONLY))
  204.     fmode |= S_IWRITE;
  205.   if ((access & O_RDWR) || (access & O_RDONLY))
  206.     fmode |= S_IREAD;
  207.   return (sh_open(path, access, fmode));
  208. }
  209.  
  210. scanfor(char *token, FILE * in)
  211. {
  212.   long pos;
  213.  
  214.   pos = ftell(in);
  215.   while (fgets(buf, MAX_BUF, in) && strncmpi(buf, token, strlen(token))) {
  216.     pos = ftell(in);
  217.   }
  218.   rewind(in);
  219.   fseek(in, pos, 0);
  220.   pos = ftell(in);
  221.   return (!strncmpi(buf, "begin ", 6));
  222. }
  223.  
  224. scanfornet(FILE * in, char *net_name)
  225. {
  226.   char *ss;
  227.   int found = 0;
  228.  
  229.   while ((fgets(buf, MAX_BUF, in)) && !found) {
  230.     if (strncmpi(buf, "subject:", strlen("subject:"))) {
  231.       ss = strtok(buf, " ");
  232.       ss = strtok(NULL, " ");
  233.       if (ss[0] == '@') {
  234.         found = 1;
  235.         strcpy(net_name, ss);
  236.       }
  237.     }
  238.   }
  239.   rewind(in);
  240.   return found;
  241. }
  242.  
  243. void cd_to(char *s)
  244. {
  245.   char s1[81];
  246.   int i, db;
  247.  
  248.   strcpy(s1, s);
  249.   i = strlen(s1) - 1;
  250.   db = (s1[i] == '\\');
  251.   if (i == 0)
  252.     db = 0;
  253.   if ((i == 2) && (s1[1] == ':'))
  254.     db = 0;
  255.   if (db)
  256.     s1[i] = 0;
  257.   chdir(s1);
  258.   if (s[1] == ':') {
  259.     setdisk(s[0] - 'A');
  260.     if (s[2] == 0)
  261.       chdir("\\");
  262.   }
  263. }
  264.  
  265. void get_dir(char *s, int be)
  266. {
  267.   strcpy(s, "X:\\");
  268.   s[0] = 'A' + getdisk();
  269.   getcurdir(0, &(s[3]));
  270.   if (be) {
  271.     if (s[strlen(s) - 1] != '\\')
  272.       strcat(s, "\\");
  273.   }
  274. }
  275.  
  276. int exist(char *s)
  277. {
  278.   int i;
  279.   struct ffblk ff;
  280.  
  281.   i = findfirst(s, &ff, FA_HIDDEN);
  282.   if (i)
  283.     return (0);
  284.   else
  285.     return (1);
  286. }
  287.  
  288. int copyfile(char *input, char *output)
  289. {
  290.   int f1, f2, i;
  291.   char *b, s[181], s1[21], s2[81], s3[81];
  292.   struct ftime ft;
  293.  
  294.   if ((strcmp(input, output) != 0) && (exist(input))) {
  295.     if (exist(output)) {
  296.       fnsplit(output, s3, s2, s1, NULL);
  297.       i = 0;
  298.       sprintf(s, "%s%s%s.%03d", s3, s2, s1, i);
  299.       while (exist(s))
  300.         sprintf(s, "%s%s%s.%03d", s3, s2, s1, ++i);
  301.       strcpy(output, s);
  302.     }
  303.     fprintf(stderr, "\n ■ Creating File   : %s", output);
  304.     if ((b = (char *) farmalloc(16400)) == NULL)
  305.       return 0;
  306.     f1 = open(input, O_RDONLY | O_BINARY, S_IREAD | S_IWRITE);
  307.     if (!f1) {
  308.       farfree(b);
  309.       return 0;
  310.     }
  311.     getftime(f1, &ft);
  312.     f2 = open(output, O_RDWR | O_BINARY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
  313.     if (!f2) {
  314.       farfree(b);
  315.       close(f1);
  316.       return 0;
  317.     }
  318.     i = read(f1, (void *) b, 16384);
  319.     while (i > 0) {
  320.       write(f2, (void *) b, i);
  321.       i = read(f1, (void *) b, 16384);
  322.     }
  323.     f1 = close(f1);
  324.     setftime(f2, &ft);
  325.     f2 = close(f2);
  326.     farfree(b);
  327.   }
  328.   return 1;
  329. }
  330.  
  331. char *stripfn(char *fn)
  332. {
  333.   static char ofn[15];
  334.   int i, i1;
  335.   char s[81];
  336.  
  337.   i1 = -1;
  338.   for (i = 0; i < strlen(fn); i++)
  339.     if ((fn[i] == '\\') || (fn[i] == ':') || (fn[i] == '/'))
  340.       i1 = i;
  341.   if (i1 != -1)
  342.     strcpy(s, &(fn[i1 + 1]));
  343.   else
  344.     strcpy(s, fn);
  345.   for (i = 0; i < strlen(s); i++)
  346.     if ((s[i] >= 'A') && (s[i] <= 'Z'))
  347.       s[i] = s[i] - 'A' + 'a';
  348.   i = 0;
  349.   while (s[i] != 0) {
  350.     if (s[i] == 32)
  351.       strcpy(&s[i], &s[i + 1]);
  352.     else
  353.       ++i;
  354.   }
  355.   strcpy(ofn, s);
  356.   return (ofn);
  357. }
  358.  
  359. void ssm(char *s)
  360. {
  361.   int f, i, i1;
  362.   char s1[161];
  363.   shortmsgrec sm;
  364.   configrec syscfg;
  365.  
  366.   sprintf(s1, "CONFIG.DAT");
  367.   f = sh_open1(s1, O_RDWR | O_BINARY);
  368.   if (f < 0)
  369.     return;
  370.   sh_read(f, (void *) (&syscfg), sizeof(configrec));
  371.   sh_close(f);
  372.   sprintf(s1, "%sSMW.DAT", syscfg.datadir);
  373.   f = sh_open(s1, O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
  374.   if (f < 0)
  375.     return;
  376.   i = (int) (filelength(f) / sizeof(shortmsgrec));
  377.   i1 = i - 1;
  378.   if (i1 >= 0) {
  379.     sh_lseek(f, ((long) (i1)) * sizeof(shortmsgrec), SEEK_SET);
  380.     sh_read(f, (void *) &sm, sizeof(shortmsgrec));
  381.     while ((sm.tosys == 0) && (sm.touser == 0) && (i1 > 0)) {
  382.       --i1;
  383.       sh_lseek(f, ((long) (i1)) * sizeof(shortmsgrec), SEEK_SET);
  384.       sh_read(f, (void *) &sm, sizeof(shortmsgrec));
  385.     }
  386.     if ((sm.tosys) || (sm.touser))
  387.       ++i1;
  388.   } else
  389.     i1 = 0;
  390.   sm.tosys = 0;
  391.   sm.touser = 1;
  392.   strncpy(sm.message, s, 80);
  393.   sm.message[80] = 0;
  394.   sh_lseek(f, ((long) (i1)) * sizeof(shortmsgrec), SEEK_SET);
  395.   sh_write(f, (void *) &sm, sizeof(shortmsgrec));
  396.   sh_close(f);
  397. }
  398.  
  399. main(int argc, char *argv[])
  400. {
  401.   FILE *out = NULL, *in = NULL;
  402.   char outname[181], net_name[21];
  403.   char cur_dir[181], temp_dir[181], s1[181], s2[181], s3[21], s4[21], s5[21];
  404.   int i;
  405.  
  406.   detect_multitask();
  407.  
  408.   if (argc == 4 || argc == 5) {
  409.     if (strcmpi(argv[1], "-encode") == 0 && argc == 4) {
  410.       if (((in = fopen(argv[2], "r")) != NULL) && ((out = fopen(argv[3], "at+")) != NULL)) {
  411.         fprintf(stderr, " %ld bytes.", uuencode(in, out, argv[2]));
  412.         if (in)
  413.           fclose(in);
  414.         if (out)
  415.           fclose(out);
  416.         exit(EXIT_SUCCESS);
  417.       } else {
  418.         exit(EXIT_FAILURE);
  419.       }
  420.     } else
  421.       if (strcmpi(argv[1], "-decode") == 0 && argc == 5) {
  422.       get_dir(cur_dir, 1);
  423.       strcpy(temp_dir, argv[3]);
  424.       if (LAST(temp_dir) == '\\')
  425.         LAST(temp_dir) = 0;
  426.       cd_to(temp_dir);
  427.       if ((in = fopen(argv[2], "r")) != NULL) {
  428.         if (!scanfornet(in, net_name))
  429.           net_name[0] = 0;
  430.         rewind(in);
  431.         scanfor("begin", in);
  432.         switch (uudecode(in, argv[3], outname)) {
  433.           case UU_NO_MEM:
  434.             fprintf(stderr, "\n ■ Insufficient memory to decode... moved to CHECKNET.");
  435.             sprintf(s1, "%s\\%s", temp_dir, argv[2]);
  436.             sprintf(s2, "%sCHECKNET\\%s", argv[4], argv[2]);
  437.             if (copyfile(s1, s2))
  438.               unlink(s1);
  439.             cd_to(cur_dir);
  440.             if (in)
  441.               fclose(in);
  442.             sprintf(s1, "Out of memory decoding %s - moved to CHECKNET!", argv[2]);
  443.             ssm(s1);
  444.             exit(EXIT_FAILURE);
  445.           case UU_BAD_BEGIN:
  446.             fprintf(stderr, "\n ■ Appears to be a standard message... moving to SPOOL");
  447.             sprintf(s1, "%s\\%s", temp_dir, argv[2]);
  448.             while (LAST(argv[2]) != '.')
  449.               LAST(argv[2]) = 0;
  450.             strcat(argv[2], "MSG");
  451.             sprintf(s2, "%sSPOOL\\%s", argv[4], argv[2]);
  452.             if (copyfile(s1, s2))
  453.               unlink(s1);
  454.             cd_to(cur_dir);
  455.             if (in)
  456.               fclose(in);
  457.             exit(EXIT_FAILURE);
  458.           case UU_CANT_OPEN:
  459.             fprintf(stderr, "\n ■ Cannot open %s%s", argv[4], outname);
  460.             cd_to(cur_dir);
  461.             if (in)
  462.               fclose(in);
  463.             exit(EXIT_FAILURE);
  464.           case UU_CHECKSUM:
  465.             fprintf(stderr, "\n ■ Bad checksum... moving packet to CHECKNET");
  466.             sprintf(s1, "%s\\%s", temp_dir, argv[2]);
  467.             while (LAST(argv[2]) != '.')
  468.               LAST(argv[2]) = 0;
  469.             strcat(argv[2], "MSG");
  470.             sprintf(s2, "%sCHECKNET\\%s", argv[4], argv[2]);
  471.             if (copyfile(s1, s2))
  472.               unlink(s1);
  473.             cd_to(cur_dir);
  474.             if (in)
  475.               fclose(in);
  476.             sprintf(s1, "Bad checksum decoding %s - moved to CHECKNET!", argv[2]);
  477.             ssm(s1);
  478.             exit(EXIT_FAILURE);
  479.           case UU_BAD_END:
  480.             fprintf(stderr, "\n ■ \'end\' not found in message... moving 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)
  490.               fclose(in);
  491.             sprintf(s1, "No \'end\' found in %s - moved to CHECKNET!", argv[2]);
  492.             ssm(s1);
  493.             exit(EXIT_FAILURE);
  494.           case UU_SUCCESS:
  495.             fprintf(stderr, "\n ■ Successfully decoded : %s", stripfn(outname));
  496.             fnsplit(outname, NULL, NULL, s4, s3);
  497.             sprintf(s5, "%s%s", s4, s3);
  498.             if ((strcmpi(s3, ".NET") == NULL) && (strncmpi(argv[2], "PKT", 3) == NULL)) {
  499.               unlink(argv[2]);
  500.               i = 0;
  501.               sprintf(s2, "%sP1-%03d.NET", argv[4], i);
  502.               while (exist(s2))
  503.                 sprintf(s2, "%sP1-%03d.NET", argv[4], ++i);
  504.               sprintf(s1, "%s\\%s", temp_dir, stripfn(outname));
  505.               if (!copyfile(s1, s2)) {
  506.                 fprintf(stderr, "\n ■ Error creating %s", s2);
  507.                 cd_to(cur_dir);
  508.                 if (in)
  509.                   fclose(in);
  510.                 exit(EXIT_FAILURE);
  511.               } else
  512.                 unlink(s1);
  513.             } else {
  514.               sprintf(s1, "Received transferred file : %s.", s5);
  515.               ssm(s1);
  516.               sprintf(s1, "%s\\%s", temp_dir, stripfn(outname));
  517.               sprintf(s2, "%sCHECKNET\\%s", argv[4], stripfn(outname));
  518.               if (exist(s2)) {
  519.                 s2[strlen(s2 - 1)] = 0;
  520.                 i = 0;
  521.                 sprintf(s2, "%sCHECKNET\\%s%d", argv[4], stripfn(outname), i);
  522.                 while (exist(s2)) {
  523.                   sprintf(s2, "%sCHECKNET\\%s%d", argv[4], stripfn(outname), ++i);
  524.                   if (i > 9)
  525.                     break;
  526.                 }
  527.               }
  528.               if (!copyfile(s1, s2)) {
  529.                 fprintf(stderr, "\n ■ Error creating %s", s2);
  530.                 cd_to(cur_dir);
  531.                 if (in)
  532.                   fclose(in);
  533.                 exit(EXIT_FAILURE);
  534.               } else {
  535.                 unlink(s1);
  536.                 sprintf(s1, "%s\\%s", temp_dir, argv[2]);
  537.                 unlink(s1);
  538.               }
  539.             }
  540.             cd_to(cur_dir);
  541.             if (in)
  542.               fclose(in);
  543.             exit(EXIT_SUCCESS);
  544.           default:
  545.             fprintf(stderr, "\n ■ Unknown error... moving packet to CHECKNET");
  546.             sprintf(s1, "%s\\%s", temp_dir, argv[2]);
  547.             while (LAST(argv[2]) != '.')
  548.               LAST(argv[2]) = 0;
  549.             strcat(argv[2], "MSG");
  550.             sprintf(s2, "%sCHECKNET\\%s", argv[4], argv[2]);
  551.             if (copyfile(s1, s2))
  552.               unlink(s1);
  553.             cd_to(cur_dir);
  554.             if (in)
  555.               fclose(in);
  556.             sprintf(s1, "Unknown error processing %s - moved to CHECKNET!", argv[2]);
  557.             ssm(s1);
  558.             exit(EXIT_FAILURE);
  559.         }
  560.       } else {
  561.         cd_to(cur_dir);
  562.         fprintf(stderr, "\n ■ Input file %s not found.", argv[2]);
  563.         if (in)
  564.           fclose(in);
  565.         exit(EXIT_FAILURE);
  566.       }
  567.       }
  568.   } else
  569.     fprintf(stderr, "\n ■ %s\n", version);
  570.   if (in)
  571.     fclose(in);
  572.   return 0;
  573. }
  574.