home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.wwiv.com
/
ftp.wwiv.com.zip
/
ftp.wwiv.com
/
pub
/
PPPBCKP
/
SRC15B23.ZIP
/
UUMAIN.C
< prev
next >
Wrap
C/C++ Source or Header
|
1997-03-30
|
11KB
|
378 lines
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <dir.h>
#include <dos.h>
#include <share.h>
#include <sys\stat.h>
#include <alloc.h>
#include <io.h>
#include <fcntl.h>
#include <time.h>
#include "uu.h"
#include "net.h"
unsigned _stklen = 16384;
#define MAX_BUF 1024
#define LAST(s) s[strlen(s)-1]
char buf[MAX_BUF];
scanfor(char *token, FILE * in)
{
long pos;
pos = ftell(in);
while (fgets(buf, MAX_BUF, in) && strncmpi(buf, token, strlen(token))) {
pos = ftell(in);
}
rewind(in);
fseek(in, pos, 0);
pos = ftell(in);
return (!strncmpi(buf, "begin ", 6));
}
scanfornet(FILE * in, char *net_name)
{
char *ss;
int found = 0;
while ((fgets(buf, MAX_BUF, in)) && !found) {
if (strncmpi(buf, "subject:", strlen("subject:"))) {
ss = strtok(buf, " ");
ss = strtok(NULL, " ");
if (ss[0] == '@') {
found = 1;
strcpy(net_name, ss);
}
}
}
rewind(in);
return found;
}
void cd_to(char *s)
{
char s1[81];
int i, db;
strcpy(s1, s);
i = strlen(s1) - 1;
db = (s1[i] == '\\');
if (i == 0)
db = 0;
if ((i == 2) && (s1[1] == ':'))
db = 0;
if (db)
s1[i] = 0;
chdir(s1);
if (s[1] == ':') {
setdisk(s[0] - 'A');
if (s[2] == 0)
chdir("\\");
}
}
void get_dir(char *s, int be)
{
strcpy(s, "X:\\");
s[0] = 'A' + getdisk();
getcurdir(0, &(s[3]));
if (be) {
if (s[strlen(s) - 1] != '\\')
strcat(s, "\\");
}
}
int exist(char *s)
{
int i;
struct ffblk ff;
i = findfirst(s, &ff, FA_HIDDEN);
if (i)
return (0);
else
return (1);
}
int copyfile(char *input, char *output)
{
int f1, f2, i;
char *b, s[181], s1[21], s2[81], s3[81];
struct ftime ft;
if ((strcmp(input, output) != 0) && (exist(input))) {
if (exist(output)) {
fnsplit(output, s3, s2, s1, NULL);
i = 0;
sprintf(s, "%s%s%s.%03d", s3, s2, s1, i);
while (exist(s))
sprintf(s, "%s%s%s.%03d", s3, s2, s1, ++i);
strcpy(output, s);
}
fprintf(stderr, "\n ■ Creating File : %s", output);
if ((b = (char *) farmalloc(16400)) == NULL)
return 0;
f1 = open(input, O_RDONLY | O_BINARY, S_IREAD | S_IWRITE);
if (!f1) {
farfree(b);
return 0;
}
getftime(f1, &ft);
f2 = open(output, O_RDWR | O_BINARY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
if (!f2) {
farfree(b);
close(f1);
return 0;
}
i = read(f1, (void *) b, 16384);
while (i > 0) {
write(f2, (void *) b, i);
i = read(f1, (void *) b, 16384);
}
f1 = close(f1);
setftime(f2, &ft);
f2 = close(f2);
farfree(b);
}
return 1;
}
char *stripfn(char *fn)
{
static char ofn[15];
int i, i1;
char s[81];
i1 = -1;
for (i = 0; i < strlen(fn); i++)
if ((fn[i] == '\\') || (fn[i] == ':') || (fn[i] == '/'))
i1 = i;
if (i1 != -1)
strcpy(s, &(fn[i1 + 1]));
else
strcpy(s, fn);
for (i = 0; i < strlen(s); i++)
if ((s[i] >= 'A') && (s[i] <= 'Z'))
s[i] = s[i] - 'A' + 'a';
i = 0;
while (s[i] != 0) {
if (s[i] == 32)
strcpy(&s[i], &s[i + 1]);
else
++i;
}
strcpy(ofn, s);
return (ofn);
}
void ssm(char *fpath, char *ssmsend)
{
int i, f;
char s1[161];
net_header_rec nh;
nh.tosys = 0;
nh.touser = 1;
nh.fromsys = 32767;
nh.fromuser = 1;
nh.main_type = main_type_ssm;
nh.minor_type = 0;
nh.list_len = 0;
time((long *) &nh.daten);
if (strlen(ssmsend) > 80)
ssmsend[80] = 0;
nh.length = strlen(ssmsend);
nh.method = 0;
i = 0;
sprintf(s1, "%sP1-SSM%d.NET", fpath, i);
while (exist(s1))
sprintf(s1, "%sP1-SSM%d.NET", fpath, ++i);
f = open(s1, O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
lseek(f, 0L, SEEK_END);
write(f, (void *) &nh, sizeof(net_header_rec));
write(f, (void *) ssmsend, nh.length);
close(f);
}
main(int argc, char *argv[])
{
FILE *out = NULL, *in = NULL;
char outname[181], net_name[21];
char cur_dir[181], temp_dir[181], s1[181], s2[181], s3[21], s4[21], s5[21];
int i;
if (argc == 4 || argc == 5) {
if (strcmpi(argv[1], "-encode") == 0 && argc == 4) {
if (((in = fopen(argv[2], "r")) != NULL) && ((out = fopen(argv[3], "at+")) != NULL)) {
fprintf(stderr, " %ld bytes.", uuencode(in, out, argv[2]));
if (in)
fclose(in);
if (out)
fclose(out);
exit(EXIT_SUCCESS);
} else {
exit(EXIT_FAILURE);
}
} else
if (strcmpi(argv[1], "-decode") == 0 && argc == 5) {
get_dir(cur_dir, 1);
strcpy(temp_dir, argv[3]);
if (LAST(temp_dir) == '\\')
LAST(temp_dir) = 0;
cd_to(temp_dir);
if ((in = fopen(argv[2], "r")) != NULL) {
if (!scanfornet(in, net_name))
net_name[0] = 0;
rewind(in);
scanfor("begin", in);
switch (uudecode(in, argv[3], outname)) {
case UU_NO_MEM:
fprintf(stderr, "\n ■ Insufficient memory to decode... moved to CHECKNET.");
sprintf(s1, "Out of memory decoding %s - moved to CHECKNET!", argv[2]);
ssm(s1, argv[4]);
sprintf(s1, "%s\\%s", temp_dir, argv[2]);
sprintf(s2, "%sCHECKNET\\%s", argv[4], argv[2]);
if (copyfile(s1, s2))
unlink(s1);
cd_to(cur_dir);
if (in)
fclose(in);
exit(EXIT_FAILURE);
case UU_BAD_BEGIN:
fprintf(stderr, "\n ■ Appears to be a standard message... moving to SPOOL");
sprintf(s1, "%s\\%s", temp_dir, argv[2]);
while (LAST(argv[2]) != '.')
LAST(argv[2]) = 0;
strcat(argv[2], "MSG");
sprintf(s2, "%sSPOOL\\%s", argv[4], argv[2]);
if (copyfile(s1, s2))
unlink(s1);
cd_to(cur_dir);
if (in)
fclose(in);
exit(EXIT_FAILURE);
case UU_CANT_OPEN:
fprintf(stderr, "\n ■ Cannot open %s%s", argv[4], outname);
cd_to(cur_dir);
if (in)
fclose(in);
exit(EXIT_FAILURE);
case UU_CHECKSUM:
fprintf(stderr, "\n ■ Bad checksum... moving packet to CHECKNET");
sprintf(s1, "Bad checksum decoding %s - moved to CHECKNET!", argv[2]);
ssm(s1, argv[4]);
sprintf(s1, "%s\\%s", temp_dir, argv[2]);
while (LAST(argv[2]) != '.')
LAST(argv[2]) = 0;
strcat(argv[2], "MSG");
sprintf(s2, "%sCHECKNET\\%s", argv[4], argv[2]);
if (copyfile(s1, s2))
unlink(s1);
cd_to(cur_dir);
if (in)
fclose(in);
exit(EXIT_FAILURE);
case UU_BAD_END:
fprintf(stderr, "\n ■ \'end\' not found in message... moving to CHECKNET");
sprintf(s1, "No \'end\' found in %s - moved to CHECKNET!", argv[2]);
ssm(s1, argv[4]);
sprintf(s1, "%s\\%s", temp_dir, argv[2]);
while (LAST(argv[2]) != '.')
LAST(argv[2]) = 0;
strcat(argv[2], "MSG");
sprintf(s2, "%sCHECKNET\\%s", argv[4], argv[2]);
if (copyfile(s1, s2))
unlink(s1);
cd_to(cur_dir);
if (in)
fclose(in);
exit(EXIT_FAILURE);
case UU_SUCCESS:
fprintf(stderr, "\n ■ Successfully decoded %s", stripfn(outname));
fnsplit(outname, NULL, NULL, s4, s3);
sprintf(s5, "%s%s", s4, s3);
if (strcmpi(s3, ".NET") == NULL) {
unlink(argv[2]);
i = 0;
sprintf(s2, "%sP1-%03d.NET", argv[4], i);
while (exist(s2))
sprintf(s2, "%sP1-%03d.NET", argv[4], ++i);
sprintf(s1, "%s\\%s", temp_dir, stripfn(outname));
if (!copyfile(s1, s2)) {
fprintf(stderr, "\n ■ Error creating %s", s2);
cd_to(cur_dir);
if (in)
fclose(in);
exit(EXIT_FAILURE);
} else
unlink(s1);
} else {
sprintf(s1, "%s\\%s", temp_dir, stripfn(outname));
sprintf(s2, "%sCHECKNET\\%s", argv[4], stripfn(outname));
if (exist(s2)) {
s2[strlen(s2 - 1)] = 0;
i = 0;
sprintf(s2, "%sCHECKNET\\%s%d", argv[4], stripfn(outname), i);
while (exist(s2)) {
sprintf(s2, "%sCHECKNET\\%s%d", argv[4], stripfn(outname), ++i);
if (i > 9)
break;
}
}
if (!copyfile(s1, s2)) {
fprintf(stderr, "\n ■ Error creating %s", s2);
cd_to(cur_dir);
if (in)
fclose(in);
exit(EXIT_FAILURE);
}
sprintf(s1, "%s\\%s", temp_dir, argv[2]);
while (LAST(argv[2]) != '.')
LAST(argv[2]) = 0;
strcat(argv[2], "MSG");
sprintf(s2, "%sCHECKNET\\%s", argv[4], argv[2]);
if (copyfile(s1, s2)) {
unlink(s1);
sprintf(s1, "Received file : %s!", s2);
ssm(s1, argv[4]);
}
}
cd_to(cur_dir);
if (in)
fclose(in);
exit(EXIT_SUCCESS);
default:
fprintf(stderr, "\n ■ Unknown error... moving packet to CHECKNET");
sprintf(s1, "Unknown error processing %s - moved to CHECKNET!", argv[2]);
ssm(s1, argv[4]);
sprintf(s1, "%s\\%s", temp_dir, argv[2]);
while (LAST(argv[2]) != '.')
LAST(argv[2]) = 0;
strcat(argv[2], "MSG");
sprintf(s2, "%sCHECKNET\\%s", argv[4], argv[2]);
if (copyfile(s1, s2))
unlink(s1);
cd_to(cur_dir);
if (in)
fclose(in);
exit(EXIT_FAILURE);
}
} else {
cd_to(cur_dir);
fprintf(stderr, "\n ■ Input file %s not found.", argv[2]);
if (in)
fclose(in);
exit(EXIT_FAILURE);
}
}
}
if (in)
fclose(in);
exit(EXIT_SUCCESS);
return 0;
}