home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC / SRC15B96.ZIP / PPPUTIL.CPP < prev    next >
Text File  |  1998-11-11  |  8KB  |  344 lines

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <io.h>
  4. #include <string.h>
  5. #include <time.h>
  6. #include <malloc.h>
  7. #include <fcntl.h>
  8. #include <errno.h>
  9. #include <share.h>
  10. #include <dir.h>
  11. #include <sys\stat.h>
  12. #include <dos.h>
  13. #include "vardec.h"
  14. #include "net.h"
  15.  
  16. #define WAIT_TIME 10
  17. #define TRIES 100
  18.  
  19. configrec syscfg;
  20.  
  21. char net_name[16], net_data[MAXPATH];
  22.  
  23. void name_chunk(char *chunkname)
  24. {
  25.   int i, ok;
  26.   struct stat info;
  27.  
  28.   ok = 0;
  29.   for (i = 0; ((i < 1000) && (!ok)); i++) {
  30.     sprintf(chunkname, "%sSPOOL\\UNK-9%3.3d.UUE", net_data, i);
  31.     if (stat(chunkname, &info) == -1)
  32.       ok = 1;
  33.   }
  34. }
  35.  
  36. long sh_lseek(int handle, long offset, int fromwhere)
  37. {
  38.   if (handle == -1) {
  39.     return (-1L);
  40.   }
  41.   return (lseek(handle, offset, fromwhere));
  42. }
  43.  
  44. FILE *fsh_open(char *path, char *fmode)
  45. {
  46.   FILE *f;
  47.   int count, share, md, fd;
  48.   char drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE], ext[MAXEXT];
  49.  
  50.   share = SH_DENYWR;
  51.   md = 0;
  52.   if (((char *) _fstrchr(fmode, 'w')) != NULL) {
  53.     share = SH_DENYRD;
  54.     md = O_RDWR | O_CREAT | O_TRUNC;
  55.   } else
  56.     if (((char *) _fstrchr(fmode, 'a')) != NULL) {
  57.     share = SH_DENYRD;
  58.     md = O_RDWR | O_CREAT;
  59.   } else {
  60.     md = O_RDONLY;
  61.   }
  62.   if (((char *) _fstrchr(fmode, 'b')) != NULL) {
  63.     md |= O_BINARY;
  64.   }
  65.   if (((char *) _fstrchr(fmode, '+')) != NULL) {
  66.     md &= ~O_RDONLY;
  67.     md |= O_RDWR;
  68.     share = SH_DENYRD;
  69.   }
  70.   fd = open(path, md | share, S_IREAD | S_IWRITE);
  71.   if (fd < 0) {
  72.     count = 1;
  73.     fnsplit(path, drive, dir, file, ext);
  74.     if ((access(path, 0)) != -1) {
  75.       delay(WAIT_TIME);
  76.       fd = open(path, md | share, S_IREAD | S_IWRITE);
  77.       while (((fd < 0) && (errno == EACCES)) && (count < TRIES)) {
  78.         delay(WAIT_TIME);
  79.         count++;
  80.         fd = open(path, md | share, S_IREAD | S_IWRITE);
  81.       }
  82.     }
  83.   }
  84.   if (fd > 0) {
  85.     if (((char *) _fstrchr(fmode, 'a')) != NULL)
  86.       sh_lseek(fd, 0L, SEEK_END);
  87.     f = fdopen(fd, fmode);
  88.     if (!f) {
  89.       close(fd);
  90.     }
  91.   } else
  92.     f = NULL;
  93.   return (f);
  94. }
  95.  
  96. int chunk(char *fn)
  97. {
  98.   char s[255], outfn[MAXPATH], *hdrbuf;
  99.   int done;
  100.   long textlen, curpos;
  101.   size_t hdrlen;
  102.   FILE *in, *out;
  103.  
  104.   fprintf(stderr, "\n ■ Splitting %s into 32K chunks.", fn);
  105.  
  106.   in = fsh_open(fn, "rb");
  107.   if (in == NULL) {
  108.     fprintf(stderr, "\n ! Unable to open %s.", fn);
  109.     return 1;
  110.   }
  111.  
  112.   done = 0;
  113.   do {
  114.     fgets(s, 254, in);
  115.     if (*s == '')
  116.       hdrlen = (size_t) ftell(in);
  117.     else
  118.       done = 1;
  119.   } while (!done);
  120.  
  121.   if ((hdrbuf = (char *) farmalloc(hdrlen + 1)) == NULL) {
  122.     fprintf(stderr, "\n ! Unable to allocate %ld bytes for header.", hdrlen);
  123.     fclose(in);
  124.     return 1;
  125.   }
  126.  
  127.   rewind(in);
  128.   fread((void *) hdrbuf, sizeof(char), hdrlen, in);
  129.  
  130.   curpos = hdrlen;
  131.   done = 0;
  132.   rewind(in);
  133.   while (!done) {
  134.     name_chunk(outfn);
  135.     if ((out = fsh_open(outfn, "wb+")) == NULL) {
  136.       farfree(hdrbuf);
  137.       fclose(in);
  138.       return 1;
  139.     }
  140.     fwrite((void *) hdrbuf, sizeof(char), hdrlen, out);
  141.     fprintf(out, "\n");
  142.     fseek(in, curpos, SEEK_SET);
  143.     textlen = 0L;
  144.     do {
  145.       fgets(s, 254, in);
  146.       textlen += fprintf(out, s);
  147.     } while ((textlen < (32000 - hdrlen)) && (feof(in) == 0));
  148.     if (feof(in) != 0)
  149.       done = 1;
  150.     else {
  151.       fprintf(out, "\n\nContinued in next message...\n");
  152.       curpos = ftell(in);
  153.     }
  154.     if (out != NULL)
  155.       fclose(out);
  156.   }
  157.   if (in != NULL)
  158.     fclose(in);
  159.   if (hdrbuf)
  160.     farfree(hdrbuf);
  161.   return 0;
  162. }
  163.  
  164. void purge_sent(int days)
  165. {
  166.   char s[121];
  167.   int f1, howmany = 0;
  168.   long age;
  169.   struct ffblk ff;
  170.   struct stat fileinfo;
  171.  
  172.   sprintf(s, "%sSENT\\*.*", net_data);
  173.   f1 = findfirst(s, &ff, 0);
  174.   while (f1 == 0) {
  175.     sprintf(s, "%sSENT\\%s", net_data, ff.ff_name);
  176.     if (stat(s, &fileinfo) == 0) {
  177.       age = (time(NULL) - fileinfo.st_atime);
  178.       if (age > (86400L * days)) {
  179.         ++howmany;
  180.         unlink(s);
  181.       }
  182.     }
  183.     f1 = findnext(&ff);
  184.   }
  185.   fprintf(stderr, " %d packet%s deleted.", howmany, howmany == 1 ? "" : "s");
  186. }
  187.  
  188.  
  189. #define MAX_LOG 1000
  190.  
  191. void trim_log(char *ol)
  192. {
  193.   int num_lines, total_lines, kill_lines;
  194.   FILE *old_log, *new_log;
  195.   char nl[MAXPATH], s[160];
  196.  
  197.   sprintf(ol, "%sNEWS.LOG", net_data);
  198.   sprintf(nl, "%sNEWS.ZZZ", net_data);
  199.  
  200.   old_log = fsh_open(ol, "r");
  201.   new_log = fsh_open(nl, "a");
  202.  
  203.   total_lines = 0;
  204.   if (old_log != NULL) {
  205.     while (!(fgets(s, 160, old_log) == NULL))
  206.       ++total_lines;
  207.     rewind(old_log);
  208.     if (total_lines < MAX_LOG) {
  209.       fclose(old_log);
  210.       if (new_log != NULL)
  211.         fclose(new_log);
  212.       unlink(nl);
  213.       return;
  214.     }
  215.     kill_lines = total_lines - MAX_LOG;
  216.     num_lines = 0;
  217.     while ((fgets(s, 160, old_log)) && (num_lines < kill_lines))
  218.       num_lines++;
  219.     while ((_fstrstr(s, "Freeware PPP Project") == NULL) &&
  220.           (num_lines < total_lines)) {
  221.       fgets(s, 160, old_log);
  222.       num_lines++;
  223.     }
  224.     fputs(s, new_log);
  225.     while ((!(fgets(s, 160, old_log) == NULL)))
  226.       fputs(s, new_log);
  227.   }
  228.   if (old_log != NULL)
  229.     fclose(old_log);
  230.   if (new_log != NULL)
  231.     fclose(new_log);
  232.   unlink(ol);
  233.   rename(nl, ol);
  234. }
  235.  
  236.  
  237. #define MAX_LEN 12288L
  238.  
  239. int open_netlog(char *fn)
  240. {
  241.   int f, count = 0;
  242.  
  243.   do {
  244.     f = open(fn, O_RDWR | O_BINARY | SH_DENYRW | O_CREAT, S_IREAD | S_IWRITE);
  245.   } while ((f < 0) && (errno == EACCES) && (count++ < 500));
  246.  
  247.   return (f);
  248. }
  249.  
  250.  
  251. int write_netlog(int sn, long sent, long recd, char *tmused)
  252. {
  253.   int f;
  254.   char *ss, s[101], s1[81], s2[81], fn[121];
  255.   long l;
  256.   struct tm *time_now;
  257.   time_t some;
  258.  
  259.   time(&some);
  260.   time_now = localtime(&some);
  261.   strftime(s1, 35, "%m/%d/%y %H:%M:%S", time_now);
  262.  
  263.   ss = (char *) farmalloc(MAX_LEN + 1024L);
  264.   if (ss == NULL)
  265.     return 1;
  266.   if ((sent) || (recd)) {
  267.     if ((recd) && (!sent))
  268.       sprintf(s2, "       , R:%4ldk,", recd);
  269.     else {
  270.       if ((recd) && (sent))
  271.         sprintf(s2, "S:%4ldk, R:%4ldk,", sent, recd);
  272.       else
  273.         sprintf(s2, "S:%4ldk,         ", sent);
  274.     }
  275.   } else
  276.     strcpy(s2, "                 ");
  277.   sprintf(s, "%s To %5d, %s         %5s min  %s\r\n", s1, sn, s2,
  278.           tmused, net_name);
  279.  
  280.   strcpy(ss, s);
  281.  
  282.   sprintf(fn, "%sNET.LOG", syscfg.gfilesdir);
  283.   f = open_netlog(fn);
  284.   lseek(f, 0L, SEEK_SET);
  285.   l = (long) (read(f, (void *) (&(ss[strlen(s)])), (int) MAX_LEN) + strlen(s));
  286.   while ((l > 0L) && (ss[(int) l] != '\n'))
  287.     --l;
  288.   lseek(f, 0L, SEEK_SET);
  289.   write(f, (void *) ss, (int) l + 1);
  290.   chsize(f, l + 1);
  291.   if (ss) {
  292.     farfree(ss);
  293.     ss = NULL;
  294.   }
  295.   close(f);
  296.   return 0;
  297. }
  298.  
  299. #pragma warn -par
  300. main(int argc, char *argv[])
  301. {
  302.   char s[101];
  303.   unsigned int sy;
  304.   unsigned long sent, recd;
  305.   int i, f;
  306.  
  307.   strcpy(s, "CONFIG.DAT");
  308.   f = open(s, O_RDONLY | O_BINARY);
  309.   if (f < 0)
  310.     return 1;
  311.   read(f, (void *) (&syscfg), sizeof(configrec));
  312.   close(f);
  313.  
  314.   if (strncmp(argv[1], "NETLOG", 6) == 0) {
  315.     strcpy(net_name, argv[6]);
  316.     sy = atoi(argv[2]);
  317.     sent = atol(argv[3]);
  318.     recd = atol(argv[4]);
  319.     if (write_netlog(sy, sent, recd, argv[5]))
  320.       return 1;
  321.   }
  322.  
  323.   if (strncmp(argv[1], "TRIM", 4) == 0) {
  324.     strcpy(net_data, argv[2]);
  325.     sprintf(s, "%s%s", net_data, argv[3]);
  326.     trim_log(s);
  327.   }
  328.  
  329.   if (strncmp(argv[1], "PURGE", 5) == 0) {
  330.     strcpy(net_data, argv[2]);
  331.     i = atoi(argv[3]);
  332.     purge_sent(i);
  333.   }
  334.  
  335.   if (strncmp(argv[1], "CHUNK", 5) == 0) {
  336.     strcpy(net_data, argv[2]);
  337.     sprintf(s, "%sINBOUND\\%s", net_data, argv[3]);
  338.     if (!chunk(s))
  339.       unlink(s);
  340.   }
  341.  
  342.   return 0;
  343. }
  344.