home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.wwiv.com
/
ftp.wwiv.com.zip
/
ftp.wwiv.com
/
pub
/
PPPBCKP
/
SRC
/
SRC15B63.ZIP
/
UUMAIN.CPP
< prev
next >
Wrap
C/C++ Source or Header
|
1997-11-09
|
34KB
|
1,382 lines
#include <stdio.h>
#include <stdarg.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 <errno.h>
#include <process.h>
#include "uu.h"
#include "net.h"
#include "vardec.h"
#include "version.h"
#include "retcode.h"
#define MAX_BUF 1024
#define MAX_DIZ_LINES 10
#define SHARE_LEVEL 10
#define WAIT_TIME 10
#define TRIES 100
#define MT_DESQVIEW 0x01
#define MT_WINDOWS 0x02
#define MT_OS2 0x04
#define MT_NB 0x40
#define SETREC(f,i) sh_lseek(f,((long) (i)) * \
((long)sizeof(uploadsrec)), SEEK_SET);
#define LAST(s) s[strlen(s)-1]
extern unsigned _stklen = 6000U;
char net_name[21], net_data[MAXPATH], fdldesc[81], fdlfn[40], fdlupby[40], edlfn[MAXPATH];
char temp_dir[MAXPATH], maindir[MAXPATH], dlfn[MAXPATH];
char *xenviron[50];
unsigned short fdltype;
int num_dirs, numf, multitasker = 0;
char *version = "Freeware PPP Project UU Encode/Decode " VERSION;
directoryrec huge *directories;
configrec syscfg;
void dv_pause(void)
{
__emit__(0xb8, 0x1a, 0x10, 0xcd, 0x15);
__emit__(0xb8, 0x00, 0x10, 0xcd, 0x15);
__emit__(0xb8, 0x25, 0x10, 0xcd, 0x15);
}
void win_pause(void)
{
__emit__(0x55, 0xb8, 0x80, 0x16, 0xcd, 0x2f, 0x5d);
}
int get_dos_version(void)
{
_AX = 0x3000;
geninterrupt(0x21);
if (_AX % 256 >= 10) {
multitasker |= MT_OS2;
}
return (_AX);
}
int get_dv_version(void)
{
int v;
if (multitasker & MT_OS2)
return 0;
_AX = 0x2b01;
_CX = 0x4445;
_DX = 0x5351;
geninterrupt(0x21);
if (_AL == 0xff) {
return 0;
} else {
v = _BX;
multitasker |= MT_DESQVIEW;
return v;
}
}
int get_win_version(void)
{
int v = 0;
__emit__(0x55, 0x06, 0x53);
_AX = 0x352f;
geninterrupt(0x21);
_AX = _ES;
if (_AX | _BX) {
_AX = 0x1600;
geninterrupt(0x2f);
v = _AX;
if (v % 256 <= 1)
v = 0;
}
__emit__(0x5b, 0x07, 0x5d);
if (v != 0)
multitasker |= MT_WINDOWS;
return (v);
}
int get_nb_version(void)
{
_AX = 0;
geninterrupt(0x2A);
return (_AH);
}
void detect_multitask(void)
{
get_dos_version();
get_win_version();
get_dv_version();
if (multitasker < 2)
if (get_nb_version())
multitasker = MT_NB;
}
void giveup_timeslice(void)
{
if (multitasker) {
switch (multitasker) {
case 1:
case 3:
dv_pause();
break;
case 2:
case 4:
case 5:
case 6:
case 7:
win_pause();
break;
default:
break;
}
}
}
int sh_read(int handle, void *buf, unsigned len)
{
if (handle == -1) {
return (-1);
}
return (read(handle, buf, len));
}
long sh_lseek(int handle, long offset, int fromwhere)
{
if (handle == -1) {
return (-1L);
}
return (lseek(handle, offset, fromwhere));
}
int sh_write(int handle, void *buffer, unsigned long len)
{
if (handle == -1) {
return (-1);
}
return (write(handle, buffer, (unsigned) len));
}
int sh_close(int f)
{
if (f != -1)
close(f);
return (-1);
}
int sh_open(char *path, int file_access, unsigned fmode)
{
int handle, count, share;
char drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE], ext[MAXEXT];
if ((file_access & O_RDWR) || (file_access & O_WRONLY) || (fmode & S_IWRITE)) {
share = SH_DENYRW;
} else {
share = SH_DENYWR;
}
handle = open(path, file_access | share, fmode);
if (handle < 0) {
count = 1;
fnsplit(path, drive, dir, file, ext);
if (access(path, 0) != -1) {
delay(WAIT_TIME);
handle = open(path, file_access | share, fmode);
while (((handle < 0) && (errno == EACCES)) && (count < TRIES)) {
if (count % 2)
delay(WAIT_TIME);
else
giveup_timeslice();
count++;
handle = open(path, file_access | share, fmode);
}
}
}
return (handle);
}
int sh_open1(char *path, int access)
{
unsigned fmode;
fmode = 0;
if ((access & O_RDWR) || (access & O_WRONLY))
fmode |= S_IWRITE;
if ((access & O_RDWR) || (access & O_RDONLY))
fmode |= S_IREAD;
return (sh_open(path, access, fmode));
}
FILE *fsh_open(char *path, char *fmode)
{
FILE *f;
int count, share, md, fd;
char drive[MAXDRIVE], dir[MAXDIR], file[MAXFILE], ext[MAXEXT];
share = SH_DENYWR;
md = 0;
if (((char *) _fstrchr(fmode, 'w')) != NULL) {
share = SH_DENYRD;
md = O_RDWR | O_CREAT | O_TRUNC;
} else
if (((char *) _fstrchr(fmode, 'a')) != NULL) {
share = SH_DENYRD;
md = O_RDWR | O_CREAT;
} else {
md = O_RDONLY;
}
if (((char *) _fstrchr(fmode, 'b')) != NULL) {
md |= O_BINARY;
}
if (((char *) _fstrchr(fmode, '+')) != NULL) {
md &= ~O_RDONLY;
md |= O_RDWR;
share = SH_DENYRD;
}
fd = open(path, md | share, S_IREAD | S_IWRITE);
if (fd < 0) {
count = 1;
fnsplit(path, drive, dir, file, ext);
if ((access(path, 0)) != -1) {
delay(WAIT_TIME);
fd = open(path, md | share, S_IREAD | S_IWRITE);
while (((fd < 0) && (errno == EACCES)) && (count < TRIES)) {
delay(WAIT_TIME);
count++;
fd = open(path, md | share, S_IREAD | S_IWRITE);
}
}
}
if (fd > 0) {
if (((char *) _fstrchr(fmode, 'a')) != NULL)
sh_lseek(fd, 0L, SEEK_END);
f = fdopen(fd, fmode);
if (!f) {
close(fd);
}
} else
f = NULL;
return (f);
}
#define MAX_XFER 61440L
long huge_xfer(int fd, void huge * buf, unsigned sz, unsigned nel, int wr)
{
long nxfr = 0, len = ((long) sz) * ((long) nel);
char huge *xbuf = (char huge *) buf;
unsigned cur, cur1;
while (len > 0) {
if (len > MAX_XFER)
cur = (unsigned int) MAX_XFER;
else
cur = (unsigned int) len;
if (wr)
cur1 = sh_write(fd, (char *) xbuf, cur);
else
cur1 = sh_read(fd, (char *) xbuf, cur);
if (cur1 != 65535L) {
len -= cur1;
nxfr += cur1;
xbuf = ((char huge *) buf) + nxfr;
}
if (cur1 != cur)
break;
}
return (nxfr);
}
void output(char *fmt,...)
{
va_list v;
char s[255];
va_start(v, fmt);
vsprintf(s, fmt, v);
va_end(v);
fputs(s, stderr);
}
void far *mallocx(unsigned long l)
{
void *x;
char huge *xx;
if (!l)
l = 1;
x = farmalloc(l);
if (!x) {
output("\n ■ Insufficient memory (%ld bytes) to read all directories.", l);
return NULL;
}
xx = (char huge *) x;
while (l >= 1) {
if (l > 32768L) {
memset((void *) xx, 0, 32768L);
l -= 32768L;
xx += 32768L;
} else {
memset((void *) xx, 0, l);
break;
}
}
return (x);
}
int scanfor(char *token, FILE * in)
{
char buf[MAX_BUF];
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", 7));
}
unsigned char *trimstr1(unsigned char *s)
{
int i;
static char *whitespace = " \r\n\t";
i = strlen(s);
while ((i > 0) && (_fstrchr(whitespace, s[i - 1])))
--i;
while ((i > 0) && (_fstrchr(whitespace, *s))) {
memmove(s, s + 1, --i);
}
s[i] = 0;
return (s);
}
void scanfdl(FILE * in)
{
char *ss, buf[MAX_BUF];
int done = 0;
while ((fgets(buf, MAX_BUF, in)) && !done) {
ss = NULL;
if (strncmpi(buf, "subject:", 8) == 0) {
if (_fstrstr(buf, "FILE TRANSFER")) {
ss = strtok(buf, "@");
if (ss) {
ss = strtok(NULL, " ");
strcpy(net_name, ss);
ss = strtok(NULL, ":");
if (ss) {
ss = strtok(NULL, "\r\n");
if (ss) {
strcpy(fdlfn, ss);
trimstr1(fdlfn);
}
}
}
}
} else
if (strncmpi(buf, "Description:", 12) == 0) {
ss = strtok(buf, " ");
if (ss) {
ss = strtok(NULL, "\r\n");
strcpy(fdldesc, ss);
}
} else
if (strncmpi(buf, "FDL Type:", 9) == 0) {
ss = strtok(buf, ":");
if (ss) {
ss = strtok(NULL, "\r\n");
trimstr1(ss);
fdltype = atoi(ss);
}
} else
if (strncmpi(buf, "Originating System:", 19) == 0) {
ss = strtok(buf, ":");
if (ss) {
ss = strtok(NULL, "\r\n");
trimstr1(ss);
strcpy(fdlupby, ss);
}
} else
if (strncmpi(buf, "begin 600", 9) == 0)
done = 1;
}
return;
}
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(int display, char *infile, char *outfile)
{
int f1, f2, i;
char *b, s[181], s1[21], s2[81], s3[81];
struct ftime ft;
if ((strcmp(infile, outfile) != 0) && (exist(infile))) {
if (exist(outfile)) {
fnsplit(outfile, 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(outfile, s);
}
fnsplit(outfile, NULL, NULL, s3, s2);
if (display)
output(" as %s%s", s3, s2);
if ((b = (char *) farmalloc(16400)) == NULL)
return 1;
f1 = open(infile, O_RDONLY | O_BINARY, S_IREAD | S_IWRITE);
if (!f1) {
farfree(b);
return 1;
}
getftime(f1, &ft);
f2 = open(outfile, O_RDWR | O_BINARY | O_CREAT | O_TRUNC, S_IREAD | S_IWRITE);
if (!f2) {
farfree(b);
close(f1);
return 1;
}
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 0;
}
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 *s)
{
int f, i, i1;
char s1[161];
shortmsgrec sm;
sprintf(s1, "%sSMW.DAT", syscfg.datadir);
f = sh_open(s1, O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
if (f < 0)
return;
i = (int) (filelength(f) / sizeof(shortmsgrec));
i1 = i - 1;
if (i1 >= 0) {
sh_lseek(f, ((long) (i1)) * sizeof(shortmsgrec), SEEK_SET);
sh_read(f, (void *) &sm, sizeof(shortmsgrec));
while ((sm.tosys == 0) && (sm.touser == 0) && (i1 > 0)) {
--i1;
sh_lseek(f, ((long) (i1)) * sizeof(shortmsgrec), SEEK_SET);
sh_read(f, (void *) &sm, sizeof(shortmsgrec));
}
if ((sm.tosys) || (sm.touser))
++i1;
} else
i1 = 0;
sm.tosys = 0;
sm.touser = 1;
strncpy(sm.message, s, 80);
sm.message[80] = 0;
sh_lseek(f, ((long) (i1)) * sizeof(shortmsgrec), SEEK_SET);
sh_write(f, (void *) &sm, sizeof(shortmsgrec));
sh_close(f);
}
int do_it(char *cl)
{
int i, i1, l, result;
char s[101], *ss[20];
strcpy(s, cl);
ss[0] = s;
i = 1;
l = strlen(s);
for (i1 = 1; i1 < l; i1++)
if (s[i1] == 32) {
s[i1] = 0;
ss[i++] = &(s[i1 + 1]);
}
ss[i] = NULL;
result = spawnvpe(P_WAIT, ss[0], ss, xenviron);
return result;
}
void align(char *s)
{
char f[40], e[40], s1[20], *s2;
int i, i1, i2;
i1 = 0;
if (s[0] == '.')
i1 = 1;
for (i = 0; i < strlen(s); i++)
if ((s[i] == '\\') || (s[i] == '/') || (s[i] == ':') || (s[i] == '<') ||
(s[i] == '>') || (s[i] == '|'))
i1 = 1;
if (i1) {
strcpy(s, "!");
return;
}
s2 = _fstrchr(s, '.');
if (s2 == NULL) {
e[0] = 0;
} else {
strcpy(e, &(s2[1]));
e[3] = 0;
s2[0] = 0;
}
strcpy(f, s);
for (i = strlen(f); i < 8; i++)
f[i] = 32;
f[8] = 0;
i1 = 0;
i2 = 0;
for (i = 0; i < 8; i++) {
if (f[i] == '*')
i1 = 1;
if (f[i] == ' ')
i2 = 1;
if (i2)
f[i] = ' ';
if (i1)
f[i] = '?';
}
for (i = strlen(e); i < 3; i++)
e[i] = 32;
e[3] = 0;
i1 = 0;
for (i = 0; i < 3; i++) {
if (e[i] == '*')
i1 = 1;
if (i1)
e[i] = '?';
}
for (i = 0; i < 12; i++)
s1[i] = 32;
strcpy(s1, f);
s1[8] = '.';
strcpy(&(s1[9]), e);
strcpy(s, s1);
strupr(s);
}
void stuff_in(char *s, char *s1, char *f1, char *f2, char *f3, char *f4, char *f5)
{
int r = 0, w = 0;
while (s1[r] != 0) {
if (s1[r] == '%') {
++r;
s[w] = 0;
switch (s1[r]) {
case '1':
strcat(s, f1);
break;
case '2':
strcat(s, f2);
break;
case '3':
strcat(s, f3);
break;
case '4':
strcat(s, f4);
break;
case '5':
strcat(s, f5);
break;
}
w = strlen(s);
r++;
} else
s[w++] = s1[r++];
}
s[w] = 0;
}
char upcase(char ch)
{
if ((ch > '`') && (ch < '{'))
ch = ch - 32;
return (ch);
}
char *make_abs_cmd(char *out)
{
char s[161], s1[161], s2[161], *ss, *ss1;
char *exts[] = {"", ".COM", ".EXE", ".BAT", 0};
int i;
strcpy(s1, out);
if (s1[1] == ':') {
if (s1[2] != '\\') {
getcurdir(upcase(s1[0]) - 'A' + 1, s);
sprintf(out, "%c:\\%s\\%s", s1[0], s, s1 + 2);
}
goto got_cmd;
}
if (out[0] == '\\') {
sprintf(out, "%c:%s", maindir[0], s1);
goto got_cmd;
}
ss = _fstrchr(s1, ' ');
if (ss) {
*ss = 0;
sprintf(s2, " %s", ss + 1);
} else {
s2[0] = 0;
}
for (i = 0; exts[i]; i++) {
if (i == 0) {
ss1 = _fstrrchr(s1, '\\');
if (!ss1)
ss1 = s1;
if (_fstrchr(ss1, '.') == 0)
continue;
}
sprintf(s, "%s%s", s1, exts[i]);
if (exist(s)) {
sprintf(out, "%s%s%s", maindir, s, s2);
goto got_cmd;
} else {
ss1 = searchpath(s);
if (ss1) {
sprintf(out, "%s%s", ss1, s2);
goto got_cmd;
}
}
}
sprintf(out, "%s%s%s", maindir, s1, s2);
got_cmd:
return (out);
}
void get_arc_cmd(char *out, char *arcfn, int cmd, char *ofn)
{
char *ss, s[161];
int i;
out[0] = 0;
ss = _fstrchr(arcfn, '.');
if (ss == NULL)
return;
++ss;
for (i = 0; i < 4; i++)
if (stricmp(ss, syscfg.arcs[i].extension) == 0) {
switch (cmd) {
case 0:
strcpy(s, syscfg.arcs[i].arcl);
break;
case 1:
strcpy(s, syscfg.arcs[i].arce);
break;
case 2:
strcpy(s, syscfg.arcs[i].arca);
break;
}
if (s[0] == 0)
return;
stuff_in(out, s, arcfn, ofn, "", "", "");
make_abs_cmd(out);
return;
}
}
void add_extended_description(char *fn, char *desc)
{
ext_desc_type ed;
int f;
strcpy(ed.name, fn);
ed.len = strlen(desc);
f = sh_open(edlfn, O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
sh_lseek(f, 0L, SEEK_END);
sh_write(f, &ed, sizeof(ext_desc_type));
sh_write(f, desc, ed.len);
f = sh_close(f);
}
int get_file_idz(uploadsrec * u, directoryrec * d)
{
char *b, *ss, cmd[255], cmdline[255], s3[121], s4[121];
int f, i, ok = 0;
ss = _fstrchr(stripfn(u->filename), '.');
if (ss == NULL)
return 1;
++ss;
for (i = 0; i < 4 && !ok; i++)
if (!ok)
ok = (stricmp(ss, syscfg.arcs[i].extension) == 0);
if (!ok)
return 1;
sprintf(s4, "%sFILE_ID.DIZ", temp_dir);
unlink(s4);
sprintf(s4, "%sDESC.SDI", temp_dir);
unlink(s4);
strcpy(s3, d->path);
cd_to(s3);
get_dir(s4, 1);
strcat(s4, stripfn(u->filename));
cd_to(maindir);
get_arc_cmd(cmd, s4, 1, "FILE_ID.DIZ DESC.SDI");
sprintf(cmdline, "%s", cmd);
cd_to(temp_dir);
do_it(cmdline);
cd_to(maindir);
sprintf(s4, "%sFILE_ID.DIZ", temp_dir);
if (!exist(s4))
sprintf(s4, "%sDESC.SDI", temp_dir);
if (exist(s4)) {
stripfn(s4);
if ((b = (char *) farmalloc((long) MAX_DIZ_LINES * 256 + 1)) == NULL)
return 1;
f = sh_open1(s4, O_RDONLY | O_BINARY);
if (filelength(f) < (MAX_DIZ_LINES * 256)) {
sh_read(f, b, (int) filelength(f));
b[filelength(f)] = 0;
} else {
sh_read(f, b, (int) MAX_DIZ_LINES * 256);
b[(int) MAX_DIZ_LINES * 256] = 0;
}
sh_close(f);
ss = strtok(b, "\n");
if (LAST(ss) == '\r')
LAST(ss) = '\0';
sprintf(u->description, "%.58s", ss);
ss = strtok(NULL, "");
if (ss) {
for (i = strlen(ss) - 1; i > 0; i--) {
if ((ss[i] == 26) || (ss[i] == 12))
ss[i] = 32;
}
add_extended_description(u->filename, ss);
u->mask |= mask_extended;
}
if (b)
farfree(b);
sprintf(s4, "%sFILE_ID.DIZ", temp_dir);
unlink(s4);
sprintf(s4, "%sDESC.SDI", temp_dir);
unlink(s4);
}
return 0;
}
static char *fdldate(void)
{
char *ds;
struct date today;
ds = NULL;
getdate(&today);
sprintf(ds, "%02d/%02d/%02d", today.da_mon, today.da_day, today.da_year - 1900);
return ds;
}
int compare(char *s1, char *s2)
{
int ok, i;
ok = 1;
for (i = 0; i < 12; i++)
if ((s1[i] != s2[i]) && (s1[i] != '?') && (s2[i] != '?'))
ok = 0;
return (ok);
}
void dliscan(int dn)
{
int i, f;
long l;
uploadsrec u;
directoryrec d;
d = directories[dn];
sprintf(dlfn, "%s%s.DIR", syscfg.datadir, d.filename);
sprintf(edlfn, "%s%s/EXT", syscfg.datadir, d.filename);
f = sh_open(dlfn, O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
i = filelength(f) / sizeof(uploadsrec);
if (i == 0) {
output("\n ■ Creating new directory %s.", dlfn);
memset(&u, 0, sizeof(uploadsrec));
strcpy(u.filename, "|MARKER|");
time(&l);
u.daten = l;
SETREC(f, 0);
sh_write(f, (void *) &u, sizeof(uploadsrec));
} else {
SETREC(f, 0);
sh_read(f, (void *) &u, sizeof(uploadsrec));
if (strcmp(u.filename, "|MARKER|")) {
numf = u.numbytes;
memset(&u, 0, sizeof(uploadsrec));
strcpy(u.filename, "|MARKER|");
time(&l);
u.daten = l;
u.numbytes = numf;
SETREC(f, 0);
sh_write(f, &u, sizeof(uploadsrec));
}
}
if (f > -1)
f = sh_close(f);
numf = u.numbytes;
}
int recno(char *s)
{
int i, f;
uploadsrec u;
i = 1;
if (numf < 1)
return (-1);
f = sh_open1(dlfn, O_RDONLY | O_BINARY);
SETREC(f, i);
sh_read(f, (void *) &u, sizeof(uploadsrec));
while ((i < numf) && (compare(s, u.filename) == 0)) {
++i;
SETREC(f, i);
sh_read(f, (void *) &u, sizeof(uploadsrec));
}
f = sh_close(f);
if (compare(s, u.filename))
return i;
else
return -1;
}
char *make_abs_path(unsigned char *checkdir)
{
char newdir[MAXPATH];
if ((strlen(checkdir) < 3) || (checkdir[1] != ':') || (checkdir[2] != '\\')) {
cd_to(maindir);
cd_to(checkdir);
if (LAST(checkdir) == '\\')
get_dir(newdir, 1);
else
get_dir(newdir, 0);
cd_to(maindir);
strcpy(checkdir, newdir);
}
return checkdir;
}
void upload_file(int dn, char *fn, char *desc, char *upby)
{
char s1[MAXPATH], ff[MAXPATH], temppath[MAXPATH], date[61];
int f;
long l;
directoryrec d;
uploadsrec u, u1;
d = directories[dn];
strcpy(u.filename, fn);
align(u.filename);
u.ownerusr = 1;
u.ownersys = 0;
u.numdloads = 0;
u.filetype = 0;
u.mask = 0;
strcpy(temppath, d.path);
make_abs_path(temppath);
sprintf(ff, "%s%s", temppath, fn);
f = sh_open1(ff, O_RDONLY | O_BINARY);
l = filelength(f);
u.numbytes = l;
sh_close(f);
strcpy(u.upby, upby);
strcpy(u.date, fdldate());
strcpy(u.description, desc);
time(&l);
u.daten = l;
get_file_idz(&u, &d);
f = sh_open(dlfn, O_RDWR | O_BINARY | O_CREAT, S_IREAD | S_IWRITE);
SETREC(f, 0);
sh_read(f, &u1, sizeof(uploadsrec));
numf = u1.numbytes;
++numf;
SETREC(f, numf);
sh_write(f, (void *) &u, sizeof(uploadsrec));
SETREC(f, 0);
sh_read(f, &u1, sizeof(uploadsrec));
u1.numbytes = numf;
u1.daten = l;
SETREC(f, 0);
sh_write(f, (void *) &u1, sizeof(uploadsrec));
f = sh_close(f);
sprintf(s1, "FDL : %s uploaded on %s", u.filename, d.name);
ssm(s1);
output("\n ■ %s", s1);
}
void free_dirs(void)
{
if (directories != NULL)
farfree(directories);
directories = NULL;
}
int read_dirs(void)
{
char fn[MAXPATH];
int f, numdirs = 0;
size_t len;
if (directories != NULL)
farfree(directories);
directories = NULL;
directories = (directoryrec *) mallocx((long) syscfg.max_dirs * (long) sizeof(directoryrec));
if (directories == NULL)
output("\n ■ Error : Insufficient memory to read all directories!");
else {
sprintf(fn, "%sDIRS.DAT", syscfg.datadir);
f = sh_open1(fn, O_RDONLY | O_BINARY);
if (f < 0)
output("\n ■ Unable to read %s.", fn);
else {
numdirs = (int) huge_xfer(f, directories, sizeof(directoryrec),
syscfg.max_dirs, 0) / sizeof(directoryrec);
len = (filelength(f) / numdirs);
if (len != sizeof(directoryrec)) {
output("\n ■ Modified directoryrec - %d bytes - no upload attempts.", len);
numdirs = -1;
}
f = sh_close(f);
}
}
return numdirs;
}
void name_file(int msg, char *newname)
{
int ok;
struct stat info;
int i;
ok = 0;
for (i = 0; ((i < 10000) && (!ok)); i++) {
sprintf(newname, "%s%s\\%s-%04.04d.MSG",
net_data, msg ? "CHECKNET" : "SPOOL",
msg ? "CHK" : "UNK", i);
if (stat(newname, &info) == -1)
ok = 1;
}
}
void move_bad(char *src)
{
char dest[MAXPATH], file[12], ext[5];
int i = 0;
fnsplit(src, NULL, NULL, file, ext);
sprintf(dest, "%sCHECKNET\\%s%s", net_data, file, ext);
while (exist(dest))
sprintf(dest, "%sCHECKNET\\%s.%03d", net_data, file, i++);
if (copyfile(0, src, dest))
unlink(src);
}
int init(void)
{
int i, i1, f;
char fn[MAXPATH];
get_dir(maindir, 1);
i = i1 = 0;
while (environ[i] != NULL) {
xenviron[i1++] = environ[i];
++i;
}
xenviron[i1] = NULL;
sprintf(fn, "%sCONFIG.DAT", maindir);
f = sh_open1(fn, O_RDONLY | O_BINARY);
if (f < 0) {
output("\n ■ Unable to read %s.", fn);
return 1;
}
sh_read(f, (void *) (&syscfg), sizeof(configrec));
sh_close(f);
return 0;
}
main(int argc, char *argv[])
{
char outname[181], buf[MAXPATH], dirfn[21], dirpath[MAXPATH], junk[80];
char *ss, argfile[21], s1[181], s2[181], s3[21], s4[21], s5[21], fn[MAXPATH];
int i, ok, ok1, dn, result;
unsigned short dirtype;
FILE *in, *out, *fp;
detect_multitask();
ok1 = 0;
if (argc == 4 || argc == 5) {
if (strcmpi(argv[1], "-encode") == 0 && argc == 4) {
if (((in = fsh_open(argv[2], "r")) != NULL) && ((out = fsh_open(argv[3], "at+")) != NULL)) {
output(" %ld bytes.", uuencode(in, out, argv[2]));
if (in != NULL)
fclose(in);
if (out != NULL)
fclose(out);
ok1 = EXIT_SUCCESS;
} else
ok1 = EXIT_FAILURE;
} else
if (strcmpi(argv[1], "-decode") == 0 && argc == 5) {
strcpy(argfile, argv[2]);
strcpy(temp_dir, argv[3]);
strcpy(net_data, argv[4]);
if (init()) {
output("\n ■ Failed to initialize UU variables.");
ok1 = EXIT_FAILURE;
}
cd_to(temp_dir);
if ((in = fsh_open(argfile, "r")) != NULL) {
net_name[0] = 0;
fdldesc[0] = 0;
fdltype = 0;
fdlfn[0] = 0;
scanfdl(in);
rewind(in);
scanfor("begin", in);
result = uudecode(in, temp_dir, outname);
if (in != NULL)
fclose(in);
switch (result) {
case UU_NO_MEM:
sprintf(s1, "%s%s", temp_dir, argfile);
move_bad(s1);
cd_to(maindir);
sprintf(s1, "Error decoding %s.", argfile);
output("\n ■ %s", s1);
ssm(s1);
sprintf(s1, "%s%s", argv[4], outname);
unlink(s1);
ok1 = EXIT_FAILURE;
break;
case UU_BAD_BEGIN:
sprintf(s1, "%s%s", temp_dir, argfile);
name_file(0, s2);
if (copyfile(0, s1, s2))
move_bad(s2);
else
unlink(s1);
cd_to(maindir);
sprintf(s1, "%s%s", argv[4], outname);
unlink(s1);
ok1 = EXIT_FAILURE;
break;
case UU_CANT_OPEN:
output("\n ■ Cannot open %s%s", argv[4], outname);
cd_to(maindir);
ok1 = EXIT_FAILURE;
break;
case UU_CHECKSUM:
sprintf(s1, "%s%s", temp_dir, argfile);
move_bad(s1);
cd_to(maindir);
sprintf(s1, "Checksum error decoding %s - moved to CHECKNET!", argfile);
output("\n ■ %s", s1);
ssm(s1);
sprintf(s1, "%s%s", argv[4], outname);
unlink(s1);
ok1 = EXIT_FAILURE;
break;
case UU_BAD_END:
sprintf(s1, "%s%s", temp_dir, argfile);
move_bad(s1);
cd_to(maindir);
sprintf(s1, "No \'end\' found in %s - moved to CHECKNET!", argfile);
output("\n ■ %s", s1);
ssm(s1);
sprintf(s1, "%s%s", argv[4], outname);
unlink(s1);
ok1 = EXIT_FAILURE;
break;
case UU_SUCCESS:
sprintf(s1, "%s%s", temp_dir, argfile);
unlink(s1);
output("\n ■ Decoded %s", stripfn(outname));
fnsplit(outname, NULL, NULL, s4, s3);
sprintf(s5, "%s%s", s4, s3);
if ((strcmpi(s3, ".NET") == NULL) && (strncmpi(argfile, "PKT", 3) == NULL)) {
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(1, s1, s2)) {
output("\n ■ Error creating %s", s2);
cd_to(maindir);
ok1 = EXIT_FAILURE;
break;
} else
unlink(s1);
} else {
if (fdltype)
sprintf(s1, "Received : %s (FDL %hu)", s5, fdltype);
else
sprintf(s1, "Received transferred file : %s.", s5);
ssm(s1);
output("\n ■ %s", s1);
sprintf(s1, "%s%s", temp_dir, s5);
num_dirs = read_dirs();
if (num_dirs) {
dn = 0;
if (fdltype) {
for (i = 0; (i < num_dirs && !dn); i++) {
if (directories[i].type == fdltype) {
dn = i;
break;
}
}
if (!dn) {
output("\n ■ FDL %hu not in DIREDIT... checking FDLFTS.CFG.",
fdltype);
sprintf(fn, "%sFDLFTS.CFG", net_data);
if ((fp = fsh_open(fn, "rt")) == NULL) {
output("\n ■ FDLFTS.CFG does not exist... uploading to Sysop.");
} else {
ok = 0;
while ((fgets(buf, 80, fp)) && !ok) {
if (buf[0] == ':')
continue;
if (strncmpi(buf, "FDL", 3) == 0) {
sscanf(buf, "%s %hu %s %s", junk, &dirtype, dirfn, dirpath);
trimstr1(dirfn);
if (dirtype == fdltype) {
for (i = 0; i < num_dirs; i++) {
if (strncmpi(directories[i].filename, dirfn, strlen(dirfn)) == 0) {
dn = i;
ok = 1;
break;
}
}
}
}
}
if (fp != NULL)
fclose(fp);
}
}
if ((!ok) && (fdltype))
output("\n ■ FDL %hu not defined in FDLFTS.CFG... uploading to Sysop.",
fdltype);
} else {
dn = 0;
strcpy(fdldesc, "[No Description Found]");
strcpy(fdlupby, "FILEnet Transfer");
sprintf(fn, "%sFDLFTS.CFG", net_data);
if ((fp = fsh_open(fn, "rt")) == NULL) {
output("\n ■ %s does not exist... upload to Sysop.", fn);
} else {
ok = 0;
while ((fgets(buf, 80, fp)) && !ok) {
if (strncmpi(buf, "NOREQUEST_DIR", 13) == 0) {
ss = strtok(buf, " ");
if (ss) {
ss = strtok(NULL, "\r\n");
if (ss) {
strcpy(dirfn, ss);
trimstr1(dirfn);
ok = 1;
for (i = 0; i < num_dirs; i++) {
if (strncmpi(directories[i].filename, dirfn, strlen(dirfn)) == 0) {
dn = i;
break;
}
}
}
}
}
}
if (fp != NULL)
fclose(fp);
}
if (!ok)
output("\n ■ NOREQUEST_DIR not defined in %s.", fn);
}
sprintf(s1, "%s%s", temp_dir, s5);
strcpy(buf, directories[dn].path);
make_abs_path(buf);
sprintf(s2, "%s%s", buf, s5);
if (exist(s2)) {
output("\n ■ %s already exists", s2);
move_bad(s1);
} else {
if (copyfile(0, s1, s2)) {
output("\n ■ Error copying %s", s2);
move_bad(s1);
} else {
dliscan(dn);
strcpy(fn, s5);
align(fn);
ok = recno(fn);
if (ok == -1)
upload_file(dn, s5, fdldesc, fdlupby);
else
output("\n ■ %s already in %s.", fn, directories[dn].name);
}
}
unlink(s1);
} else {
output("\n ■ Moving %s to CHECKNET", s1);
move_bad(s1);
}
fdltype = 0;
fdlfn[0] = 0;
fdldesc[0] = 0;
free_dirs();
}
cd_to(maindir);
ok1 = EXIT_SUCCESS;
break;
default:
sprintf(s1, "%s%s", temp_dir, argfile);
name_file(1, s2);
output("\n ■ Unknown error %s...", s2);
if (copyfile(1, s1, s2))
output("\n ■ Error during copy...");
else
unlink(s1);
cd_to(maindir);
sprintf(s1, "Unknown error %s...", s2);
ssm(s1);
ok1 = EXIT_FAILURE;
break;
}
} else {
cd_to(maindir);
output("\n ■ Input file %s not found.", argfile);
ok1 = EXIT_FAILURE;
}
}
} else
output("\n ■ %s\n", version);
if (in != NULL)
fclose(in);
if (out != NULL)
fclose(out);
return ok1;
}