disk stats are collected off alternate clock, if it exists
[unix-history] / usr / src / usr.bin / rdist / main.c
CommitLineData
29e83471 1#ifndef lint
f7770429 2static char *sccsid = "@(#)main.c 4.5 (Berkeley) 83/10/12";
29e83471
RC
3#endif
4
5#include "defs.h"
6
7/*
8 * Remote distribution program.
9 */
10
11char *distfile = "distfile";
82572cb6
RC
12char tmpfile[] = "/tmp/rdistAXXXXXX";
13char *tmpname = &tmpfile[5];
14char *tmpinc = &tmpfile[10];
29e83471
RC
15
16int debug; /* debugging flag */
17int nflag; /* NOP flag, just print commands without executing */
18int qflag; /* Quiet. Don't print messages */
f7770429 19int options; /* global options */
29e83471
RC
20int iamremote; /* act as remote server for transfering files */
21
22int filec; /* number of files to update */
23char **filev; /* list of files/directories to update */
24FILE *fin = NULL; /* input file pointer */
25int rem = 0; /* file descriptor to remote source/sink process */
26char host[32]; /* host name */
27int errs; /* number of errors while sending/receiving */
28char user[10]; /* user's name */
29char homedir[128]; /* user's home directory */
30int userid; /* user's user ID */
82572cb6 31int groupid; /* user's group ID */
29e83471
RC
32
33int cleanup();
34int lostconn();
35
36main(argc, argv)
37 int argc;
38 char *argv[];
39{
40 register char *arg;
41 register struct passwd *pw;
f7770429 42 int cmdargs = 0;
29e83471 43
29e83471 44 pw = getpwuid(userid = getuid());
29e83471 45 if (pw == NULL) {
82572cb6 46 fprintf(stderr, "%s: Who are you?\n", argv[0]);
29e83471
RC
47 exit(1);
48 }
49 strcpy(user, pw->pw_name);
50 strcpy(homedir, pw->pw_dir);
82572cb6 51 groupid = pw->pw_gid;
29e83471
RC
52 gethostname(host, sizeof(host));
53
54 while (--argc > 0) {
55 if ((arg = *++argv)[0] != '-')
56 break;
57 if (!strcmp(arg, "-Server"))
58 iamremote++;
59 else while (*++arg)
60 switch (*arg) {
61 case 'f':
62 if (--argc <= 0)
63 usage();
64 distfile = *++argv;
65 if (distfile[0] == '-' && distfile[1] == '\0')
66 fin = stdin;
67 break;
68
69 case 'd':
82572cb6
RC
70 if (--argc <= 0)
71 usage();
72 define(*++argv);
73 break;
74
75 case 'D':
29e83471
RC
76 debug++;
77 break;
78
f7770429
RC
79 case 'c':
80 cmdargs++;
81 break;
82
29e83471
RC
83 case 'n':
84 nflag++;
85 break;
86
87 case 'q':
88 qflag++;
89 break;
90
91 case 'v':
f7770429
RC
92 options |= VERIFY;
93 break;
94
95 case 'w':
96 options |= WHOLE;
29e83471
RC
97 break;
98
99 case 'y':
f7770429 100 options |= YOUNGER;
29e83471
RC
101 break;
102
103 default:
104 usage();
105 }
106 }
82572cb6
RC
107
108 mktemp(tmpfile);
29e83471
RC
109 signal(SIGPIPE, lostconn);
110 if (iamremote) {
111 server();
112 exit(errs);
113 }
29e83471 114
29e83471
RC
115 signal(SIGHUP, cleanup);
116 signal(SIGINT, cleanup);
117 signal(SIGQUIT, cleanup);
118 signal(SIGTERM, cleanup);
119
f7770429
RC
120 if (cmdargs)
121 docmdargs(argc, argv);
122 else {
123 filec = argc;
124 filev = argv;
125 if (fin == NULL && (fin = fopen(distfile, "r")) == NULL) {
126 perror(distfile);
127 exit(1);
128 }
129 yyparse();
82572cb6
RC
130 }
131
29e83471
RC
132 exit(errs);
133}
134
135usage()
136{
f7770429
RC
137 printf("Usage: rdist [-nqvwyD] [-f distfile] [-d var=value] [file ...]\n");
138 printf("or: rdist [-nqvwyD] -c source [...] machine[:dest]\n");
82572cb6
RC
139 exit(1);
140}
141
f7770429
RC
142/*
143 * rcp like interface for distributing files.
144 */
145docmdargs(nargs, args)
146 int nargs;
147 char *args[];
148{
149 struct block *bp, *files, *hosts, *cmds, *prev;
150 int i;
151 char *pos, dest[BUFSIZ];
152
153 if (nargs < 2)
154 usage();
155
156 prev = NULL;
157 bp = files = ALLOC(block);
158 for (i = 0; i < nargs - 1; bp = ALLOC(block), i++) {
159 bp->b_type = NAME;
160 bp->b_name = args[i];
161 if (prev != NULL)
162 prev->b_next = bp;
163 bp->b_next = bp->b_args = NULL;
164 prev = bp;
165 }
166
167 hosts = ALLOC(block);
168 hosts->b_type = NAME;
169 hosts->b_name = args[i];
170 hosts->b_name = args[i];
171 hosts->b_next = hosts->b_args = NULL;
172 if ((pos = index(hosts->b_name, ':')) != NULL) {
173 *pos++ = '\0';
174 strcpy(dest, pos);
175 } else
176 dest[0] = '\0';
177
178 hosts = expand(hosts, 0);
179
180 if (dest[0] == '\0')
181 cmds = NULL;
182 else {
183 cmds = ALLOC(block);
184 cmds->b_type = INSTALL;
185 cmds->b_options = options;
186 cmds->b_name = dest;
187 cmds->b_next = cmds->b_args = NULL;
188 }
189
190 if (debug) {
191 printf("docmdargs()\nfiles = ");
192 prnames(files);
193 printf("hosts = ");
194 prnames(hosts);
195 }
196 dohcmds(files, hosts, cmds);
197}
198
29e83471
RC
199/*
200 * Remove temporary files and do any cleanup operations before exiting.
201 */
202cleanup()
203{
f7770429
RC
204 do {
205 (void) unlink(tmpfile);
206 (*tmpinc)--;
207 } while (*tmpinc >= 'A');
29e83471
RC
208 exit(1);
209}
210
211/*
212 * Print a list of NAME blocks (mostly for debugging).
213 */
214prnames(bp)
215 register struct block *bp;
216{
217 printf("( ");
218 while (bp != NULL) {
219 printf("%s ", bp->b_name);
220 bp = bp->b_next;
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}