xref: /csrg-svn/usr.bin/rdist/main.c (revision 16317)
114921Sralph #ifndef lint
2*16317Sralph static	char *sccsid = "@(#)main.c	4.11 (Berkeley) 84/04/09";
314921Sralph #endif
414921Sralph 
514921Sralph #include "defs.h"
614921Sralph 
714921Sralph /*
814921Sralph  * Remote distribution program.
914921Sralph  */
1014921Sralph 
1114921Sralph char	*distfile = "distfile";
1215611Sralph char	tmpfile[] = "/tmp/rdistXXXXXX";
1315113Sralph char	*tmpname = &tmpfile[5];
1414921Sralph 
1514921Sralph int	debug;		/* debugging flag */
1614921Sralph int	nflag;		/* NOP flag, just print commands without executing */
1714921Sralph int	qflag;		/* Quiet. Don't print messages */
1815218Sralph int	options;	/* global options */
1914921Sralph int	iamremote;	/* act as remote server for transfering files */
2014921Sralph 
2114921Sralph FILE	*fin = NULL;	/* input file pointer */
2216028Sralph int	rem = -1;	/* file descriptor to remote source/sink process */
2314921Sralph char	host[32];	/* host name */
2416028Sralph int	nerrs;		/* number of errors while sending/receiving */
2514921Sralph char	user[10];	/* user's name */
2614921Sralph char	homedir[128];	/* user's home directory */
2714921Sralph int	userid;		/* user's user ID */
2815113Sralph int	groupid;	/* user's group ID */
2914921Sralph 
3015349Sralph struct	passwd *pw;	/* pointer to static area used by getpwent */
3115349Sralph struct	group *gr;	/* pointer to static area used by getgrent */
3215349Sralph 
3314921Sralph main(argc, argv)
3414921Sralph 	int argc;
3514921Sralph 	char *argv[];
3614921Sralph {
3714921Sralph 	register char *arg;
3815218Sralph 	int cmdargs = 0;
3914921Sralph 
4014921Sralph 	pw = getpwuid(userid = getuid());
4114921Sralph 	if (pw == NULL) {
4215113Sralph 		fprintf(stderr, "%s: Who are you?\n", argv[0]);
4314921Sralph 		exit(1);
4414921Sralph 	}
4514921Sralph 	strcpy(user, pw->pw_name);
4614921Sralph 	strcpy(homedir, pw->pw_dir);
4715113Sralph 	groupid = pw->pw_gid;
4814921Sralph 	gethostname(host, sizeof(host));
4914921Sralph 
5014921Sralph 	while (--argc > 0) {
5114921Sralph 		if ((arg = *++argv)[0] != '-')
5214921Sralph 			break;
5314921Sralph 		if (!strcmp(arg, "-Server"))
5414921Sralph 			iamremote++;
5514921Sralph 		else while (*++arg)
5614921Sralph 			switch (*arg) {
5714921Sralph 			case 'f':
5814921Sralph 				if (--argc <= 0)
5914921Sralph 					usage();
6014921Sralph 				distfile = *++argv;
6114921Sralph 				if (distfile[0] == '-' && distfile[1] == '\0')
6214921Sralph 					fin = stdin;
6314921Sralph 				break;
6414921Sralph 
6514921Sralph 			case 'd':
6615113Sralph 				if (--argc <= 0)
6715113Sralph 					usage();
6815113Sralph 				define(*++argv);
6915113Sralph 				break;
7015113Sralph 
7115113Sralph 			case 'D':
7214921Sralph 				debug++;
7314921Sralph 				break;
7414921Sralph 
7515218Sralph 			case 'c':
7615218Sralph 				cmdargs++;
7715218Sralph 				break;
7815218Sralph 
7914921Sralph 			case 'n':
8015611Sralph 				if (options & VERIFY) {
8115611Sralph 					printf("rdist: -n overrides -v\n");
8215611Sralph 					options &= ~VERIFY;
8315611Sralph 				}
8414921Sralph 				nflag++;
8514921Sralph 				break;
8614921Sralph 
8714921Sralph 			case 'q':
8814921Sralph 				qflag++;
8914921Sralph 				break;
9014921Sralph 
9115293Sralph 			case 'b':
9215293Sralph 				options |= COMPARE;
9315293Sralph 				break;
9415293Sralph 
9515611Sralph 			case 'R':
9615271Sralph 				options |= REMOVE;
9715271Sralph 				break;
9815271Sralph 
9914921Sralph 			case 'v':
10015611Sralph 				if (nflag) {
10115611Sralph 					printf("rdist: -n overrides -v\n");
10215611Sralph 					break;
10315611Sralph 				}
10415218Sralph 				options |= VERIFY;
10514921Sralph 				break;
10614921Sralph 
10715218Sralph 			case 'w':
10815218Sralph 				options |= WHOLE;
10915218Sralph 				break;
11015218Sralph 
11114921Sralph 			case 'y':
11215218Sralph 				options |= YOUNGER;
11314921Sralph 				break;
11414921Sralph 
11514921Sralph 			default:
11614921Sralph 				usage();
11714921Sralph 			}
11814921Sralph 	}
11915113Sralph 
12015113Sralph 	mktemp(tmpfile);
12116028Sralph 
12214921Sralph 	if (iamremote) {
12314921Sralph 		server();
12416028Sralph 		exit(nerrs);
12514921Sralph 	}
12614921Sralph 
12715218Sralph 	if (cmdargs)
12815218Sralph 		docmdargs(argc, argv);
12915218Sralph 	else {
13015218Sralph 		if (fin == NULL && (fin = fopen(distfile, "r")) == NULL) {
13115218Sralph 			perror(distfile);
13215218Sralph 			exit(1);
13315218Sralph 		}
13415218Sralph 		yyparse();
13516028Sralph 		if (nerrs == 0)
13616028Sralph 			docmds(argc, argv);
13715113Sralph 	}
13815113Sralph 
13916028Sralph 	exit(nerrs);
14014921Sralph }
14114921Sralph 
14214921Sralph usage()
14314921Sralph {
14415293Sralph 	printf("Usage: rdist [-nqbrvwyD] [-f distfile] [-d var=value] [file ...]\n");
14515293Sralph 	printf("or: rdist [-nqbrvwyD] -c source [...] machine[:dest]\n");
14614921Sralph 	exit(1);
14714921Sralph }
14814921Sralph 
14914921Sralph /*
15015218Sralph  * rcp like interface for distributing files.
15115218Sralph  */
15215218Sralph docmdargs(nargs, args)
15315218Sralph 	int nargs;
15415218Sralph 	char *args[];
15515218Sralph {
15616028Sralph 	register struct namelist *nl, *prev;
15716028Sralph 	register char *cp;
15816028Sralph 	struct namelist *files, *hosts;
15916028Sralph 	struct subcmd *cmds;
16016028Sralph 	char *dest;
16116028Sralph 	static struct namelist tnl = { NULL, NULL };
16215218Sralph 	int i;
16315218Sralph 
16415218Sralph 	if (nargs < 2)
16515218Sralph 		usage();
16615218Sralph 
16715218Sralph 	prev = NULL;
16816028Sralph 	for (i = 0; i < nargs - 1; i++) {
16916028Sralph 		nl = makenl(args[i]);
17016028Sralph 		if (prev == NULL)
17116028Sralph 			files = prev = nl;
17216028Sralph 		else {
17316028Sralph 			prev->n_next = nl;
17416028Sralph 			prev = nl;
17516028Sralph 		}
17615218Sralph 	}
17715218Sralph 
17816028Sralph 	cp = args[i];
17916028Sralph 	if ((dest = index(cp, ':')) != NULL)
18016028Sralph 		*dest++ = '\0';
18116028Sralph 	tnl.n_name = cp;
18216028Sralph 	hosts = expand(&tnl, E_ALL);
18315218Sralph 
18416028Sralph 	if (dest == NULL || *dest == '\0')
18515218Sralph 		cmds = NULL;
18615218Sralph 	else {
18716028Sralph 		cmds = makesubcmd(INSTALL);
18816028Sralph 		cmds->sc_options = options;
18916028Sralph 		cmds->sc_name = dest;
19015218Sralph 	}
19115218Sralph 
19215218Sralph 	if (debug) {
19315218Sralph 		printf("docmdargs()\nfiles = ");
19415218Sralph 		prnames(files);
19515218Sralph 		printf("hosts = ");
19615218Sralph 		prnames(hosts);
19715218Sralph 	}
198*16317Sralph 	insert(NULL, files, hosts, cmds);
19916028Sralph 	docmds(0, NULL);
20015218Sralph }
20115218Sralph 
20215218Sralph /*
20314921Sralph  * Print a list of NAME blocks (mostly for debugging).
20414921Sralph  */
20516028Sralph prnames(nl)
20616028Sralph 	register struct namelist *nl;
20714921Sralph {
20814921Sralph 	printf("( ");
20916028Sralph 	while (nl != NULL) {
21016028Sralph 		printf("%s ", nl->n_name);
21116028Sralph 		nl = nl->n_next;
21214921Sralph 	}
21314921Sralph 	printf(")\n");
21414921Sralph }
21514921Sralph 
21614921Sralph /*VARARGS*/
21714921Sralph warn(fmt, a1, a2,a3)
21814921Sralph 	char *fmt;
21914921Sralph {
22014921Sralph 	extern int yylineno;
22114921Sralph 
22214921Sralph 	fprintf(stderr, "rdist: line %d: Warning: ", yylineno);
22314921Sralph 	fprintf(stderr, fmt, a1, a2, a3);
22414921Sralph 	fputc('\n', stderr);
22514921Sralph }
226