xref: /csrg-svn/usr.bin/uucp/uux/uux.c (revision 18627)
113695Ssam #ifndef lint
2*18627Sralph static char sccsid[] = "@(#)uux.c	5.3 (Berkeley) 04/10/85";
313695Ssam #endif
413695Ssam 
513695Ssam #include "uucp.h"
613695Ssam 
713695Ssam #define NOSYSPART 0
813695Ssam #define HASSYSPART 1
913695Ssam 
1013695Ssam #define APPCMD(d) {\
1117845Sralph char *p; for (p = d; *p != '\0';) *cmdp++ = *p++; *cmdp++ = ' '; *cmdp = '\0';}
1213695Ssam 
1313695Ssam #define GENSEND(f, a, b, c, d, e) {\
1417845Sralph fprintf(f, "S %s %s %s -%s %s 0666\n", a, b, c, d, e); }
1517845Sralph #define GENRCV(f, a, b, c) {fprintf(f, "R %s %s %s - \n", a, b, c);}
1613695Ssam 
1713695Ssam main(argc, argv)
1813695Ssam char *argv[];
1913695Ssam {
2013695Ssam 	char cfile[NAMESIZE];	/* send commands for files from here */
2113695Ssam 	char dfile[NAMESIZE];	/* used for all data files from here */
2213695Ssam 	char rxfile[NAMESIZE];	/* to be sent to xqt file (X. ...) */
2313695Ssam 	char tfile[NAMESIZE];	/* temporary file name */
2413695Ssam 	char tcfile[NAMESIZE];	/* temporary file name */
2513695Ssam 	char t2file[NAMESIZE];	/* temporary file name */
2613695Ssam 	int cflag = 0;		/*  commands in C. file flag  */
2713695Ssam 	int rflag = 0;		/*  C. files for receiving flag  */
2817845Sralph #ifdef DONTCOPY
2917845Sralph 	int Copy = 0;		/* Don't Copy spool files */
3017845Sralph #else !DONTCOPY
3113695Ssam 	int Copy = 1;		/* Copy spool files */
3217845Sralph #endif !DONTCOPY
3317845Sralph 	int Linkit = 0;		/* Try link before copy */
3413695Ssam 	char buf[BUFSIZ];
3513695Ssam 	char inargs[BUFSIZ];
3613695Ssam 	int pipein = 0;
3713695Ssam 	int startjob = 1;
3813695Ssam 	char Grade = 'A';
3913695Ssam 	char path[MAXFULLNAME];
4013695Ssam 	char cmd[BUFSIZ];
4113695Ssam 	char *ap, *cmdp;
4213695Ssam 	char prm[BUFSIZ];
4313695Ssam 	char syspart[8], rest[MAXFULLNAME];
44*18627Sralph 	char Xsys[8], local[8];
45*18627Sralph 	char *xsys = Xsys;
4613695Ssam 	FILE *fprx, *fpc, *fpd, *fp;
4713695Ssam 	extern char *getprm(), *lastpart();
4813695Ssam 	extern FILE *ufopen();
4913695Ssam 	int uid, ret;
5013695Ssam 	char redir = '\0';
5113695Ssam 	int nonoti = 0;
5213695Ssam 	int nonzero = 0;
5317845Sralph 	int link_failed;
5417845Sralph 	char *ReturnTo = NULL;
5517845Sralph 	extern int LocalOnly;
5613695Ssam 
5713695Ssam 	strcpy(Progname, "uux");
5813695Ssam 	uucpname(Myname);
5913695Ssam 	umask(WFMASK);
6013695Ssam 	Ofn = 1;
6113695Ssam 	Ifn = 0;
6217845Sralph #ifdef	VMS
6317845Sralph 	arg_fix(argc, argv);
6417845Sralph #endif
6513695Ssam 	while (argc>1 && argv[1][0] == '-') {
6613695Ssam 		switch(argv[1][1]){
6713695Ssam 		case 'p':
6813695Ssam 		case '\0':
6913695Ssam 			pipein = 1;
7013695Ssam 			break;
7113695Ssam 		case 'r':
7213695Ssam 			startjob = 0;
7313695Ssam 			break;
7413695Ssam 		case 'c':
7517845Sralph 			Copy = 0;
7617845Sralph 			Linkit = 0;
7717845Sralph 			break;
7813695Ssam 		case 'l':
7913695Ssam 			Copy = 0;
8017845Sralph 			Linkit = 1;
8113695Ssam 			break;
8217845Sralph 		case 'C':
8317845Sralph 			Copy = 1;
8417845Sralph 			Linkit = 0;
8517845Sralph 			break;
8613695Ssam 		case 'g':
8713695Ssam 			Grade = argv[1][2];
8813695Ssam 			break;
8913695Ssam 		case 'x':
9017845Sralph 			chkdebug();
9113695Ssam 			Debug = atoi(&argv[1][2]);
9213695Ssam 			if (Debug <= 0)
9313695Ssam 				Debug = 1;
9413695Ssam 			break;
9513695Ssam 		case 'n':
9613695Ssam 			nonoti = 1;
9713695Ssam 			break;
9813695Ssam 		case 'z':
9913695Ssam 			nonzero = 1;
10013695Ssam 			break;
10117845Sralph 		case 'L':
10217845Sralph 			LocalOnly++;
10317845Sralph 			break;
10417845Sralph 		case 'a':
10517845Sralph 			ReturnTo = &argv[1][2];
10617845Sralph 			break;
10713695Ssam 		default:
10813695Ssam 			fprintf(stderr, "unknown flag %s\n", argv[1]);
10913695Ssam 				break;
11013695Ssam 		}
11113695Ssam 		--argc;  argv++;
11213695Ssam 	}
11317845Sralph 	if (argc > 2) {
11417845Sralph 		ret = gwd(Wrkdir);
11517845Sralph 		if (ret != 0) {
11617845Sralph 			fprintf(stderr, "can't get working directory; will try to continue\n");
11717845Sralph 			strcpy(Wrkdir, "/UNKNOWN");
11817845Sralph 		}
11917845Sralph 	}
12013695Ssam 
12113695Ssam 	DEBUG(4, "\n\n** %s **\n", "START");
12213695Ssam 
12313695Ssam 	inargs[0] = '\0';
12413695Ssam 	for (argv++; argc > 1; argc--) {
12513695Ssam 		DEBUG(4, "arg - %s:", *argv);
12613695Ssam 		strcat(inargs, " ");
12713695Ssam 		strcat(inargs, *argv++);
12813695Ssam 	}
12913695Ssam 	DEBUG(4, "arg - %s\n", inargs);
13017845Sralph 	ret = subchdir(Spool);
13117845Sralph 	ASSERT(ret >= 0, "CHDIR FAILED", Spool, ret);
13213695Ssam 	uid = getuid();
13313695Ssam 	guinfo(uid, User, path);
13413695Ssam 
13513695Ssam 	sprintf(local, "%.7s", Myname);
13613695Ssam 	cmdp = cmd;
13713695Ssam 	*cmdp = '\0';
13813695Ssam 	gename(DATAPRE, local, 'X', rxfile);
13913695Ssam 	fprx = ufopen(rxfile, "w");
14013695Ssam 	ASSERT(fprx != NULL, "CAN'T OPEN", rxfile, 0);
14113695Ssam 	gename(DATAPRE, local, 'T', tcfile);
14213695Ssam 	fpc = ufopen(tcfile, "w");
14313695Ssam 	ASSERT(fpc != NULL, "CAN'T OPEN", tcfile, 0);
14413695Ssam 	fprintf(fprx, "%c %s %s\n", X_USER, User, local);
14513695Ssam 	if (nonoti)
14613695Ssam 		fprintf(fprx, "%c\n", X_NONOTI);
14713695Ssam 	if (nonzero)
14813695Ssam 		fprintf(fprx, "%c\n", X_NONZERO);
14917845Sralph 	if (ReturnTo == NULL || *ReturnTo == '\0')
15017845Sralph 		ReturnTo = User;
15117845Sralph 	fprintf(fprx, "%c %s\n", X_RETURNTO, ReturnTo);
15213695Ssam 
15313695Ssam 	/* find remote system name */
15413695Ssam 	ap = inargs;
15513695Ssam 	xsys[0] = '\0';
15613695Ssam 	while ((ap = getprm(ap, prm)) != NULL) {
15713695Ssam 		if (prm[0] == '>' || prm[0] == '<') {
15813695Ssam 			ap = getprm(ap, prm);
15913695Ssam 			continue;
16013695Ssam 		}
16113695Ssam 
16213695Ssam 
16313695Ssam 		split(prm, xsys, rest);
16413695Ssam 		break;
16513695Ssam 	}
16613695Ssam 	if (xsys[0] == '\0')
16713695Ssam 		strcpy(xsys, local);
16813695Ssam 	sprintf(Rmtname, "%.7s", xsys);
16913695Ssam 	DEBUG(4, "xsys %s\n", xsys);
170*18627Sralph 	if (versys(&xsys) != 0) {
17113695Ssam 		/*  bad system name  */
17213695Ssam 		fprintf(stderr, "bad system name: %s\n", xsys);
17313695Ssam 		fclose(fprx);
17413695Ssam 		fclose(fpc);
17513695Ssam 		cleanup(EX_NOHOST);
17613695Ssam 	}
17713695Ssam 
17813695Ssam 	if (pipein) {
17913695Ssam 		gename(DATAPRE, local, 'B', dfile);
18013695Ssam 		fpd = ufopen(dfile, "w");
18113695Ssam 		ASSERT(fpd != NULL, "CAN'T OPEN", dfile, 0);
18213695Ssam 		while (!feof(stdin)) {
18313695Ssam 			ret = fread(buf, 1, BUFSIZ, stdin);
18413695Ssam 			fwrite(buf, 1, ret, fpd);
18513695Ssam 		}
18613695Ssam 		fclose(fpd);
18717845Sralph 		strcpy(tfile, dfile);
18813695Ssam 		if (strcmp(local, xsys) != SAME) {
18917845Sralph 			tfile[strlen(local) + 2] = 'S';
19017845Sralph 			GENSEND(fpc, dfile, tfile, User, "", dfile);
19113695Ssam 			cflag++;
19213695Ssam 		}
19317845Sralph 		fprintf(fprx, "%c %s\n", X_RQDFILE, tfile);
19417845Sralph 		fprintf(fprx, "%c %s\n", X_STDIN, tfile);
19513695Ssam 	}
19613695Ssam 	/* parse command */
19713695Ssam 	ap = inargs;
19813695Ssam 	while ((ap = getprm(ap, prm)) != NULL) {
19913695Ssam 		DEBUG(4, "prm - %s\n", prm);
20013695Ssam 		if (prm[0] == '>' || prm[0] == '<') {
20113695Ssam 			redir = prm[0];
20213695Ssam 			continue;
20313695Ssam 		}
20413695Ssam 
20513695Ssam 		if (prm[0] == ';') {
20613695Ssam 			APPCMD(prm);
20713695Ssam 			continue;
20813695Ssam 		}
20913695Ssam 
21013695Ssam 		if (prm[0] == '|' || prm[0] == '^') {
21113695Ssam 			if (cmdp != cmd)
21213695Ssam 				APPCMD(prm);
21313695Ssam 			continue;
21413695Ssam 		}
21513695Ssam 
21613695Ssam 		/* process command or file or option */
21713695Ssam 		ret = split(prm, syspart, rest);
21813695Ssam 		DEBUG(4, "s - %s, ", syspart);
21913695Ssam 		DEBUG(4, "r - %s, ", rest);
22013695Ssam 		DEBUG(4, "ret - %d\n", ret);
22113695Ssam 		if (syspart[0] == '\0')
22213695Ssam 			strcpy(syspart, local);
22313695Ssam 
22413695Ssam 		if (cmdp == cmd && redir == '\0') {
22513695Ssam 			/* command */
22613695Ssam 			APPCMD(rest);
22713695Ssam 			continue;
22813695Ssam 		}
22913695Ssam 
23013695Ssam 		/* process file or option */
23113695Ssam 		DEBUG(4, "file s- %s, ", syspart);
23213695Ssam 		DEBUG(4, "local - %s\n", local);
23313695Ssam 		/* process file */
23413695Ssam 		if (redir == '>') {
23513695Ssam 			if (rest[0] != '~')
23613695Ssam 				if (ckexpf(rest))
23713695Ssam 					cleanup(EX_CANTCREAT);
23813695Ssam 			fprintf(fprx, "%c %s %s\n", X_STDOUT, rest,
23913695Ssam 			 syspart);
24013695Ssam 			redir = '\0';
24113695Ssam 			continue;
24213695Ssam 		}
24313695Ssam 
24413695Ssam 		if (ret == NOSYSPART && redir == '\0') {
24513695Ssam 			/* option */
24613695Ssam 			APPCMD(rest);
24713695Ssam 			continue;
24813695Ssam 		}
24913695Ssam 
25013695Ssam 		if (strcmp(xsys, local) == SAME
25113695Ssam 		 && strcmp(xsys, syspart) == SAME) {
25213695Ssam 			if (ckexpf(rest))
25313695Ssam 				cleanup(EX_CANTCREAT);
25413695Ssam 			if (redir == '<')
25513695Ssam 				fprintf(fprx, "%c %s\n", X_STDIN, rest);
25613695Ssam 			else
25713695Ssam 				APPCMD(rest);
25813695Ssam 			redir = '\0';
25913695Ssam 			continue;
26013695Ssam 		}
26113695Ssam 
26213695Ssam 		if (strcmp(syspart, local) == SAME) {
26313695Ssam 			/*  generate send file */
26413695Ssam 			if (ckexpf(rest))
26513695Ssam 				cleanup(EX_CANTCREAT);
26613695Ssam 			gename(DATAPRE, local, 'A', dfile);
26713695Ssam 			DEBUG(4, "rest %s\n", rest);
26813695Ssam 			if ((chkpth(User, "", rest) || anyread(rest)) != 0) {
26913695Ssam 				fprintf(stderr, "permission denied %s\n", rest);
27013695Ssam 				cleanup(EX_NOINPUT);
27113695Ssam 			}
27217845Sralph 			link_failed = 0;
27317845Sralph 			if (Linkit) {
27417845Sralph 				if (link(subfile(rest), subfile(dfile)) != 0)
27517845Sralph 					link_failed++;
27617845Sralph 				else
27717845Sralph 					GENSEND(fpc, rest, dfile, User, "", dfile);
27817845Sralph 			}
27917845Sralph 			if (Copy || link_failed) {
28013695Ssam 				if (xcp(rest, dfile) != 0) {
28113695Ssam 					fprintf(stderr, "can't copy %s to %s\n", rest, dfile);
28213695Ssam 					cleanup(EX_NOINPUT);
28313695Ssam 				}
28413695Ssam 				GENSEND(fpc, rest, dfile, User, "", dfile);
28513695Ssam 			}
28617845Sralph 			if (!Copy && !Linkit) {
28713695Ssam 				GENSEND(fpc, rest, dfile, User, "c", "D.0");
28813695Ssam 			}
28913695Ssam 			cflag++;
29013695Ssam 			if (redir == '<') {
29113695Ssam 				fprintf(fprx, "%c %s\n", X_STDIN, dfile);
29213695Ssam 				fprintf(fprx, "%c %s\n", X_RQDFILE, dfile);
293*18627Sralph 			} else {
29413695Ssam 				APPCMD(lastpart(rest));
29513695Ssam 				fprintf(fprx, "%c %s %s\n", X_RQDFILE,
29613695Ssam 				 dfile, lastpart(rest));
29713695Ssam 			}
29813695Ssam 			redir = '\0';
29913695Ssam 			continue;
30013695Ssam 		}
30113695Ssam 
30213695Ssam 		if (strcmp(local, xsys) == SAME) {
30313695Ssam 			/*  generate local receive  */
30413695Ssam 			gename(CMDPRE, syspart, 'R', tfile);
30513695Ssam 			strcpy(dfile, tfile);
30613695Ssam 			dfile[0] = DATAPRE;
30713695Ssam 			fp = ufopen(tfile, "w");
30813695Ssam 			ASSERT(fp != NULL, "CAN'T OPEN", tfile, 0);
30913695Ssam 			if (ckexpf(rest))
31013695Ssam 				cleanup(EX_CANTCREAT);
31113695Ssam 			GENRCV(fp, rest, dfile, User);
31213695Ssam 			fclose(fp);
31313695Ssam 			rflag++;
31413695Ssam 			if (rest[0] != '~')
31513695Ssam 				if (ckexpf(rest))
31613695Ssam 					cleanup(EX_CANTCREAT);
31713695Ssam 			if (redir == '<') {
31813695Ssam 				fprintf(fprx, "%c %s\n", X_RQDFILE, dfile);
31913695Ssam 				fprintf(fprx, "%c %s\n", X_STDIN, dfile);
320*18627Sralph 			} else {
32113695Ssam 				fprintf(fprx, "%c %s %s\n", X_RQDFILE, dfile,
32213695Ssam 				  lastpart(rest));
32313695Ssam 				APPCMD(lastpart(rest));
32413695Ssam 			}
32513695Ssam 
32613695Ssam 			redir = '\0';
32713695Ssam 			continue;
32813695Ssam 		}
32913695Ssam 
33013695Ssam 		if (strcmp(syspart, xsys) != SAME) {
33113695Ssam 			/* generate remote receives */
33213695Ssam 			gename(DATAPRE, syspart, 'R', dfile);
33313695Ssam 			strcpy(tfile, dfile);
33413695Ssam 			tfile[0] = CMDPRE;
33513695Ssam 			fpd = ufopen(dfile, "w");
33613695Ssam 			ASSERT(fpd != NULL, "CAN'T OPEN", dfile, 0);
33713695Ssam 			gename(DATAPRE, local, 'T', t2file);
33813695Ssam 			GENRCV(fpd, rest, t2file, User);
33913695Ssam 			fclose(fpd);
34013695Ssam 			GENSEND(fpc, dfile, tfile, User, "", dfile);
34113695Ssam 			cflag++;
34213695Ssam 			if (redir == '<') {
34313695Ssam 				fprintf(fprx, "%c %s\n", X_RQDFILE, t2file);
34413695Ssam 				fprintf(fprx, "%c %s\n", X_STDIN, t2file);
345*18627Sralph 			} else {
34613695Ssam 				fprintf(fprx, "%c %s %s\n", X_RQDFILE, t2file,
34713695Ssam 				  lastpart(rest));
34813695Ssam 				APPCMD(lastpart(rest));
34913695Ssam 			}
35013695Ssam 			redir = '\0';
35113695Ssam 			continue;
35213695Ssam 		}
35313695Ssam 
35413695Ssam 		/* file on remote system */
35513695Ssam 		if (rest[0] != '~')
35613695Ssam 			if (ckexpf(rest))
35713695Ssam 				cleanup(EX_CANTCREAT);
35813695Ssam 		if (redir == '<')
35913695Ssam 			fprintf(fprx, "%c %s\n", X_STDIN, rest);
36013695Ssam 		else
36113695Ssam 			APPCMD(rest);
36213695Ssam 		redir = '\0';
36313695Ssam 		continue;
36413695Ssam 
36513695Ssam 	}
36617845Sralph 	/*
36717845Sralph 	 * clean up trailing ' ' in command.
36817845Sralph 	 */
36917845Sralph 	if (cmdp > cmd && cmdp[0] == '\0' && cmdp[-1] == ' ')
37017845Sralph 		*--cmdp = '\0';
37117845Sralph 	/* block multi-hop uux, which doesn't work */
37217845Sralph 	for (ap = cmd; *ap && *ap != ' '; ap++)
37317845Sralph 		if (*ap == '!') {
37417845Sralph 			fprintf(stderr, "uux handles only adjacent sites.\n");
37517845Sralph 			fprintf(stderr, "Try uusend for multi-hop delivery.\n");
37617845Sralph 			cleanup(1);
37717845Sralph 		}
37813695Ssam 
37913695Ssam 	fprintf(fprx, "%c %s\n", X_CMD, cmd);
38013695Ssam 	logent(cmd, "XQT QUE'D");
38113695Ssam 	fclose(fprx);
38213695Ssam 
38317845Sralph 	gename(XQTPRE, local, Grade, tfile);
38413695Ssam 	if (strcmp(xsys, local) == SAME) {
38513695Ssam 		/* rti!trt: xmv() works across filesystems, link(II) doesnt */
38613695Ssam 		xmv(rxfile, tfile);
38713695Ssam 		if (startjob)
38813695Ssam 			if (rflag)
38913695Ssam 				xuucico(xsys);
39013695Ssam 			else
39113695Ssam 				xuuxqt();
39213695Ssam 	}
39313695Ssam 	else {
39413695Ssam 		GENSEND(fpc, rxfile, tfile, User, "", rxfile);
39513695Ssam 		cflag++;
39613695Ssam 	}
39713695Ssam 
39813695Ssam 	fclose(fpc);
39913695Ssam 	if (cflag) {
40013695Ssam 		gename(CMDPRE, xsys, Grade, cfile);
40113695Ssam 		/* rti!trt: use xmv() rather than link(II) */
40213695Ssam 		xmv(tcfile, cfile);
40313695Ssam 		if (startjob)
40413695Ssam 			xuucico(xsys);
40513695Ssam 		cleanup(0);
40613695Ssam 	}
40713695Ssam 	else
40813695Ssam 		unlink(subfile(tcfile));
409*18627Sralph 	exit(0);
41013695Ssam }
41113695Ssam 
41213695Ssam #define FTABSIZE 30
41313695Ssam char Fname[FTABSIZE][NAMESIZE];
41413695Ssam int Fnamect = 0;
41513695Ssam 
41617845Sralph /*
41717845Sralph  *	cleanup and unlink if error
41813695Ssam  *
41913695Ssam  *	return - none - do exit()
42013695Ssam  */
42113695Ssam 
42213695Ssam cleanup(code)
42313695Ssam int code;
42413695Ssam {
42513695Ssam 	int i;
42613695Ssam 
42713695Ssam 	logcls();
42813695Ssam 	rmlock(CNULL);
42913695Ssam 	if (code) {
43013695Ssam 		for (i = 0; i < Fnamect; i++)
43113695Ssam 			unlink(subfile(Fname[i]));
43213695Ssam 		fprintf(stderr, "uux failed. code %d\n", code);
43313695Ssam 	}
43413695Ssam 	DEBUG(1, "exit code %d\n", code);
43513695Ssam 	exit(code);
43613695Ssam }
43713695Ssam 
43817845Sralph /*
43917845Sralph  *	open file and record name
44013695Ssam  *
44113695Ssam  *	return file pointer.
44213695Ssam  */
44313695Ssam 
44413695Ssam FILE *ufopen(file, mode)
44513695Ssam char *file, *mode;
44613695Ssam {
44713695Ssam 	if (Fnamect < FTABSIZE)
44813695Ssam 		strcpy(Fname[Fnamect++], file);
44913695Ssam 	else
45013695Ssam 		logent("Fname", "TABLE OVERFLOW");
45117845Sralph 	return fopen(subfile(file), mode);
45213695Ssam }
45317845Sralph #ifdef	VMS
45417845Sralph /*
45517845Sralph  * EUNICE bug:
45617845Sralph  *	quotes are not stripped from DCL.  Do it here.
45717845Sralph  *	Note if we are running under Unix shell we don't
45817845Sralph  *	do the right thing.
45917845Sralph  */
46017845Sralph arg_fix(argc, argv)
46117845Sralph char **argv;
46217845Sralph {
46317845Sralph 	register char *cp, *tp;
46417845Sralph 
46517845Sralph 	for (; argc > 0; --argc, argv++) {
46617845Sralph 		cp = *argv;
46717845Sralph 		if (cp == (char *)0 || *cp++ != '"')
46817845Sralph 			continue;
46917845Sralph 		tp = cp;
47017845Sralph 		while (*tp++) ;
47117845Sralph 		tp -= 2;
47217845Sralph 		if (*tp == '"') {
47317845Sralph 			*tp = '\0';
47417845Sralph 			*argv = cp;
47517845Sralph 		}
47617845Sralph 	}
47717845Sralph }
47817845Sralph #endif VMS
479