main.c revision 1.1.1.2 1 1.1 cgd /*
2 1.1.1.2 mrg * Copyright (c) 1983, 1993
3 1.1.1.2 mrg * The Regents of the University of California. All rights reserved.
4 1.1 cgd *
5 1.1 cgd * Redistribution and use in source and binary forms, with or without
6 1.1 cgd * modification, are permitted provided that the following conditions
7 1.1 cgd * are met:
8 1.1 cgd * 1. Redistributions of source code must retain the above copyright
9 1.1 cgd * notice, this list of conditions and the following disclaimer.
10 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer in the
12 1.1 cgd * documentation and/or other materials provided with the distribution.
13 1.1 cgd * 3. All advertising materials mentioning features or use of this software
14 1.1 cgd * must display the following acknowledgement:
15 1.1 cgd * This product includes software developed by the University of
16 1.1 cgd * California, Berkeley and its contributors.
17 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
18 1.1 cgd * may be used to endorse or promote products derived from this software
19 1.1 cgd * without specific prior written permission.
20 1.1 cgd *
21 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 1.1 cgd * SUCH DAMAGE.
32 1.1 cgd */
33 1.1 cgd
34 1.1 cgd #ifndef lint
35 1.1.1.2 mrg static char copyright[] =
36 1.1.1.2 mrg "@(#) Copyright (c) 1983, 1993\n\
37 1.1.1.2 mrg The Regents of the University of California. All rights reserved.\n";
38 1.1 cgd #endif /* not lint */
39 1.1 cgd
40 1.1 cgd #ifndef lint
41 1.1.1.2 mrg static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 6/9/93";
42 1.1 cgd #endif /* not lint */
43 1.1 cgd
44 1.1 cgd #include "defs.h"
45 1.1 cgd
46 1.1 cgd #define NHOSTS 100
47 1.1 cgd
48 1.1 cgd /*
49 1.1 cgd * Remote distribution program.
50 1.1 cgd */
51 1.1 cgd
52 1.1 cgd char *distfile = NULL;
53 1.1 cgd #define _RDIST_TMP "/rdistXXXXXX"
54 1.1 cgd char tempfile[sizeof _PATH_TMP + sizeof _RDIST_TMP + 1];
55 1.1 cgd char *tempname;
56 1.1 cgd
57 1.1 cgd int debug; /* debugging flag */
58 1.1 cgd int nflag; /* NOP flag, just print commands without executing */
59 1.1 cgd int qflag; /* Quiet. Don't print messages */
60 1.1 cgd int options; /* global options */
61 1.1 cgd int iamremote; /* act as remote server for transfering files */
62 1.1 cgd
63 1.1 cgd FILE *fin = NULL; /* input file pointer */
64 1.1 cgd int rem = -1; /* file descriptor to remote source/sink process */
65 1.1 cgd char host[32]; /* host name */
66 1.1 cgd int nerrs; /* number of errors while sending/receiving */
67 1.1 cgd char user[10]; /* user's name */
68 1.1 cgd char homedir[128]; /* user's home directory */
69 1.1 cgd int userid; /* user's user ID */
70 1.1 cgd int groupid; /* user's group ID */
71 1.1 cgd
72 1.1 cgd struct passwd *pw; /* pointer to static area used by getpwent */
73 1.1 cgd struct group *gr; /* pointer to static area used by getgrent */
74 1.1 cgd
75 1.1.1.2 mrg static void usage __P((void));
76 1.1.1.2 mrg static void docmdargs __P((int, char *[]));
77 1.1.1.2 mrg
78 1.1.1.2 mrg int
79 1.1 cgd main(argc, argv)
80 1.1 cgd int argc;
81 1.1 cgd char *argv[];
82 1.1 cgd {
83 1.1 cgd register char *arg;
84 1.1 cgd int cmdargs = 0;
85 1.1 cgd char *dhosts[NHOSTS], **hp = dhosts;
86 1.1 cgd
87 1.1 cgd pw = getpwuid(userid = getuid());
88 1.1 cgd if (pw == NULL) {
89 1.1 cgd fprintf(stderr, "%s: Who are you?\n", argv[0]);
90 1.1 cgd exit(1);
91 1.1 cgd }
92 1.1 cgd strcpy(user, pw->pw_name);
93 1.1 cgd strcpy(homedir, pw->pw_dir);
94 1.1 cgd groupid = pw->pw_gid;
95 1.1 cgd gethostname(host, sizeof(host));
96 1.1 cgd strcpy(tempfile, _PATH_TMP);
97 1.1 cgd strcat(tempfile, _RDIST_TMP);
98 1.1 cgd if ((tempname = rindex(tempfile, '/')) != 0)
99 1.1 cgd tempname++;
100 1.1 cgd else
101 1.1 cgd tempname = tempfile;
102 1.1 cgd
103 1.1 cgd while (--argc > 0) {
104 1.1 cgd if ((arg = *++argv)[0] != '-')
105 1.1 cgd break;
106 1.1 cgd if (!strcmp(arg, "-Server"))
107 1.1 cgd iamremote++;
108 1.1 cgd else while (*++arg)
109 1.1 cgd switch (*arg) {
110 1.1 cgd case 'f':
111 1.1 cgd if (--argc <= 0)
112 1.1 cgd usage();
113 1.1 cgd distfile = *++argv;
114 1.1 cgd if (distfile[0] == '-' && distfile[1] == '\0')
115 1.1 cgd fin = stdin;
116 1.1 cgd break;
117 1.1 cgd
118 1.1 cgd case 'm':
119 1.1 cgd if (--argc <= 0)
120 1.1 cgd usage();
121 1.1 cgd if (hp >= &dhosts[NHOSTS-2]) {
122 1.1 cgd fprintf(stderr, "rdist: too many destination hosts\n");
123 1.1 cgd exit(1);
124 1.1 cgd }
125 1.1 cgd *hp++ = *++argv;
126 1.1 cgd break;
127 1.1 cgd
128 1.1 cgd case 'd':
129 1.1 cgd if (--argc <= 0)
130 1.1 cgd usage();
131 1.1 cgd define(*++argv);
132 1.1 cgd break;
133 1.1 cgd
134 1.1 cgd case 'D':
135 1.1 cgd debug++;
136 1.1 cgd break;
137 1.1 cgd
138 1.1 cgd case 'c':
139 1.1 cgd cmdargs++;
140 1.1 cgd break;
141 1.1 cgd
142 1.1 cgd case 'n':
143 1.1 cgd if (options & VERIFY) {
144 1.1 cgd printf("rdist: -n overrides -v\n");
145 1.1 cgd options &= ~VERIFY;
146 1.1 cgd }
147 1.1 cgd nflag++;
148 1.1 cgd break;
149 1.1 cgd
150 1.1 cgd case 'q':
151 1.1 cgd qflag++;
152 1.1 cgd break;
153 1.1 cgd
154 1.1 cgd case 'b':
155 1.1 cgd options |= COMPARE;
156 1.1 cgd break;
157 1.1 cgd
158 1.1 cgd case 'R':
159 1.1 cgd options |= REMOVE;
160 1.1 cgd break;
161 1.1 cgd
162 1.1 cgd case 'v':
163 1.1 cgd if (nflag) {
164 1.1 cgd printf("rdist: -n overrides -v\n");
165 1.1 cgd break;
166 1.1 cgd }
167 1.1 cgd options |= VERIFY;
168 1.1 cgd break;
169 1.1 cgd
170 1.1 cgd case 'w':
171 1.1 cgd options |= WHOLE;
172 1.1 cgd break;
173 1.1 cgd
174 1.1 cgd case 'y':
175 1.1 cgd options |= YOUNGER;
176 1.1 cgd break;
177 1.1 cgd
178 1.1 cgd case 'h':
179 1.1 cgd options |= FOLLOW;
180 1.1 cgd break;
181 1.1 cgd
182 1.1 cgd case 'i':
183 1.1 cgd options |= IGNLNKS;
184 1.1 cgd break;
185 1.1 cgd
186 1.1 cgd default:
187 1.1 cgd usage();
188 1.1 cgd }
189 1.1 cgd }
190 1.1 cgd *hp = NULL;
191 1.1 cgd
192 1.1.1.2 mrg seteuid(userid);
193 1.1 cgd mktemp(tempfile);
194 1.1 cgd
195 1.1 cgd if (iamremote) {
196 1.1 cgd server();
197 1.1 cgd exit(nerrs != 0);
198 1.1 cgd }
199 1.1 cgd
200 1.1 cgd if (cmdargs)
201 1.1 cgd docmdargs(argc, argv);
202 1.1 cgd else {
203 1.1 cgd if (fin == NULL) {
204 1.1 cgd if(distfile == NULL) {
205 1.1 cgd if((fin = fopen("distfile","r")) == NULL)
206 1.1 cgd fin = fopen("Distfile", "r");
207 1.1 cgd } else
208 1.1 cgd fin = fopen(distfile, "r");
209 1.1 cgd if(fin == NULL) {
210 1.1 cgd perror(distfile ? distfile : "distfile");
211 1.1 cgd exit(1);
212 1.1 cgd }
213 1.1 cgd }
214 1.1 cgd yyparse();
215 1.1 cgd if (nerrs == 0)
216 1.1 cgd docmds(dhosts, argc, argv);
217 1.1 cgd }
218 1.1 cgd
219 1.1 cgd exit(nerrs != 0);
220 1.1 cgd }
221 1.1 cgd
222 1.1.1.2 mrg static void
223 1.1 cgd usage()
224 1.1 cgd {
225 1.1 cgd printf("Usage: rdist [-nqbhirvwyD] [-f distfile] [-d var=value] [-m host] [file ...]\n");
226 1.1 cgd printf("or: rdist [-nqbhirvwyD] -c source [...] machine[:dest]\n");
227 1.1 cgd exit(1);
228 1.1 cgd }
229 1.1 cgd
230 1.1 cgd /*
231 1.1 cgd * rcp like interface for distributing files.
232 1.1 cgd */
233 1.1.1.2 mrg static void
234 1.1 cgd docmdargs(nargs, args)
235 1.1 cgd int nargs;
236 1.1 cgd char *args[];
237 1.1 cgd {
238 1.1 cgd register struct namelist *nl, *prev;
239 1.1 cgd register char *cp;
240 1.1 cgd struct namelist *files, *hosts;
241 1.1 cgd struct subcmd *cmds;
242 1.1 cgd char *dest;
243 1.1 cgd static struct namelist tnl = { NULL, NULL };
244 1.1 cgd int i;
245 1.1 cgd
246 1.1 cgd if (nargs < 2)
247 1.1 cgd usage();
248 1.1 cgd
249 1.1 cgd prev = NULL;
250 1.1 cgd for (i = 0; i < nargs - 1; i++) {
251 1.1 cgd nl = makenl(args[i]);
252 1.1 cgd if (prev == NULL)
253 1.1 cgd files = prev = nl;
254 1.1 cgd else {
255 1.1 cgd prev->n_next = nl;
256 1.1 cgd prev = nl;
257 1.1 cgd }
258 1.1 cgd }
259 1.1 cgd
260 1.1 cgd cp = args[i];
261 1.1 cgd if ((dest = index(cp, ':')) != NULL)
262 1.1 cgd *dest++ = '\0';
263 1.1 cgd tnl.n_name = cp;
264 1.1 cgd hosts = expand(&tnl, E_ALL);
265 1.1 cgd if (nerrs)
266 1.1 cgd exit(1);
267 1.1 cgd
268 1.1 cgd if (dest == NULL || *dest == '\0')
269 1.1 cgd cmds = NULL;
270 1.1 cgd else {
271 1.1 cgd cmds = makesubcmd(INSTALL);
272 1.1 cgd cmds->sc_options = options;
273 1.1 cgd cmds->sc_name = dest;
274 1.1 cgd }
275 1.1 cgd
276 1.1 cgd if (debug) {
277 1.1 cgd printf("docmdargs()\nfiles = ");
278 1.1 cgd prnames(files);
279 1.1 cgd printf("hosts = ");
280 1.1 cgd prnames(hosts);
281 1.1 cgd }
282 1.1 cgd insert(NULL, files, hosts, cmds);
283 1.1 cgd docmds(NULL, 0, NULL);
284 1.1 cgd }
285 1.1 cgd
286 1.1 cgd /*
287 1.1 cgd * Print a list of NAME blocks (mostly for debugging).
288 1.1 cgd */
289 1.1.1.2 mrg void
290 1.1 cgd prnames(nl)
291 1.1 cgd register struct namelist *nl;
292 1.1 cgd {
293 1.1 cgd printf("( ");
294 1.1 cgd while (nl != NULL) {
295 1.1 cgd printf("%s ", nl->n_name);
296 1.1 cgd nl = nl->n_next;
297 1.1 cgd }
298 1.1 cgd printf(")\n");
299 1.1 cgd }
300 1.1 cgd
301 1.1.1.2 mrg #if __STDC__
302 1.1.1.2 mrg #include <stdarg.h>
303 1.1.1.2 mrg #else
304 1.1.1.2 mrg #include <varargs.h>
305 1.1.1.2 mrg #endif
306 1.1.1.2 mrg
307 1.1.1.2 mrg void
308 1.1.1.2 mrg #if __STDC__
309 1.1.1.2 mrg warn(const char *fmt, ...)
310 1.1.1.2 mrg #else
311 1.1.1.2 mrg warn(fmt, va_alist)
312 1.1 cgd char *fmt;
313 1.1.1.2 mrg va_dcl
314 1.1.1.2 mrg #endif
315 1.1 cgd {
316 1.1 cgd extern int yylineno;
317 1.1.1.2 mrg va_list ap;
318 1.1.1.2 mrg #if __STDC__
319 1.1.1.2 mrg va_start(ap, fmt);
320 1.1.1.2 mrg #else
321 1.1.1.2 mrg va_start(ap);
322 1.1.1.2 mrg #endif
323 1.1.1.2 mrg (void)fprintf(stderr, "rdist: line %d: Warning: ", yylineno);
324 1.1.1.2 mrg (void)vfprintf(stderr, fmt, ap);
325 1.1.1.2 mrg (void)fprintf(stderr, "\n");
326 1.1.1.2 mrg va_end(ap);
327 1.1 cgd }
328