Also check for Distfile as well as distfile
[unix-history] / usr / src / usr.bin / rdist / main.c
CommitLineData
29e83471 1#ifndef lint
78a18ca3 2static char *sccsid = "@(#)main.c 4.14 (Berkeley) 85/02/04";
29e83471
RC
3#endif
4
5#include "defs.h"
6
78a18ca3
RC
7#define NHOSTS 100
8
29e83471
RC
9/*
10 * Remote distribution program.
11 */
12
13char *distfile = "distfile";
d6bccb44 14char tmpfile[] = "/tmp/rdistXXXXXX";
82572cb6 15char *tmpname = &tmpfile[5];
29e83471
RC
16
17int debug; /* debugging flag */
18int nflag; /* NOP flag, just print commands without executing */
19int qflag; /* Quiet. Don't print messages */
f7770429 20int options; /* global options */
29e83471
RC
21int iamremote; /* act as remote server for transfering files */
22
29e83471 23FILE *fin = NULL; /* input file pointer */
0fccdfef 24int rem = -1; /* file descriptor to remote source/sink process */
29e83471 25char host[32]; /* host name */
0fccdfef 26int nerrs; /* number of errors while sending/receiving */
29e83471
RC
27char user[10]; /* user's name */
28char homedir[128]; /* user's home directory */
29int userid; /* user's user ID */
82572cb6 30int groupid; /* user's group ID */
29e83471 31
e8109cf8
RC
32struct passwd *pw; /* pointer to static area used by getpwent */
33struct group *gr; /* pointer to static area used by getgrent */
34
29e83471
RC
35main(argc, argv)
36 int argc;
37 char *argv[];
38{
39 register char *arg;
f7770429 40 int cmdargs = 0;
78a18ca3 41 char *dhosts[NHOSTS], **hp = dhosts;
29e83471 42
29e83471 43 pw = getpwuid(userid = getuid());
29e83471 44 if (pw == NULL) {
82572cb6 45 fprintf(stderr, "%s: Who are you?\n", argv[0]);
29e83471
RC
46 exit(1);
47 }
48 strcpy(user, pw->pw_name);
49 strcpy(homedir, pw->pw_dir);
82572cb6 50 groupid = pw->pw_gid;
29e83471
RC
51 gethostname(host, sizeof(host));
52
53 while (--argc > 0) {
54 if ((arg = *++argv)[0] != '-')
55 break;
56 if (!strcmp(arg, "-Server"))
57 iamremote++;
58 else while (*++arg)
59 switch (*arg) {
60 case 'f':
61 if (--argc <= 0)
62 usage();
63 distfile = *++argv;
64 if (distfile[0] == '-' && distfile[1] == '\0')
65 fin = stdin;
66 break;
67
78a18ca3
RC
68 case 'm':
69 if (--argc <= 0)
70 usage();
71 if (hp >= &dhosts[NHOSTS-2]) {
72 fprintf(stderr, "rdist: too many destination hosts\n");
73 exit(1);
74 }
75 *hp++ = *++argv;
76 break;
77
29e83471 78 case 'd':
82572cb6
RC
79 if (--argc <= 0)
80 usage();
81 define(*++argv);
82 break;
83
84 case 'D':
29e83471
RC
85 debug++;
86 break;
87
f7770429
RC
88 case 'c':
89 cmdargs++;
90 break;
91
29e83471 92 case 'n':
d6bccb44
RC
93 if (options & VERIFY) {
94 printf("rdist: -n overrides -v\n");
95 options &= ~VERIFY;
96 }
29e83471
RC
97 nflag++;
98 break;
99
100 case 'q':
101 qflag++;
102 break;
103
024fde5b
RC
104 case 'b':
105 options |= COMPARE;
106 break;
107
d6bccb44 108 case 'R':
d1dee8e8
RC
109 options |= REMOVE;
110 break;
111
29e83471 112 case 'v':
d6bccb44
RC
113 if (nflag) {
114 printf("rdist: -n overrides -v\n");
115 break;
116 }
f7770429
RC
117 options |= VERIFY;
118 break;
119
120 case 'w':
121 options |= WHOLE;
29e83471
RC
122 break;
123
124 case 'y':
f7770429 125 options |= YOUNGER;
29e83471
RC
126 break;
127
a3e6fd64
RC
128 case 'h':
129 options |= FOLLOW;
130 break;
131
132 case 'i':
133 options |= IGNLNKS;
134 break;
135
29e83471
RC
136 default:
137 usage();
138 }
139 }
78a18ca3 140 *hp = NULL;
82572cb6 141
80babee1 142 setreuid(0, userid);
82572cb6 143 mktemp(tmpfile);
0fccdfef 144
29e83471
RC
145 if (iamremote) {
146 server();
80babee1 147 exit(nerrs != 0);
29e83471 148 }
29e83471 149
f7770429
RC
150 if (cmdargs)
151 docmdargs(argc, argv);
152 else {
f7770429
RC
153 if (fin == NULL && (fin = fopen(distfile, "r")) == NULL) {
154 perror(distfile);
155 exit(1);
156 }
157 yyparse();
0fccdfef 158 if (nerrs == 0)
78a18ca3 159 docmds(dhosts, argc, argv);
82572cb6
RC
160 }
161
80babee1 162 exit(nerrs != 0);
29e83471
RC
163}
164
165usage()
166{
78a18ca3 167 printf("Usage: rdist [-nqbhirvwyD] [-f distfile] [-d var=value] [-m host] [file ...]\n");
a3e6fd64 168 printf("or: rdist [-nqbhirvwyD] -c source [...] machine[:dest]\n");
82572cb6
RC
169 exit(1);
170}
171
f7770429
RC
172/*
173 * rcp like interface for distributing files.
174 */
175docmdargs(nargs, args)
176 int nargs;
177 char *args[];
178{
0fccdfef
RC
179 register struct namelist *nl, *prev;
180 register char *cp;
181 struct namelist *files, *hosts;
182 struct subcmd *cmds;
183 char *dest;
184 static struct namelist tnl = { NULL, NULL };
f7770429 185 int i;
f7770429
RC
186
187 if (nargs < 2)
188 usage();
189
190 prev = NULL;
0fccdfef
RC
191 for (i = 0; i < nargs - 1; i++) {
192 nl = makenl(args[i]);
193 if (prev == NULL)
194 files = prev = nl;
195 else {
196 prev->n_next = nl;
197 prev = nl;
198 }
f7770429
RC
199 }
200
0fccdfef
RC
201 cp = args[i];
202 if ((dest = index(cp, ':')) != NULL)
203 *dest++ = '\0';
204 tnl.n_name = cp;
205 hosts = expand(&tnl, E_ALL);
78a18ca3
RC
206 if (nerrs)
207 exit(1);
f7770429 208
0fccdfef 209 if (dest == NULL || *dest == '\0')
f7770429
RC
210 cmds = NULL;
211 else {
0fccdfef
RC
212 cmds = makesubcmd(INSTALL);
213 cmds->sc_options = options;
214 cmds->sc_name = dest;
f7770429
RC
215 }
216
217 if (debug) {
218 printf("docmdargs()\nfiles = ");
219 prnames(files);
220 printf("hosts = ");
221 prnames(hosts);
222 }
032b4373 223 insert(NULL, files, hosts, cmds);
78a18ca3 224 docmds(NULL, 0, NULL);
29e83471
RC
225}
226
227/*
228 * Print a list of NAME blocks (mostly for debugging).
229 */
0fccdfef
RC
230prnames(nl)
231 register struct namelist *nl;
29e83471
RC
232{
233 printf("( ");
0fccdfef
RC
234 while (nl != NULL) {
235 printf("%s ", nl->n_name);
236 nl = nl->n_next;
29e83471
RC
237 }
238 printf(")\n");
239}
240
241/*VARARGS*/
242warn(fmt, a1, a2,a3)
243 char *fmt;
244{
245 extern int yylineno;
246
247 fprintf(stderr, "rdist: line %d: Warning: ", yylineno);
248 fprintf(stderr, fmt, a1, a2, a3);
249 fputc('\n', stderr);
250}