change exp_pat to except_pat.
[unix-history] / usr / src / usr.bin / rdist / main.c
CommitLineData
29e83471 1#ifndef lint
80babee1 2static char *sccsid = "@(#)main.c 4.13 (Berkeley) 84/12/06";
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 127
80babee1 128 setreuid(0, userid);
82572cb6 129 mktemp(tmpfile);
0fccdfef 130
29e83471
RC
131 if (iamremote) {
132 server();
80babee1 133 exit(nerrs != 0);
29e83471 134 }
29e83471 135
f7770429
RC
136 if (cmdargs)
137 docmdargs(argc, argv);
138 else {
f7770429
RC
139 if (fin == NULL && (fin = fopen(distfile, "r")) == NULL) {
140 perror(distfile);
141 exit(1);
142 }
143 yyparse();
0fccdfef
RC
144 if (nerrs == 0)
145 docmds(argc, argv);
82572cb6
RC
146 }
147
80babee1 148 exit(nerrs != 0);
29e83471
RC
149}
150
151usage()
152{
a3e6fd64
RC
153 printf("Usage: rdist [-nqbhirvwyD] [-f distfile] [-d var=value] [file ...]\n");
154 printf("or: rdist [-nqbhirvwyD] -c source [...] machine[:dest]\n");
82572cb6
RC
155 exit(1);
156}
157
f7770429
RC
158/*
159 * rcp like interface for distributing files.
160 */
161docmdargs(nargs, args)
162 int nargs;
163 char *args[];
164{
0fccdfef
RC
165 register struct namelist *nl, *prev;
166 register char *cp;
167 struct namelist *files, *hosts;
168 struct subcmd *cmds;
169 char *dest;
170 static struct namelist tnl = { NULL, NULL };
f7770429 171 int i;
f7770429
RC
172
173 if (nargs < 2)
174 usage();
175
176 prev = NULL;
0fccdfef
RC
177 for (i = 0; i < nargs - 1; i++) {
178 nl = makenl(args[i]);
179 if (prev == NULL)
180 files = prev = nl;
181 else {
182 prev->n_next = nl;
183 prev = nl;
184 }
f7770429
RC
185 }
186
0fccdfef
RC
187 cp = args[i];
188 if ((dest = index(cp, ':')) != NULL)
189 *dest++ = '\0';
190 tnl.n_name = cp;
191 hosts = expand(&tnl, E_ALL);
f7770429 192
0fccdfef 193 if (dest == NULL || *dest == '\0')
f7770429
RC
194 cmds = NULL;
195 else {
0fccdfef
RC
196 cmds = makesubcmd(INSTALL);
197 cmds->sc_options = options;
198 cmds->sc_name = dest;
f7770429
RC
199 }
200
201 if (debug) {
202 printf("docmdargs()\nfiles = ");
203 prnames(files);
204 printf("hosts = ");
205 prnames(hosts);
206 }
032b4373 207 insert(NULL, files, hosts, cmds);
0fccdfef 208 docmds(0, NULL);
29e83471
RC
209}
210
211/*
212 * Print a list of NAME blocks (mostly for debugging).
213 */
0fccdfef
RC
214prnames(nl)
215 register struct namelist *nl;
29e83471
RC
216{
217 printf("( ");
0fccdfef
RC
218 while (nl != NULL) {
219 printf("%s ", nl->n_name);
220 nl = nl->n_next;
29e83471
RC
221 }
222 printf(")\n");
223}
224
225/*VARARGS*/
226warn(fmt, a1, a2,a3)
227 char *fmt;
228{
229 extern int yylineno;
230
231 fprintf(stderr, "rdist: line %d: Warning: ", yylineno);
232 fprintf(stderr, fmt, a1, a2, a3);
233 fputc('\n', stderr);
234}