xref: /csrg-svn/usr.bin/rdist/main.c (revision 16028)
114921Sralph #ifndef lint
2*16028Sralph static	char *sccsid = "@(#)main.c	4.10 (Berkeley) 84/02/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 int	filec;		/* number of files to update */
2214921Sralph char	**filev;	/* list of files/directories to update */
2314921Sralph FILE	*fin = NULL;	/* input file pointer */
24*16028Sralph int	rem = -1;	/* file descriptor to remote source/sink process */
2514921Sralph char	host[32];	/* host name */
26*16028Sralph int	nerrs;		/* number of errors while sending/receiving */
2714921Sralph char	user[10];	/* user's name */
2814921Sralph char	homedir[128];	/* user's home directory */
2914921Sralph int	userid;		/* user's user ID */
3015113Sralph int	groupid;	/* user's group ID */
3114921Sralph 
3215349Sralph struct	passwd *pw;	/* pointer to static area used by getpwent */
3315349Sralph struct	group *gr;	/* pointer to static area used by getgrent */
3415349Sralph 
3514921Sralph main(argc, argv)
3614921Sralph 	int argc;
3714921Sralph 	char *argv[];
3814921Sralph {
3914921Sralph 	register char *arg;
4015218Sralph 	int cmdargs = 0;
4114921Sralph 
4214921Sralph 	pw = getpwuid(userid = getuid());
4314921Sralph 	if (pw == NULL) {
4415113Sralph 		fprintf(stderr, "%s: Who are you?\n", argv[0]);
4514921Sralph 		exit(1);
4614921Sralph 	}
4714921Sralph 	strcpy(user, pw->pw_name);
4814921Sralph 	strcpy(homedir, pw->pw_dir);
4915113Sralph 	groupid = pw->pw_gid;
5014921Sralph 	gethostname(host, sizeof(host));
5114921Sralph 
5214921Sralph 	while (--argc > 0) {
5314921Sralph 		if ((arg = *++argv)[0] != '-')
5414921Sralph 			break;
5514921Sralph 		if (!strcmp(arg, "-Server"))
5614921Sralph 			iamremote++;
5714921Sralph 		else while (*++arg)
5814921Sralph 			switch (*arg) {
5914921Sralph 			case 'f':
6014921Sralph 				if (--argc <= 0)
6114921Sralph 					usage();
6214921Sralph 				distfile = *++argv;
6314921Sralph 				if (distfile[0] == '-' && distfile[1] == '\0')
6414921Sralph 					fin = stdin;
6514921Sralph 				break;
6614921Sralph 
6714921Sralph 			case 'd':
6815113Sralph 				if (--argc <= 0)
6915113Sralph 					usage();
7015113Sralph 				define(*++argv);
7115113Sralph 				break;
7215113Sralph 
7315113Sralph 			case 'D':
7414921Sralph 				debug++;
7514921Sralph 				break;
7614921Sralph 
7715218Sralph 			case 'c':
7815218Sralph 				cmdargs++;
7915218Sralph 				break;
8015218Sralph 
8114921Sralph 			case 'n':
8215611Sralph 				if (options & VERIFY) {
8315611Sralph 					printf("rdist: -n overrides -v\n");
8415611Sralph 					options &= ~VERIFY;
8515611Sralph 				}
8614921Sralph 				nflag++;
8714921Sralph 				break;
8814921Sralph 
8914921Sralph 			case 'q':
9014921Sralph 				qflag++;
9114921Sralph 				break;
9214921Sralph 
9315293Sralph 			case 'b':
9415293Sralph 				options |= COMPARE;
9515293Sralph 				break;
9615293Sralph 
9715611Sralph 			case 'R':
9815271Sralph 				options |= REMOVE;
9915271Sralph 				break;
10015271Sralph 
10114921Sralph 			case 'v':
10215611Sralph 				if (nflag) {
10315611Sralph 					printf("rdist: -n overrides -v\n");
10415611Sralph 					break;
10515611Sralph 				}
10615218Sralph 				options |= VERIFY;
10714921Sralph 				break;
10814921Sralph 
10915218Sralph 			case 'w':
11015218Sralph 				options |= WHOLE;
11115218Sralph 				break;
11215218Sralph 
11314921Sralph 			case 'y':
11415218Sralph 				options |= YOUNGER;
11514921Sralph 				break;
11614921Sralph 
11714921Sralph 			default:
11814921Sralph 				usage();
11914921Sralph 			}
12014921Sralph 	}
12115113Sralph 
12215113Sralph 	mktemp(tmpfile);
123*16028Sralph 
12414921Sralph 	if (iamremote) {
12514921Sralph 		server();
126*16028Sralph 		exit(nerrs);
12714921Sralph 	}
12814921Sralph 
12915218Sralph 	if (cmdargs)
13015218Sralph 		docmdargs(argc, argv);
13115218Sralph 	else {
13215218Sralph 		if (fin == NULL && (fin = fopen(distfile, "r")) == NULL) {
13315218Sralph 			perror(distfile);
13415218Sralph 			exit(1);
13515218Sralph 		}
13615218Sralph 		yyparse();
137*16028Sralph 		if (nerrs == 0)
138*16028Sralph 			docmds(argc, argv);
13915113Sralph 	}
14015113Sralph 
141*16028Sralph 	exit(nerrs);
14214921Sralph }
14314921Sralph 
14414921Sralph usage()
14514921Sralph {
14615293Sralph 	printf("Usage: rdist [-nqbrvwyD] [-f distfile] [-d var=value] [file ...]\n");
14715293Sralph 	printf("or: rdist [-nqbrvwyD] -c source [...] machine[:dest]\n");
14814921Sralph 	exit(1);
14914921Sralph }
15014921Sralph 
15114921Sralph /*
15215218Sralph  * rcp like interface for distributing files.
15315218Sralph  */
15415218Sralph docmdargs(nargs, args)
15515218Sralph 	int nargs;
15615218Sralph 	char *args[];
15715218Sralph {
158*16028Sralph 	register struct namelist *nl, *prev;
159*16028Sralph 	register char *cp;
160*16028Sralph 	struct namelist *files, *hosts;
161*16028Sralph 	struct subcmd *cmds;
162*16028Sralph 	char *dest;
163*16028Sralph 	static struct namelist tnl = { NULL, NULL };
16415218Sralph 	int i;
16515218Sralph 
16615218Sralph 	if (nargs < 2)
16715218Sralph 		usage();
16815218Sralph 
16915218Sralph 	prev = NULL;
170*16028Sralph 	for (i = 0; i < nargs - 1; i++) {
171*16028Sralph 		nl = makenl(args[i]);
172*16028Sralph 		if (prev == NULL)
173*16028Sralph 			files = prev = nl;
174*16028Sralph 		else {
175*16028Sralph 			prev->n_next = nl;
176*16028Sralph 			prev = nl;
177*16028Sralph 		}
17815218Sralph 	}
17915218Sralph 
180*16028Sralph 	cp = args[i];
181*16028Sralph 	if ((dest = index(cp, ':')) != NULL)
182*16028Sralph 		*dest++ = '\0';
183*16028Sralph 	tnl.n_name = cp;
184*16028Sralph 	hosts = expand(&tnl, E_ALL);
18515218Sralph 
186*16028Sralph 	if (dest == NULL || *dest == '\0')
18715218Sralph 		cmds = NULL;
18815218Sralph 	else {
189*16028Sralph 		cmds = makesubcmd(INSTALL);
190*16028Sralph 		cmds->sc_options = options;
191*16028Sralph 		cmds->sc_name = dest;
19215218Sralph 	}
19315218Sralph 
19415218Sralph 	if (debug) {
19515218Sralph 		printf("docmdargs()\nfiles = ");
19615218Sralph 		prnames(files);
19715218Sralph 		printf("hosts = ");
19815218Sralph 		prnames(hosts);
19915218Sralph 	}
200*16028Sralph 	insert(files, hosts, cmds);
201*16028Sralph 	docmds(0, NULL);
20215218Sralph }
20315218Sralph 
20415218Sralph /*
20514921Sralph  * Print a list of NAME blocks (mostly for debugging).
20614921Sralph  */
207*16028Sralph prnames(nl)
208*16028Sralph 	register struct namelist *nl;
20914921Sralph {
21014921Sralph 	printf("( ");
211*16028Sralph 	while (nl != NULL) {
212*16028Sralph 		printf("%s ", nl->n_name);
213*16028Sralph 		nl = nl->n_next;
21414921Sralph 	}
21514921Sralph 	printf(")\n");
21614921Sralph }
21714921Sralph 
21814921Sralph /*VARARGS*/
21914921Sralph warn(fmt, a1, a2,a3)
22014921Sralph 	char *fmt;
22114921Sralph {
22214921Sralph 	extern int yylineno;
22314921Sralph 
22414921Sralph 	fprintf(stderr, "rdist: line %d: Warning: ", yylineno);
22514921Sralph 	fprintf(stderr, fmt, a1, a2, a3);
22614921Sralph 	fputc('\n', stderr);
22714921Sralph }
228