122374Sdist /*
2*63070Sbostic * Copyright (c) 1983, 1993
3*63070Sbostic * The Regents of the University of California. All rights reserved.
433415Sbostic *
542760Sbostic * %sccs.include.redist.c%
622374Sdist */
722374Sdist
814921Sralph #ifndef lint
9*63070Sbostic static char copyright[] =
10*63070Sbostic "@(#) Copyright (c) 1983, 1993\n\
11*63070Sbostic The Regents of the University of California. All rights reserved.\n";
1233415Sbostic #endif /* not lint */
1314921Sralph
1422374Sdist #ifndef lint
15*63070Sbostic static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 06/09/93";
1633415Sbostic #endif /* not lint */
1722374Sdist
1814921Sralph #include "defs.h"
1914921Sralph
2017914Sralph #define NHOSTS 100
2117914Sralph
2214921Sralph /*
2314921Sralph * Remote distribution program.
2414921Sralph */
2514921Sralph
2620846Sbloom char *distfile = NULL;
2745137Seric #define _RDIST_TMP "/rdistXXXXXX"
2845137Seric char tempfile[sizeof _PATH_TMP + sizeof _RDIST_TMP + 1];
2945137Seric char *tempname;
3014921Sralph
3114921Sralph int debug; /* debugging flag */
3214921Sralph int nflag; /* NOP flag, just print commands without executing */
3314921Sralph int qflag; /* Quiet. Don't print messages */
3415218Sralph int options; /* global options */
3514921Sralph int iamremote; /* act as remote server for transfering files */
3614921Sralph
3714921Sralph FILE *fin = NULL; /* input file pointer */
3816028Sralph int rem = -1; /* file descriptor to remote source/sink process */
3914921Sralph char host[32]; /* host name */
4016028Sralph int nerrs; /* number of errors while sending/receiving */
4114921Sralph char user[10]; /* user's name */
4214921Sralph char homedir[128]; /* user's home directory */
4314921Sralph int userid; /* user's user ID */
4415113Sralph int groupid; /* user's group ID */
4514921Sralph
4615349Sralph struct passwd *pw; /* pointer to static area used by getpwent */
4715349Sralph struct group *gr; /* pointer to static area used by getgrent */
4815349Sralph
4954705Sbostic static void usage __P((void));
5054705Sbostic static void docmdargs __P((int, char *[]));
5154705Sbostic
5254705Sbostic int
main(argc,argv)5314921Sralph main(argc, argv)
5414921Sralph int argc;
5514921Sralph char *argv[];
5614921Sralph {
5714921Sralph register char *arg;
5815218Sralph int cmdargs = 0;
5917914Sralph char *dhosts[NHOSTS], **hp = dhosts;
6014921Sralph
6114921Sralph pw = getpwuid(userid = getuid());
6214921Sralph if (pw == NULL) {
6315113Sralph fprintf(stderr, "%s: Who are you?\n", argv[0]);
6414921Sralph exit(1);
6514921Sralph }
6614921Sralph strcpy(user, pw->pw_name);
6714921Sralph strcpy(homedir, pw->pw_dir);
6815113Sralph groupid = pw->pw_gid;
6914921Sralph gethostname(host, sizeof(host));
7045137Seric strcpy(tempfile, _PATH_TMP);
7145137Seric strcat(tempfile, _RDIST_TMP);
7245137Seric if ((tempname = rindex(tempfile, '/')) != 0)
7345137Seric tempname++;
7445137Seric else
7545137Seric tempname = tempfile;
7614921Sralph
7714921Sralph while (--argc > 0) {
7814921Sralph if ((arg = *++argv)[0] != '-')
7914921Sralph break;
8014921Sralph if (!strcmp(arg, "-Server"))
8114921Sralph iamremote++;
8214921Sralph else while (*++arg)
8314921Sralph switch (*arg) {
8414921Sralph case 'f':
8514921Sralph if (--argc <= 0)
8614921Sralph usage();
8714921Sralph distfile = *++argv;
8814921Sralph if (distfile[0] == '-' && distfile[1] == '\0')
8914921Sralph fin = stdin;
9014921Sralph break;
9114921Sralph
9217914Sralph case 'm':
9317914Sralph if (--argc <= 0)
9417914Sralph usage();
9517914Sralph if (hp >= &dhosts[NHOSTS-2]) {
9617914Sralph fprintf(stderr, "rdist: too many destination hosts\n");
9717914Sralph exit(1);
9817914Sralph }
9917914Sralph *hp++ = *++argv;
10017914Sralph break;
10117914Sralph
10214921Sralph case 'd':
10315113Sralph if (--argc <= 0)
10415113Sralph usage();
10515113Sralph define(*++argv);
10615113Sralph break;
10715113Sralph
10815113Sralph case 'D':
10914921Sralph debug++;
11014921Sralph break;
11114921Sralph
11215218Sralph case 'c':
11315218Sralph cmdargs++;
11415218Sralph break;
11515218Sralph
11614921Sralph case 'n':
11715611Sralph if (options & VERIFY) {
11815611Sralph printf("rdist: -n overrides -v\n");
11915611Sralph options &= ~VERIFY;
12015611Sralph }
12114921Sralph nflag++;
12214921Sralph break;
12314921Sralph
12414921Sralph case 'q':
12514921Sralph qflag++;
12614921Sralph break;
12714921Sralph
12815293Sralph case 'b':
12915293Sralph options |= COMPARE;
13015293Sralph break;
13115293Sralph
13215611Sralph case 'R':
13315271Sralph options |= REMOVE;
13415271Sralph break;
13515271Sralph
13614921Sralph case 'v':
13715611Sralph if (nflag) {
13815611Sralph printf("rdist: -n overrides -v\n");
13915611Sralph break;
14015611Sralph }
14115218Sralph options |= VERIFY;
14214921Sralph break;
14314921Sralph
14415218Sralph case 'w':
14515218Sralph options |= WHOLE;
14615218Sralph break;
14715218Sralph
14814921Sralph case 'y':
14915218Sralph options |= YOUNGER;
15014921Sralph break;
15114921Sralph
15216650Sralph case 'h':
15316650Sralph options |= FOLLOW;
15416650Sralph break;
15516650Sralph
15616650Sralph case 'i':
15716650Sralph options |= IGNLNKS;
15816650Sralph break;
15916650Sralph
16014921Sralph default:
16114921Sralph usage();
16214921Sralph }
16314921Sralph }
16417914Sralph *hp = NULL;
16515113Sralph
16654844Smckusick seteuid(userid);
16745137Seric mktemp(tempfile);
16816028Sralph
16914921Sralph if (iamremote) {
17014921Sralph server();
17117481Sralph exit(nerrs != 0);
17214921Sralph }
17314921Sralph
17415218Sralph if (cmdargs)
17515218Sralph docmdargs(argc, argv);
17615218Sralph else {
17720846Sbloom if (fin == NULL) {
17820846Sbloom if(distfile == NULL) {
17920846Sbloom if((fin = fopen("distfile","r")) == NULL)
18020846Sbloom fin = fopen("Distfile", "r");
18120846Sbloom } else
18220846Sbloom fin = fopen(distfile, "r");
18320846Sbloom if(fin == NULL) {
18420846Sbloom perror(distfile ? distfile : "distfile");
18520846Sbloom exit(1);
18620846Sbloom }
18715218Sralph }
18815218Sralph yyparse();
18916028Sralph if (nerrs == 0)
19017914Sralph docmds(dhosts, argc, argv);
19115113Sralph }
19215113Sralph
19317481Sralph exit(nerrs != 0);
19414921Sralph }
19514921Sralph
19654705Sbostic static void
usage()19714921Sralph usage()
19814921Sralph {
19917914Sralph printf("Usage: rdist [-nqbhirvwyD] [-f distfile] [-d var=value] [-m host] [file ...]\n");
20016650Sralph printf("or: rdist [-nqbhirvwyD] -c source [...] machine[:dest]\n");
20114921Sralph exit(1);
20214921Sralph }
20314921Sralph
20414921Sralph /*
20515218Sralph * rcp like interface for distributing files.
20615218Sralph */
20754705Sbostic static void
docmdargs(nargs,args)20815218Sralph docmdargs(nargs, args)
20915218Sralph int nargs;
21015218Sralph char *args[];
21115218Sralph {
21216028Sralph register struct namelist *nl, *prev;
21316028Sralph register char *cp;
21416028Sralph struct namelist *files, *hosts;
21516028Sralph struct subcmd *cmds;
21616028Sralph char *dest;
21716028Sralph static struct namelist tnl = { NULL, NULL };
21815218Sralph int i;
21915218Sralph
22015218Sralph if (nargs < 2)
22115218Sralph usage();
22215218Sralph
22315218Sralph prev = NULL;
22416028Sralph for (i = 0; i < nargs - 1; i++) {
22516028Sralph nl = makenl(args[i]);
22616028Sralph if (prev == NULL)
22716028Sralph files = prev = nl;
22816028Sralph else {
22916028Sralph prev->n_next = nl;
23016028Sralph prev = nl;
23116028Sralph }
23215218Sralph }
23315218Sralph
23416028Sralph cp = args[i];
23516028Sralph if ((dest = index(cp, ':')) != NULL)
23616028Sralph *dest++ = '\0';
23716028Sralph tnl.n_name = cp;
23816028Sralph hosts = expand(&tnl, E_ALL);
23917914Sralph if (nerrs)
24017914Sralph exit(1);
24115218Sralph
24216028Sralph if (dest == NULL || *dest == '\0')
24315218Sralph cmds = NULL;
24415218Sralph else {
24516028Sralph cmds = makesubcmd(INSTALL);
24616028Sralph cmds->sc_options = options;
24716028Sralph cmds->sc_name = dest;
24815218Sralph }
24915218Sralph
25015218Sralph if (debug) {
25115218Sralph printf("docmdargs()\nfiles = ");
25215218Sralph prnames(files);
25315218Sralph printf("hosts = ");
25415218Sralph prnames(hosts);
25515218Sralph }
25616317Sralph insert(NULL, files, hosts, cmds);
25717914Sralph docmds(NULL, 0, NULL);
25815218Sralph }
25915218Sralph
26015218Sralph /*
26114921Sralph * Print a list of NAME blocks (mostly for debugging).
26214921Sralph */
26354705Sbostic void
prnames(nl)26416028Sralph prnames(nl)
26516028Sralph register struct namelist *nl;
26614921Sralph {
26714921Sralph printf("( ");
26816028Sralph while (nl != NULL) {
26916028Sralph printf("%s ", nl->n_name);
27016028Sralph nl = nl->n_next;
27114921Sralph }
27214921Sralph printf(")\n");
27314921Sralph }
27414921Sralph
27554705Sbostic #if __STDC__
27654705Sbostic #include <stdarg.h>
27754705Sbostic #else
27854705Sbostic #include <varargs.h>
27954705Sbostic #endif
28054705Sbostic
28154705Sbostic void
28254705Sbostic #if __STDC__
warn(const char * fmt,...)28354705Sbostic warn(const char *fmt, ...)
28454705Sbostic #else
28554705Sbostic warn(fmt, va_alist)
28614921Sralph char *fmt;
28754705Sbostic va_dcl
28854705Sbostic #endif
28914921Sralph {
29014921Sralph extern int yylineno;
29154705Sbostic va_list ap;
29254705Sbostic #if __STDC__
29354705Sbostic va_start(ap, fmt);
29454705Sbostic #else
29554705Sbostic va_start(ap);
29654705Sbostic #endif
29754705Sbostic (void)fprintf(stderr, "rdist: line %d: Warning: ", yylineno);
29854705Sbostic (void)vfprintf(stderr, fmt, ap);
29954705Sbostic (void)fprintf(stderr, "\n");
30054705Sbostic va_end(ap);
30114921Sralph }
302