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