Commit | Line | Data |
---|---|---|
7172eb74 DF |
1 | /* |
2 | * Copyright (c) 1983 Regents of the University of California. | |
0718fb71 KB |
3 | * All rights reserved. |
4 | * | |
6d936b27 | 5 | * %sccs.include.redist.c% |
7172eb74 DF |
6 | */ |
7 | ||
8 | #ifndef lint | |
9 | char copyright[] = | |
10 | "@(#) Copyright (c) 1983 Regents of the University of California.\n\ | |
11 | All rights reserved.\n"; | |
0718fb71 | 12 | #endif /* not lint */ |
7172eb74 | 13 | |
29e83471 | 14 | #ifndef lint |
9bb272cd | 15 | static char sccsid[] = "@(#)main.c 5.8 (Berkeley) %G%"; |
0718fb71 | 16 | #endif /* not lint */ |
29e83471 RC |
17 | |
18 | #include "defs.h" | |
19 | ||
78a18ca3 RC |
20 | #define NHOSTS 100 |
21 | ||
29e83471 RC |
22 | /* |
23 | * Remote distribution program. | |
24 | */ | |
25 | ||
be5872f6 | 26 | char *distfile = NULL; |
e385b216 EA |
27 | #define _RDIST_TMP "/rdistXXXXXX" |
28 | char tempfile[sizeof _PATH_TMP + sizeof _RDIST_TMP + 1]; | |
29 | char *tempname; | |
29e83471 RC |
30 | |
31 | int debug; /* debugging flag */ | |
32 | int nflag; /* NOP flag, just print commands without executing */ | |
33 | int qflag; /* Quiet. Don't print messages */ | |
f7770429 | 34 | int options; /* global options */ |
29e83471 RC |
35 | int iamremote; /* act as remote server for transfering files */ |
36 | ||
29e83471 | 37 | FILE *fin = NULL; /* input file pointer */ |
0fccdfef | 38 | int rem = -1; /* file descriptor to remote source/sink process */ |
29e83471 | 39 | char host[32]; /* host name */ |
0fccdfef | 40 | int nerrs; /* number of errors while sending/receiving */ |
29e83471 RC |
41 | char user[10]; /* user's name */ |
42 | char homedir[128]; /* user's home directory */ | |
43 | int userid; /* user's user ID */ | |
82572cb6 | 44 | int groupid; /* user's group ID */ |
29e83471 | 45 | |
e8109cf8 RC |
46 | struct passwd *pw; /* pointer to static area used by getpwent */ |
47 | struct group *gr; /* pointer to static area used by getgrent */ | |
48 | ||
a94625d9 KB |
49 | static void usage __P((void)); |
50 | static void docmdargs __P((int, char *[])); | |
51 | ||
52 | int | |
29e83471 RC |
53 | main(argc, argv) |
54 | int argc; | |
55 | char *argv[]; | |
56 | { | |
57 | register char *arg; | |
f7770429 | 58 | int cmdargs = 0; |
78a18ca3 | 59 | char *dhosts[NHOSTS], **hp = dhosts; |
29e83471 | 60 | |
29e83471 | 61 | pw = getpwuid(userid = getuid()); |
29e83471 | 62 | if (pw == NULL) { |
82572cb6 | 63 | fprintf(stderr, "%s: Who are you?\n", argv[0]); |
29e83471 RC |
64 | exit(1); |
65 | } | |
66 | strcpy(user, pw->pw_name); | |
67 | strcpy(homedir, pw->pw_dir); | |
82572cb6 | 68 | groupid = pw->pw_gid; |
29e83471 | 69 | gethostname(host, sizeof(host)); |
e385b216 EA |
70 | strcpy(tempfile, _PATH_TMP); |
71 | strcat(tempfile, _RDIST_TMP); | |
72 | if ((tempname = rindex(tempfile, '/')) != 0) | |
73 | tempname++; | |
74 | else | |
75 | tempname = tempfile; | |
29e83471 RC |
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 | ||
78a18ca3 RC |
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 | ||
29e83471 | 102 | case 'd': |
82572cb6 RC |
103 | if (--argc <= 0) |
104 | usage(); | |
105 | define(*++argv); | |
106 | break; | |
107 | ||
108 | case 'D': | |
29e83471 RC |
109 | debug++; |
110 | break; | |
111 | ||
f7770429 RC |
112 | case 'c': |
113 | cmdargs++; | |
114 | break; | |
115 | ||
29e83471 | 116 | case 'n': |
d6bccb44 RC |
117 | if (options & VERIFY) { |
118 | printf("rdist: -n overrides -v\n"); | |
119 | options &= ~VERIFY; | |
120 | } | |
29e83471 RC |
121 | nflag++; |
122 | break; | |
123 | ||
124 | case 'q': | |
125 | qflag++; | |
126 | break; | |
127 | ||
024fde5b RC |
128 | case 'b': |
129 | options |= COMPARE; | |
130 | break; | |
131 | ||
d6bccb44 | 132 | case 'R': |
d1dee8e8 RC |
133 | options |= REMOVE; |
134 | break; | |
135 | ||
29e83471 | 136 | case 'v': |
d6bccb44 RC |
137 | if (nflag) { |
138 | printf("rdist: -n overrides -v\n"); | |
139 | break; | |
140 | } | |
f7770429 RC |
141 | options |= VERIFY; |
142 | break; | |
143 | ||
144 | case 'w': | |
145 | options |= WHOLE; | |
29e83471 RC |
146 | break; |
147 | ||
148 | case 'y': | |
f7770429 | 149 | options |= YOUNGER; |
29e83471 RC |
150 | break; |
151 | ||
a3e6fd64 RC |
152 | case 'h': |
153 | options |= FOLLOW; | |
154 | break; | |
155 | ||
156 | case 'i': | |
157 | options |= IGNLNKS; | |
158 | break; | |
159 | ||
29e83471 RC |
160 | default: |
161 | usage(); | |
162 | } | |
163 | } | |
78a18ca3 | 164 | *hp = NULL; |
82572cb6 | 165 | |
9bb272cd | 166 | seteuid(userid); |
e385b216 | 167 | mktemp(tempfile); |
0fccdfef | 168 | |
29e83471 RC |
169 | if (iamremote) { |
170 | server(); | |
80babee1 | 171 | exit(nerrs != 0); |
29e83471 | 172 | } |
29e83471 | 173 | |
f7770429 RC |
174 | if (cmdargs) |
175 | docmdargs(argc, argv); | |
176 | else { | |
be5872f6 JB |
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 | } | |
f7770429 RC |
187 | } |
188 | yyparse(); | |
0fccdfef | 189 | if (nerrs == 0) |
78a18ca3 | 190 | docmds(dhosts, argc, argv); |
82572cb6 RC |
191 | } |
192 | ||
80babee1 | 193 | exit(nerrs != 0); |
29e83471 RC |
194 | } |
195 | ||
a94625d9 | 196 | static void |
29e83471 RC |
197 | usage() |
198 | { | |
78a18ca3 | 199 | printf("Usage: rdist [-nqbhirvwyD] [-f distfile] [-d var=value] [-m host] [file ...]\n"); |
a3e6fd64 | 200 | printf("or: rdist [-nqbhirvwyD] -c source [...] machine[:dest]\n"); |
82572cb6 RC |
201 | exit(1); |
202 | } | |
203 | ||
f7770429 RC |
204 | /* |
205 | * rcp like interface for distributing files. | |
206 | */ | |
a94625d9 | 207 | static void |
f7770429 RC |
208 | docmdargs(nargs, args) |
209 | int nargs; | |
210 | char *args[]; | |
211 | { | |
0fccdfef RC |
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 }; | |
f7770429 | 218 | int i; |
f7770429 RC |
219 | |
220 | if (nargs < 2) | |
221 | usage(); | |
222 | ||
223 | prev = NULL; | |
0fccdfef RC |
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 | } | |
f7770429 RC |
232 | } |
233 | ||
0fccdfef RC |
234 | cp = args[i]; |
235 | if ((dest = index(cp, ':')) != NULL) | |
236 | *dest++ = '\0'; | |
237 | tnl.n_name = cp; | |
238 | hosts = expand(&tnl, E_ALL); | |
78a18ca3 RC |
239 | if (nerrs) |
240 | exit(1); | |
f7770429 | 241 | |
0fccdfef | 242 | if (dest == NULL || *dest == '\0') |
f7770429 RC |
243 | cmds = NULL; |
244 | else { | |
0fccdfef RC |
245 | cmds = makesubcmd(INSTALL); |
246 | cmds->sc_options = options; | |
247 | cmds->sc_name = dest; | |
f7770429 RC |
248 | } |
249 | ||
250 | if (debug) { | |
251 | printf("docmdargs()\nfiles = "); | |
252 | prnames(files); | |
253 | printf("hosts = "); | |
254 | prnames(hosts); | |
255 | } | |
032b4373 | 256 | insert(NULL, files, hosts, cmds); |
78a18ca3 | 257 | docmds(NULL, 0, NULL); |
29e83471 RC |
258 | } |
259 | ||
260 | /* | |
261 | * Print a list of NAME blocks (mostly for debugging). | |
262 | */ | |
a94625d9 | 263 | void |
0fccdfef RC |
264 | prnames(nl) |
265 | register struct namelist *nl; | |
29e83471 RC |
266 | { |
267 | printf("( "); | |
0fccdfef RC |
268 | while (nl != NULL) { |
269 | printf("%s ", nl->n_name); | |
270 | nl = nl->n_next; | |
29e83471 RC |
271 | } |
272 | printf(")\n"); | |
273 | } | |
274 | ||
a94625d9 KB |
275 | #if __STDC__ |
276 | #include <stdarg.h> | |
277 | #else | |
278 | #include <varargs.h> | |
279 | #endif | |
280 | ||
281 | void | |
282 | #if __STDC__ | |
283 | warn(const char *fmt, ...) | |
284 | #else | |
285 | warn(fmt, va_alist) | |
29e83471 | 286 | char *fmt; |
a94625d9 KB |
287 | va_dcl |
288 | #endif | |
29e83471 RC |
289 | { |
290 | extern int yylineno; | |
a94625d9 KB |
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); | |
29e83471 | 301 | } |