4.4BSD snapshot (revision 8.1)
[unix-history] / usr / src / usr.bin / rdist / main.c
... / ...
CommitLineData
1/*
2 * Copyright (c) 1983, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * %sccs.include.redist.c%
6 */
7
8#ifndef lint
9static char copyright[] =
10"@(#) Copyright (c) 1983, 1993\n\
11 The Regents of the University of California. All rights reserved.\n";
12#endif /* not lint */
13
14#ifndef lint
15static char sccsid[] = "@(#)main.c 8.1 (Berkeley) %G%";
16#endif /* not lint */
17
18#include "defs.h"
19
20#define NHOSTS 100
21
22/*
23 * Remote distribution program.
24 */
25
26char *distfile = NULL;
27#define _RDIST_TMP "/rdistXXXXXX"
28char tempfile[sizeof _PATH_TMP + sizeof _RDIST_TMP + 1];
29char *tempname;
30
31int debug; /* debugging flag */
32int nflag; /* NOP flag, just print commands without executing */
33int qflag; /* Quiet. Don't print messages */
34int options; /* global options */
35int iamremote; /* act as remote server for transfering files */
36
37FILE *fin = NULL; /* input file pointer */
38int rem = -1; /* file descriptor to remote source/sink process */
39char host[32]; /* host name */
40int nerrs; /* number of errors while sending/receiving */
41char user[10]; /* user's name */
42char homedir[128]; /* user's home directory */
43int userid; /* user's user ID */
44int groupid; /* user's group ID */
45
46struct passwd *pw; /* pointer to static area used by getpwent */
47struct group *gr; /* pointer to static area used by getgrent */
48
49static void usage __P((void));
50static void docmdargs __P((int, char *[]));
51
52int
53main(argc, argv)
54 int argc;
55 char *argv[];
56{
57 register char *arg;
58 int cmdargs = 0;
59 char *dhosts[NHOSTS], **hp = dhosts;
60
61 pw = getpwuid(userid = getuid());
62 if (pw == NULL) {
63 fprintf(stderr, "%s: Who are you?\n", argv[0]);
64 exit(1);
65 }
66 strcpy(user, pw->pw_name);
67 strcpy(homedir, pw->pw_dir);
68 groupid = pw->pw_gid;
69 gethostname(host, sizeof(host));
70 strcpy(tempfile, _PATH_TMP);
71 strcat(tempfile, _RDIST_TMP);
72 if ((tempname = rindex(tempfile, '/')) != 0)
73 tempname++;
74 else
75 tempname = tempfile;
76
77 while (--argc > 0) {
78 if ((arg = *++argv)[0] != '-')
79 break;
80 if (!strcmp(arg, "-Server"))
81 iamremote++;
82 else while (*++arg)
83 switch (*arg) {
84 case 'f':
85 if (--argc <= 0)
86 usage();
87 distfile = *++argv;
88 if (distfile[0] == '-' && distfile[1] == '\0')
89 fin = stdin;
90 break;
91
92 case 'm':
93 if (--argc <= 0)
94 usage();
95 if (hp >= &dhosts[NHOSTS-2]) {
96 fprintf(stderr, "rdist: too many destination hosts\n");
97 exit(1);
98 }
99 *hp++ = *++argv;
100 break;
101
102 case 'd':
103 if (--argc <= 0)
104 usage();
105 define(*++argv);
106 break;
107
108 case 'D':
109 debug++;
110 break;
111
112 case 'c':
113 cmdargs++;
114 break;
115
116 case 'n':
117 if (options & VERIFY) {
118 printf("rdist: -n overrides -v\n");
119 options &= ~VERIFY;
120 }
121 nflag++;
122 break;
123
124 case 'q':
125 qflag++;
126 break;
127
128 case 'b':
129 options |= COMPARE;
130 break;
131
132 case 'R':
133 options |= REMOVE;
134 break;
135
136 case 'v':
137 if (nflag) {
138 printf("rdist: -n overrides -v\n");
139 break;
140 }
141 options |= VERIFY;
142 break;
143
144 case 'w':
145 options |= WHOLE;
146 break;
147
148 case 'y':
149 options |= YOUNGER;
150 break;
151
152 case 'h':
153 options |= FOLLOW;
154 break;
155
156 case 'i':
157 options |= IGNLNKS;
158 break;
159
160 default:
161 usage();
162 }
163 }
164 *hp = NULL;
165
166 seteuid(userid);
167 mktemp(tempfile);
168
169 if (iamremote) {
170 server();
171 exit(nerrs != 0);
172 }
173
174 if (cmdargs)
175 docmdargs(argc, argv);
176 else {
177 if (fin == NULL) {
178 if(distfile == NULL) {
179 if((fin = fopen("distfile","r")) == NULL)
180 fin = fopen("Distfile", "r");
181 } else
182 fin = fopen(distfile, "r");
183 if(fin == NULL) {
184 perror(distfile ? distfile : "distfile");
185 exit(1);
186 }
187 }
188 yyparse();
189 if (nerrs == 0)
190 docmds(dhosts, argc, argv);
191 }
192
193 exit(nerrs != 0);
194}
195
196static void
197usage()
198{
199 printf("Usage: rdist [-nqbhirvwyD] [-f distfile] [-d var=value] [-m host] [file ...]\n");
200 printf("or: rdist [-nqbhirvwyD] -c source [...] machine[:dest]\n");
201 exit(1);
202}
203
204/*
205 * rcp like interface for distributing files.
206 */
207static void
208docmdargs(nargs, args)
209 int nargs;
210 char *args[];
211{
212 register struct namelist *nl, *prev;
213 register char *cp;
214 struct namelist *files, *hosts;
215 struct subcmd *cmds;
216 char *dest;
217 static struct namelist tnl = { NULL, NULL };
218 int i;
219
220 if (nargs < 2)
221 usage();
222
223 prev = NULL;
224 for (i = 0; i < nargs - 1; i++) {
225 nl = makenl(args[i]);
226 if (prev == NULL)
227 files = prev = nl;
228 else {
229 prev->n_next = nl;
230 prev = nl;
231 }
232 }
233
234 cp = args[i];
235 if ((dest = index(cp, ':')) != NULL)
236 *dest++ = '\0';
237 tnl.n_name = cp;
238 hosts = expand(&tnl, E_ALL);
239 if (nerrs)
240 exit(1);
241
242 if (dest == NULL || *dest == '\0')
243 cmds = NULL;
244 else {
245 cmds = makesubcmd(INSTALL);
246 cmds->sc_options = options;
247 cmds->sc_name = dest;
248 }
249
250 if (debug) {
251 printf("docmdargs()\nfiles = ");
252 prnames(files);
253 printf("hosts = ");
254 prnames(hosts);
255 }
256 insert(NULL, files, hosts, cmds);
257 docmds(NULL, 0, NULL);
258}
259
260/*
261 * Print a list of NAME blocks (mostly for debugging).
262 */
263void
264prnames(nl)
265 register struct namelist *nl;
266{
267 printf("( ");
268 while (nl != NULL) {
269 printf("%s ", nl->n_name);
270 nl = nl->n_next;
271 }
272 printf(")\n");
273}
274
275#if __STDC__
276#include <stdarg.h>
277#else
278#include <varargs.h>
279#endif
280
281void
282#if __STDC__
283warn(const char *fmt, ...)
284#else
285warn(fmt, va_alist)
286 char *fmt;
287 va_dcl
288#endif
289{
290 extern int yylineno;
291 va_list ap;
292#if __STDC__
293 va_start(ap, fmt);
294#else
295 va_start(ap);
296#endif
297 (void)fprintf(stderr, "rdist: line %d: Warning: ", yylineno);
298 (void)vfprintf(stderr, fmt, ap);
299 (void)fprintf(stderr, "\n");
300 va_end(ap);
301}