home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.wwiv.com
/
ftp.wwiv.com.zip
/
ftp.wwiv.com
/
pub
/
PPPBCKP
/
SRC
/
SRC15B82.ZIP
/
PPPUTIL.CPP
< prev
next >
Wrap
Text File
|
1998-01-01
|
6KB
|
276 lines
#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <string.h>
#include <time.h>
#include <malloc.h>
#include <fcntl.h>
#include <errno.h>
#include <share.h>
#include <dir.h>
#include <sys\stat.h>
#include "vardec.h"
#include "net.h"
configrec syscfg;
char net_name[16], net_data[MAXPATH];
void name_chunk(char *chunkname)
{
int i, ok;
struct stat info;
ok = 0;
for (i = 0; ((i < 1000) && (!ok)); i++) {
sprintf(chunkname, "%sSPOOL\\UNK-9%3.3d.UUE", net_data, i);
if (stat(chunkname, &info) == -1)
ok = 1;
}
}
int chunk(char *fn)
{
char s[255], outfn[MAXPATH], *hdrbuf;
int done;
long textlen, curpos;
size_t hdrlen;
FILE *in, *out;
fprintf(stderr, "\n ■ Splitting %s into 32K chunks.", fn);
if ((in = fopen(fn, "rb")) == NULL)
return 1;
done = 0;
do {
fgets(s, 254, in);
if (*s == '')
hdrlen = (size_t) ftell(in);
else
done = 1;
} while (!done);
if ((hdrbuf = (char *) farmalloc(hdrlen + 1)) == NULL) {
fclose(in);
return 1;
}
rewind(in);
fread((void *) hdrbuf, sizeof(char), hdrlen, in);
curpos = hdrlen;
done = 0;
rewind(in);
while (!done) {
name_chunk(outfn);
fprintf(stderr, "\n - Creating %s.", outfn);
if ((out = fopen(outfn, "wb+")) == NULL) {
farfree(hdrbuf);
fclose(in);
return 1;
}
fwrite((void *) hdrbuf, sizeof(char), hdrlen, out);
fprintf(out, "\n");
fseek(in, curpos, SEEK_SET);
textlen = 0L;
do {
fgets(s, 254, in);
textlen += fprintf(out, s);
} while ((textlen < (32000 - hdrlen)) && (feof(in) == 0));
if (feof(in) != 0)
done = 1;
else {
fprintf(out, "\nContinued in next message...\n");
curpos = ftell(in);
}
if (out != NULL)
fclose(out);
}
if (in != NULL)
fclose(in);
if (hdrbuf)
farfree(hdrbuf);
return 0;
}
void purge_sent(char *fpath, int days)
{
char s[121];
int f1, howmany = 0;
long age;
struct ffblk ff;
struct stat fileinfo;
sprintf(s, "%s\\*.*", fpath);
f1 = findfirst(s, &ff, 0);
while (f1 == 0) {
sprintf(s, "%sSENT%s", net_data, ff.ff_name);
if (stat(s, &fileinfo) == 0) {
age = (time(NULL) - fileinfo.st_atime);
if (age > (86400L * days)) {
++howmany;
unlink(s);
}
}
f1 = findnext(&ff);
}
fprintf(stderr, " %d packet%s deleted.", howmany, howmany == 1 ? "" : "s");
}
#define MAX_LOG 1000
void trim_log(char *ol)
{
int num_lines, total_lines, kill_lines;
FILE *old_log, *new_log;
char nl[MAXPATH], s[160];
sprintf(ol, "%sNEWS.LOG", net_data);
sprintf(nl, "%sNEWS.ZZZ", net_data);
old_log = fopen(ol, "r");
new_log = fopen(nl, "a");
total_lines = 0;
if (old_log != NULL) {
while (!(fgets(s, 160, old_log) == NULL))
++total_lines;
rewind(old_log);
if (total_lines < MAX_LOG) {
fclose(old_log);
if (new_log != NULL)
fclose(new_log);
unlink(nl);
return;
}
kill_lines = total_lines - MAX_LOG;
num_lines = 0;
while ((fgets(s, 160, old_log)) && (num_lines < kill_lines))
num_lines++;
while ((_fstrstr(s, "Freeware PPP Project") == NULL) &&
(num_lines < total_lines)) {
fgets(s, 160, old_log);
num_lines++;
}
fputs(s, new_log);
while ((!(fgets(s, 160, old_log) == NULL)))
fputs(s, new_log);
}
if (old_log != NULL)
fclose(old_log);
if (new_log != NULL)
fclose(new_log);
unlink(ol);
rename(nl, ol);
}
#define MAX_LEN 12288L
int open_netlog(char *fn)
{
int f, count = 0;
do {
f = open(fn, O_RDWR | O_BINARY | SH_DENYRW | O_CREAT, S_IREAD | S_IWRITE);
} while ((f < 0) && (errno == EACCES) && (count++ < 500));
return (f);
}
int write_netlog(int sn, long sent, long recd, char *tmused)
{
int f;
char *ss, s[101], s1[81], s2[81], fn[121];
long l;
struct tm *time_now;
time_t some;
time(&some);
time_now = localtime(&some);
strftime(s1, 35, "%m/%d/%y %H:%M:%S", time_now);
ss = (char *) farmalloc(MAX_LEN + 1024L);
if (ss == NULL)
return 1;
if ((sent) || (recd)) {
if ((recd) && (!sent))
sprintf(s2, " , R:%4ldk,", recd);
else {
if ((recd) && (sent))
sprintf(s2, "S:%4ldk, R:%4ldk,", sent, recd);
else
sprintf(s2, "S:%4ldk, ", sent);
}
} else
strcpy(s2, " ");
sprintf(s, "%s To %5d, %s %5s min %s\r\n", s1, sn, s2,
tmused, net_name);
strcpy(ss, s);
sprintf(fn, "%sNET.LOG", syscfg.gfilesdir);
f = open_netlog(fn);
lseek(f, 0L, SEEK_SET);
l = (long) (read(f, (void *) (&(ss[strlen(s)])), (int) MAX_LEN) + strlen(s));
while ((l > 0L) && (ss[(int) l] != '\n'))
--l;
lseek(f, 0L, SEEK_SET);
write(f, (void *) ss, (int) l + 1);
chsize(f, l + 1);
if (ss) {
farfree(ss);
ss = NULL;
}
close(f);
return 0;
}
#pragma warn -par
main(int argc, char *argv[])
{
char s[101];
unsigned int sy;
unsigned long sent, recd;
int i, f;
strcpy(s, "CONFIG.DAT");
f = open(s, O_RDONLY | O_BINARY);
if (f < 0)
return 1;
read(f, (void *) (&syscfg), sizeof(configrec));
close(f);
if (strncmp(argv[1], "NETLOG", 6) == 0) {
strcpy(net_name, argv[6]);
sy = atoi(argv[2]);
sent = atol(argv[3]);
recd = atol(argv[4]);
if (write_netlog(sy, sent, recd, argv[5]))
return 1;
}
if (strncmp(argv[1], "TRIM", 4) == 0) {
strcpy(net_data, argv[2]);
sprintf(s, "%s%s", net_data, argv[3]);
trim_log(s);
}
if (strncmp(argv[1], "PURGE", 5) == 0) {
i = atoi(argv[3]);
purge_sent(argv[2], i);
}
if (strncmp(argv[1], "CHUNK", 5) == 0) {
strcpy(net_data, argv[2]);
sprintf(s, "%sINBOUND\\%s", net_data, argv[3]);
if (!chunk(s))
unlink(s);
}
return 0;
}