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