home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.wwiv.com
/
ftp.wwiv.com.zip
/
ftp.wwiv.com
/
pub
/
PPPBCKP
/
SRC
/
SRC15B24.ZIP
/
UUMAIN.C
< prev
next >
Wrap
C/C++ Source or Header
|
1997-04-05
|
15KB
|
595 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 <errno.h>
#include "uu.h"
#include "net.h"
#include "vardec.h"
unsigned _stklen = 16384;
#define MAX_BUF 1024
#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 LAST(s) s[strlen(s)-1]
char buf[MAX_BUF];
int multitasker = 0;
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();
}
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));
}
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 *s)
{
int f, i, i1;
char s1[161];
shortmsgrec sm;
configrec syscfg;
sprintf(s1, "CONFIG.DAT");
f = sh_open1(s1, O_RDWR | O_BINARY);
if (f < 0)
return;
sh_read(f, (void *) (&syscfg), sizeof(configrec));
sh_close(f);
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);
}
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 (get_nb_version())
multitasker = 8;
else
detect_multitask();
switch (multitasker) {
case 1:
get_dv_version();
break;
case 2:
get_win_version();
break;
case 3:
get_dv_version();
break;
case 4:
case 5:
case 6:
case 7:
break;
case 8:
multitasker = 1;
break;
default:
break;
}
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, "%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);
sprintf(s1, "Out of memory decoding %s - moved to CHECKNET!", argv[2]);
ssm(s1);
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, "%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);
sprintf(s1, "Bad checksum decoding %s - moved to CHECKNET!", argv[2]);
ssm(s1);
exit(EXIT_FAILURE);
case UU_BAD_END:
fprintf(stderr, "\n ■ \'end\' not found in message... moving to CHECKNET");
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);
sprintf(s1, "No \'end\' found in %s - moved to CHECKNET!", argv[2]);
ssm(s1);
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, "Received file : %s!", s5);
ssm(s1);
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);
}
cd_to(cur_dir);
if (in)
fclose(in);
exit(EXIT_SUCCESS);
default:
fprintf(stderr, "\n ■ Unknown error... moving packet to CHECKNET");
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);
sprintf(s1, "Unknown error processing %s - moved to CHECKNET!", argv[2]);
ssm(s1);
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;
}