util.c revision 1.89 1 1.89 lukem /* $NetBSD: util.c,v 1.89 2000/01/26 10:15:40 lukem Exp $ */
2 1.27 thorpej
3 1.27 thorpej /*-
4 1.72 lukem * Copyright (c) 1997-1999 The NetBSD Foundation, Inc.
5 1.27 thorpej * All rights reserved.
6 1.27 thorpej *
7 1.27 thorpej * This code is derived from software contributed to The NetBSD Foundation
8 1.72 lukem * by Luke Mewburn.
9 1.72 lukem *
10 1.72 lukem * This code is derived from software contributed to The NetBSD Foundation
11 1.27 thorpej * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
12 1.27 thorpej * NASA Ames Research Center.
13 1.27 thorpej *
14 1.27 thorpej * Redistribution and use in source and binary forms, with or without
15 1.27 thorpej * modification, are permitted provided that the following conditions
16 1.27 thorpej * are met:
17 1.27 thorpej * 1. Redistributions of source code must retain the above copyright
18 1.27 thorpej * notice, this list of conditions and the following disclaimer.
19 1.27 thorpej * 2. Redistributions in binary form must reproduce the above copyright
20 1.27 thorpej * notice, this list of conditions and the following disclaimer in the
21 1.27 thorpej * documentation and/or other materials provided with the distribution.
22 1.27 thorpej * 3. All advertising materials mentioning features or use of this software
23 1.27 thorpej * must display the following acknowledgement:
24 1.27 thorpej * This product includes software developed by the NetBSD
25 1.27 thorpej * Foundation, Inc. and its contributors.
26 1.27 thorpej * 4. Neither the name of The NetBSD Foundation nor the names of its
27 1.27 thorpej * contributors may be used to endorse or promote products derived
28 1.27 thorpej * from this software without specific prior written permission.
29 1.27 thorpej *
30 1.27 thorpej * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
31 1.27 thorpej * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
32 1.27 thorpej * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
33 1.27 thorpej * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
34 1.27 thorpej * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
35 1.27 thorpej * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
36 1.27 thorpej * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
37 1.27 thorpej * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
38 1.27 thorpej * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
39 1.27 thorpej * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
40 1.27 thorpej * POSSIBILITY OF SUCH DAMAGE.
41 1.27 thorpej */
42 1.1 lukem
43 1.1 lukem /*
44 1.1 lukem * Copyright (c) 1985, 1989, 1993, 1994
45 1.1 lukem * The Regents of the University of California. All rights reserved.
46 1.1 lukem *
47 1.1 lukem * Redistribution and use in source and binary forms, with or without
48 1.1 lukem * modification, are permitted provided that the following conditions
49 1.1 lukem * are met:
50 1.1 lukem * 1. Redistributions of source code must retain the above copyright
51 1.1 lukem * notice, this list of conditions and the following disclaimer.
52 1.1 lukem * 2. Redistributions in binary form must reproduce the above copyright
53 1.1 lukem * notice, this list of conditions and the following disclaimer in the
54 1.1 lukem * documentation and/or other materials provided with the distribution.
55 1.1 lukem * 3. All advertising materials mentioning features or use of this software
56 1.1 lukem * must display the following acknowledgement:
57 1.1 lukem * This product includes software developed by the University of
58 1.1 lukem * California, Berkeley and its contributors.
59 1.1 lukem * 4. Neither the name of the University nor the names of its contributors
60 1.1 lukem * may be used to endorse or promote products derived from this software
61 1.1 lukem * without specific prior written permission.
62 1.1 lukem *
63 1.1 lukem * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
64 1.1 lukem * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
65 1.1 lukem * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
66 1.1 lukem * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
67 1.1 lukem * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
68 1.1 lukem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
69 1.1 lukem * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
70 1.1 lukem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
71 1.1 lukem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
72 1.1 lukem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
73 1.1 lukem * SUCH DAMAGE.
74 1.1 lukem */
75 1.1 lukem
76 1.10 lukem #include <sys/cdefs.h>
77 1.1 lukem #ifndef lint
78 1.89 lukem __RCSID("$NetBSD: util.c,v 1.89 2000/01/26 10:15:40 lukem Exp $");
79 1.1 lukem #endif /* not lint */
80 1.1 lukem
81 1.1 lukem /*
82 1.1 lukem * FTP User Program -- Misc support routines
83 1.1 lukem */
84 1.27 thorpej #include <sys/types.h>
85 1.27 thorpej #include <sys/socket.h>
86 1.1 lukem #include <sys/ioctl.h>
87 1.1 lukem #include <sys/time.h>
88 1.78 lukem #include <netinet/in.h>
89 1.1 lukem #include <arpa/ftp.h>
90 1.1 lukem
91 1.1 lukem #include <ctype.h>
92 1.1 lukem #include <err.h>
93 1.53 lukem #include <errno.h>
94 1.1 lukem #include <fcntl.h>
95 1.1 lukem #include <glob.h>
96 1.23 christos #include <signal.h>
97 1.10 lukem #include <limits.h>
98 1.7 lukem #include <pwd.h>
99 1.1 lukem #include <stdio.h>
100 1.10 lukem #include <stdlib.h>
101 1.1 lukem #include <string.h>
102 1.68 lukem #include <termios.h>
103 1.1 lukem #include <time.h>
104 1.17 lukem #include <tzfile.h>
105 1.1 lukem #include <unistd.h>
106 1.1 lukem
107 1.1 lukem #include "ftp_var.h"
108 1.1 lukem
109 1.1 lukem /*
110 1.1 lukem * Connect to peer server and
111 1.1 lukem * auto-login, if possible.
112 1.1 lukem */
113 1.1 lukem void
114 1.1 lukem setpeer(argc, argv)
115 1.1 lukem int argc;
116 1.1 lukem char *argv[];
117 1.1 lukem {
118 1.1 lukem char *host;
119 1.54 itojun char *port;
120 1.1 lukem
121 1.79 lukem if (argc == 0)
122 1.79 lukem goto usage;
123 1.1 lukem if (connected) {
124 1.24 lukem fprintf(ttyout, "Already connected to %s, use close first.\n",
125 1.5 lukem hostname);
126 1.1 lukem code = -1;
127 1.1 lukem return;
128 1.1 lukem }
129 1.1 lukem if (argc < 2)
130 1.5 lukem (void)another(&argc, &argv, "to");
131 1.1 lukem if (argc < 2 || argc > 3) {
132 1.79 lukem usage:
133 1.24 lukem fprintf(ttyout, "usage: %s host-name [port]\n", argv[0]);
134 1.1 lukem code = -1;
135 1.1 lukem return;
136 1.1 lukem }
137 1.12 lukem if (gatemode)
138 1.12 lukem port = gateport;
139 1.12 lukem else
140 1.12 lukem port = ftpport;
141 1.54 itojun if (argc > 2)
142 1.80 lukem port = argv[2];
143 1.12 lukem
144 1.12 lukem if (gatemode) {
145 1.12 lukem if (gateserver == NULL || *gateserver == '\0')
146 1.12 lukem errx(1, "gateserver not defined (shouldn't happen)");
147 1.12 lukem host = hookup(gateserver, port);
148 1.12 lukem } else
149 1.12 lukem host = hookup(argv[1], port);
150 1.12 lukem
151 1.1 lukem if (host) {
152 1.1 lukem int overbose;
153 1.1 lukem
154 1.49 lukem if (gatemode && verbose) {
155 1.49 lukem fprintf(ttyout,
156 1.49 lukem "Connecting via pass-through server %s\n",
157 1.49 lukem gateserver);
158 1.12 lukem }
159 1.12 lukem
160 1.1 lukem connected = 1;
161 1.1 lukem /*
162 1.1 lukem * Set up defaults for FTP.
163 1.1 lukem */
164 1.65 lukem (void)strlcpy(typename, "ascii", sizeof(typename));
165 1.65 lukem type = TYPE_A;
166 1.1 lukem curtype = TYPE_A;
167 1.65 lukem (void)strlcpy(formname, "non-print", sizeof(formname));
168 1.65 lukem form = FORM_N;
169 1.65 lukem (void)strlcpy(modename, "stream", sizeof(modename));
170 1.65 lukem mode = MODE_S;
171 1.65 lukem (void)strlcpy(structname, "file", sizeof(structname));
172 1.65 lukem stru = STRU_F;
173 1.65 lukem (void)strlcpy(bytename, "8", sizeof(bytename));
174 1.65 lukem bytesize = 8;
175 1.1 lukem if (autologin)
176 1.28 lukem (void)ftp_login(argv[1], NULL, NULL);
177 1.1 lukem
178 1.1 lukem overbose = verbose;
179 1.1 lukem if (debug == 0)
180 1.1 lukem verbose = -1;
181 1.1 lukem if (command("SYST") == COMPLETE && overbose) {
182 1.1 lukem char *cp, c;
183 1.1 lukem c = 0;
184 1.24 lukem cp = strchr(reply_string + 4, ' ');
185 1.1 lukem if (cp == NULL)
186 1.24 lukem cp = strchr(reply_string + 4, '\r');
187 1.1 lukem if (cp) {
188 1.1 lukem if (cp[-1] == '.')
189 1.1 lukem cp--;
190 1.1 lukem c = *cp;
191 1.1 lukem *cp = '\0';
192 1.1 lukem }
193 1.1 lukem
194 1.24 lukem fprintf(ttyout, "Remote system type is %s.\n",
195 1.24 lukem reply_string + 4);
196 1.1 lukem if (cp)
197 1.1 lukem *cp = c;
198 1.1 lukem }
199 1.1 lukem if (!strncmp(reply_string, "215 UNIX Type: L8", 17)) {
200 1.1 lukem if (proxy)
201 1.1 lukem unix_proxy = 1;
202 1.1 lukem else
203 1.1 lukem unix_server = 1;
204 1.1 lukem /*
205 1.1 lukem * Set type to 0 (not specified by user),
206 1.1 lukem * meaning binary by default, but don't bother
207 1.1 lukem * telling server. We can use binary
208 1.1 lukem * for text files unless changed by the user.
209 1.1 lukem */
210 1.1 lukem type = 0;
211 1.65 lukem (void)strlcpy(typename, "binary", sizeof(typename));
212 1.1 lukem if (overbose)
213 1.24 lukem fprintf(ttyout,
214 1.24 lukem "Using %s mode to transfer files.\n",
215 1.1 lukem typename);
216 1.1 lukem } else {
217 1.1 lukem if (proxy)
218 1.1 lukem unix_proxy = 0;
219 1.1 lukem else
220 1.1 lukem unix_server = 0;
221 1.1 lukem if (overbose &&
222 1.1 lukem !strncmp(reply_string, "215 TOPS20", 10))
223 1.24 lukem fputs(
224 1.24 lukem "Remember to set tenex mode when transferring binary files from this machine.\n",
225 1.24 lukem ttyout);
226 1.1 lukem }
227 1.1 lukem verbose = overbose;
228 1.1 lukem }
229 1.7 lukem }
230 1.7 lukem
231 1.7 lukem /*
232 1.79 lukem * Reset the various variables that indicate connection state back to
233 1.79 lukem * disconnected settings.
234 1.79 lukem * The caller is responsible for issuing any commands to the remote server
235 1.79 lukem * to perform a clean shutdown before this is invoked.
236 1.79 lukem */
237 1.79 lukem void
238 1.79 lukem cleanuppeer()
239 1.79 lukem {
240 1.79 lukem
241 1.79 lukem if (cout)
242 1.79 lukem (void)fclose(cout);
243 1.79 lukem cout = NULL;
244 1.79 lukem connected = 0;
245 1.79 lukem /*
246 1.79 lukem * determine if anonftp was specifically set with -a
247 1.79 lukem * (1), or implicitly set by auto_fetch() (2). in the
248 1.79 lukem * latter case, disable after the current xfer
249 1.79 lukem */
250 1.79 lukem if (anonftp == 2)
251 1.79 lukem anonftp = 0;
252 1.79 lukem data = -1;
253 1.79 lukem epsv4bad = 0;
254 1.81 lukem if (username)
255 1.81 lukem free(username);
256 1.84 lukem username = NULL;
257 1.79 lukem if (!proxy)
258 1.79 lukem macnum = 0;
259 1.79 lukem }
260 1.79 lukem
261 1.79 lukem /*
262 1.79 lukem * Top-level signal handler for interrupted commands.
263 1.79 lukem */
264 1.79 lukem void
265 1.79 lukem intr(dummy)
266 1.79 lukem int dummy;
267 1.79 lukem {
268 1.79 lukem
269 1.79 lukem alarmtimer(0);
270 1.79 lukem if (fromatty)
271 1.79 lukem write(fileno(ttyout), "\n", 1);
272 1.79 lukem siglongjmp(toplevel, 1);
273 1.79 lukem }
274 1.79 lukem
275 1.79 lukem /*
276 1.79 lukem * Signal handler for lost connections; cleanup various elements of
277 1.79 lukem * the connection state, and call cleanuppeer() to finish it off.
278 1.79 lukem */
279 1.79 lukem void
280 1.79 lukem lostpeer(dummy)
281 1.79 lukem int dummy;
282 1.79 lukem {
283 1.79 lukem int oerrno = errno;
284 1.79 lukem
285 1.79 lukem alarmtimer(0);
286 1.79 lukem if (connected) {
287 1.79 lukem if (cout != NULL) {
288 1.79 lukem (void)shutdown(fileno(cout), 1+1);
289 1.79 lukem (void)fclose(cout);
290 1.79 lukem cout = NULL;
291 1.79 lukem }
292 1.79 lukem if (data >= 0) {
293 1.79 lukem (void)shutdown(data, 1+1);
294 1.79 lukem (void)close(data);
295 1.79 lukem data = -1;
296 1.79 lukem }
297 1.79 lukem connected = 0;
298 1.79 lukem }
299 1.79 lukem pswitch(1);
300 1.79 lukem if (connected) {
301 1.79 lukem if (cout != NULL) {
302 1.79 lukem (void)shutdown(fileno(cout), 1+1);
303 1.79 lukem (void)fclose(cout);
304 1.79 lukem cout = NULL;
305 1.79 lukem }
306 1.79 lukem connected = 0;
307 1.79 lukem }
308 1.79 lukem proxflag = 0;
309 1.79 lukem pswitch(0);
310 1.79 lukem cleanuppeer();
311 1.79 lukem errno = oerrno;
312 1.79 lukem }
313 1.79 lukem
314 1.79 lukem
315 1.79 lukem /*
316 1.7 lukem * login to remote host, using given username & password if supplied
317 1.7 lukem */
318 1.7 lukem int
319 1.28 lukem ftp_login(host, user, pass)
320 1.7 lukem const char *host;
321 1.29 mycroft const char *user, *pass;
322 1.7 lukem {
323 1.7 lukem char tmp[80];
324 1.29 mycroft const char *acct;
325 1.14 lukem struct passwd *pw;
326 1.79 lukem int n, aflag, rval, freeuser, freepass, freeacct;
327 1.7 lukem
328 1.7 lukem acct = NULL;
329 1.47 lukem aflag = rval = freeuser = freepass = freeacct = 0;
330 1.7 lukem
331 1.83 lukem if (debug)
332 1.83 lukem fprintf(ttyout, "ftp_login: user `%s' pass `%s' host `%s'\n",
333 1.83 lukem user ? user : "<null>", pass ? pass : "<null>",
334 1.83 lukem host ? host : "<null>");
335 1.83 lukem
336 1.83 lukem
337 1.7 lukem /*
338 1.7 lukem * Set up arguments for an anonymous FTP session, if necessary.
339 1.7 lukem */
340 1.47 lukem if (anonftp) {
341 1.9 lukem user = "anonymous"; /* as per RFC 1635 */
342 1.79 lukem pass = getoptionvalue("anonpass");
343 1.7 lukem }
344 1.7 lukem
345 1.53 lukem if (user == NULL)
346 1.47 lukem freeuser = 1;
347 1.53 lukem if (pass == NULL)
348 1.53 lukem freepass = 1;
349 1.53 lukem freeacct = 1;
350 1.53 lukem if (ruserpass(host, &user, &pass, &acct) < 0) {
351 1.53 lukem code = -1;
352 1.53 lukem goto cleanup_ftp_login;
353 1.47 lukem }
354 1.47 lukem
355 1.7 lukem while (user == NULL) {
356 1.29 mycroft const char *myname = getlogin();
357 1.7 lukem
358 1.14 lukem if (myname == NULL && (pw = getpwuid(getuid())) != NULL)
359 1.14 lukem myname = pw->pw_name;
360 1.7 lukem if (myname)
361 1.24 lukem fprintf(ttyout, "Name (%s:%s): ", host, myname);
362 1.7 lukem else
363 1.24 lukem fprintf(ttyout, "Name (%s): ", host);
364 1.19 lukem *tmp = '\0';
365 1.49 lukem if (fgets(tmp, sizeof(tmp) - 1, stdin) == NULL) {
366 1.49 lukem fprintf(ttyout, "\nEOF received; login aborted.\n");
367 1.77 lukem clearerr(stdin);
368 1.49 lukem code = -1;
369 1.49 lukem goto cleanup_ftp_login;
370 1.49 lukem }
371 1.7 lukem tmp[strlen(tmp) - 1] = '\0';
372 1.51 christos freeuser = 0;
373 1.7 lukem if (*tmp == '\0')
374 1.7 lukem user = myname;
375 1.7 lukem else
376 1.7 lukem user = tmp;
377 1.7 lukem }
378 1.49 lukem
379 1.49 lukem if (gatemode) {
380 1.49 lukem char *nuser;
381 1.49 lukem int len;
382 1.49 lukem
383 1.49 lukem len = strlen(user) + 1 + strlen(host) + 1;
384 1.49 lukem nuser = xmalloc(len);
385 1.65 lukem (void)strlcpy(nuser, user, len);
386 1.65 lukem (void)strlcat(nuser, "@", len);
387 1.65 lukem (void)strlcat(nuser, host, len);
388 1.51 christos freeuser = 1;
389 1.49 lukem user = nuser;
390 1.49 lukem }
391 1.49 lukem
392 1.7 lukem n = command("USER %s", user);
393 1.7 lukem if (n == CONTINUE) {
394 1.51 christos if (pass == NULL) {
395 1.51 christos freepass = 0;
396 1.7 lukem pass = getpass("Password:");
397 1.51 christos }
398 1.7 lukem n = command("PASS %s", pass);
399 1.7 lukem }
400 1.7 lukem if (n == CONTINUE) {
401 1.7 lukem aflag++;
402 1.47 lukem if (acct == NULL) {
403 1.51 christos freeacct = 0;
404 1.7 lukem acct = getpass("Account:");
405 1.49 lukem }
406 1.49 lukem if (acct[0] == '\0') {
407 1.49 lukem warnx("Login failed.");
408 1.49 lukem goto cleanup_ftp_login;
409 1.47 lukem }
410 1.7 lukem n = command("ACCT %s", acct);
411 1.7 lukem }
412 1.7 lukem if ((n != COMPLETE) ||
413 1.7 lukem (!aflag && acct != NULL && command("ACCT %s", acct) != COMPLETE)) {
414 1.7 lukem warnx("Login failed.");
415 1.47 lukem goto cleanup_ftp_login;
416 1.47 lukem }
417 1.48 matthias rval = 1;
418 1.81 lukem username = xstrdup(user);
419 1.48 matthias if (proxy)
420 1.47 lukem goto cleanup_ftp_login;
421 1.48 matthias
422 1.7 lukem connected = -1;
423 1.7 lukem for (n = 0; n < macnum; ++n) {
424 1.7 lukem if (!strcmp("init", macros[n].mac_name)) {
425 1.65 lukem (void)strlcpy(line, "$init", sizeof(line));
426 1.7 lukem makeargv();
427 1.7 lukem domacro(margc, margv);
428 1.7 lukem break;
429 1.7 lukem }
430 1.7 lukem }
431 1.83 lukem updateremotepwd();
432 1.83 lukem
433 1.47 lukem cleanup_ftp_login:
434 1.47 lukem if (user != NULL && freeuser)
435 1.47 lukem free((char *)user);
436 1.47 lukem if (pass != NULL && freepass)
437 1.47 lukem free((char *)pass);
438 1.47 lukem if (acct != NULL && freeacct)
439 1.47 lukem free((char *)acct);
440 1.47 lukem return (rval);
441 1.1 lukem }
442 1.1 lukem
443 1.1 lukem /*
444 1.5 lukem * `another' gets another argument, and stores the new argc and argv.
445 1.79 lukem * It reverts to the top level (via intr()) on EOF/error.
446 1.1 lukem *
447 1.1 lukem * Returns false if no new arguments have been added.
448 1.1 lukem */
449 1.1 lukem int
450 1.1 lukem another(pargc, pargv, prompt)
451 1.1 lukem int *pargc;
452 1.1 lukem char ***pargv;
453 1.1 lukem const char *prompt;
454 1.1 lukem {
455 1.1 lukem int len = strlen(line), ret;
456 1.1 lukem
457 1.1 lukem if (len >= sizeof(line) - 3) {
458 1.24 lukem fputs("sorry, arguments too long.\n", ttyout);
459 1.75 lukem intr(0);
460 1.1 lukem }
461 1.24 lukem fprintf(ttyout, "(%s) ", prompt);
462 1.1 lukem line[len++] = ' ';
463 1.77 lukem if (fgets(&line[len], sizeof(line) - len, stdin) == NULL) {
464 1.77 lukem clearerr(stdin);
465 1.75 lukem intr(0);
466 1.77 lukem }
467 1.1 lukem len += strlen(&line[len]);
468 1.1 lukem if (len > 0 && line[len - 1] == '\n')
469 1.1 lukem line[len - 1] = '\0';
470 1.1 lukem makeargv();
471 1.1 lukem ret = margc > *pargc;
472 1.1 lukem *pargc = margc;
473 1.1 lukem *pargv = margv;
474 1.1 lukem return (ret);
475 1.1 lukem }
476 1.1 lukem
477 1.5 lukem /*
478 1.5 lukem * glob files given in argv[] from the remote server.
479 1.5 lukem * if errbuf isn't NULL, store error messages there instead
480 1.5 lukem * of writing to the screen.
481 1.5 lukem */
482 1.1 lukem char *
483 1.5 lukem remglob(argv, doswitch, errbuf)
484 1.1 lukem char *argv[];
485 1.1 lukem int doswitch;
486 1.5 lukem char **errbuf;
487 1.1 lukem {
488 1.1 lukem char temp[MAXPATHLEN];
489 1.1 lukem static char buf[MAXPATHLEN];
490 1.1 lukem static FILE *ftemp = NULL;
491 1.1 lukem static char **args;
492 1.75 lukem int oldverbose, oldhash, fd, len;
493 1.1 lukem char *cp, *mode;
494 1.1 lukem
495 1.79 lukem if (!mflag || !connected) {
496 1.5 lukem if (!doglob)
497 1.1 lukem args = NULL;
498 1.1 lukem else {
499 1.1 lukem if (ftemp) {
500 1.5 lukem (void)fclose(ftemp);
501 1.1 lukem ftemp = NULL;
502 1.1 lukem }
503 1.1 lukem }
504 1.1 lukem return (NULL);
505 1.1 lukem }
506 1.1 lukem if (!doglob) {
507 1.1 lukem if (args == NULL)
508 1.1 lukem args = argv;
509 1.1 lukem if ((cp = *++args) == NULL)
510 1.1 lukem args = NULL;
511 1.1 lukem return (cp);
512 1.1 lukem }
513 1.1 lukem if (ftemp == NULL) {
514 1.75 lukem len = strlcpy(temp, tmpdir, sizeof(temp));
515 1.75 lukem if (temp[len - 1] != '/')
516 1.74 simonb (void)strlcat(temp, "/", sizeof(temp));
517 1.74 simonb (void)strlcat(temp, TMPFILE, sizeof(temp));
518 1.5 lukem if ((fd = mkstemp(temp)) < 0) {
519 1.1 lukem warn("unable to create temporary file %s", temp);
520 1.1 lukem return (NULL);
521 1.1 lukem }
522 1.1 lukem close(fd);
523 1.5 lukem oldverbose = verbose;
524 1.5 lukem verbose = (errbuf != NULL) ? -1 : 0;
525 1.5 lukem oldhash = hash;
526 1.5 lukem hash = 0;
527 1.5 lukem if (doswitch)
528 1.1 lukem pswitch(!proxy);
529 1.1 lukem for (mode = "w"; *++argv != NULL; mode = "a")
530 1.12 lukem recvrequest("NLST", temp, *argv, mode, 0, 0);
531 1.5 lukem if ((code / 100) != COMPLETE) {
532 1.5 lukem if (errbuf != NULL)
533 1.5 lukem *errbuf = reply_string;
534 1.5 lukem }
535 1.5 lukem if (doswitch)
536 1.1 lukem pswitch(!proxy);
537 1.5 lukem verbose = oldverbose;
538 1.5 lukem hash = oldhash;
539 1.1 lukem ftemp = fopen(temp, "r");
540 1.5 lukem (void)unlink(temp);
541 1.1 lukem if (ftemp == NULL) {
542 1.5 lukem if (errbuf == NULL)
543 1.24 lukem fputs(
544 1.24 lukem "can't find list of remote files, oops.\n",
545 1.24 lukem ttyout);
546 1.5 lukem else
547 1.5 lukem *errbuf =
548 1.5 lukem "can't find list of remote files, oops.";
549 1.1 lukem return (NULL);
550 1.1 lukem }
551 1.1 lukem }
552 1.5 lukem if (fgets(buf, sizeof(buf), ftemp) == NULL) {
553 1.5 lukem (void)fclose(ftemp);
554 1.5 lukem ftemp = NULL;
555 1.1 lukem return (NULL);
556 1.1 lukem }
557 1.1 lukem if ((cp = strchr(buf, '\n')) != NULL)
558 1.1 lukem *cp = '\0';
559 1.1 lukem return (buf);
560 1.1 lukem }
561 1.1 lukem
562 1.1 lukem /*
563 1.65 lukem * Glob a local file name specification with the expectation of a single
564 1.65 lukem * return value. Can't control multiple values being expanded from the
565 1.65 lukem * expression, we return only the first.
566 1.65 lukem * Returns NULL on error, or a pointer to a buffer containing the filename
567 1.65 lukem * that's the caller's responsiblity to free(3) when finished with.
568 1.1 lukem */
569 1.65 lukem char *
570 1.65 lukem globulize(pattern)
571 1.65 lukem const char *pattern;
572 1.1 lukem {
573 1.1 lukem glob_t gl;
574 1.1 lukem int flags;
575 1.65 lukem char *p;
576 1.1 lukem
577 1.1 lukem if (!doglob)
578 1.65 lukem return (xstrdup(pattern));
579 1.1 lukem
580 1.25 kleink flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE;
581 1.1 lukem memset(&gl, 0, sizeof(gl));
582 1.65 lukem if (glob(pattern, flags, NULL, &gl) || gl.gl_pathc == 0) {
583 1.65 lukem warnx("%s: not found", pattern);
584 1.1 lukem globfree(&gl);
585 1.65 lukem return (NULL);
586 1.1 lukem }
587 1.65 lukem p = xstrdup(gl.gl_pathv[0]);
588 1.1 lukem globfree(&gl);
589 1.65 lukem return (p);
590 1.1 lukem }
591 1.1 lukem
592 1.1 lukem /*
593 1.1 lukem * determine size of remote file
594 1.1 lukem */
595 1.1 lukem off_t
596 1.1 lukem remotesize(file, noisy)
597 1.1 lukem const char *file;
598 1.1 lukem int noisy;
599 1.1 lukem {
600 1.1 lukem int overbose;
601 1.1 lukem off_t size;
602 1.1 lukem
603 1.1 lukem overbose = verbose;
604 1.1 lukem size = -1;
605 1.1 lukem if (debug == 0)
606 1.1 lukem verbose = -1;
607 1.11 lukem if (command("SIZE %s", file) == COMPLETE) {
608 1.11 lukem char *cp, *ep;
609 1.11 lukem
610 1.11 lukem cp = strchr(reply_string, ' ');
611 1.11 lukem if (cp != NULL) {
612 1.11 lukem cp++;
613 1.32 lukem #ifndef NO_QUAD
614 1.32 lukem size = strtoq(cp, &ep, 10);
615 1.32 lukem #else
616 1.23 christos size = strtol(cp, &ep, 10);
617 1.23 christos #endif
618 1.23 christos if (*ep != '\0' && !isspace((unsigned char)*ep))
619 1.11 lukem size = -1;
620 1.11 lukem }
621 1.24 lukem } else if (noisy && debug == 0) {
622 1.24 lukem fputs(reply_string, ttyout);
623 1.24 lukem putc('\n', ttyout);
624 1.24 lukem }
625 1.1 lukem verbose = overbose;
626 1.1 lukem return (size);
627 1.1 lukem }
628 1.1 lukem
629 1.1 lukem /*
630 1.1 lukem * determine last modification time (in GMT) of remote file
631 1.1 lukem */
632 1.1 lukem time_t
633 1.1 lukem remotemodtime(file, noisy)
634 1.1 lukem const char *file;
635 1.1 lukem int noisy;
636 1.1 lukem {
637 1.1 lukem int overbose;
638 1.1 lukem time_t rtime;
639 1.14 lukem int ocode;
640 1.1 lukem
641 1.1 lukem overbose = verbose;
642 1.14 lukem ocode = code;
643 1.1 lukem rtime = -1;
644 1.1 lukem if (debug == 0)
645 1.1 lukem verbose = -1;
646 1.1 lukem if (command("MDTM %s", file) == COMPLETE) {
647 1.1 lukem struct tm timebuf;
648 1.87 lukem char *timestr, *frac;
649 1.87 lukem
650 1.87 lukem /*
651 1.87 lukem * time-val = 14DIGIT [ "." 1*DIGIT ]
652 1.87 lukem * YYYYMMDDHHMMSS[.sss]
653 1.87 lukem * mdtm-response = "213" SP time-val CRLF / error-response
654 1.87 lukem */
655 1.87 lukem timestr = reply_string + 4;
656 1.87 lukem
657 1.87 lukem /*
658 1.87 lukem * parse fraction.
659 1.87 lukem * XXX: ignored for now
660 1.87 lukem */
661 1.87 lukem frac = strchr(timestr, '\r');
662 1.87 lukem if (frac != NULL)
663 1.87 lukem *frac = '\0';
664 1.87 lukem frac = strchr(timestr, '.');
665 1.87 lukem if (frac != NULL)
666 1.87 lukem *frac++ = '\0';
667 1.87 lukem if (strlen(timestr) == 15 && strncmp(timestr, "191", 3) == 0) {
668 1.87 lukem /*
669 1.88 lukem * XXX: Workaround for lame ftpd's that return
670 1.88 lukem * `19100' instead of `2000'
671 1.87 lukem */
672 1.87 lukem fprintf(ttyout,
673 1.87 lukem "Y2K warning! Incorrect time-val `%s' received from server.\n",
674 1.87 lukem timestr);
675 1.87 lukem timestr++;
676 1.87 lukem timestr[0] = '2';
677 1.87 lukem timestr[1] = '0';
678 1.87 lukem fprintf(ttyout, "Converted to `%s'\n", timestr);
679 1.87 lukem }
680 1.87 lukem if (strlen(timestr) != 14 ||
681 1.89 lukem strptime(timestr, "%Y %m %d %H %M %S", &timebuf) == NULL) {
682 1.87 lukem bad_parse_time:
683 1.87 lukem fprintf(ttyout, "Can't parse time %s.\n", timestr);
684 1.87 lukem goto cleanup_parse_time;
685 1.87 lukem }
686 1.61 lukem rtime = timegm(&timebuf);
687 1.89 lukem if (rtime == -1) {
688 1.89 lukem if (noisy || debug != 0)
689 1.89 lukem goto bad_parse_time;
690 1.89 lukem else
691 1.89 lukem goto cleanup_parse_time;
692 1.89 lukem } else if (debug)
693 1.89 lukem fprintf(ttyout, "parsed date as: %s", ctime(&rtime));
694 1.24 lukem } else if (noisy && debug == 0) {
695 1.24 lukem fputs(reply_string, ttyout);
696 1.24 lukem putc('\n', ttyout);
697 1.24 lukem }
698 1.87 lukem cleanup_parse_time:
699 1.1 lukem verbose = overbose;
700 1.14 lukem if (rtime == -1)
701 1.14 lukem code = ocode;
702 1.1 lukem return (rtime);
703 1.81 lukem }
704 1.81 lukem
705 1.81 lukem /*
706 1.81 lukem * update global `remotepwd', which contains the state of the remote cwd
707 1.81 lukem */
708 1.81 lukem void
709 1.81 lukem updateremotepwd()
710 1.81 lukem {
711 1.81 lukem int overbose, ocode, i;
712 1.81 lukem char *cp;
713 1.81 lukem
714 1.81 lukem overbose = verbose;
715 1.81 lukem ocode = code;
716 1.81 lukem if (debug == 0)
717 1.81 lukem verbose = -1;
718 1.81 lukem if (command("PWD") != COMPLETE)
719 1.81 lukem goto badremotepwd;
720 1.81 lukem cp = strchr(reply_string, ' ');
721 1.81 lukem if (cp == NULL || cp[0] == '\0' || cp[1] != '"')
722 1.81 lukem goto badremotepwd;
723 1.81 lukem cp += 2;
724 1.81 lukem for (i = 0; *cp && i < sizeof(remotepwd) - 1; i++, cp++) {
725 1.81 lukem if (cp[0] == '"') {
726 1.81 lukem if (cp[1] == '"')
727 1.81 lukem cp++;
728 1.81 lukem else
729 1.81 lukem break;
730 1.81 lukem }
731 1.81 lukem remotepwd[i] = *cp;
732 1.81 lukem }
733 1.81 lukem remotepwd[i] = '\0';
734 1.81 lukem if (debug)
735 1.81 lukem fprintf(ttyout, "got remotepwd as `%s'\n", remotepwd);
736 1.81 lukem goto cleanupremotepwd;
737 1.81 lukem badremotepwd:
738 1.81 lukem remotepwd[0]='\0';
739 1.81 lukem cleanupremotepwd:
740 1.81 lukem verbose = overbose;
741 1.81 lukem code = ocode;
742 1.1 lukem }
743 1.1 lukem
744 1.50 cgd #ifndef NO_PROGRESS
745 1.10 lukem
746 1.24 lukem /*
747 1.24 lukem * return non-zero if we're the current foreground process
748 1.24 lukem */
749 1.24 lukem int
750 1.24 lukem foregroundproc()
751 1.3 lukem {
752 1.9 lukem static pid_t pgrp = -1;
753 1.3 lukem
754 1.9 lukem if (pgrp == -1)
755 1.9 lukem pgrp = getpgrp();
756 1.9 lukem
757 1.62 lukem return (tcgetpgrp(fileno(ttyout)) == pgrp);
758 1.24 lukem }
759 1.24 lukem
760 1.24 lukem
761 1.24 lukem static void updateprogressmeter __P((int));
762 1.24 lukem
763 1.78 lukem /*
764 1.78 lukem * SIGALRM handler to update the progress meter
765 1.78 lukem */
766 1.24 lukem static void
767 1.24 lukem updateprogressmeter(dummy)
768 1.24 lukem int dummy;
769 1.24 lukem {
770 1.75 lukem int oerrno = errno;
771 1.24 lukem
772 1.45 lukem progressmeter(0);
773 1.53 lukem errno = oerrno;
774 1.3 lukem }
775 1.50 cgd #endif /* NO_PROGRESS */
776 1.3 lukem
777 1.38 lukem
778 1.38 lukem /*
779 1.38 lukem * List of order of magnitude prefixes.
780 1.38 lukem * The last is `P', as 2^64 = 16384 Petabytes
781 1.38 lukem */
782 1.38 lukem static const char prefixes[] = " KMGTP";
783 1.38 lukem
784 1.1 lukem /*
785 1.1 lukem * Display a transfer progress bar if progress is non-zero.
786 1.3 lukem * SIGALRM is hijacked for use by this function.
787 1.3 lukem * - Before the transfer, set filesize to size of file (or -1 if unknown),
788 1.3 lukem * and call with flag = -1. This starts the once per second timer,
789 1.3 lukem * and a call to updateprogressmeter() upon SIGALRM.
790 1.3 lukem * - During the transfer, updateprogressmeter will call progressmeter
791 1.3 lukem * with flag = 0
792 1.3 lukem * - After the transfer, call with flag = 1
793 1.1 lukem */
794 1.1 lukem static struct timeval start;
795 1.21 lukem static struct timeval lastupdate;
796 1.1 lukem
797 1.79 lukem #define BUFLEFT (sizeof(buf) - len)
798 1.69 lukem
799 1.1 lukem void
800 1.1 lukem progressmeter(flag)
801 1.1 lukem int flag;
802 1.1 lukem {
803 1.3 lukem static off_t lastsize;
804 1.52 lukem #ifndef NO_PROGRESS
805 1.3 lukem struct timeval now, td, wait;
806 1.34 lukem off_t cursize, abbrevsize, bytespersec;
807 1.17 lukem double elapsed;
808 1.17 lukem int ratio, barlength, i, len, remaining;
809 1.59 lukem
810 1.59 lukem /*
811 1.59 lukem * Work variables for progress bar.
812 1.59 lukem *
813 1.59 lukem * XXX: if the format of the progress bar changes
814 1.59 lukem * (especially the number of characters in the
815 1.59 lukem * `static' portion of it), be sure to update
816 1.59 lukem * these appropriately.
817 1.59 lukem */
818 1.59 lukem char buf[256]; /* workspace for progress bar */
819 1.79 lukem #define BAROVERHEAD 43 /* non `*' portion of progress bar */
820 1.59 lukem /*
821 1.59 lukem * stars should contain at least
822 1.59 lukem * sizeof(buf) - BAROVERHEAD entries
823 1.59 lukem */
824 1.59 lukem const char stars[] =
825 1.59 lukem "*****************************************************************************"
826 1.59 lukem "*****************************************************************************"
827 1.59 lukem "*****************************************************************************";
828 1.59 lukem
829 1.52 lukem #endif
830 1.1 lukem
831 1.3 lukem if (flag == -1) {
832 1.19 lukem (void)gettimeofday(&start, NULL);
833 1.3 lukem lastupdate = start;
834 1.3 lukem lastsize = restart_point;
835 1.3 lukem }
836 1.52 lukem #ifndef NO_PROGRESS
837 1.52 lukem len = 0;
838 1.2 thorpej if (!progress || filesize <= 0)
839 1.1 lukem return;
840 1.45 lukem
841 1.45 lukem (void)gettimeofday(&now, NULL);
842 1.1 lukem cursize = bytes + restart_point;
843 1.45 lukem timersub(&now, &lastupdate, &wait);
844 1.45 lukem if (cursize > lastsize) {
845 1.45 lukem lastupdate = now;
846 1.45 lukem lastsize = cursize;
847 1.45 lukem wait.tv_sec = 0;
848 1.45 lukem }
849 1.45 lukem
850 1.45 lukem /*
851 1.45 lukem * print progress bar only if we are foreground process.
852 1.45 lukem */
853 1.45 lukem if (! foregroundproc())
854 1.45 lukem return;
855 1.67 lukem
856 1.34 lukem ratio = (int)((double)cursize * 100.0 / (double)filesize);
857 1.1 lukem ratio = MAX(ratio, 0);
858 1.1 lukem ratio = MIN(ratio, 100);
859 1.67 lukem len += snprintf(buf + len, BUFLEFT, "\r%3d%% ", ratio);
860 1.1 lukem
861 1.59 lukem /*
862 1.59 lukem * calculate the length of the `*' bar, ensuring that
863 1.59 lukem * the number of stars won't exceed the buffer size
864 1.59 lukem */
865 1.59 lukem barlength = MIN(sizeof(buf) - 1, ttywidth) - BAROVERHEAD;
866 1.1 lukem if (barlength > 0) {
867 1.1 lukem i = barlength * ratio / 100;
868 1.67 lukem len += snprintf(buf + len, BUFLEFT,
869 1.67 lukem "|%.*s%*s|", i, stars, barlength - i, "");
870 1.1 lukem }
871 1.1 lukem
872 1.1 lukem abbrevsize = cursize;
873 1.34 lukem for (i = 0; abbrevsize >= 100000 && i < sizeof(prefixes); i++)
874 1.1 lukem abbrevsize >>= 10;
875 1.67 lukem len += snprintf(buf + len, BUFLEFT,
876 1.32 lukem #ifndef NO_QUAD
877 1.60 lukem " %5lld %c%c ", (long long)abbrevsize,
878 1.32 lukem #else
879 1.32 lukem " %5ld %c%c ", (long)abbrevsize,
880 1.32 lukem #endif
881 1.38 lukem prefixes[i],
882 1.34 lukem i == 0 ? ' ' : 'B');
883 1.3 lukem
884 1.1 lukem timersub(&now, &start, &td);
885 1.1 lukem elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
886 1.3 lukem
887 1.34 lukem bytespersec = 0;
888 1.34 lukem if (bytes > 0) {
889 1.34 lukem bytespersec = bytes;
890 1.34 lukem if (elapsed > 0.0)
891 1.34 lukem bytespersec /= elapsed;
892 1.34 lukem }
893 1.39 lukem for (i = 1; bytespersec >= 1024000 && i < sizeof(prefixes); i++)
894 1.34 lukem bytespersec >>= 10;
895 1.67 lukem len += snprintf(buf + len, BUFLEFT,
896 1.34 lukem #ifndef NO_QUAD
897 1.60 lukem " %3lld.%02d %cB/s ", (long long)bytespersec / 1024,
898 1.34 lukem #else
899 1.39 lukem " %3ld.%02d %cB/s ", (long)bytespersec / 1024,
900 1.34 lukem #endif
901 1.39 lukem (int)((bytespersec % 1024) * 100 / 1024),
902 1.38 lukem prefixes[i]);
903 1.34 lukem
904 1.8 lukem if (bytes <= 0 || elapsed <= 0.0 || cursize > filesize) {
905 1.67 lukem len += snprintf(buf + len, BUFLEFT, " --:-- ETA");
906 1.3 lukem } else if (wait.tv_sec >= STALLTIME) {
907 1.67 lukem len += snprintf(buf + len, BUFLEFT, " - stalled -");
908 1.1 lukem } else {
909 1.17 lukem remaining = (int)
910 1.17 lukem ((filesize - restart_point) / (bytes / elapsed) - elapsed);
911 1.17 lukem if (remaining >= 100 * SECSPERHOUR)
912 1.67 lukem len += snprintf(buf + len, BUFLEFT, " --:-- ETA");
913 1.15 mycroft else {
914 1.17 lukem i = remaining / SECSPERHOUR;
915 1.15 mycroft if (i)
916 1.67 lukem len += snprintf(buf + len, BUFLEFT, "%2d:", i);
917 1.15 mycroft else
918 1.67 lukem len += snprintf(buf + len, BUFLEFT, " ");
919 1.17 lukem i = remaining % SECSPERHOUR;
920 1.67 lukem len += snprintf(buf + len, BUFLEFT,
921 1.67 lukem "%02d:%02d ETA", i / 60, i % 60);
922 1.15 mycroft }
923 1.1 lukem }
924 1.67 lukem if (flag == 1)
925 1.67 lukem len += snprintf(buf + len, BUFLEFT, "\n");
926 1.42 lukem (void)write(fileno(ttyout), buf, len);
927 1.1 lukem
928 1.3 lukem if (flag == -1) {
929 1.71 lukem (void)xsignal_restart(SIGALRM, updateprogressmeter, 1);
930 1.3 lukem alarmtimer(1); /* set alarm timer for 1 Hz */
931 1.3 lukem } else if (flag == 1) {
932 1.33 lukem (void)xsignal(SIGALRM, SIG_DFL);
933 1.3 lukem alarmtimer(0);
934 1.3 lukem }
935 1.52 lukem #endif /* !NO_PROGRESS */
936 1.1 lukem }
937 1.1 lukem
938 1.1 lukem /*
939 1.1 lukem * Display transfer statistics.
940 1.1 lukem * Requires start to be initialised by progressmeter(-1),
941 1.1 lukem * direction to be defined by xfer routines, and filesize and bytes
942 1.1 lukem * to be updated by xfer routines
943 1.24 lukem * If siginfo is nonzero, an ETA is displayed, and the output goes to stderr
944 1.24 lukem * instead of ttyout.
945 1.1 lukem */
946 1.1 lukem void
947 1.1 lukem ptransfer(siginfo)
948 1.1 lukem int siginfo;
949 1.1 lukem {
950 1.21 lukem struct timeval now, td, wait;
951 1.1 lukem double elapsed;
952 1.34 lukem off_t bytespersec;
953 1.38 lukem int remaining, hh, i, len;
954 1.59 lukem
955 1.67 lukem char buf[256]; /* Work variable for transfer status. */
956 1.1 lukem
957 1.52 lukem if (!verbose && !progress && !siginfo)
958 1.1 lukem return;
959 1.1 lukem
960 1.19 lukem (void)gettimeofday(&now, NULL);
961 1.1 lukem timersub(&now, &start, &td);
962 1.1 lukem elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
963 1.34 lukem bytespersec = 0;
964 1.34 lukem if (bytes > 0) {
965 1.34 lukem bytespersec = bytes;
966 1.34 lukem if (elapsed > 0.0)
967 1.34 lukem bytespersec /= elapsed;
968 1.34 lukem }
969 1.17 lukem len = 0;
970 1.67 lukem len += snprintf(buf + len, BUFLEFT,
971 1.23 christos #ifndef NO_QUAD
972 1.60 lukem "%lld byte%s %s in ", (long long)bytes,
973 1.23 christos #else
974 1.23 christos "%ld byte%s %s in ", (long)bytes,
975 1.23 christos #endif
976 1.23 christos bytes == 1 ? "" : "s", direction);
977 1.21 lukem remaining = (int)elapsed;
978 1.21 lukem if (remaining > SECSPERDAY) {
979 1.21 lukem int days;
980 1.21 lukem
981 1.21 lukem days = remaining / SECSPERDAY;
982 1.21 lukem remaining %= SECSPERDAY;
983 1.67 lukem len += snprintf(buf + len, BUFLEFT,
984 1.67 lukem "%d day%s ", days, days == 1 ? "" : "s");
985 1.21 lukem }
986 1.21 lukem hh = remaining / SECSPERHOUR;
987 1.21 lukem remaining %= SECSPERHOUR;
988 1.21 lukem if (hh)
989 1.67 lukem len += snprintf(buf + len, BUFLEFT, "%2d:", hh);
990 1.67 lukem len += snprintf(buf + len, BUFLEFT,
991 1.67 lukem "%02d:%02d ", remaining / 60, remaining % 60);
992 1.38 lukem
993 1.39 lukem for (i = 1; bytespersec >= 1024000 && i < sizeof(prefixes); i++)
994 1.38 lukem bytespersec >>= 10;
995 1.67 lukem len += snprintf(buf + len, BUFLEFT,
996 1.38 lukem #ifndef NO_QUAD
997 1.60 lukem "(%lld.%02d %cB/s)", (long long)bytespersec / 1024,
998 1.38 lukem #else
999 1.39 lukem "(%ld.%02d %cB/s)", (long)bytespersec / 1024,
1000 1.38 lukem #endif
1001 1.39 lukem (int)((bytespersec % 1024) * 100 / 1024),
1002 1.38 lukem prefixes[i]);
1003 1.21 lukem
1004 1.8 lukem if (siginfo && bytes > 0 && elapsed > 0.0 && filesize >= 0
1005 1.8 lukem && bytes + restart_point <= filesize) {
1006 1.1 lukem remaining = (int)((filesize - restart_point) /
1007 1.1 lukem (bytes / elapsed) - elapsed);
1008 1.17 lukem hh = remaining / SECSPERHOUR;
1009 1.17 lukem remaining %= SECSPERHOUR;
1010 1.67 lukem len += snprintf(buf + len, BUFLEFT, " ETA: ");
1011 1.21 lukem if (hh)
1012 1.67 lukem len += snprintf(buf + len, BUFLEFT, "%2d:", hh);
1013 1.67 lukem len += snprintf(buf + len, BUFLEFT, "%02d:%02d",
1014 1.59 lukem remaining / 60, remaining % 60);
1015 1.21 lukem timersub(&now, &lastupdate, &wait);
1016 1.21 lukem if (wait.tv_sec >= STALLTIME)
1017 1.67 lukem len += snprintf(buf + len, BUFLEFT, " (stalled)");
1018 1.1 lukem }
1019 1.67 lukem len += snprintf(buf + len, BUFLEFT, "\n");
1020 1.42 lukem (void)write(siginfo ? STDERR_FILENO : fileno(ttyout), buf, len);
1021 1.1 lukem }
1022 1.1 lukem
1023 1.1 lukem /*
1024 1.78 lukem * SIG{INFO,QUIT} handler to print transfer stats if a transfer is in progress
1025 1.73 lukem */
1026 1.73 lukem void
1027 1.73 lukem psummary(notused)
1028 1.73 lukem int notused;
1029 1.73 lukem {
1030 1.75 lukem int oerrno = errno;
1031 1.73 lukem
1032 1.76 lukem if (bytes > 0) {
1033 1.77 lukem if (fromatty)
1034 1.77 lukem write(fileno(ttyout), "\n", 1);
1035 1.73 lukem ptransfer(1);
1036 1.76 lukem }
1037 1.73 lukem errno = oerrno;
1038 1.73 lukem }
1039 1.73 lukem
1040 1.73 lukem /*
1041 1.1 lukem * List words in stringlist, vertically arranged
1042 1.1 lukem */
1043 1.1 lukem void
1044 1.1 lukem list_vertical(sl)
1045 1.1 lukem StringList *sl;
1046 1.1 lukem {
1047 1.1 lukem int i, j, w;
1048 1.1 lukem int columns, width, lines, items;
1049 1.1 lukem char *p;
1050 1.1 lukem
1051 1.1 lukem width = items = 0;
1052 1.1 lukem
1053 1.1 lukem for (i = 0 ; i < sl->sl_cur ; i++) {
1054 1.1 lukem w = strlen(sl->sl_str[i]);
1055 1.1 lukem if (w > width)
1056 1.1 lukem width = w;
1057 1.1 lukem }
1058 1.1 lukem width = (width + 8) &~ 7;
1059 1.1 lukem
1060 1.1 lukem columns = ttywidth / width;
1061 1.1 lukem if (columns == 0)
1062 1.1 lukem columns = 1;
1063 1.1 lukem lines = (sl->sl_cur + columns - 1) / columns;
1064 1.1 lukem for (i = 0; i < lines; i++) {
1065 1.1 lukem for (j = 0; j < columns; j++) {
1066 1.1 lukem p = sl->sl_str[j * lines + i];
1067 1.1 lukem if (p)
1068 1.24 lukem fputs(p, ttyout);
1069 1.1 lukem if (j * lines + i + lines >= sl->sl_cur) {
1070 1.24 lukem putc('\n', ttyout);
1071 1.1 lukem break;
1072 1.1 lukem }
1073 1.1 lukem w = strlen(p);
1074 1.1 lukem while (w < width) {
1075 1.1 lukem w = (w + 8) &~ 7;
1076 1.24 lukem (void)putc('\t', ttyout);
1077 1.1 lukem }
1078 1.1 lukem }
1079 1.1 lukem }
1080 1.3 lukem }
1081 1.3 lukem
1082 1.3 lukem /*
1083 1.3 lukem * Update the global ttywidth value, using TIOCGWINSZ.
1084 1.3 lukem */
1085 1.3 lukem void
1086 1.3 lukem setttywidth(a)
1087 1.3 lukem int a;
1088 1.3 lukem {
1089 1.3 lukem struct winsize winsize;
1090 1.75 lukem int oerrno = errno;
1091 1.3 lukem
1092 1.34 lukem if (ioctl(fileno(ttyout), TIOCGWINSZ, &winsize) != -1 &&
1093 1.34 lukem winsize.ws_col != 0)
1094 1.3 lukem ttywidth = winsize.ws_col;
1095 1.3 lukem else
1096 1.3 lukem ttywidth = 80;
1097 1.53 lukem errno = oerrno;
1098 1.3 lukem }
1099 1.3 lukem
1100 1.73 lukem /*
1101 1.73 lukem * Change the rate limit up (SIGUSR1) or down (SIGUSR2)
1102 1.73 lukem */
1103 1.53 lukem void
1104 1.76 lukem crankrate(sig)
1105 1.76 lukem int sig;
1106 1.53 lukem {
1107 1.53 lukem
1108 1.53 lukem switch (sig) {
1109 1.53 lukem case SIGUSR1:
1110 1.53 lukem if (rate_get)
1111 1.53 lukem rate_get += rate_get_incr;
1112 1.53 lukem if (rate_put)
1113 1.53 lukem rate_put += rate_put_incr;
1114 1.53 lukem break;
1115 1.53 lukem case SIGUSR2:
1116 1.53 lukem if (rate_get && rate_get > rate_get_incr)
1117 1.53 lukem rate_get -= rate_get_incr;
1118 1.53 lukem if (rate_put && rate_put > rate_put_incr)
1119 1.53 lukem rate_put -= rate_put_incr;
1120 1.53 lukem break;
1121 1.53 lukem default:
1122 1.53 lukem err(1, "crankrate invoked with unknown signal: %d", sig);
1123 1.53 lukem }
1124 1.53 lukem }
1125 1.53 lukem
1126 1.53 lukem
1127 1.3 lukem /*
1128 1.3 lukem * Set the SIGALRM interval timer for wait seconds, 0 to disable.
1129 1.3 lukem */
1130 1.3 lukem void
1131 1.3 lukem alarmtimer(wait)
1132 1.3 lukem int wait;
1133 1.3 lukem {
1134 1.3 lukem struct itimerval itv;
1135 1.3 lukem
1136 1.3 lukem itv.it_value.tv_sec = wait;
1137 1.3 lukem itv.it_value.tv_usec = 0;
1138 1.3 lukem itv.it_interval = itv.it_value;
1139 1.3 lukem setitimer(ITIMER_REAL, &itv, NULL);
1140 1.1 lukem }
1141 1.6 lukem
1142 1.6 lukem /*
1143 1.6 lukem * Setup or cleanup EditLine structures
1144 1.6 lukem */
1145 1.50 cgd #ifndef NO_EDITCOMPLETE
1146 1.6 lukem void
1147 1.6 lukem controlediting()
1148 1.6 lukem {
1149 1.6 lukem if (editing && el == NULL && hist == NULL) {
1150 1.16 christos HistEvent ev;
1151 1.30 lukem int editmode;
1152 1.16 christos
1153 1.24 lukem el = el_init(__progname, stdin, ttyout, stderr);
1154 1.23 christos /* init editline */
1155 1.6 lukem hist = history_init(); /* init the builtin history */
1156 1.23 christos history(hist, &ev, H_SETSIZE, 100);/* remember 100 events */
1157 1.6 lukem el_set(el, EL_HIST, history, hist); /* use history */
1158 1.6 lukem
1159 1.6 lukem el_set(el, EL_EDITOR, "emacs"); /* default editor is emacs */
1160 1.82 lukem el_set(el, EL_PROMPT, prompt); /* set the prompt functions */
1161 1.82 lukem el_set(el, EL_RPROMPT, rprompt);
1162 1.6 lukem
1163 1.6 lukem /* add local file completion, bind to TAB */
1164 1.6 lukem el_set(el, EL_ADDFN, "ftp-complete",
1165 1.6 lukem "Context sensitive argument completion",
1166 1.6 lukem complete);
1167 1.6 lukem el_set(el, EL_BIND, "^I", "ftp-complete", NULL);
1168 1.6 lukem el_source(el, NULL); /* read ~/.editrc */
1169 1.30 lukem if ((el_get(el, EL_EDITMODE, &editmode) != -1) && editmode == 0)
1170 1.30 lukem editing = 0; /* the user doesn't want editing,
1171 1.30 lukem * so disable, and let statement
1172 1.30 lukem * below cleanup */
1173 1.30 lukem else
1174 1.30 lukem el_set(el, EL_SIGNAL, 1);
1175 1.30 lukem }
1176 1.30 lukem if (!editing) {
1177 1.6 lukem if (hist) {
1178 1.6 lukem history_end(hist);
1179 1.6 lukem hist = NULL;
1180 1.6 lukem }
1181 1.6 lukem if (el) {
1182 1.6 lukem el_end(el);
1183 1.6 lukem el = NULL;
1184 1.6 lukem }
1185 1.6 lukem }
1186 1.6 lukem }
1187 1.50 cgd #endif /* !NO_EDITCOMPLETE */
1188 1.27 thorpej
1189 1.27 thorpej /*
1190 1.53 lukem * Convert the string `arg' to an int, which may have an optional SI suffix
1191 1.53 lukem * (`b', `k', `m', `g'). Returns the number for success, -1 otherwise.
1192 1.27 thorpej */
1193 1.27 thorpej int
1194 1.53 lukem strsuftoi(arg)
1195 1.27 thorpej const char *arg;
1196 1.27 thorpej {
1197 1.27 thorpej char *cp;
1198 1.53 lukem long val;
1199 1.27 thorpej
1200 1.35 christos if (!isdigit((unsigned char)arg[0]))
1201 1.27 thorpej return (-1);
1202 1.27 thorpej
1203 1.27 thorpej val = strtol(arg, &cp, 10);
1204 1.27 thorpej if (cp != NULL) {
1205 1.53 lukem if (cp[0] != '\0' && cp[1] != '\0')
1206 1.27 thorpej return (-1);
1207 1.53 lukem switch (tolower((unsigned char)cp[0])) {
1208 1.53 lukem case '\0':
1209 1.53 lukem case 'b':
1210 1.53 lukem break;
1211 1.53 lukem case 'k':
1212 1.53 lukem val <<= 10;
1213 1.53 lukem break;
1214 1.53 lukem case 'm':
1215 1.53 lukem val <<= 20;
1216 1.53 lukem break;
1217 1.53 lukem case 'g':
1218 1.53 lukem val <<= 30;
1219 1.53 lukem break;
1220 1.53 lukem default:
1221 1.53 lukem return (-1);
1222 1.53 lukem }
1223 1.27 thorpej }
1224 1.53 lukem if (val < 0 || val > INT_MAX)
1225 1.27 thorpej return (-1);
1226 1.27 thorpej
1227 1.27 thorpej return (val);
1228 1.27 thorpej }
1229 1.27 thorpej
1230 1.27 thorpej /*
1231 1.27 thorpej * Set up socket buffer sizes before a connection is made.
1232 1.27 thorpej */
1233 1.27 thorpej void
1234 1.27 thorpej setupsockbufsize(sock)
1235 1.27 thorpej int sock;
1236 1.27 thorpej {
1237 1.27 thorpej
1238 1.58 lukem if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (void *) &sndbuf_size,
1239 1.58 lukem sizeof(rcvbuf_size)) < 0)
1240 1.58 lukem warn("unable to set sndbuf size %d", sndbuf_size);
1241 1.58 lukem
1242 1.58 lukem if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (void *) &rcvbuf_size,
1243 1.58 lukem sizeof(rcvbuf_size)) < 0)
1244 1.58 lukem warn("unable to set rcvbuf size %d", rcvbuf_size);
1245 1.43 lukem }
1246 1.43 lukem
1247 1.78 lukem /*
1248 1.78 lukem * Copy characters from src into dst, \ quoting characters that require it
1249 1.78 lukem */
1250 1.43 lukem void
1251 1.43 lukem ftpvis(dst, dstlen, src, srclen)
1252 1.43 lukem char *dst;
1253 1.43 lukem size_t dstlen;
1254 1.43 lukem const char *src;
1255 1.43 lukem size_t srclen;
1256 1.43 lukem {
1257 1.43 lukem int di, si;
1258 1.43 lukem
1259 1.43 lukem for (di = si = 0;
1260 1.43 lukem src[si] != '\0' && di < dstlen && si < srclen;
1261 1.43 lukem di++, si++) {
1262 1.43 lukem switch (src[si]) {
1263 1.43 lukem case '\\':
1264 1.43 lukem case ' ':
1265 1.43 lukem case '\t':
1266 1.43 lukem case '\r':
1267 1.44 lukem case '\n':
1268 1.43 lukem case '"':
1269 1.43 lukem dst[di++] = '\\';
1270 1.43 lukem if (di >= dstlen)
1271 1.43 lukem break;
1272 1.43 lukem /* FALLTHROUGH */
1273 1.43 lukem default:
1274 1.43 lukem dst[di] = src[si];
1275 1.43 lukem }
1276 1.43 lukem }
1277 1.43 lukem dst[di] = '\0';
1278 1.82 lukem }
1279 1.82 lukem
1280 1.82 lukem /*
1281 1.82 lukem * Copy src into buf (which is len bytes long), expanding % sequences.
1282 1.82 lukem */
1283 1.82 lukem void
1284 1.82 lukem formatbuf(buf, len, src)
1285 1.82 lukem char *buf;
1286 1.82 lukem size_t len;
1287 1.82 lukem const char *src;
1288 1.82 lukem {
1289 1.82 lukem const char *p;
1290 1.82 lukem char *p2, *q;
1291 1.82 lukem int i, op, updirs, pdirs;
1292 1.82 lukem
1293 1.82 lukem #define ADDBUF(x) do { \
1294 1.82 lukem if (i >= len - 1) \
1295 1.82 lukem goto endbuf; \
1296 1.82 lukem buf[i++] = (x); \
1297 1.82 lukem } while (0)
1298 1.82 lukem
1299 1.82 lukem p = src;
1300 1.82 lukem for (i = 0; *p; p++) {
1301 1.82 lukem if (*p != '%') {
1302 1.82 lukem ADDBUF(*p);
1303 1.82 lukem continue;
1304 1.82 lukem }
1305 1.82 lukem p++;
1306 1.82 lukem
1307 1.82 lukem switch (op = *p) {
1308 1.82 lukem
1309 1.82 lukem case '/':
1310 1.82 lukem case '.':
1311 1.82 lukem case 'c':
1312 1.82 lukem p2 = connected ? remotepwd : "";
1313 1.82 lukem updirs = pdirs = 0;
1314 1.82 lukem
1315 1.82 lukem /* option to determine fixed # of dirs from path */
1316 1.82 lukem if (op == '.' || op == 'c') {
1317 1.82 lukem int skip;
1318 1.82 lukem
1319 1.82 lukem q = p2;
1320 1.82 lukem while (*p2) /* calc # of /'s */
1321 1.82 lukem if (*p2++ == '/')
1322 1.82 lukem updirs++;
1323 1.82 lukem if (p[1] == '0') { /* print <x> or ... */
1324 1.82 lukem pdirs = 1;
1325 1.82 lukem p++;
1326 1.82 lukem }
1327 1.82 lukem if (p[1] >= '1' && p[1] <= '9') {
1328 1.82 lukem /* calc # to skip */
1329 1.82 lukem skip = p[1] - '0';
1330 1.82 lukem p++;
1331 1.82 lukem } else
1332 1.82 lukem skip = 1;
1333 1.82 lukem
1334 1.82 lukem updirs -= skip;
1335 1.82 lukem while (skip-- > 0) {
1336 1.82 lukem while ((p2 > q) && (*p2 != '/'))
1337 1.82 lukem p2--; /* back up */
1338 1.82 lukem if (skip && p2 > q)
1339 1.82 lukem p2--;
1340 1.82 lukem }
1341 1.82 lukem if (*p2 == '/' && p2 != q)
1342 1.82 lukem p2++;
1343 1.82 lukem }
1344 1.82 lukem
1345 1.82 lukem if (updirs > 0 && pdirs) {
1346 1.82 lukem if (i >= len - 5)
1347 1.82 lukem break;
1348 1.82 lukem if (op == '.') {
1349 1.82 lukem ADDBUF('.');
1350 1.82 lukem ADDBUF('.');
1351 1.82 lukem ADDBUF('.');
1352 1.82 lukem } else {
1353 1.82 lukem ADDBUF('/');
1354 1.82 lukem ADDBUF('<');
1355 1.82 lukem if (updirs > 9) {
1356 1.82 lukem ADDBUF('9');
1357 1.82 lukem ADDBUF('+');
1358 1.82 lukem } else
1359 1.82 lukem ADDBUF('0' + updirs);
1360 1.82 lukem ADDBUF('>');
1361 1.82 lukem }
1362 1.82 lukem }
1363 1.82 lukem for (; *p2; p2++)
1364 1.82 lukem ADDBUF(*p2);
1365 1.82 lukem break;
1366 1.82 lukem
1367 1.82 lukem case 'M':
1368 1.82 lukem case 'm':
1369 1.82 lukem for (p2 = connected ? hostname : "-"; *p2; p2++) {
1370 1.82 lukem if (op == 'm' && *p2 == '.')
1371 1.82 lukem break;
1372 1.82 lukem ADDBUF(*p2);
1373 1.82 lukem }
1374 1.82 lukem break;
1375 1.82 lukem
1376 1.82 lukem case 'n':
1377 1.82 lukem for (p2 = connected ? username : "-"; *p2 ; p2++)
1378 1.82 lukem ADDBUF(*p2);
1379 1.82 lukem break;
1380 1.82 lukem
1381 1.82 lukem case '%':
1382 1.82 lukem ADDBUF('%');
1383 1.82 lukem break;
1384 1.82 lukem
1385 1.82 lukem default: /* display unknown codes literally */
1386 1.82 lukem ADDBUF('%');
1387 1.82 lukem ADDBUF(op);
1388 1.82 lukem break;
1389 1.82 lukem
1390 1.82 lukem }
1391 1.82 lukem }
1392 1.82 lukem endbuf:
1393 1.82 lukem buf[i] = '\0';
1394 1.27 thorpej }
1395 1.55 lukem
1396 1.55 lukem /*
1397 1.55 lukem * Determine if given string is an IPv6 address or not.
1398 1.55 lukem * Return 1 for yes, 0 for no
1399 1.55 lukem */
1400 1.55 lukem int
1401 1.55 lukem isipv6addr(addr)
1402 1.55 lukem const char *addr;
1403 1.55 lukem {
1404 1.55 lukem int rv = 0;
1405 1.55 lukem #ifdef INET6
1406 1.78 lukem struct sockaddr_in6 su_sin6;
1407 1.55 lukem
1408 1.78 lukem rv = inet_pton(AF_INET6, addr, &su_sin6.sin6_addr);
1409 1.55 lukem if (debug)
1410 1.55 lukem fprintf(ttyout, "isipv6addr: got %d for %s\n", rv, addr);
1411 1.55 lukem #endif
1412 1.86 itojun return (rv == 1) ? 1 : 0;
1413 1.55 lukem }
1414 1.55 lukem
1415 1.27 thorpej
1416 1.27 thorpej /*
1417 1.27 thorpej * Internal version of connect(2); sets socket buffer sizes first.
1418 1.27 thorpej */
1419 1.27 thorpej int
1420 1.27 thorpej xconnect(sock, name, namelen)
1421 1.27 thorpej int sock;
1422 1.27 thorpej const struct sockaddr *name;
1423 1.27 thorpej int namelen;
1424 1.27 thorpej {
1425 1.27 thorpej
1426 1.27 thorpej setupsockbufsize(sock);
1427 1.27 thorpej return (connect(sock, name, namelen));
1428 1.27 thorpej }
1429 1.27 thorpej
1430 1.27 thorpej /*
1431 1.27 thorpej * Internal version of listen(2); sets socket buffer sizes first.
1432 1.27 thorpej */
1433 1.27 thorpej int
1434 1.27 thorpej xlisten(sock, backlog)
1435 1.27 thorpej int sock, backlog;
1436 1.27 thorpej {
1437 1.27 thorpej
1438 1.27 thorpej setupsockbufsize(sock);
1439 1.27 thorpej return (listen(sock, backlog));
1440 1.31 lukem }
1441 1.31 lukem
1442 1.78 lukem /*
1443 1.78 lukem * malloc() with inbuilt error checking
1444 1.78 lukem */
1445 1.31 lukem void *
1446 1.31 lukem xmalloc(size)
1447 1.31 lukem size_t size;
1448 1.31 lukem {
1449 1.31 lukem void *p;
1450 1.31 lukem
1451 1.31 lukem p = malloc(size);
1452 1.31 lukem if (p == NULL)
1453 1.31 lukem err(1, "Unable to allocate %ld bytes of memory", (long)size);
1454 1.31 lukem return (p);
1455 1.85 lukem }
1456 1.85 lukem
1457 1.85 lukem /*
1458 1.85 lukem * sl_init() with inbuilt error checking
1459 1.85 lukem */
1460 1.85 lukem StringList *
1461 1.85 lukem xsl_init()
1462 1.85 lukem {
1463 1.85 lukem StringList *p;
1464 1.85 lukem
1465 1.85 lukem p = sl_init();
1466 1.85 lukem if (p == NULL)
1467 1.85 lukem err(1, "Unable to allocate memory for stringlist");
1468 1.85 lukem return (p);
1469 1.85 lukem }
1470 1.85 lukem
1471 1.85 lukem /*
1472 1.85 lukem * sl_add() with inbuilt error checking
1473 1.85 lukem */
1474 1.85 lukem void
1475 1.85 lukem xsl_add(sl, i)
1476 1.85 lukem StringList *sl;
1477 1.85 lukem char *i;
1478 1.85 lukem {
1479 1.85 lukem
1480 1.85 lukem if (sl_add(sl, i) == -1)
1481 1.85 lukem err(1, "Unable to add `%s' to stringlist", i);
1482 1.31 lukem }
1483 1.31 lukem
1484 1.78 lukem /*
1485 1.78 lukem * strdup() with inbuilt error checking
1486 1.78 lukem */
1487 1.31 lukem char *
1488 1.31 lukem xstrdup(str)
1489 1.31 lukem const char *str;
1490 1.31 lukem {
1491 1.31 lukem char *s;
1492 1.31 lukem
1493 1.31 lukem if (str == NULL)
1494 1.31 lukem errx(1, "xstrdup() called with NULL argument");
1495 1.31 lukem s = strdup(str);
1496 1.31 lukem if (s == NULL)
1497 1.31 lukem err(1, "Unable to allocate memory for string copy");
1498 1.31 lukem return (s);
1499 1.33 lukem }
1500 1.33 lukem
1501 1.78 lukem /*
1502 1.78 lukem * Install a POSIX signal handler, allowing the invoker to set whether
1503 1.78 lukem * the signal should be restartable or not
1504 1.78 lukem */
1505 1.33 lukem sig_t
1506 1.71 lukem xsignal_restart(sig, func, restartable)
1507 1.33 lukem int sig;
1508 1.33 lukem void (*func) __P((int));
1509 1.71 lukem int restartable;
1510 1.33 lukem {
1511 1.33 lukem struct sigaction act, oact;
1512 1.71 lukem act.sa_handler = func;
1513 1.33 lukem
1514 1.33 lukem sigemptyset(&act.sa_mask);
1515 1.64 lukem #if defined(SA_RESTART) /* 4.4BSD, Posix(?), SVR4 */
1516 1.71 lukem act.sa_flags = restartable ? SA_RESTART : 0;
1517 1.64 lukem #elif defined(SA_INTERRUPT) /* SunOS 4.x */
1518 1.71 lukem act.sa_flags = restartable ? 0 : SA_INTERRUPT;
1519 1.64 lukem #else
1520 1.64 lukem #error "system must have SA_RESTART or SA_INTERRUPT"
1521 1.64 lukem #endif
1522 1.33 lukem if (sigaction(sig, &act, &oact) < 0)
1523 1.33 lukem return (SIG_ERR);
1524 1.33 lukem return (oact.sa_handler);
1525 1.71 lukem }
1526 1.71 lukem
1527 1.78 lukem /*
1528 1.78 lukem * Install a signal handler with the `restartable' flag set dependent upon
1529 1.78 lukem * which signal is being set. (This is a wrapper to xsignal_restart())
1530 1.78 lukem */
1531 1.71 lukem sig_t
1532 1.71 lukem xsignal(sig, func)
1533 1.71 lukem int sig;
1534 1.71 lukem void (*func) __P((int));
1535 1.71 lukem {
1536 1.71 lukem int restartable;
1537 1.71 lukem
1538 1.71 lukem /*
1539 1.71 lukem * Some signals print output or change the state of the process.
1540 1.71 lukem * There should be restartable, so that reads and writes are
1541 1.71 lukem * not affected. Some signals should cause program flow to change;
1542 1.71 lukem * these signals should not be restartable, so that the system call
1543 1.71 lukem * will return with EINTR, and the program will go do something
1544 1.75 lukem * different. If the signal handler calls longjmp() or siglongjmp(),
1545 1.75 lukem * it doesn't matter if it's restartable.
1546 1.71 lukem */
1547 1.71 lukem
1548 1.71 lukem switch(sig) {
1549 1.73 lukem #ifdef SIGINFO
1550 1.71 lukem case SIGINFO:
1551 1.73 lukem #endif
1552 1.73 lukem case SIGQUIT:
1553 1.71 lukem case SIGUSR1:
1554 1.71 lukem case SIGUSR2:
1555 1.71 lukem case SIGWINCH:
1556 1.71 lukem restartable = 1;
1557 1.71 lukem break;
1558 1.71 lukem
1559 1.71 lukem case SIGALRM:
1560 1.71 lukem case SIGINT:
1561 1.71 lukem case SIGPIPE:
1562 1.71 lukem restartable = 0;
1563 1.71 lukem break;
1564 1.71 lukem
1565 1.71 lukem default:
1566 1.71 lukem /*
1567 1.71 lukem * This is unpleasant, but I don't know what would be better.
1568 1.71 lukem * Right now, this "can't happen"
1569 1.71 lukem */
1570 1.79 lukem errx(1, "xsignal_restart called with signal %d", sig);
1571 1.71 lukem }
1572 1.71 lukem
1573 1.71 lukem return(xsignal_restart(sig, func, restartable));
1574 1.27 thorpej }
1575