home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC / SRC15B97.ZIP / UUMAIN.CPP < prev    next >
C/C++ Source or Header  |  1998-07-06  |  37KB  |  1,481 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 <process.h>
  15. #include "uu.h"
  16. #include "net.h"
  17. #include "vardec.h"
  18. #include "version.h"
  19. #include "retcode.h"
  20.  
  21. #define MAX_BUF 1024
  22. #define MAX_DIZ_LINES 10
  23.  
  24. #define SHARE_LEVEL 10
  25. #define WAIT_TIME 10
  26. #define TRIES 100
  27.  
  28. #define SETREC(f,i)  sh_lseek(f,((long) (i)) * ((long)sizeof(uploadsrec)), SEEK_SET);
  29.  
  30. #define LAST(s) s[strlen(s)-1]
  31.  
  32. extern unsigned _stklen = (15*1024);
  33.  
  34. char netname[21], net_name[21], netdata[MAXPATH], net_data[MAXPATH], fdldesc[81], fdlfn[40], fdlupby[40], edlfn[MAXPATH];
  35. char net_pkt[21], temp_dir[MAXPATH], maindir[MAXPATH], dlfn[MAXPATH];
  36. char *xenviron[40];
  37. unsigned short fdltype, net_sysnum;
  38. int numf, net_num;
  39. char *version = "Freeware PPP Project UU Encode/Decode " VERSION;
  40.  
  41. directoryrec huge *dirs;
  42. configrec syscfg;
  43. net_networks_rec netcfg;
  44.  
  45. struct ts_os_ver {
  46.   int maj;
  47.   int min;
  48. };
  49.  
  50. #define DOS     0
  51. #define OS2     1
  52. #define DV      2
  53. #define WINS    3
  54. #define WIN3    4
  55.  
  56. #define MT_DOS  0x01
  57. #define MT_OS2  0x02
  58. #define MT_DV   0x04
  59. #define MT_WINS 0x08
  60. #define MT_WIN3 0x10
  61.  
  62. struct ts_os_ver t_os_ver[5];
  63. int t_os_type;
  64. int t_os;
  65. char t_os_name[41];
  66.  
  67. int detect_multitask(void)
  68. {
  69.   union REGS t_regs;
  70.  
  71.   t_os_type = 0;
  72.   t_os = 0;
  73.  
  74.   if (_osmajor < 10) {
  75.     t_os_ver[DOS].maj = _osmajor;
  76.     t_os_ver[DOS].min = _osminor;
  77.     t_os_type = t_os_type | MT_DOS;
  78.     strcpy(t_os_name, "DOS");
  79.   } else {
  80.     t_os_type = t_os_type | MT_OS2;
  81.     t_os_ver[OS2].maj = _osmajor / 10;
  82.     t_os_ver[OS2].min = _osminor;
  83.     if (t_os_ver[OS2].maj == 3) {
  84.       strcpy(t_os_name, "OS/2 Warp");
  85.     } else {
  86.       strcpy(t_os_name, "OS/2");
  87.     }
  88.   }
  89.  
  90.   t_regs.x.ax = 0x4680;
  91.   int86(0x2F, &t_regs, &t_regs);
  92.  
  93.   if (t_regs.x.ax == 0x0000) {
  94.     t_os_ver[WINS].maj = 3;
  95.     t_os_ver[WINS].min = 0;
  96.     t_os_type = t_os_type | MT_WINS;
  97.   } else {
  98.     t_regs.x.ax = 0x1600;
  99.     int86(0x2F, &t_regs, &t_regs);
  100.     switch (t_regs.h.al) {
  101.       case 0x00:
  102.       case 0x80:
  103.       case 0x01:
  104.       case 0xFF:
  105.         break;
  106.       default:
  107.         t_os_type = t_os_type | MT_WIN3;
  108.         t_os_ver[WIN3].maj = t_regs.h.al;
  109.         t_os_ver[WIN3].min = t_regs.h.ah;
  110.         if (t_os_ver[WIN3].maj == 4) {
  111.           strcpy(t_os_name, "Windows 95");
  112.           t_os_ver[WIN3].maj = t_os_ver[WIN3].maj - 3;
  113.         } else {
  114.           strcpy(t_os_name, "Windows");
  115.         }
  116.         break;
  117.     }
  118.   }
  119.  
  120.   t_regs.x.cx = 0x4445;
  121.   t_regs.x.dx = 0x5351;
  122.   t_regs.x.ax = 0x2B01;
  123.  
  124.   intdos(&t_regs, &t_regs);
  125.   if (t_regs.h.al != 0xFF) {
  126.     t_os_type = t_os_type | MT_DV;
  127.     t_os_ver[DV].maj = t_regs.h.bh;
  128.     t_os_ver[DV].min = t_regs.h.bl;
  129.     strcpy(t_os_name, "DESQview");
  130.   }
  131.   if (t_os_type & MT_DOS)
  132.     t_os = DOS;
  133.   if (t_os_type & MT_DV)
  134.     t_os = DV;
  135.   if (t_os_type & MT_WINS)
  136.     t_os = WINS;
  137.   if (t_os_type & MT_WIN3)
  138.     t_os = WIN3;
  139.   if (t_os_type & MT_OS2)
  140.     t_os = OS2;
  141.   return (t_os - 1);
  142. }
  143.  
  144. void giveup_timeslice(void)
  145. {
  146.   union REGS t_regs;
  147.  
  148.   switch (t_os) {
  149.     case DOS:
  150.       break;
  151.     case OS2:
  152.     case WIN3:
  153.     case WINS:
  154.       t_regs.x.ax = 0x1680;
  155.       int86(0x2f, &t_regs, &t_regs);
  156.       break;
  157.     case DV:
  158.       t_regs.x.ax = 0x1000;
  159.       int86(0x15, &t_regs, &t_regs);
  160.       break;
  161.   }
  162. }
  163.  
  164. int sh_read(int handle, void *buf, unsigned len)
  165. {
  166.   if (handle == -1) {
  167.     return (-1);
  168.   }
  169.   return (read(handle, buf, len));
  170. }
  171.  
  172. long sh_lseek(int handle, long offset, int fromwhere)
  173. {
  174.   if (handle == -1) {
  175.     return (-1L);
  176.   }
  177.   return (lseek(handle, offset, fromwhere));
  178. }
  179.  
  180. int sh_write(int handle, void *buffer, unsigned long len)
  181. {
  182.   if (handle == -1) {
  183.     return (-1);
  184.   }
  185.   return (write(handle, buffer, (unsigned) len));
  186. }
  187.  
  188. int sh_close(int f)
  189. {
  190.   if (f != -1)
  191.     close(f);
  192.   return (-1);
  193. }
  194.  
  195. int sh_open(char *path, int file_access, unsigned fmode)
  196. {
  197.   int handle, count, share;
  198.   char drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE], ext[MAXEXT];
  199.  
  200.   if ((file_access & O_RDWR) || (file_access & O_WRONLY) || (fmode & S_IWRITE)) {
  201.     share = SH_DENYRW;
  202.   } else {
  203.     share = SH_DENYWR;
  204.   }
  205.   handle = open(path, file_access | share, fmode);
  206.   if (handle < 0) {
  207.     count = 1;
  208.     fnsplit(path, drive, dir, file, ext);
  209.     if (access(path, 0) != -1) {
  210.       delay(WAIT_TIME);
  211.       handle = open(path, file_access | share, fmode);
  212.       while (((handle < 0) && (errno == EACCES)) && (count < TRIES)) {
  213.         if (count % 2)
  214.           delay(WAIT_TIME);
  215.         else
  216.           giveup_timeslice();
  217.         count++;
  218.         handle = open(path, file_access | share, fmode);
  219.       }
  220.     }
  221.   }
  222.   return (handle);
  223. }
  224.  
  225. int sh_open1(char *path, int access)
  226. {
  227.   unsigned fmode;
  228.  
  229.   fmode = 0;
  230.   if ((access & O_RDWR) || (access & O_WRONLY))
  231.     fmode |= S_IWRITE;
  232.   if ((access & O_RDWR) || (access & O_RDONLY))
  233.     fmode |= S_IREAD;
  234.   return (sh_open(path, access, fmode));
  235. }
  236.  
  237. FILE *fsh_open(char *path, char *fmode)
  238. {
  239.   FILE *f;
  240.   int count, share, md, fd;
  241.   char drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE], ext[MAXEXT];
  242.  
  243.   share = SH_DENYWR;
  244.   md = 0;
  245.   if (((char *) _fstrchr(fmode, 'w')) != NULL) {
  246.     share = SH_DENYRD;
  247.     md = O_RDWR | O_CREAT | O_TRUNC;
  248.   } else
  249.     if (((char *) _fstrchr(fmode, 'a')) != NULL) {
  250.     share = SH_DENYRD;
  251.     md = O_RDWR | O_CREAT;
  252.   } else {
  253.     md = O_RDONLY;
  254.   }
  255.   if (((char *) _fstrchr(fmode, 'b')) != NULL) {
  256.     md |= O_BINARY;
  257.   }
  258.   if (((char *) _fstrchr(fmode, '+')) != NULL) {
  259.     md &= ~O_RDONLY;
  260.     md |= O_RDWR;
  261.     share = SH_DENYRD;
  262.   }
  263.   fd = open(path, md | share, S_IREAD | S_IWRITE);
  264.   if (fd < 0) {
  265.     count = 1;
  266.     fnsplit(path, drive, dir, file, ext);
  267.     if ((access(path, 0)) != -1) {
  268.       delay(WAIT_TIME);
  269.       fd = open(path, md | share, S_IREAD | S_IWRITE);
  270.       while (((fd < 0) && (errno == EACCES)) && (count < TRIES)) {
  271.         delay(WAIT_TIME);
  272.         count++;
  273.         fd = open(path, md | share, S_IREAD | S_IWRITE);
  274.       }
  275.     }
  276.   }
  277.   if (fd > 0) {
  278.     if (((char *) _fstrchr(fmode, 'a')) != NULL)
  279.       sh_lseek(fd, 0L, SEEK_END);
  280.     f = fdopen(fd, fmode);
  281.     if (!f) {
  282.       close(fd);
  283.     }
  284.   } else
  285.     f = NULL;
  286.   return (f);
  287. }
  288.  
  289. int log_it(int display, char *fmt,...)
  290. {
  291.   va_list v;
  292.   char s[255], fn[MAXPATH];
  293.   FILE *fp;
  294.  
  295.   sprintf(fn, "%sNEWS.LOG", netdata);
  296.   if ((fp = fsh_open(fn, "at")) == NULL)
  297.     return 1;
  298.   va_start(v, fmt);
  299.   vsprintf(s, fmt, v);
  300.   va_end(v);
  301.   fputs(s, fp);
  302.   fclose(fp);
  303.   if (display)
  304.     fputs(s, stderr);
  305.   return 0;
  306. }
  307.  
  308.  
  309. #define MAX_XFER 61440L
  310.  
  311. long huge_xfer(int fd, void huge * buf, unsigned sz, unsigned nel, int wr)
  312. {
  313.   long nxfr = 0, len = ((long) sz) * ((long) nel);
  314.   char huge *xbuf = (char huge *) buf;
  315.   unsigned cur, cur1;
  316.  
  317.   while (len > 0) {
  318.  
  319.     if (len > MAX_XFER)
  320.       cur = (unsigned int) MAX_XFER;
  321.     else
  322.       cur = (unsigned int) len;
  323.  
  324.     if (wr)
  325.       cur1 = sh_write(fd, (char *) xbuf, cur);
  326.     else
  327.       cur1 = sh_read(fd, (char *) xbuf, cur);
  328.  
  329.     if (cur1 != 65535L) {
  330.       len -= cur1;
  331.       nxfr += cur1;
  332.       xbuf = ((char huge *) buf) + nxfr;
  333.     }
  334.     if (cur1 != cur)
  335.       break;
  336.   }
  337.  
  338.   return (nxfr);
  339. }
  340.  
  341. void output(char *fmt,...)
  342. {
  343.   va_list v;
  344.   char s[255];
  345.  
  346.   va_start(v, fmt);
  347.   vsprintf(s, fmt, v);
  348.   va_end(v);
  349.   fputs(s, stderr);
  350. }
  351.  
  352.  
  353.  
  354. void far *mallocx(unsigned long l)
  355. {
  356.   void *x;
  357.   char huge *xx;
  358.  
  359.   if (!l)
  360.     l = 1;
  361.   x = farmalloc(l);
  362.   if (!x) {
  363.     log_it(1, "\n ! Insufficient memory (%ld bytes) to read all directories.", l);
  364.     return NULL;
  365.   }
  366.   xx = (char huge *) x;
  367.   while (l >= 1) {
  368.     if (l > 32768L) {
  369.       memset((void *) xx, 0, (size_t)32768L);
  370.       l -= 32768L;
  371.       xx += 32768L;
  372.     } else {
  373.       memset((void *) xx, 0, (size_t)l);
  374.       break;
  375.     }
  376.   }
  377.   return (x);
  378. }
  379.  
  380. int scanfor(char *token, FILE * in)
  381. {
  382.   char buf[MAX_BUF];
  383.   long pos;
  384.  
  385.   pos = ftell(in);
  386.   while (fgets(buf, MAX_BUF, in) && strncmpi(buf, token, strlen(token))) {
  387.     pos = ftell(in);
  388.   }
  389.   rewind(in);
  390.   fseek(in, pos, 0);
  391.   pos = ftell(in);
  392.   return (!strncmpi(buf, "begin 6", 7));
  393. }
  394.  
  395. unsigned char *trimstr1(unsigned char *s)
  396. {
  397.   int i;
  398.   static char *whitespace = " \r\n\t";
  399.  
  400.   i = strlen(s);
  401.   while ((i > 0) && (_fstrchr(whitespace, s[i - 1])))
  402.     --i;
  403.   while ((i > 0) && (_fstrchr(whitespace, *s))) {
  404.     memmove(s, s + 1, --i);
  405.   }
  406.   s[i] = 0;
  407.   return (s);
  408. }
  409.  
  410.  
  411. void scanfdl(FILE * in)
  412. {
  413.   char *ss, buf[MAX_BUF];
  414.   int done = 0;
  415.  
  416.   while ((fgets(buf, MAX_BUF, in)) && !done) {
  417.     ss = NULL;
  418.     if (strncmpi(buf, "subject:", 8) == 0) {
  419.       if (_fstrstr(buf, "FILE TRANSFER")) {
  420.         ss = strtok(buf, "@");
  421.         if (ss) {
  422.           ss = strtok(NULL, " ");
  423.           strcpy(netname, ss);
  424.           ss = strtok(NULL, ":");
  425.           if (ss) {
  426.             ss = strtok(NULL, "\r\n");
  427.             if (ss) {
  428.               strcpy(fdlfn, ss);
  429.               trimstr1(fdlfn);
  430.             }
  431.           }
  432.         }
  433.       }
  434.     } else
  435.       if (strncmpi(buf, "Description:", 12) == 0) {
  436.       ss = strtok(buf, " ");
  437.       if (ss) {
  438.         ss = strtok(NULL, "\r\n");
  439.         strcpy(fdldesc, ss);
  440.       }
  441.     } else
  442.       if (strncmpi(buf, "FDL Type:", 9) == 0) {
  443.       ss = strtok(buf, ":");
  444.       if (ss) {
  445.         ss = strtok(NULL, "\r\n");
  446.         trimstr1(ss);
  447.         fdltype = atoi(ss);
  448.       }
  449.     } else
  450.       if (strncmpi(buf, "Originating System:", 19) == 0) {
  451.       ss = strtok(buf, ":");
  452.       if (ss) {
  453.         ss = strtok(NULL, "\r\n");
  454.         trimstr1(ss);
  455.         strcpy(fdlupby, ss);
  456.       }
  457.     } else
  458.       if (strncmpi(buf, "begin 600", 9) == 0)
  459.       done = 1;
  460.   }
  461.   return;
  462. }
  463.  
  464. void cd_to(char *s)
  465. {
  466.   char s1[81];
  467.   int i, db;
  468.  
  469.   strcpy(s1, s);
  470.   i = strlen(s1) - 1;
  471.   db = (s1[i] == '\\');
  472.   if (i == 0)
  473.     db = 0;
  474.   if ((i == 2) && (s1[1] == ':'))
  475.     db = 0;
  476.   if (db)
  477.     s1[i] = 0;
  478.   chdir(s1);
  479.   if (s[1] == ':') {
  480.     setdisk(s[0] - 'A');
  481.     if (s[2] == 0)
  482.       chdir("\\");
  483.   }
  484. }
  485.  
  486. void get_dir(char *s, int be)
  487. {
  488.   strcpy(s, "X:\\");
  489.   s[0] = 'A' + getdisk();
  490.   getcurdir(0, &(s[3]));
  491.   if (be) {
  492.     if (s[strlen(s) - 1] != '\\')
  493.       strcat(s, "\\");
  494.   }
  495. }
  496.  
  497. int exist(char *s)
  498. {
  499.   int i;
  500.   struct ffblk ff;
  501.  
  502.   i = findfirst(s, &ff, FA_HIDDEN);
  503.   if (i)
  504.     return (0);
  505.   else
  506.     return (1);
  507. }
  508.  
  509. int copyfile(int display, char *infile, char *outfile)
  510. {
  511.   int f1, f2, i;
  512.   char *b, s[181], s1[21], s2[81], s3[81];
  513.   struct ftime ft;
  514.  
  515.   if ((strcmp(infile, outfile) != 0) && (exist(infile))) {
  516.     if (exist(outfile)) {
  517.       fnsplit(outfile, s3, s2, s1, NULL);
  518.       i = 0;
  519.       sprintf(s, "%s%s%s.%03d", s3, s2, s1, i);
  520.       while (exist(s))
  521.         sprintf(s, "%s%s%s.%03d", s3, s2, s1, ++i);
  522.       strcpy(outfile, s);
  523.     }
  524.     fnsplit(outfile, NULL, NULL, s3, s2);
  525.     if (display)
  526.       output(" as %s%s", s3, s2);
  527.     if ((b = (char *) farmalloc(16400)) == NULL)
  528.       return 1;
  529.     f1 = open(infile, O_RDONLY | O_BINARY, S_IREAD | S_IWRITE);
  530.     if (!f1) {
  531.       farfree(b);
  532.       return 1;
  533.     }
  534.     getftime(f1, &ft);
  535.     f2 = open(outfile, O_RDWR | O_BINARY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
  536.     if (!f2) {
  537.       farfree(b);
  538.       close(f1);
  539.       return 1;
  540.     }
  541.     i = read(f1, (void *) b, 16384);
  542.     while (i > 0) {
  543.       write(f2, (void *) b, i);
  544.       i = read(f1, (void *) b, 16384);
  545.     }
  546.     f1 = close(f1);
  547.     setftime(f2, &ft);
  548.     f2 = close(f2);
  549.     farfree(b);
  550.   }
  551.   return 0;
  552. }
  553.  
  554. char *stripfn(char *fn)
  555. {
  556.   static char ofn[15];
  557.   int i, i1;
  558.   char s[81];
  559.  
  560.   i1 = -1;
  561.   for (i = 0; i < strlen(fn); i++)
  562.     if ((fn[i] == '\\') || (fn[i] == ':') || (fn[i] == '/'))
  563.       i1 = i;
  564.   if (i1 != -1)
  565.     strcpy(s, &(fn[i1 + 1]));
  566.   else
  567.     strcpy(s, fn);
  568.   for (i = 0; i < strlen(s); i++)
  569.     if ((s[i] >= 'A') && (s[i] <= 'Z'))
  570.       s[i] = s[i] - 'A' + 'a';
  571.   i = 0;
  572.   while (s[i] != 0) {
  573.     if (s[i] == 32)
  574.       strcpy(&s[i], &s[i + 1]);
  575.     else
  576.       ++i;
  577.   }
  578.   strcpy(ofn, s);
  579.  
  580.   return (ofn);
  581. }
  582.  
  583. void ssm(char *s)
  584. {
  585.   int f, i, i1;
  586.   char s1[161];
  587.   shortmsgrec sm;
  588.  
  589.   sprintf(s1, "%sSMW.DAT", syscfg.datadir);
  590.   f = sh_open(s1, O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
  591.   if (f < 0)
  592.     return;
  593.   i = (int) (filelength(f) / sizeof(shortmsgrec));
  594.   i1 = i - 1;
  595.   if (i1 >= 0) {
  596.     sh_lseek(f, ((long) (i1)) * sizeof(shortmsgrec), SEEK_SET);
  597.     sh_read(f, (void *) &sm, sizeof(shortmsgrec));
  598.     while ((sm.tosys == 0) && (sm.touser == 0) && (i1 > 0)) {
  599.       --i1;
  600.       sh_lseek(f, ((long) (i1)) * sizeof(shortmsgrec), SEEK_SET);
  601.       sh_read(f, (void *) &sm, sizeof(shortmsgrec));
  602.     }
  603.     if ((sm.tosys) || (sm.touser))
  604.       ++i1;
  605.   } else
  606.     i1 = 0;
  607.   sm.tosys = 0;
  608.   sm.touser = 1;
  609.   strncpy(sm.message, s, 80);
  610.   sm.message[80] = 0;
  611.   sh_lseek(f, ((long) (i1)) * sizeof(shortmsgrec), SEEK_SET);
  612.   sh_write(f, (void *) &sm, sizeof(shortmsgrec));
  613.   sh_close(f);
  614. }
  615.  
  616. int do_it(char *cl)
  617. {
  618.   int i, i1, l, result;
  619.   char s[101], *ss[20];
  620.  
  621.   strcpy(s, cl);
  622.   ss[0] = s;
  623.   i = 1;
  624.   l = strlen(s);
  625.   for (i1 = 1; i1 < l; i1++)
  626.     if (s[i1] == 32) {
  627.       s[i1] = 0;
  628.       ss[i++] = &(s[i1 + 1]);
  629.     }
  630.   ss[i] = NULL;
  631.   result = spawnvpe(P_WAIT, ss[0], ss, xenviron);
  632.   return result;
  633. }
  634.  
  635. void align(char *s)
  636. {
  637.   char f[40], e[40], s1[20], *s2;
  638.   int i, i1, i2;
  639.  
  640.   i1 = 0;
  641.   if (s[0] == '.')
  642.     i1 = 1;
  643.   for (i = 0; i < strlen(s); i++)
  644.     if ((s[i] == '\\') || (s[i] == '/') || (s[i] == ':') || (s[i] == '<') ||
  645.         (s[i] == '>') || (s[i] == '|'))
  646.       i1 = 1;
  647.   if (i1) {
  648.     strcpy(s, "!");
  649.     return;
  650.   }
  651.   s2 = _fstrchr(s, '.');
  652.   if (s2 == NULL) {
  653.     e[0] = 0;
  654.   } else {
  655.     strcpy(e, &(s2[1]));
  656.     e[3] = 0;
  657.     s2[0] = 0;
  658.   }
  659.   strcpy(f, s);
  660.  
  661.   for (i = strlen(f); i < 8; i++)
  662.     f[i] = 32;
  663.   f[8] = 0;
  664.   i1 = 0;
  665.   i2 = 0;
  666.   for (i = 0; i < 8; i++) {
  667.     if (f[i] == '*')
  668.       i1 = 1;
  669.     if (f[i] == ' ')
  670.       i2 = 1;
  671.     if (i2)
  672.       f[i] = ' ';
  673.     if (i1)
  674.       f[i] = '?';
  675.   }
  676.  
  677.   for (i = strlen(e); i < 3; i++)
  678.     e[i] = 32;
  679.   e[3] = 0;
  680.   i1 = 0;
  681.   for (i = 0; i < 3; i++) {
  682.     if (e[i] == '*')
  683.       i1 = 1;
  684.     if (i1)
  685.       e[i] = '?';
  686.   }
  687.  
  688.   for (i = 0; i < 12; i++)
  689.     s1[i] = 32;
  690.   strcpy(s1, f);
  691.   s1[8] = '.';
  692.   strcpy(&(s1[9]), e);
  693.   strcpy(s, s1);
  694.   strupr(s);
  695. }
  696.  
  697.  
  698. void stuff_in(char *s, char *s1, char *f1, char *f2, char *f3, char *f4, char *f5)
  699. {
  700.   int r = 0, w = 0;
  701.  
  702.   while (s1[r] != 0) {
  703.     if (s1[r] == '%') {
  704.       ++r;
  705.       s[w] = 0;
  706.       switch (s1[r]) {
  707.         case '1':
  708.           strcat(s, f1);
  709.           break;
  710.         case '2':
  711.           strcat(s, f2);
  712.           break;
  713.         case '3':
  714.           strcat(s, f3);
  715.           break;
  716.         case '4':
  717.           strcat(s, f4);
  718.           break;
  719.         case '5':
  720.           strcat(s, f5);
  721.           break;
  722.       }
  723.       w = strlen(s);
  724.       r++;
  725.     } else
  726.       s[w++] = s1[r++];
  727.   }
  728.   s[w] = 0;
  729. }
  730.  
  731. char upcase(char ch)
  732. {
  733.   if ((ch > '`') && (ch < '{'))
  734.     ch = ch - 32;
  735.   return (ch);
  736. }
  737.  
  738.  
  739. char *make_abs_cmd(char *out)
  740. {
  741.   char s[161], s1[161], s2[161], *ss, *ss1;
  742.   char *exts[] = {"", ".COM", ".EXE", ".BAT", 0};
  743.   int i;
  744.  
  745.   strcpy(s1, out);
  746.  
  747.   if (s1[1] == ':') {
  748.     if (s1[2] != '\\') {
  749.       getcurdir(upcase(s1[0]) - 'A' + 1, s);
  750.       sprintf(out, "%c:\\%s\\%s", s1[0], s, s1 + 2);
  751.     }
  752.     goto got_cmd;
  753.   }
  754.   if (out[0] == '\\') {
  755.     sprintf(out, "%c:%s", maindir[0], s1);
  756.     goto got_cmd;
  757.   }
  758.   ss = _fstrchr(s1, ' ');
  759.   if (ss) {
  760.     *ss = 0;
  761.     sprintf(s2, " %s", ss + 1);
  762.   } else {
  763.     s2[0] = 0;
  764.   }
  765.   for (i = 0; exts[i]; i++) {
  766.     if (i == 0) {
  767.       ss1 = _fstrrchr(s1, '\\');
  768.       if (!ss1)
  769.         ss1 = s1;
  770.       if (_fstrchr(ss1, '.') == 0)
  771.         continue;
  772.     }
  773.     sprintf(s, "%s%s", s1, exts[i]);
  774.     if (exist(s)) {
  775.       sprintf(out, "%s%s%s", maindir, s, s2);
  776.       goto got_cmd;
  777.     } else {
  778.       ss1 = searchpath(s);
  779.       if (ss1) {
  780.         sprintf(out, "%s%s", ss1, s2);
  781.         goto got_cmd;
  782.       }
  783.     }
  784.   }
  785.  
  786.   sprintf(out, "%s%s%s", maindir, s1, s2);
  787.  
  788. got_cmd:
  789.   return (out);
  790. }
  791.  
  792.  
  793. void get_arc_cmd(char *out, char *arcfn, int cmd, char *ofn)
  794. {
  795.   char *ss, s[161];
  796.   int i;
  797.  
  798.   out[0] = 0;
  799.   ss = _fstrchr(arcfn, '.');
  800.   if (ss == NULL)
  801.     return;
  802.   ++ss;
  803.   for (i = 0; i < 4; i++)
  804.     if (stricmp(ss, syscfg.arcs[i].extension) == 0) {
  805.       switch (cmd) {
  806.         case 0:
  807.           strcpy(s, syscfg.arcs[i].arcl);
  808.           break;
  809.         case 1:
  810.           strcpy(s, syscfg.arcs[i].arce);
  811.           break;
  812.         case 2:
  813.           strcpy(s, syscfg.arcs[i].arca);
  814.           break;
  815.       }
  816.       if (s[0] == 0)
  817.         return;
  818.       stuff_in(out, s, arcfn, ofn, "", "", "");
  819.       make_abs_cmd(out);
  820.       return;
  821.     }
  822. }
  823.  
  824. void add_extended_description(char *fn, char *desc)
  825. {
  826.   ext_desc_type ed;
  827.   int f;
  828.  
  829.   strcpy(ed.name, fn);
  830.   ed.len = strlen(desc);
  831.   f = sh_open(edlfn, O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
  832.   sh_lseek(f, 0L, SEEK_END);
  833.   sh_write(f, &ed, sizeof(ext_desc_type));
  834.   sh_write(f, desc, ed.len);
  835.   f = sh_close(f);
  836. }
  837.  
  838.  
  839.  
  840. int get_file_idz(uploadsrec * u, directoryrec * d)
  841. {
  842.   char *b, *ss, cmd[255], cmdline[255], s3[121], s4[121];
  843.   int f, i, ok = 0;
  844.  
  845.   ss = _fstrchr(stripfn(u->filename), '.');
  846.   if (ss == NULL)
  847.     return 1;
  848.   ++ss;
  849.   for (i = 0; i < 4 && !ok; i++)
  850.     if (!ok)
  851.       ok = (stricmp(ss, syscfg.arcs[i].extension) == 0);
  852.   if (!ok)
  853.     return 1;
  854.   sprintf(s4, "%sFILE_ID.DIZ", temp_dir);
  855.   unlink(s4);
  856.   sprintf(s4, "%sDESC.SDI", temp_dir);
  857.   unlink(s4);
  858.   strcpy(s3, d->path);
  859.   cd_to(s3);
  860.   get_dir(s4, 1);
  861.   strcat(s4, stripfn(u->filename));
  862.   cd_to(maindir);
  863.   get_arc_cmd(cmd, s4, 1, "FILE_ID.DIZ DESC.SDI");
  864.   sprintf(cmdline, "%s", cmd);
  865.   cd_to(temp_dir);
  866.   do_it(cmdline);
  867.   cd_to(maindir);
  868.   sprintf(s4, "%sFILE_ID.DIZ", temp_dir);
  869.   if (!exist(s4))
  870.     sprintf(s4, "%sDESC.SDI", temp_dir);
  871.   if (exist(s4)) {
  872.     stripfn(s4);
  873.     if ((b = (char *) farmalloc((long) MAX_DIZ_LINES * 256 + 1)) == NULL)
  874.       return 1;
  875.     f = sh_open1(s4, O_RDONLY | O_BINARY);
  876.     if (filelength(f) < (MAX_DIZ_LINES * 256)) {
  877.       sh_read(f, b, (int) filelength(f));
  878.       b[(int)filelength(f)] = 0;
  879.     } else {
  880.       sh_read(f, b, (int) MAX_DIZ_LINES * 256);
  881.       b[(int) MAX_DIZ_LINES * 256] = 0;
  882.     }
  883.     sh_close(f);
  884.     ss = strtok(b, "\n");
  885.     if (LAST(ss) == '\r')
  886.       LAST(ss) = '\0';
  887.     sprintf(u->description, "%.58s", ss);
  888.     ss = strtok(NULL, "");
  889.     if (ss) {
  890.       for (i = strlen(ss) - 1; i > 0; i--) {
  891.         if ((ss[i] == 26) || (ss[i] == 12))
  892.           ss[i] = 32;
  893.       }
  894.       add_extended_description(u->filename, ss);
  895.       u->mask |= mask_extended;
  896.     }
  897.     if (b)
  898.       farfree(b);
  899.     sprintf(s4, "%sFILE_ID.DIZ", temp_dir);
  900.     unlink(s4);
  901.     sprintf(s4, "%sDESC.SDI", temp_dir);
  902.     unlink(s4);
  903.   }
  904.   return 0;
  905. }
  906.  
  907.  
  908. static char *fdldate(void)
  909. {
  910.   char *ds;
  911.   struct date today;
  912.  
  913.   ds = NULL;
  914.   getdate(&today);
  915.   sprintf(ds, "%02d/%02d/%02d", today.da_mon, today.da_day, today.da_year - 1900);
  916.   return ds;
  917. }
  918.  
  919. int compare(char *s1, char *s2)
  920. {
  921.   int ok, i;
  922.  
  923.   ok = 1;
  924.   for (i = 0; i < 12; i++)
  925.     if ((s1[i] != s2[i]) && (s1[i] != '?') && (s2[i] != '?'))
  926.       ok = 0;
  927.   return (ok);
  928. }
  929.  
  930. void dliscan(int dn)
  931. {
  932.   int i, f;
  933.   long l;
  934.   uploadsrec u;
  935.   directoryrec d;
  936.  
  937.   d = dirs[dn];
  938.   sprintf(dlfn, "%s%s.DIR", syscfg.datadir, d.filename);
  939.   sprintf(edlfn, "%s%s.EXT", syscfg.datadir, d.filename);
  940.   f = sh_open(dlfn, O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
  941.   i = (int)(filelength(f) / sizeof(uploadsrec));
  942.   if (i == 0) {
  943.     log_it(1, "\n ■ Creating new directory %s.", dlfn);
  944.     memset(&u, 0, sizeof(uploadsrec));
  945.     strcpy(u.filename, "|MARKER|");
  946.     time(&l);
  947.     u.daten = l;
  948.     SETREC(f, 0);
  949.     sh_write(f, (void *) &u, sizeof(uploadsrec));
  950.   } else {
  951.     SETREC(f, 0);
  952.     sh_read(f, (void *) &u, sizeof(uploadsrec));
  953.     if (strcmp(u.filename, "|MARKER|")) {
  954.       numf = (int)u.numbytes;
  955.       memset(&u, 0, sizeof(uploadsrec));
  956.       strcpy(u.filename, "|MARKER|");
  957.       time(&l);
  958.       u.daten = l;
  959.       u.numbytes = numf;
  960.       SETREC(f, 0);
  961.       sh_write(f, &u, sizeof(uploadsrec));
  962.     }
  963.   }
  964.   if (f > -1)
  965.     f = sh_close(f);
  966.   numf = (int)u.numbytes;
  967. }
  968.  
  969.  
  970.  
  971.  
  972. int recno(char *s)
  973. {
  974.   int i, f;
  975.   uploadsrec u;
  976.  
  977.   i = 1;
  978.   if (numf < 1)
  979.     return (-1);
  980.   f = sh_open1(dlfn, O_RDONLY | O_BINARY);
  981.   SETREC(f, i);
  982.   sh_read(f, (void *) &u, sizeof(uploadsrec));
  983.   while ((i < numf) && (compare(s, u.filename) == 0)) {
  984.     ++i;
  985.     SETREC(f, i);
  986.     sh_read(f, (void *) &u, sizeof(uploadsrec));
  987.   }
  988.   f = sh_close(f);
  989.   if (compare(s, u.filename))
  990.     return i;
  991.   else
  992.     return -1;
  993. }
  994.  
  995. char *make_abs_path(unsigned char *checkdir)
  996. {
  997.   char newdir[MAXPATH];
  998.  
  999.   if ((strlen(checkdir) < 3) || (checkdir[1] != ':') || (checkdir[2] != '\\')) {
  1000.     cd_to(maindir);
  1001.     cd_to(checkdir);
  1002.     if (LAST(checkdir) == '\\')
  1003.       get_dir(newdir, 1);
  1004.     else
  1005.       get_dir(newdir, 0);
  1006.     cd_to(maindir);
  1007.     strcpy(checkdir, newdir);
  1008.   }
  1009.   return checkdir;
  1010. }
  1011.  
  1012. void upload_file(int dn, char *fn, char *desc, char *upby)
  1013. {
  1014.   char s1[MAXPATH], ff[MAXPATH], temppath[MAXPATH];
  1015.   int f;
  1016.   long l;
  1017.   directoryrec d;
  1018.   uploadsrec u, u1;
  1019.  
  1020.   d = dirs[dn];
  1021.  
  1022.   strcpy(u.filename, fn);
  1023.   align(u.filename);
  1024.   u.ownerusr = 1;
  1025.   u.ownersys = 0;
  1026.   u.numdloads = 0;
  1027.   u.filetype = 0;
  1028.   u.mask = 0;
  1029.   strcpy(temppath, d.path);
  1030.   make_abs_path(temppath);
  1031.   sprintf(ff, "%s%s", temppath, fn);
  1032.   f = sh_open1(ff, O_RDONLY | O_BINARY);
  1033.   l = filelength(f);
  1034.   u.numbytes = l;
  1035.   sh_close(f);
  1036.   strcpy(u.upby, upby);
  1037.   strcpy(u.date, fdldate());
  1038.   strcpy(u.description, desc);
  1039.   time(&l);
  1040.   u.daten = l;
  1041.   get_file_idz(&u, &d);
  1042.   f = sh_open(dlfn, O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
  1043.   SETREC(f, 0);
  1044.   sh_read(f, &u1, sizeof(uploadsrec));
  1045.   numf = (int)u1.numbytes;
  1046.   ++numf;
  1047.   SETREC(f, numf);
  1048.   sh_write(f, (void *) &u, sizeof(uploadsrec));
  1049.   SETREC(f, 0);
  1050.   sh_read(f, &u1, sizeof(uploadsrec));
  1051.   u1.numbytes = numf;
  1052.   u1.daten = l;
  1053.   SETREC(f, 0);
  1054.   sh_write(f, (void *) &u1, sizeof(uploadsrec));
  1055.   f = sh_close(f);
  1056.   sprintf(s1, "FDL : %s uploaded on %s", u.filename, d.name);
  1057.   ssm(s1);
  1058.   log_it(1, "\n ■ %s", s1);
  1059. }
  1060.  
  1061. void free_dirs(void)
  1062. {
  1063.   if (dirs != NULL)
  1064.     farfree(dirs);
  1065.   dirs = NULL;
  1066. }
  1067.  
  1068.  
  1069. void name_file(int msg, char *newname)
  1070. {
  1071.   int ok;
  1072.   struct stat info;
  1073.   int i;
  1074.  
  1075.   ok = 0;
  1076.   for (i = 0; ((i < 10000) && (!ok)); i++) {
  1077.     sprintf(newname, "%s%s\\%s-%04.04d.MSG",
  1078.             netdata, msg ? "CHECKNET" : "SPOOL",
  1079.             msg ? "CHK" : "UNK", i);
  1080.     if (stat(newname, &info) == -1)
  1081.       ok = 1;
  1082.   }
  1083. }
  1084.  
  1085. void move_bad(char *src)
  1086. {
  1087.   char dest[MAXPATH], file[12], ext[5];
  1088.   int i = 0;
  1089.  
  1090.   fnsplit(src, NULL, NULL, file, ext);
  1091.  
  1092.   sprintf(dest, "%sCHECKNET\\%s%s", netdata, file, ext);
  1093.   while (exist(dest))
  1094.     sprintf(dest, "%sCHECKNET\\%s.%03d", netdata, file, i++);
  1095.  
  1096.   if (copyfile(0, src, dest))
  1097.     unlink(src);
  1098. }
  1099.  
  1100. int init(void)
  1101. {
  1102.   int i, i1, f;
  1103.   char fn[MAXPATH];
  1104.  
  1105.   i = i1 = 0;
  1106.   while (environ[i] != NULL) {
  1107.     xenviron[i1++] = environ[i];
  1108.     ++i;
  1109.   }
  1110.   xenviron[i1] = NULL;
  1111.  
  1112.   sprintf(fn, "%sCONFIG.DAT", maindir);
  1113.   f = sh_open1(fn, O_RDONLY | O_BINARY);
  1114.   if (f < 0) {
  1115.     output("\n ■ Unable to read %s.", fn);
  1116.     return 1;
  1117.   }
  1118.   sh_read(f, (void *) (&syscfg), sizeof(configrec));
  1119.   sh_close(f);
  1120.   return 0;
  1121. }
  1122.  
  1123. int match_net(char *fn)
  1124. {
  1125.   char line[161];
  1126.   int found = 0;
  1127.   FILE *fp;
  1128.  
  1129.   if ((fp = fsh_open(fn, "rt")) != NULL) {
  1130.     while (!feof(fp) && (!found)) {
  1131.       fgets(line, 160, fp);
  1132.       if (strnicmp(line, "NET: ", 5) == 0) {
  1133.         strcpy(net_pkt, &line[5]);
  1134.         trimstr1(net_pkt);
  1135.         found = 1;
  1136.       }
  1137.     }
  1138.     fclose(fp);
  1139.   }
  1140.   return found;
  1141. }
  1142.  
  1143. int read_networks(void)
  1144. {
  1145.   int f, max;
  1146.   char s[121];
  1147.  
  1148.   sprintf(s, "%sNETWORKS.DAT", syscfg.datadir);
  1149.   f = sh_open1(s, O_RDONLY | O_BINARY);
  1150.   max = 0;
  1151.   if (f > 0)
  1152.     max = (int) (filelength(f) / sizeof(net_networks_rec));
  1153.   sh_close(f);
  1154.   return max;
  1155. }
  1156.  
  1157. void set_net_num(int n)
  1158. {
  1159.   char s[121];
  1160.   int f;
  1161.  
  1162.   sprintf(s, "%sNETWORKS.DAT", syscfg.datadir);
  1163.   f = sh_open1(s, O_RDONLY | O_BINARY);
  1164.   if (f < 0)
  1165.     return;
  1166.   lseek(f, ((long) (n)) * sizeof(net_networks_rec), SEEK_SET);
  1167.   sh_read(f, &netcfg, sizeof(net_networks_rec));
  1168.   close(f);
  1169.   net_num = n;
  1170.   net_sysnum = netcfg.sysnum;
  1171.   strcpy(net_name, netcfg.name);
  1172.   strcpy(net_data, make_abs_path(netcfg.dir));
  1173.   if (LAST(net_data) != '\\')
  1174.     strcat(net_data, "\\");
  1175. }
  1176.  
  1177. main(int argc, char *argv[])
  1178. {
  1179.   char outname[181], buf[MAXPATH], dirfn[21], dirpath[MAXPATH], junk[80];
  1180.   char *ss, argfile[21], s1[181], s2[181], s3[21], s4[21], s5[21], fn[MAXPATH];
  1181.   int i, f, ok, found, ok1, dn, result, num_dirs, net_num_max;
  1182.   long l;
  1183.   unsigned short dirtype;
  1184.   FILE *in, *out, *fp;
  1185.  
  1186.   detect_multitask();
  1187.  
  1188.   ok1 = 0;
  1189.  
  1190.   if (argc == 4 || argc == 5) {
  1191.     if (strcmpi(argv[1], "-encode") == 0 && argc == 4) {
  1192.       if (((in = fsh_open(argv[2], "r")) != NULL) && ((out = fsh_open(argv[3], "at+")) != NULL)) {
  1193.         output(" %ld bytes.", uuencode(in, out, argv[2]));
  1194.         if (in != NULL)
  1195.           fclose(in);
  1196.         if (out != NULL)
  1197.           fclose(out);
  1198.         ok1 = EXIT_SUCCESS;
  1199.       } else
  1200.         ok1 = EXIT_FAILURE;
  1201.     } else
  1202.       if (strcmpi(argv[1], "-decode") == 0 && argc == 5) {
  1203.       strcpy(argfile, argv[2]);
  1204.       strcpy(temp_dir, argv[3]);
  1205.       strcpy(netdata, argv[4]);
  1206.       strcpy(maindir, argv[0]);
  1207.       while(LAST(maindir) != '\\')
  1208.         LAST(maindir) = 0;
  1209.       if (init()) {
  1210.         log_it(1, "\n ■ Failed to initialize UU variables.");
  1211.         ok1 = EXIT_FAILURE;
  1212.       }
  1213.       cd_to(temp_dir);
  1214.  
  1215.       if ((in = fsh_open(argfile, "r")) != NULL) {
  1216.         netname[0] = 0;
  1217.         fdldesc[0] = 0;
  1218.         fdltype = 0;
  1219.         fdlfn[0] = 0;
  1220.         scanfdl(in);
  1221.         rewind(in);
  1222.         scanfor("begin", in);
  1223.         result = uudecode(in, temp_dir, outname);
  1224.         if (in != NULL)
  1225.           fclose(in);
  1226.         switch (result) {
  1227.           case UU_NO_MEM:
  1228.             sprintf(s1, "%s%s", temp_dir, argfile);
  1229.             move_bad(s1);
  1230.             cd_to(maindir);
  1231.             sprintf(s1, "Error decoding %s.", argfile);
  1232.             log_it(1, "\n ■ %s", s1);
  1233.             ssm(s1);
  1234.             sprintf(s1, "%s%s", netdata, outname);
  1235.             unlink(s1);
  1236.             ok1 = EXIT_FAILURE;
  1237.             break;
  1238.           case UU_BAD_BEGIN:
  1239.             sprintf(s1, "%s%s", temp_dir, argfile);
  1240.             name_file(0, s2);
  1241.             if (copyfile(0, s1, s2))
  1242.               move_bad(s2);
  1243.             else
  1244.               unlink(s1);
  1245.             cd_to(maindir);
  1246.             sprintf(s1, "%s%s", netdata, outname);
  1247.             unlink(s1);
  1248.             ok1 = EXIT_FAILURE;
  1249.             break;
  1250.           case UU_CANT_OPEN:
  1251.             log_it(1, "\n ■ Cannot open %s%s", netdata, outname);
  1252.             cd_to(maindir);
  1253.             ok1 = EXIT_FAILURE;
  1254.             break;
  1255.           case UU_CHECKSUM:
  1256.             sprintf(s1, "%s%s", temp_dir, argfile);
  1257.             move_bad(s1);
  1258.             cd_to(maindir);
  1259.             sprintf(s1, "Checksum error decoding %s - moved to CHECKNET!", argfile);
  1260.             log_it(1, "\n ■ %s", s1);
  1261.             ssm(s1);
  1262.             sprintf(s1, "%s%s", netdata, outname);
  1263.             unlink(s1);
  1264.             ok1 = EXIT_FAILURE;
  1265.             break;
  1266.           case UU_BAD_END:
  1267.             sprintf(s1, "%s%s", temp_dir, argfile);
  1268.             move_bad(s1);
  1269.             cd_to(maindir);
  1270.             sprintf(s1, "No \'end\' found in %s - moved to CHECKNET!", argfile);
  1271.             log_it(1, "\n ■ %s", s1);
  1272.             ssm(s1);
  1273.             sprintf(s1, "%s%s", netdata, outname);
  1274.             unlink(s1);
  1275.             ok1 = EXIT_FAILURE;
  1276.             break;
  1277.           case UU_SUCCESS:
  1278.             sprintf(s1, "%s%s", temp_dir, argfile);
  1279.             found = (match_net(s1));
  1280.             unlink(s1);
  1281.             stripfn(outname);
  1282.             fnsplit(outname, NULL, NULL, s4, s3);
  1283.             sprintf(s5, "%s%s", s4, s3);
  1284.             if ((strcmpi(s3, ".NET") == NULL) && (strncmpi(argfile, "PKT", 3) == NULL)) {
  1285.               if (found) {
  1286.                 found = 0;
  1287.                 net_num_max = read_networks();
  1288.                 for (i = 0; ((i < net_num_max) && (!found)); i++) {
  1289.                   set_net_num(i);
  1290.                   if (strnicmp(net_name, net_pkt, strlen(net_name)) == 0) {
  1291.                     strcpy(netname, net_name);
  1292.                     strcpy(netdata, net_data);
  1293.                     found = 1;
  1294.                   }
  1295.                 }
  1296.                 if (!found) {
  1297.                   strcat(netdata, "CHECKNET\\");
  1298.                   log_it(1, "\n ! Unknown network %s in %s - moved to CHECKNET.", net_pkt, s5);
  1299.                 }
  1300.               } else
  1301.                 log_it(0, "\n ■ No network specified in %s packet.", s5);
  1302.               i = 0;
  1303.               sprintf(s2, "%sP1-%03d.NET", netdata, i);
  1304.               while (exist(s2))
  1305.                 sprintf(s2, "%sP1-%03d.NET", netdata, ++i);
  1306.               sprintf(s1, "%s%s", temp_dir, stripfn(outname));
  1307.               if (copyfile(0, s1, s2)) {
  1308.                 log_it(1, "\n ■ Error creating %s", s2);
  1309.                 cd_to(maindir);
  1310.                 ok1 = EXIT_FAILURE;
  1311.                 break;
  1312.               } else
  1313.                 unlink(s1);
  1314.             } else {
  1315.               if (fdltype)
  1316.                 sprintf(s1, "Received : %s (FDL %hu)", s5, fdltype);
  1317.               else
  1318.                 sprintf(s1, "Received transferred file : %s.", s5);
  1319.               ssm(s1);
  1320.               log_it(1, "\n ■ %s", s1);
  1321.               num_dirs = 0;
  1322.               dirs = (directoryrec huge *) mallocx(((long) syscfg.max_dirs) *
  1323.                   ((long) sizeof(directoryrec)));
  1324.               if (dirs == NULL)
  1325.                 log_it(0, "\n - Insufficient memory to read directories!");
  1326.               else {
  1327.                 sprintf(fn, "%sDIRS.DAT", syscfg.datadir);
  1328.                 f = sh_open1(fn, O_RDONLY | O_BINARY);
  1329.                 if (f < 0)
  1330.                   log_it(1, "\n ! Unable to read %s", fn);
  1331.                 else {
  1332.                   num_dirs = (int) huge_xfer(f, dirs, sizeof(directoryrec),
  1333.                                        syscfg.max_dirs, 0) / sizeof(directoryrec);
  1334.                   log_it(0, "\n - Total dirs %d", num_dirs);
  1335.                   if (num_dirs)
  1336.                     l = filelength(f) / num_dirs;
  1337.                   if (l != sizeof(directoryrec))
  1338.                     num_dirs = 0;
  1339.                 }
  1340.                 f =sh_close(f);
  1341.               }
  1342.               sprintf(s1, "%s%s", temp_dir, s5);
  1343.               if (num_dirs) {
  1344.                 dn = 0;
  1345.                 if (fdltype) {
  1346.                   for (i = 0; (i < num_dirs && !dn); i++) {
  1347.                     if (dirs[i].type == fdltype) {
  1348.                       dn = i;
  1349.                       break;
  1350.                     }
  1351.                   }
  1352.                   if (!dn) {
  1353.                     log_it(1, "\n ■ FDL %hu not in DIREDIT... checking FDLFTS.CFG.",
  1354.                            fdltype);
  1355.                     sprintf(fn, "%sFDLFTS.CFG", netdata);
  1356.                     if ((fp = fsh_open(fn, "rt")) == NULL) {
  1357.                       log_it(1, "\n ■ FDLFTS.CFG does not exist... uploading to Sysop.");
  1358.                     } else {
  1359.                       ok = 0;
  1360.                       while ((fgets(buf, 80, fp)) && !ok) {
  1361.                         if (buf[0] == ':')
  1362.                           continue;
  1363.                         if (strncmpi(buf, "FDL", 3) == 0) {
  1364.                           sscanf(buf, "%s %hu %s %s", junk, &dirtype, dirfn, dirpath);
  1365.                           trimstr1(dirfn);
  1366.                           if (dirtype == fdltype) {
  1367.                             for (i = 0; i < num_dirs; i++) {
  1368.                               if (strnicmp((char *)dirs[i].filename, dirfn, strlen(dirfn)) == 0) {
  1369.                                 dn = i;
  1370.                                 ok = 1;
  1371.                                 break;
  1372.                               }
  1373.                             }
  1374.                           }
  1375.                         }
  1376.                       }
  1377.                       if (fp != NULL)
  1378.                         fclose(fp);
  1379.                     }
  1380.                   }
  1381.                   if ((!ok) && (fdltype))
  1382.                     log_it(1, "\n ■ FDL %hu not defined in FDLFTS.CFG... uploading to Sysop.",
  1383.                            fdltype);
  1384.                 } else {
  1385.                   dn = 0;
  1386.                   strcpy(fdldesc, "[No Description Found]");
  1387.                   strcpy(fdlupby, "FILEnet Transfer");
  1388.                   sprintf(fn, "%sFDLFTS.CFG", netdata);
  1389.                   if ((fp = fsh_open(fn, "rt")) == NULL) {
  1390.                     log_it(1, "\n ■ %s does not exist... upload to Sysop.", fn);
  1391.                   } else {
  1392.                     ok = 0;
  1393.                     while ((fgets(buf, 80, fp)) && !ok) {
  1394.                       if (strncmpi(buf, "NOREQUEST_DIR", 13) == 0) {
  1395.                         ss = strtok(buf, " ");
  1396.                         if (ss) {
  1397.                           ss = strtok(NULL, "\r\n");
  1398.                           if (ss) {
  1399.                             strcpy(dirfn, ss);
  1400.                             trimstr1(dirfn);
  1401.                             ok = 1;
  1402.                             for (i = 0; i < num_dirs; i++) {
  1403.                               if (strnicmp(dirs[i].filename, dirfn, strlen(dirfn)) == 0) {
  1404.                                 dn = i;
  1405.                                 break;
  1406.                               }
  1407.                             }
  1408.                           }
  1409.                         }
  1410.                       }
  1411.                     }
  1412.                     if (fp != NULL)
  1413.                       fclose(fp);
  1414.                   }
  1415.                   if (!ok)
  1416.                     log_it(1, "\n ■ NOREQUEST_DIR not defined in %s.", fn);
  1417.                 }
  1418.                 sprintf(s1, "%s%s", temp_dir, s5);
  1419.                 strcpy(buf, (char *)dirs[dn].path);
  1420.                 make_abs_path(buf);
  1421.                 sprintf(s2, "%s%s", buf, s5);
  1422.                 if (exist(s2)) {
  1423.                   log_it(1, "\n ■ %s already exists", s2);
  1424.                   move_bad(s1);
  1425.                 } else {
  1426.                   if (copyfile(0, s1, s2)) {
  1427.                     log_it(1, "\n ■ Error copying %s", s2);
  1428.                     move_bad(s1);
  1429.                   } else {
  1430.                     dliscan(dn);
  1431.                     strcpy(fn, s5);
  1432.                     align(fn);
  1433.                     ok = recno(fn);
  1434.                     if (ok == -1)
  1435.                       upload_file(dn, s5, fdldesc, fdlupby);
  1436.                     else
  1437.                       log_it(1, "\n ■ %s already in %s.", fn, dirs[dn].name);
  1438.                   }
  1439.                 }
  1440.                 unlink(s1);
  1441.               } else {
  1442.                 log_it(1, "\n ■ Moving %s to CHECKNET", s1);
  1443.                 move_bad(s1);
  1444.               }
  1445.               fdltype = 0;
  1446.               fdlfn[0] = 0;
  1447.               fdldesc[0] = 0;
  1448.               free_dirs();
  1449.             }
  1450.             cd_to(maindir);
  1451.             ok1 = EXIT_SUCCESS;
  1452.             break;
  1453.           default:
  1454.             sprintf(s1, "%s%s", temp_dir, argfile);
  1455.             name_file(1, s2);
  1456.             log_it(1, "\n ■ Unknown error %s...", s2);
  1457.             if (copyfile(1, s1, s2))
  1458.               log_it(1, "\n ■ Error during copy...");
  1459.             else
  1460.               unlink(s1);
  1461.             cd_to(maindir);
  1462.             sprintf(s1, "Unknown error %s...", s2);
  1463.             ssm(s1);
  1464.             ok1 = EXIT_FAILURE;
  1465.             break;
  1466.         }
  1467.       } else {
  1468.         cd_to(maindir);
  1469.         log_it(1, "\n ■ Input file %s not found.", argfile);
  1470.         ok1 = EXIT_FAILURE;
  1471.       }
  1472.       }
  1473.   } else
  1474.     output("\n ■ %s\n", version);
  1475.   if (in != NULL)
  1476.     fclose(in);
  1477.   if (out != NULL)
  1478.     fclose(out);
  1479.   return ok1;
  1480. }
  1481.