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