home *** CD-ROM | disk | FTP | other *** search
/ ftp.wwiv.com / ftp.wwiv.com.zip / ftp.wwiv.com / pub / PPPBCKP / SRC / SRC15B98.ZIP / PPPUTIL.CPP < prev    next >
Text File  |  1998-11-16  |  6KB  |  281 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. configrec syscfg;
  17.  
  18. char net_name[16], net_data[MAXPATH];
  19.  
  20. void name_chunk(char *chunkname)
  21. {
  22.   int i, ok;
  23.   struct stat info;
  24.  
  25.   ok = 0;
  26.   for (i = 0; ((i < 1000) && (!ok)); i++) {
  27.     sprintf(chunkname, "%sSPOOL\\UNK-9%3.3d.UUE", net_data, i);
  28.     if (stat(chunkname, &info) == -1)
  29.       ok = 1;
  30.   }
  31. }
  32.  
  33. int chunk(char *fn)
  34. {
  35.   char s[255], outfn[MAXPATH], *hdrbuf;
  36.   int done;
  37.   long textlen, curpos;
  38.   size_t hdrlen;
  39.   FILE *in, *out;
  40.  
  41.   fprintf(stderr, "\n ■ Splitting %s into 32K chunks.", fn);
  42.  
  43.   in = fopen(fn, "rb");
  44.   if (in == NULL) {
  45.     fprintf(stderr, "\n ! Unable to open %s.", fn);
  46.     return 1;
  47.   }
  48.  
  49.   done = 0;
  50.   do {
  51.     fgets(s, 254, in);
  52.     if (*s == '')
  53.       hdrlen = (size_t) ftell(in);
  54.     else
  55.       done = 1;
  56.   } while (!done);
  57.  
  58.   if ((hdrbuf = (char *) farmalloc(hdrlen + 1)) == NULL) {
  59.     fprintf(stderr, "\n ! Unable to allocate %ld bytes for header.", hdrlen);
  60.     fclose(in);
  61.     return 1;
  62.   }
  63.  
  64.   rewind(in);
  65.   fread((void *) hdrbuf, sizeof(char), hdrlen, in);
  66.  
  67.   curpos = hdrlen;
  68.   done = 0;
  69.   rewind(in);
  70.   while (!done) {
  71.     name_chunk(outfn);
  72.     if ((out = fopen(outfn, "wb+")) == NULL) {
  73.       farfree(hdrbuf);
  74.       fclose(in);
  75.       return 1;
  76.     }
  77.     fwrite((void *) hdrbuf, sizeof(char), hdrlen, out);
  78.     fprintf(out, "\n");
  79.     fseek(in, curpos, SEEK_SET);
  80.     textlen = 0L;
  81.     do {
  82.       fgets(s, 254, in);
  83.       textlen += fprintf(out, s);
  84.     } while ((textlen < (32000 - hdrlen)) && (feof(in) == 0));
  85.     if (feof(in) != 0)
  86.       done = 1;
  87.     else {
  88.       fprintf(out, "\r\n\r\nContinued in next message...\r\n");
  89.       curpos = ftell(in);
  90.     }
  91.     if (out != NULL)
  92.       fclose(out);
  93.   }
  94.   if (in != NULL)
  95.     fclose(in);
  96.   if (hdrbuf)
  97.     farfree(hdrbuf);
  98.   return 0;
  99. }
  100.  
  101. void purge_sent(int days)
  102. {
  103.   char s[121];
  104.   int f1, howmany = 0;
  105.   long age;
  106.   struct ffblk ff;
  107.   struct stat fileinfo;
  108.  
  109.   sprintf(s, "%sSENT\\*.*", net_data);
  110.   f1 = findfirst(s, &ff, 0);
  111.   while (f1 == 0) {
  112.     sprintf(s, "%sSENT\\%s", net_data, ff.ff_name);
  113.     if (stat(s, &fileinfo) == 0) {
  114.       age = (time(NULL) - fileinfo.st_atime);
  115.       if (age > (86400L * days)) {
  116.         ++howmany;
  117.         unlink(s);
  118.       }
  119.     }
  120.     f1 = findnext(&ff);
  121.   }
  122.   fprintf(stderr, " %d packet%s deleted.", howmany, howmany == 1 ? "" : "s");
  123. }
  124.  
  125.  
  126. #define MAX_LOG 1000
  127.  
  128. void trim_log(char *ol)
  129. {
  130.   int num_lines, total_lines, kill_lines;
  131.   FILE *old_log, *new_log;
  132.   char nl[MAXPATH], s[160];
  133.  
  134.   sprintf(ol, "%sNEWS.LOG", net_data);
  135.   sprintf(nl, "%sNEWS.ZZZ", net_data);
  136.  
  137.   old_log = fopen(ol, "r");
  138.   new_log = fopen(nl, "a");
  139.  
  140.   total_lines = 0;
  141.   if (old_log != NULL) {
  142.     while (!(fgets(s, 160, old_log) == NULL))
  143.       ++total_lines;
  144.     rewind(old_log);
  145.     if (total_lines < MAX_LOG) {
  146.       fclose(old_log);
  147.       if (new_log != NULL)
  148.         fclose(new_log);
  149.       unlink(nl);
  150.       return;
  151.     }
  152.     kill_lines = total_lines - MAX_LOG;
  153.     num_lines = 0;
  154.     while ((fgets(s, 160, old_log)) && (num_lines < kill_lines))
  155.       num_lines++;
  156.     while ((_fstrstr(s, "Freeware PPP Project") == NULL) &&
  157.           (num_lines < total_lines)) {
  158.       fgets(s, 160, old_log);
  159.       num_lines++;
  160.     }
  161.     fputs(s, new_log);
  162.     while ((!(fgets(s, 160, old_log) == NULL)))
  163.       fputs(s, new_log);
  164.   }
  165.   if (old_log != NULL)
  166.     fclose(old_log);
  167.   if (new_log != NULL)
  168.     fclose(new_log);
  169.   unlink(ol);
  170.   rename(nl, ol);
  171. }
  172.  
  173.  
  174. #define MAX_LEN 12288L
  175.  
  176. int open_netlog(char *fn)
  177. {
  178.   int f, count = 0;
  179.  
  180.   do {
  181.     f = open(fn, O_RDWR | O_BINARY | SH_DENYRW | O_CREAT, S_IREAD | S_IWRITE);
  182.   } while ((f < 0) && (errno == EACCES) && (count++ < 500));
  183.  
  184.   return (f);
  185. }
  186.  
  187.  
  188. int write_netlog(int sn, long sent, long recd, char *tmused)
  189. {
  190.   int f;
  191.   char *ss, s[101], s1[81], s2[81], fn[121];
  192.   long l;
  193.   struct tm *time_now;
  194.   time_t some;
  195.  
  196.   time(&some);
  197.   time_now = localtime(&some);
  198.   strftime(s1, 35, "%m/%d/%y %H:%M:%S", time_now);
  199.  
  200.   ss = (char *) farmalloc(MAX_LEN + 1024L);
  201.   if (ss == NULL)
  202.     return 1;
  203.   if ((sent) || (recd)) {
  204.     if ((recd) && (!sent))
  205.       sprintf(s2, "       , R:%4ldk,", recd);
  206.     else {
  207.       if ((recd) && (sent))
  208.         sprintf(s2, "S:%4ldk, R:%4ldk,", sent, recd);
  209.       else
  210.         sprintf(s2, "S:%4ldk,         ", sent);
  211.     }
  212.   } else
  213.     strcpy(s2, "                 ");
  214.   sprintf(s, "%s To %5d, %s         %5s min  %s\r\n", s1, sn, s2,
  215.           tmused, net_name);
  216.  
  217.   strcpy(ss, s);
  218.  
  219.   sprintf(fn, "%sNET.LOG", syscfg.gfilesdir);
  220.   f = open_netlog(fn);
  221.   lseek(f, 0L, SEEK_SET);
  222.   l = (long) (read(f, (void *) (&(ss[strlen(s)])), (int) MAX_LEN) + strlen(s));
  223.   while ((l > 0L) && (ss[(int) l] != '\n'))
  224.     --l;
  225.   lseek(f, 0L, SEEK_SET);
  226.   write(f, (void *) ss, (int) l + 1);
  227.   chsize(f, l + 1);
  228.   if (ss) {
  229.     farfree(ss);
  230.     ss = NULL;
  231.   }
  232.   close(f);
  233.   return 0;
  234. }
  235.  
  236. #pragma warn -par
  237. main(int argc, char *argv[])
  238. {
  239.   char s[101];
  240.   unsigned int sy;
  241.   unsigned long sent, recd;
  242.   int i, f;
  243.  
  244.   strcpy(s, "CONFIG.DAT");
  245.   f = open(s, O_RDONLY | O_BINARY);
  246.   if (f < 0)
  247.     return 1;
  248.   read(f, (void *) (&syscfg), sizeof(configrec));
  249.   close(f);
  250.  
  251.   if (strncmp(argv[1], "NETLOG", 6) == 0) {
  252.     strcpy(net_name, argv[6]);
  253.     sy = atoi(argv[2]);
  254.     sent = atol(argv[3]);
  255.     recd = atol(argv[4]);
  256.     if (write_netlog(sy, sent, recd, argv[5]))
  257.       return 1;
  258.   }
  259.  
  260.   if (strncmp(argv[1], "TRIM", 4) == 0) {
  261.     strcpy(net_data, argv[2]);
  262.     sprintf(s, "%s%s", net_data, argv[3]);
  263.     trim_log(s);
  264.   }
  265.  
  266.   if (strncmp(argv[1], "PURGE", 5) == 0) {
  267.     strcpy(net_data, argv[2]);
  268.     i = atoi(argv[3]);
  269.     purge_sent(i);
  270.   }
  271.  
  272.   if (strncmp(argv[1], "CHUNK", 5) == 0) {
  273.     strcpy(net_data, argv[2]);
  274.     sprintf(s, "%sINBOUND\\%s", net_data, argv[3]);
  275.     if (!chunk(s))
  276.       unlink(s);
  277.   }
  278.  
  279.   return 0;
  280. }
  281.