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