home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
rtsi.com
/
2014.01.www.rtsi.com.tar
/
www.rtsi.com
/
OS9
/
OSK
/
TELECOM
/
UUCP_Blars.lzh
/
uucp.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-11-02
|
7KB
|
306 lines
/*
* uucp.c
*
* For OS-9/6809 by Mark D. Griffith
* Copyright (c) 1990
*
*/
/* Includes */
#include "uucp.h"
#include <stdio.h>
#include <modes.h>
#include <dir.h>
#include <strings.h>
#include <direct.h>
#include <errno.h>
#include <time.h>
#include <pwd.h>
#define TRUE (1)
#define FALSE (0)
#define ERROR (-1)
/* Globals */
#define BUFSIZE 16385 /* copy file buffer */
struct fildes outfdbuf, infdbuf;
FILE *fdfile, *fcfile, *fbfile, *log, *fp, *popen();
char *mfgets(), *ltoa();
int isremote, bflag, cflag, nflag, nfiles;
char Filename[200][80], copy_buf[BUFSIZE];
char bfile[32], cfile[32], dfile[32], dest_file[32], cur_path[128];
char rmtname[25], rmtuser[128], myname[25], user[25];
char n_user[25], from[256], options[10], tmp[256];
char *p, *ptr;
int uucpuid, orguid;
char grade = 'Z';
main(argc, argv)
int argc;
char **argv;
{
int i = 0;
struct passwd *pwentp;
bflag = FALSE;
cflag = FALSE;
nflag = FALSE;
nfiles = 0;
strcpy(options, "-dC");
*n_user = '\0';
if (argc < 3)
usage();
if (getwd(cur_path) == NULL) {
printf("\nUnable to determine current directory path\n");
exit(1);
}
strcat(cur_path, "/");
/* Find out who we are */
pwentp = getpwuid(orguid = getuid());
if (pwentp == NULL) {
fprintf(stderr, "Could not get user name\n");
exit(1);
}
strcpy(user, pwentp->pw_name);
pwentp = getpwname("uucp");
if (pwentp == NULL) {
fprintf(stderr, "Could not get uucp's uid\n");
exit(1);
}
uucpuid = pwentp->pw_gid << 16 | pwentp->pw_uid;
endpwent();
strcpy(n_user, user);
/* * Parse the command line arguements */
while ((--argc > 0)) {
if (*(p = *++argv) == '-') {
while (*++p) {
switch (*p) {
case 'c': /* don't copy source file */
cflag = TRUE;
options[2] = 'c';
break;
case 'n': /* mail remote user when done */
strcat(options, "n");
while (*++p == ' ') /* skip any leading white space */
;
strcpy(n_user, p);
p += (strlen(n_user) + 1);
break;
case 'b': /* force a binary file */
bflag = TRUE;
break;
case 'g': /* specify grade */
grade = *++p;
break;
case '?':
usage();
break;
default:
fprintf(stderr, "uucp - unknown flag : -%c\n", *p);
}
}
} else {
if ((ptr = index(p, '!')) == NULL) { /* if not the remote */
if (*p != '/' && *p != '~') { /* if not a full */
strcpy(Filename[nfiles], cur_path); /* pathname, prepend */
strcat(Filename[nfiles++], p); /* dir path */
} else
strcpy(Filename[nfiles++], p); /* copy the filename */
isremote = FALSE;
} else {
*ptr = '\0'; /* got the remote name */
isremote = TRUE;
strcpy(rmtname, p); /* separate into rmote */
strcpy(rmtuser, ptr + 1); /* and remote usename */
}
}
} /* end of outer while */
/* See if we talk to requested remote machine */
if (CheckLegalName(rmtname) == FALSE) {
printf("\nRemote system '%s' not known\n", rmtname);
exit(0);
}
/* Change to the spool directory */
setuid(uucpuid);
if (chdir(SPOOLDIR) < 0 || (chdir(rmtname) < 0 &&
(makdir(rmtname, S_IFDIR, S_IFDIR | S_IREAD | S_IWRITE) < 0 ||
chdir(rmtname) < 0))) {
fprintf(stderr, "Can't chdir to spool directory for %s\n", rmtname);
exit(1);
}
/* * Create the C. work file for the transfer */
sprintf(cfile, "C.%.7s%c%04x", rmtname, grade, getseq());
if ((fcfile = fopen(cfile, "w")) == NULL) {
fprintf(stderr, "Can't create %s\n", cfile);
exit(1);
}
setuid(orguid);
for (i = nfiles - 1; i > -1; i--) {
if ((p = rindex(Filename[i], '/')) != 0)
strcpy(dest_file, ++p);
else
strcpy(dest_file, Filename[i]);
if (isremote) {
if (cflag) {
sprintf(tmp, "S %s %s %s %s %s 644 %s\012", Filename[i],
rmtuser, user, options, dest_file, n_user);
} else {
sprintf(cur_path, "%s/%s/T.%.7s%04x", SPOOLDIR, rmtname,
rmtname, getseq());
copy(Filename[i], cur_path); /* copy file into spool dir */
sprintf(tmp, "S %s %s %s %s %s 644 %s\012", cur_path, rmtuser,
user, options, dest_file, n_user);
}
} else {
sprintf(tmp, "R %s %s %s %s dummy 777 %s\012", rmtuser,
Filename[i], user, options, n_user);
}
fwrite(tmp, sizeof(char), strlen(tmp), fcfile);
}
fclose(fcfile);
}
usage()
{
fprintf(stderr, "\n\
Usage: uucp [flags] filename [remote!]user\n\
-c don't copy source file (forces -b)\n\
-n mail remote user when done\n\
-b binary file\n");
exit(0);
}
/*
* Check the machine name by reading the SYSTEMS file
*
* returns FALSE if not found, else TRUE
*
*/
CheckLegalName(name)
register char *name;
{
FILE *fd;
char line[132], tmp[16];
if ((fd = fopen(SYSTEMS, "r")) == NULL)
return (FALSE);
while (mfgets(line, sizeof(line), fd) != NULL) {
sscanf(line, "%s ", tmp);
if (strnucmp(name, tmp, strlen(name)) == 0) {
fclose(fd);
return (TRUE);
}
}
fclose(fd);
return (FALSE);
}
copy(from, to)
char *from, *to;
{
register char *cp;
register int n, ipn, opn;
if ((ipn = open(from, 1)) == ERROR) {
printf("Unable to open %s .... aborting\n", from);
exit(0);
}
if ((_gs_gfd(ipn, &infdbuf, sizeof(infdbuf))) == ERROR) {
close(ipn);
fprintf(stderr, "Can't get file descriptor data from %s .... aborting\n", from);
exit(1);
}
setuid(uucpuid);
if ((opn = create(to, S_IWRITE | S_ISIZE, S_IREAD | S_IWRITE,
_gs_size(ipn))) == ERROR) {
close(ipn);
fprintf(stderr, "Unable to copy '%s' to '%s' aborting\n", from, to);
exit(1);
}
setuid(orguid);
printf("Copying %s to %s mode %s\n", from, to,
bflag ? "binary" : "ascii");
while ((n = read(ipn, copy_buf, BUFSIZE)) > 0) {
if (!bflag) {
for (cp = copy_buf; cp < ©_buf[n]; cp++) {
if (*cp == '\n')
*cp = '\l';
}
}
if ((write(opn, copy_buf, n)) != n) {
close(ipn);
close(opn);
unlink(to);
printf("Error# %d while copying %s .... aborting\n", errno, from);
exit(0);
}
}
close(ipn);
close(opn);
}
/* strnucmp
* compares s2 to s1 thru null on s2 or at most n chars
* returns 0 if equalthru null, or s1 > or < s2 at the nth char
*/
strnucmp(s1, s2, n)
register char *s1;
register char *s2;
register int n;
{
while ((n-- > 0) && (toupper(*s1) == toupper(*s2))) {
if (*s2++ == 0)
return (0);
s1++;
}
return ((n < 0) ? 0 : (toupper(*s1) - toupper(*s2)));
}
/* EOF */