util.c revision 1.78 1 1.78 lukem /* $NetBSD: util.c,v 1.78 1999/10/13 02:47:54 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.78 lukem __RCSID("$NetBSD: util.c,v 1.78 1999/10/13 02:47:54 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.1 lukem if (connected) {
122 1.24 lukem fprintf(ttyout, "Already connected to %s, use close first.\n",
123 1.5 lukem hostname);
124 1.1 lukem code = -1;
125 1.1 lukem return;
126 1.1 lukem }
127 1.1 lukem if (argc < 2)
128 1.5 lukem (void)another(&argc, &argv, "to");
129 1.1 lukem if (argc < 2 || argc > 3) {
130 1.24 lukem fprintf(ttyout, "usage: %s host-name [port]\n", argv[0]);
131 1.1 lukem code = -1;
132 1.1 lukem return;
133 1.1 lukem }
134 1.12 lukem if (gatemode)
135 1.12 lukem port = gateport;
136 1.12 lukem else
137 1.12 lukem port = ftpport;
138 1.54 itojun if (argc > 2)
139 1.54 itojun port = strdup(argv[2]);
140 1.12 lukem
141 1.12 lukem if (gatemode) {
142 1.12 lukem if (gateserver == NULL || *gateserver == '\0')
143 1.12 lukem errx(1, "gateserver not defined (shouldn't happen)");
144 1.12 lukem host = hookup(gateserver, port);
145 1.12 lukem } else
146 1.12 lukem host = hookup(argv[1], port);
147 1.12 lukem
148 1.1 lukem if (host) {
149 1.1 lukem int overbose;
150 1.1 lukem
151 1.49 lukem if (gatemode && verbose) {
152 1.49 lukem fprintf(ttyout,
153 1.49 lukem "Connecting via pass-through server %s\n",
154 1.49 lukem gateserver);
155 1.12 lukem }
156 1.12 lukem
157 1.1 lukem connected = 1;
158 1.1 lukem /*
159 1.1 lukem * Set up defaults for FTP.
160 1.1 lukem */
161 1.65 lukem (void)strlcpy(typename, "ascii", sizeof(typename));
162 1.65 lukem type = TYPE_A;
163 1.1 lukem curtype = TYPE_A;
164 1.65 lukem (void)strlcpy(formname, "non-print", sizeof(formname));
165 1.65 lukem form = FORM_N;
166 1.65 lukem (void)strlcpy(modename, "stream", sizeof(modename));
167 1.65 lukem mode = MODE_S;
168 1.65 lukem (void)strlcpy(structname, "file", sizeof(structname));
169 1.65 lukem stru = STRU_F;
170 1.65 lukem (void)strlcpy(bytename, "8", sizeof(bytename));
171 1.65 lukem bytesize = 8;
172 1.1 lukem if (autologin)
173 1.28 lukem (void)ftp_login(argv[1], NULL, NULL);
174 1.1 lukem
175 1.1 lukem overbose = verbose;
176 1.1 lukem if (debug == 0)
177 1.1 lukem verbose = -1;
178 1.1 lukem if (command("SYST") == COMPLETE && overbose) {
179 1.1 lukem char *cp, c;
180 1.1 lukem c = 0;
181 1.24 lukem cp = strchr(reply_string + 4, ' ');
182 1.1 lukem if (cp == NULL)
183 1.24 lukem cp = strchr(reply_string + 4, '\r');
184 1.1 lukem if (cp) {
185 1.1 lukem if (cp[-1] == '.')
186 1.1 lukem cp--;
187 1.1 lukem c = *cp;
188 1.1 lukem *cp = '\0';
189 1.1 lukem }
190 1.1 lukem
191 1.24 lukem fprintf(ttyout, "Remote system type is %s.\n",
192 1.24 lukem reply_string + 4);
193 1.1 lukem if (cp)
194 1.1 lukem *cp = c;
195 1.1 lukem }
196 1.1 lukem if (!strncmp(reply_string, "215 UNIX Type: L8", 17)) {
197 1.1 lukem if (proxy)
198 1.1 lukem unix_proxy = 1;
199 1.1 lukem else
200 1.1 lukem unix_server = 1;
201 1.1 lukem /*
202 1.1 lukem * Set type to 0 (not specified by user),
203 1.1 lukem * meaning binary by default, but don't bother
204 1.1 lukem * telling server. We can use binary
205 1.1 lukem * for text files unless changed by the user.
206 1.1 lukem */
207 1.1 lukem type = 0;
208 1.65 lukem (void)strlcpy(typename, "binary", sizeof(typename));
209 1.1 lukem if (overbose)
210 1.24 lukem fprintf(ttyout,
211 1.24 lukem "Using %s mode to transfer files.\n",
212 1.1 lukem typename);
213 1.1 lukem } else {
214 1.1 lukem if (proxy)
215 1.1 lukem unix_proxy = 0;
216 1.1 lukem else
217 1.1 lukem unix_server = 0;
218 1.1 lukem if (overbose &&
219 1.1 lukem !strncmp(reply_string, "215 TOPS20", 10))
220 1.24 lukem fputs(
221 1.24 lukem "Remember to set tenex mode when transferring binary files from this machine.\n",
222 1.24 lukem ttyout);
223 1.1 lukem }
224 1.1 lukem verbose = overbose;
225 1.1 lukem }
226 1.7 lukem }
227 1.7 lukem
228 1.7 lukem /*
229 1.7 lukem * login to remote host, using given username & password if supplied
230 1.7 lukem */
231 1.7 lukem int
232 1.28 lukem ftp_login(host, user, pass)
233 1.7 lukem const char *host;
234 1.29 mycroft const char *user, *pass;
235 1.7 lukem {
236 1.7 lukem char tmp[80];
237 1.29 mycroft const char *acct;
238 1.14 lukem struct passwd *pw;
239 1.49 lukem int n, aflag, rval, freeuser, freepass, freeacct, len;
240 1.7 lukem
241 1.7 lukem acct = NULL;
242 1.47 lukem aflag = rval = freeuser = freepass = freeacct = 0;
243 1.7 lukem
244 1.7 lukem /*
245 1.7 lukem * Set up arguments for an anonymous FTP session, if necessary.
246 1.7 lukem */
247 1.47 lukem if (anonftp) {
248 1.7 lukem /*
249 1.7 lukem * Set up anonymous login password.
250 1.7 lukem */
251 1.31 lukem if ((pass = getenv("FTPANONPASS")) == NULL) {
252 1.49 lukem char *anonpass;
253 1.49 lukem
254 1.31 lukem if ((pass = getlogin()) == NULL) {
255 1.31 lukem if ((pw = getpwuid(getuid())) == NULL)
256 1.31 lukem pass = "anonymous";
257 1.31 lukem else
258 1.31 lukem pass = pw->pw_name;
259 1.31 lukem }
260 1.31 lukem /*
261 1.31 lukem * Every anonymous FTP server I've encountered
262 1.31 lukem * will accept the string "username@", and will
263 1.31 lukem * append the hostname itself. We do this by default
264 1.31 lukem * since many servers are picky about not having
265 1.31 lukem * a FQDN in the anonymous password.
266 1.31 lukem * - thorpej (at) netbsd.org
267 1.31 lukem */
268 1.49 lukem len = strlen(pass) + 2;
269 1.49 lukem anonpass = xmalloc(len);
270 1.65 lukem (void)strlcpy(anonpass, pass, len);
271 1.65 lukem (void)strlcat(anonpass, "@", len);
272 1.31 lukem pass = anonpass;
273 1.49 lukem freepass = 1;
274 1.14 lukem }
275 1.9 lukem user = "anonymous"; /* as per RFC 1635 */
276 1.7 lukem }
277 1.7 lukem
278 1.53 lukem if (user == NULL)
279 1.47 lukem freeuser = 1;
280 1.53 lukem if (pass == NULL)
281 1.53 lukem freepass = 1;
282 1.53 lukem freeacct = 1;
283 1.53 lukem if (ruserpass(host, &user, &pass, &acct) < 0) {
284 1.53 lukem code = -1;
285 1.53 lukem goto cleanup_ftp_login;
286 1.47 lukem }
287 1.47 lukem
288 1.7 lukem while (user == NULL) {
289 1.29 mycroft const char *myname = getlogin();
290 1.7 lukem
291 1.14 lukem if (myname == NULL && (pw = getpwuid(getuid())) != NULL)
292 1.14 lukem myname = pw->pw_name;
293 1.7 lukem if (myname)
294 1.24 lukem fprintf(ttyout, "Name (%s:%s): ", host, myname);
295 1.7 lukem else
296 1.24 lukem fprintf(ttyout, "Name (%s): ", host);
297 1.19 lukem *tmp = '\0';
298 1.49 lukem if (fgets(tmp, sizeof(tmp) - 1, stdin) == NULL) {
299 1.49 lukem fprintf(ttyout, "\nEOF received; login aborted.\n");
300 1.77 lukem clearerr(stdin);
301 1.49 lukem code = -1;
302 1.49 lukem goto cleanup_ftp_login;
303 1.49 lukem }
304 1.7 lukem tmp[strlen(tmp) - 1] = '\0';
305 1.51 christos freeuser = 0;
306 1.7 lukem if (*tmp == '\0')
307 1.7 lukem user = myname;
308 1.7 lukem else
309 1.7 lukem user = tmp;
310 1.7 lukem }
311 1.49 lukem
312 1.49 lukem if (gatemode) {
313 1.49 lukem char *nuser;
314 1.49 lukem int len;
315 1.49 lukem
316 1.49 lukem len = strlen(user) + 1 + strlen(host) + 1;
317 1.49 lukem nuser = xmalloc(len);
318 1.65 lukem (void)strlcpy(nuser, user, len);
319 1.65 lukem (void)strlcat(nuser, "@", len);
320 1.65 lukem (void)strlcat(nuser, host, len);
321 1.51 christos freeuser = 1;
322 1.49 lukem user = nuser;
323 1.49 lukem }
324 1.49 lukem
325 1.7 lukem n = command("USER %s", user);
326 1.7 lukem if (n == CONTINUE) {
327 1.51 christos if (pass == NULL) {
328 1.51 christos freepass = 0;
329 1.7 lukem pass = getpass("Password:");
330 1.51 christos }
331 1.7 lukem n = command("PASS %s", pass);
332 1.7 lukem }
333 1.7 lukem if (n == CONTINUE) {
334 1.7 lukem aflag++;
335 1.47 lukem if (acct == NULL) {
336 1.51 christos freeacct = 0;
337 1.7 lukem acct = getpass("Account:");
338 1.49 lukem }
339 1.49 lukem if (acct[0] == '\0') {
340 1.49 lukem warnx("Login failed.");
341 1.49 lukem goto cleanup_ftp_login;
342 1.47 lukem }
343 1.7 lukem n = command("ACCT %s", acct);
344 1.7 lukem }
345 1.7 lukem if ((n != COMPLETE) ||
346 1.7 lukem (!aflag && acct != NULL && command("ACCT %s", acct) != COMPLETE)) {
347 1.7 lukem warnx("Login failed.");
348 1.47 lukem goto cleanup_ftp_login;
349 1.47 lukem }
350 1.48 matthias rval = 1;
351 1.48 matthias if (proxy)
352 1.47 lukem goto cleanup_ftp_login;
353 1.48 matthias
354 1.7 lukem connected = -1;
355 1.7 lukem for (n = 0; n < macnum; ++n) {
356 1.7 lukem if (!strcmp("init", macros[n].mac_name)) {
357 1.65 lukem (void)strlcpy(line, "$init", sizeof(line));
358 1.7 lukem makeargv();
359 1.7 lukem domacro(margc, margv);
360 1.7 lukem break;
361 1.7 lukem }
362 1.7 lukem }
363 1.47 lukem cleanup_ftp_login:
364 1.47 lukem if (user != NULL && freeuser)
365 1.47 lukem free((char *)user);
366 1.47 lukem if (pass != NULL && freepass)
367 1.47 lukem free((char *)pass);
368 1.47 lukem if (acct != NULL && freeacct)
369 1.47 lukem free((char *)acct);
370 1.47 lukem return (rval);
371 1.1 lukem }
372 1.1 lukem
373 1.1 lukem /*
374 1.5 lukem * `another' gets another argument, and stores the new argc and argv.
375 1.1 lukem * It reverts to the top level (via main.c's intr()) on EOF/error.
376 1.1 lukem *
377 1.1 lukem * Returns false if no new arguments have been added.
378 1.1 lukem */
379 1.1 lukem int
380 1.1 lukem another(pargc, pargv, prompt)
381 1.1 lukem int *pargc;
382 1.1 lukem char ***pargv;
383 1.1 lukem const char *prompt;
384 1.1 lukem {
385 1.1 lukem int len = strlen(line), ret;
386 1.1 lukem
387 1.1 lukem if (len >= sizeof(line) - 3) {
388 1.24 lukem fputs("sorry, arguments too long.\n", ttyout);
389 1.75 lukem intr(0);
390 1.1 lukem }
391 1.24 lukem fprintf(ttyout, "(%s) ", prompt);
392 1.1 lukem line[len++] = ' ';
393 1.77 lukem if (fgets(&line[len], sizeof(line) - len, stdin) == NULL) {
394 1.77 lukem clearerr(stdin);
395 1.75 lukem intr(0);
396 1.77 lukem }
397 1.1 lukem len += strlen(&line[len]);
398 1.1 lukem if (len > 0 && line[len - 1] == '\n')
399 1.1 lukem line[len - 1] = '\0';
400 1.1 lukem makeargv();
401 1.1 lukem ret = margc > *pargc;
402 1.1 lukem *pargc = margc;
403 1.1 lukem *pargv = margv;
404 1.1 lukem return (ret);
405 1.1 lukem }
406 1.1 lukem
407 1.5 lukem /*
408 1.5 lukem * glob files given in argv[] from the remote server.
409 1.5 lukem * if errbuf isn't NULL, store error messages there instead
410 1.5 lukem * of writing to the screen.
411 1.5 lukem */
412 1.1 lukem char *
413 1.5 lukem remglob(argv, doswitch, errbuf)
414 1.1 lukem char *argv[];
415 1.1 lukem int doswitch;
416 1.5 lukem char **errbuf;
417 1.1 lukem {
418 1.1 lukem char temp[MAXPATHLEN];
419 1.1 lukem static char buf[MAXPATHLEN];
420 1.1 lukem static FILE *ftemp = NULL;
421 1.1 lukem static char **args;
422 1.75 lukem int oldverbose, oldhash, fd, len;
423 1.1 lukem char *cp, *mode;
424 1.1 lukem
425 1.1 lukem if (!mflag) {
426 1.5 lukem if (!doglob)
427 1.1 lukem args = NULL;
428 1.1 lukem else {
429 1.1 lukem if (ftemp) {
430 1.5 lukem (void)fclose(ftemp);
431 1.1 lukem ftemp = NULL;
432 1.1 lukem }
433 1.1 lukem }
434 1.1 lukem return (NULL);
435 1.1 lukem }
436 1.1 lukem if (!doglob) {
437 1.1 lukem if (args == NULL)
438 1.1 lukem args = argv;
439 1.1 lukem if ((cp = *++args) == NULL)
440 1.1 lukem args = NULL;
441 1.1 lukem return (cp);
442 1.1 lukem }
443 1.1 lukem if (ftemp == NULL) {
444 1.75 lukem len = strlcpy(temp, tmpdir, sizeof(temp));
445 1.75 lukem if (temp[len - 1] != '/')
446 1.74 simonb (void)strlcat(temp, "/", sizeof(temp));
447 1.74 simonb (void)strlcat(temp, TMPFILE, sizeof(temp));
448 1.5 lukem if ((fd = mkstemp(temp)) < 0) {
449 1.1 lukem warn("unable to create temporary file %s", temp);
450 1.1 lukem return (NULL);
451 1.1 lukem }
452 1.1 lukem close(fd);
453 1.5 lukem oldverbose = verbose;
454 1.5 lukem verbose = (errbuf != NULL) ? -1 : 0;
455 1.5 lukem oldhash = hash;
456 1.5 lukem hash = 0;
457 1.5 lukem if (doswitch)
458 1.1 lukem pswitch(!proxy);
459 1.1 lukem for (mode = "w"; *++argv != NULL; mode = "a")
460 1.12 lukem recvrequest("NLST", temp, *argv, mode, 0, 0);
461 1.5 lukem if ((code / 100) != COMPLETE) {
462 1.5 lukem if (errbuf != NULL)
463 1.5 lukem *errbuf = reply_string;
464 1.5 lukem }
465 1.5 lukem if (doswitch)
466 1.1 lukem pswitch(!proxy);
467 1.5 lukem verbose = oldverbose;
468 1.5 lukem hash = oldhash;
469 1.1 lukem ftemp = fopen(temp, "r");
470 1.5 lukem (void)unlink(temp);
471 1.1 lukem if (ftemp == NULL) {
472 1.5 lukem if (errbuf == NULL)
473 1.24 lukem fputs(
474 1.24 lukem "can't find list of remote files, oops.\n",
475 1.24 lukem ttyout);
476 1.5 lukem else
477 1.5 lukem *errbuf =
478 1.5 lukem "can't find list of remote files, oops.";
479 1.1 lukem return (NULL);
480 1.1 lukem }
481 1.1 lukem }
482 1.5 lukem if (fgets(buf, sizeof(buf), ftemp) == NULL) {
483 1.5 lukem (void)fclose(ftemp);
484 1.5 lukem ftemp = NULL;
485 1.1 lukem return (NULL);
486 1.1 lukem }
487 1.1 lukem if ((cp = strchr(buf, '\n')) != NULL)
488 1.1 lukem *cp = '\0';
489 1.1 lukem return (buf);
490 1.1 lukem }
491 1.1 lukem
492 1.1 lukem /*
493 1.65 lukem * Glob a local file name specification with the expectation of a single
494 1.65 lukem * return value. Can't control multiple values being expanded from the
495 1.65 lukem * expression, we return only the first.
496 1.65 lukem * Returns NULL on error, or a pointer to a buffer containing the filename
497 1.65 lukem * that's the caller's responsiblity to free(3) when finished with.
498 1.1 lukem */
499 1.65 lukem char *
500 1.65 lukem globulize(pattern)
501 1.65 lukem const char *pattern;
502 1.1 lukem {
503 1.1 lukem glob_t gl;
504 1.1 lukem int flags;
505 1.65 lukem char *p;
506 1.1 lukem
507 1.1 lukem if (!doglob)
508 1.65 lukem return (xstrdup(pattern));
509 1.1 lukem
510 1.25 kleink flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE;
511 1.1 lukem memset(&gl, 0, sizeof(gl));
512 1.65 lukem if (glob(pattern, flags, NULL, &gl) || gl.gl_pathc == 0) {
513 1.65 lukem warnx("%s: not found", pattern);
514 1.1 lukem globfree(&gl);
515 1.65 lukem return (NULL);
516 1.1 lukem }
517 1.65 lukem p = xstrdup(gl.gl_pathv[0]);
518 1.1 lukem globfree(&gl);
519 1.65 lukem return (p);
520 1.1 lukem }
521 1.1 lukem
522 1.1 lukem /*
523 1.1 lukem * determine size of remote file
524 1.1 lukem */
525 1.1 lukem off_t
526 1.1 lukem remotesize(file, noisy)
527 1.1 lukem const char *file;
528 1.1 lukem int noisy;
529 1.1 lukem {
530 1.1 lukem int overbose;
531 1.1 lukem off_t size;
532 1.1 lukem
533 1.1 lukem overbose = verbose;
534 1.1 lukem size = -1;
535 1.1 lukem if (debug == 0)
536 1.1 lukem verbose = -1;
537 1.11 lukem if (command("SIZE %s", file) == COMPLETE) {
538 1.11 lukem char *cp, *ep;
539 1.11 lukem
540 1.11 lukem cp = strchr(reply_string, ' ');
541 1.11 lukem if (cp != NULL) {
542 1.11 lukem cp++;
543 1.32 lukem #ifndef NO_QUAD
544 1.32 lukem size = strtoq(cp, &ep, 10);
545 1.32 lukem #else
546 1.23 christos size = strtol(cp, &ep, 10);
547 1.23 christos #endif
548 1.23 christos if (*ep != '\0' && !isspace((unsigned char)*ep))
549 1.11 lukem size = -1;
550 1.11 lukem }
551 1.24 lukem } else if (noisy && debug == 0) {
552 1.24 lukem fputs(reply_string, ttyout);
553 1.24 lukem putc('\n', ttyout);
554 1.24 lukem }
555 1.1 lukem verbose = overbose;
556 1.1 lukem return (size);
557 1.1 lukem }
558 1.1 lukem
559 1.1 lukem /*
560 1.1 lukem * determine last modification time (in GMT) of remote file
561 1.1 lukem */
562 1.1 lukem time_t
563 1.1 lukem remotemodtime(file, noisy)
564 1.1 lukem const char *file;
565 1.1 lukem int noisy;
566 1.1 lukem {
567 1.1 lukem int overbose;
568 1.1 lukem time_t rtime;
569 1.14 lukem int ocode;
570 1.1 lukem
571 1.1 lukem overbose = verbose;
572 1.14 lukem ocode = code;
573 1.1 lukem rtime = -1;
574 1.1 lukem if (debug == 0)
575 1.1 lukem verbose = -1;
576 1.1 lukem if (command("MDTM %s", file) == COMPLETE) {
577 1.1 lukem struct tm timebuf;
578 1.1 lukem int yy, mo, day, hour, min, sec;
579 1.1 lukem sscanf(reply_string, "%*s %04d%02d%02d%02d%02d%02d", &yy, &mo,
580 1.1 lukem &day, &hour, &min, &sec);
581 1.1 lukem memset(&timebuf, 0, sizeof(timebuf));
582 1.1 lukem timebuf.tm_sec = sec;
583 1.1 lukem timebuf.tm_min = min;
584 1.1 lukem timebuf.tm_hour = hour;
585 1.1 lukem timebuf.tm_mday = day;
586 1.1 lukem timebuf.tm_mon = mo - 1;
587 1.22 christos timebuf.tm_year = yy - TM_YEAR_BASE;
588 1.1 lukem timebuf.tm_isdst = -1;
589 1.61 lukem rtime = timegm(&timebuf);
590 1.1 lukem if (rtime == -1 && (noisy || debug != 0))
591 1.24 lukem fprintf(ttyout, "Can't convert %s to a time.\n",
592 1.24 lukem reply_string);
593 1.24 lukem } else if (noisy && debug == 0) {
594 1.24 lukem fputs(reply_string, ttyout);
595 1.24 lukem putc('\n', ttyout);
596 1.24 lukem }
597 1.1 lukem verbose = overbose;
598 1.14 lukem if (rtime == -1)
599 1.14 lukem code = ocode;
600 1.1 lukem return (rtime);
601 1.1 lukem }
602 1.1 lukem
603 1.50 cgd #ifndef NO_PROGRESS
604 1.10 lukem
605 1.24 lukem /*
606 1.24 lukem * return non-zero if we're the current foreground process
607 1.24 lukem */
608 1.24 lukem int
609 1.24 lukem foregroundproc()
610 1.3 lukem {
611 1.9 lukem static pid_t pgrp = -1;
612 1.3 lukem
613 1.9 lukem if (pgrp == -1)
614 1.9 lukem pgrp = getpgrp();
615 1.9 lukem
616 1.62 lukem return (tcgetpgrp(fileno(ttyout)) == pgrp);
617 1.24 lukem }
618 1.24 lukem
619 1.24 lukem
620 1.24 lukem static void updateprogressmeter __P((int));
621 1.24 lukem
622 1.78 lukem /*
623 1.78 lukem * SIGALRM handler to update the progress meter
624 1.78 lukem */
625 1.24 lukem static void
626 1.24 lukem updateprogressmeter(dummy)
627 1.24 lukem int dummy;
628 1.24 lukem {
629 1.75 lukem int oerrno = errno;
630 1.24 lukem
631 1.45 lukem progressmeter(0);
632 1.53 lukem errno = oerrno;
633 1.3 lukem }
634 1.50 cgd #endif /* NO_PROGRESS */
635 1.3 lukem
636 1.38 lukem
637 1.38 lukem /*
638 1.38 lukem * List of order of magnitude prefixes.
639 1.38 lukem * The last is `P', as 2^64 = 16384 Petabytes
640 1.38 lukem */
641 1.38 lukem static const char prefixes[] = " KMGTP";
642 1.38 lukem
643 1.1 lukem /*
644 1.1 lukem * Display a transfer progress bar if progress is non-zero.
645 1.3 lukem * SIGALRM is hijacked for use by this function.
646 1.3 lukem * - Before the transfer, set filesize to size of file (or -1 if unknown),
647 1.3 lukem * and call with flag = -1. This starts the once per second timer,
648 1.3 lukem * and a call to updateprogressmeter() upon SIGALRM.
649 1.3 lukem * - During the transfer, updateprogressmeter will call progressmeter
650 1.3 lukem * with flag = 0
651 1.3 lukem * - After the transfer, call with flag = 1
652 1.1 lukem */
653 1.1 lukem static struct timeval start;
654 1.21 lukem static struct timeval lastupdate;
655 1.1 lukem
656 1.69 lukem #define BUFLEFT (sizeof(buf) - len)
657 1.69 lukem
658 1.1 lukem void
659 1.1 lukem progressmeter(flag)
660 1.1 lukem int flag;
661 1.1 lukem {
662 1.3 lukem static off_t lastsize;
663 1.52 lukem #ifndef NO_PROGRESS
664 1.3 lukem struct timeval now, td, wait;
665 1.34 lukem off_t cursize, abbrevsize, bytespersec;
666 1.17 lukem double elapsed;
667 1.17 lukem int ratio, barlength, i, len, remaining;
668 1.59 lukem
669 1.59 lukem /*
670 1.59 lukem * Work variables for progress bar.
671 1.59 lukem *
672 1.59 lukem * XXX: if the format of the progress bar changes
673 1.59 lukem * (especially the number of characters in the
674 1.59 lukem * `static' portion of it), be sure to update
675 1.59 lukem * these appropriately.
676 1.59 lukem */
677 1.59 lukem char buf[256]; /* workspace for progress bar */
678 1.59 lukem #define BAROVERHEAD 43 /* non `*' portion of progress bar */
679 1.59 lukem /*
680 1.59 lukem * stars should contain at least
681 1.59 lukem * sizeof(buf) - BAROVERHEAD entries
682 1.59 lukem */
683 1.59 lukem const char stars[] =
684 1.59 lukem "*****************************************************************************"
685 1.59 lukem "*****************************************************************************"
686 1.59 lukem "*****************************************************************************";
687 1.59 lukem
688 1.52 lukem #endif
689 1.1 lukem
690 1.3 lukem if (flag == -1) {
691 1.19 lukem (void)gettimeofday(&start, NULL);
692 1.3 lukem lastupdate = start;
693 1.3 lukem lastsize = restart_point;
694 1.3 lukem }
695 1.52 lukem #ifndef NO_PROGRESS
696 1.52 lukem len = 0;
697 1.2 thorpej if (!progress || filesize <= 0)
698 1.1 lukem return;
699 1.45 lukem
700 1.45 lukem (void)gettimeofday(&now, NULL);
701 1.1 lukem cursize = bytes + restart_point;
702 1.45 lukem timersub(&now, &lastupdate, &wait);
703 1.45 lukem if (cursize > lastsize) {
704 1.45 lukem lastupdate = now;
705 1.45 lukem lastsize = cursize;
706 1.45 lukem wait.tv_sec = 0;
707 1.45 lukem }
708 1.45 lukem
709 1.45 lukem /*
710 1.45 lukem * print progress bar only if we are foreground process.
711 1.45 lukem */
712 1.45 lukem if (! foregroundproc())
713 1.45 lukem return;
714 1.67 lukem
715 1.34 lukem ratio = (int)((double)cursize * 100.0 / (double)filesize);
716 1.1 lukem ratio = MAX(ratio, 0);
717 1.1 lukem ratio = MIN(ratio, 100);
718 1.67 lukem len += snprintf(buf + len, BUFLEFT, "\r%3d%% ", ratio);
719 1.1 lukem
720 1.59 lukem /*
721 1.59 lukem * calculate the length of the `*' bar, ensuring that
722 1.59 lukem * the number of stars won't exceed the buffer size
723 1.59 lukem */
724 1.59 lukem barlength = MIN(sizeof(buf) - 1, ttywidth) - BAROVERHEAD;
725 1.1 lukem if (barlength > 0) {
726 1.1 lukem i = barlength * ratio / 100;
727 1.67 lukem len += snprintf(buf + len, BUFLEFT,
728 1.67 lukem "|%.*s%*s|", i, stars, barlength - i, "");
729 1.1 lukem }
730 1.1 lukem
731 1.1 lukem abbrevsize = cursize;
732 1.34 lukem for (i = 0; abbrevsize >= 100000 && i < sizeof(prefixes); i++)
733 1.1 lukem abbrevsize >>= 10;
734 1.67 lukem len += snprintf(buf + len, BUFLEFT,
735 1.32 lukem #ifndef NO_QUAD
736 1.60 lukem " %5lld %c%c ", (long long)abbrevsize,
737 1.32 lukem #else
738 1.32 lukem " %5ld %c%c ", (long)abbrevsize,
739 1.32 lukem #endif
740 1.38 lukem prefixes[i],
741 1.34 lukem i == 0 ? ' ' : 'B');
742 1.3 lukem
743 1.1 lukem timersub(&now, &start, &td);
744 1.1 lukem elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
745 1.3 lukem
746 1.34 lukem bytespersec = 0;
747 1.34 lukem if (bytes > 0) {
748 1.34 lukem bytespersec = bytes;
749 1.34 lukem if (elapsed > 0.0)
750 1.34 lukem bytespersec /= elapsed;
751 1.34 lukem }
752 1.39 lukem for (i = 1; bytespersec >= 1024000 && i < sizeof(prefixes); i++)
753 1.34 lukem bytespersec >>= 10;
754 1.67 lukem len += snprintf(buf + len, BUFLEFT,
755 1.34 lukem #ifndef NO_QUAD
756 1.60 lukem " %3lld.%02d %cB/s ", (long long)bytespersec / 1024,
757 1.34 lukem #else
758 1.39 lukem " %3ld.%02d %cB/s ", (long)bytespersec / 1024,
759 1.34 lukem #endif
760 1.39 lukem (int)((bytespersec % 1024) * 100 / 1024),
761 1.38 lukem prefixes[i]);
762 1.34 lukem
763 1.8 lukem if (bytes <= 0 || elapsed <= 0.0 || cursize > filesize) {
764 1.67 lukem len += snprintf(buf + len, BUFLEFT, " --:-- ETA");
765 1.3 lukem } else if (wait.tv_sec >= STALLTIME) {
766 1.67 lukem len += snprintf(buf + len, BUFLEFT, " - stalled -");
767 1.1 lukem } else {
768 1.17 lukem remaining = (int)
769 1.17 lukem ((filesize - restart_point) / (bytes / elapsed) - elapsed);
770 1.17 lukem if (remaining >= 100 * SECSPERHOUR)
771 1.67 lukem len += snprintf(buf + len, BUFLEFT, " --:-- ETA");
772 1.15 mycroft else {
773 1.17 lukem i = remaining / SECSPERHOUR;
774 1.15 mycroft if (i)
775 1.67 lukem len += snprintf(buf + len, BUFLEFT, "%2d:", i);
776 1.15 mycroft else
777 1.67 lukem len += snprintf(buf + len, BUFLEFT, " ");
778 1.17 lukem i = remaining % SECSPERHOUR;
779 1.67 lukem len += snprintf(buf + len, BUFLEFT,
780 1.67 lukem "%02d:%02d ETA", i / 60, i % 60);
781 1.15 mycroft }
782 1.1 lukem }
783 1.67 lukem if (flag == 1)
784 1.67 lukem len += snprintf(buf + len, BUFLEFT, "\n");
785 1.42 lukem (void)write(fileno(ttyout), buf, len);
786 1.1 lukem
787 1.3 lukem if (flag == -1) {
788 1.71 lukem (void)xsignal_restart(SIGALRM, updateprogressmeter, 1);
789 1.3 lukem alarmtimer(1); /* set alarm timer for 1 Hz */
790 1.3 lukem } else if (flag == 1) {
791 1.33 lukem (void)xsignal(SIGALRM, SIG_DFL);
792 1.3 lukem alarmtimer(0);
793 1.3 lukem }
794 1.52 lukem #endif /* !NO_PROGRESS */
795 1.1 lukem }
796 1.1 lukem
797 1.1 lukem /*
798 1.1 lukem * Display transfer statistics.
799 1.1 lukem * Requires start to be initialised by progressmeter(-1),
800 1.1 lukem * direction to be defined by xfer routines, and filesize and bytes
801 1.1 lukem * to be updated by xfer routines
802 1.24 lukem * If siginfo is nonzero, an ETA is displayed, and the output goes to stderr
803 1.24 lukem * instead of ttyout.
804 1.1 lukem */
805 1.1 lukem void
806 1.1 lukem ptransfer(siginfo)
807 1.1 lukem int siginfo;
808 1.1 lukem {
809 1.21 lukem struct timeval now, td, wait;
810 1.1 lukem double elapsed;
811 1.34 lukem off_t bytespersec;
812 1.38 lukem int remaining, hh, i, len;
813 1.59 lukem
814 1.67 lukem char buf[256]; /* Work variable for transfer status. */
815 1.1 lukem
816 1.52 lukem if (!verbose && !progress && !siginfo)
817 1.1 lukem return;
818 1.1 lukem
819 1.19 lukem (void)gettimeofday(&now, NULL);
820 1.1 lukem timersub(&now, &start, &td);
821 1.1 lukem elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
822 1.34 lukem bytespersec = 0;
823 1.34 lukem if (bytes > 0) {
824 1.34 lukem bytespersec = bytes;
825 1.34 lukem if (elapsed > 0.0)
826 1.34 lukem bytespersec /= elapsed;
827 1.34 lukem }
828 1.17 lukem len = 0;
829 1.67 lukem len += snprintf(buf + len, BUFLEFT,
830 1.23 christos #ifndef NO_QUAD
831 1.60 lukem "%lld byte%s %s in ", (long long)bytes,
832 1.23 christos #else
833 1.23 christos "%ld byte%s %s in ", (long)bytes,
834 1.23 christos #endif
835 1.23 christos bytes == 1 ? "" : "s", direction);
836 1.21 lukem remaining = (int)elapsed;
837 1.21 lukem if (remaining > SECSPERDAY) {
838 1.21 lukem int days;
839 1.21 lukem
840 1.21 lukem days = remaining / SECSPERDAY;
841 1.21 lukem remaining %= SECSPERDAY;
842 1.67 lukem len += snprintf(buf + len, BUFLEFT,
843 1.67 lukem "%d day%s ", days, days == 1 ? "" : "s");
844 1.21 lukem }
845 1.21 lukem hh = remaining / SECSPERHOUR;
846 1.21 lukem remaining %= SECSPERHOUR;
847 1.21 lukem if (hh)
848 1.67 lukem len += snprintf(buf + len, BUFLEFT, "%2d:", hh);
849 1.67 lukem len += snprintf(buf + len, BUFLEFT,
850 1.67 lukem "%02d:%02d ", remaining / 60, remaining % 60);
851 1.38 lukem
852 1.39 lukem for (i = 1; bytespersec >= 1024000 && i < sizeof(prefixes); i++)
853 1.38 lukem bytespersec >>= 10;
854 1.67 lukem len += snprintf(buf + len, BUFLEFT,
855 1.38 lukem #ifndef NO_QUAD
856 1.60 lukem "(%lld.%02d %cB/s)", (long long)bytespersec / 1024,
857 1.38 lukem #else
858 1.39 lukem "(%ld.%02d %cB/s)", (long)bytespersec / 1024,
859 1.38 lukem #endif
860 1.39 lukem (int)((bytespersec % 1024) * 100 / 1024),
861 1.38 lukem prefixes[i]);
862 1.21 lukem
863 1.8 lukem if (siginfo && bytes > 0 && elapsed > 0.0 && filesize >= 0
864 1.8 lukem && bytes + restart_point <= filesize) {
865 1.1 lukem remaining = (int)((filesize - restart_point) /
866 1.1 lukem (bytes / elapsed) - elapsed);
867 1.17 lukem hh = remaining / SECSPERHOUR;
868 1.17 lukem remaining %= SECSPERHOUR;
869 1.67 lukem len += snprintf(buf + len, BUFLEFT, " ETA: ");
870 1.21 lukem if (hh)
871 1.67 lukem len += snprintf(buf + len, BUFLEFT, "%2d:", hh);
872 1.67 lukem len += snprintf(buf + len, BUFLEFT, "%02d:%02d",
873 1.59 lukem remaining / 60, remaining % 60);
874 1.21 lukem timersub(&now, &lastupdate, &wait);
875 1.21 lukem if (wait.tv_sec >= STALLTIME)
876 1.67 lukem len += snprintf(buf + len, BUFLEFT, " (stalled)");
877 1.1 lukem }
878 1.67 lukem len += snprintf(buf + len, BUFLEFT, "\n");
879 1.42 lukem (void)write(siginfo ? STDERR_FILENO : fileno(ttyout), buf, len);
880 1.1 lukem }
881 1.1 lukem
882 1.1 lukem /*
883 1.78 lukem * SIG{INFO,QUIT} handler to print transfer stats if a transfer is in progress
884 1.73 lukem */
885 1.73 lukem void
886 1.73 lukem psummary(notused)
887 1.73 lukem int notused;
888 1.73 lukem {
889 1.75 lukem int oerrno = errno;
890 1.73 lukem
891 1.76 lukem if (bytes > 0) {
892 1.77 lukem if (fromatty)
893 1.77 lukem write(fileno(ttyout), "\n", 1);
894 1.73 lukem ptransfer(1);
895 1.76 lukem }
896 1.73 lukem errno = oerrno;
897 1.73 lukem }
898 1.73 lukem
899 1.73 lukem /*
900 1.1 lukem * List words in stringlist, vertically arranged
901 1.1 lukem */
902 1.1 lukem void
903 1.1 lukem list_vertical(sl)
904 1.1 lukem StringList *sl;
905 1.1 lukem {
906 1.1 lukem int i, j, w;
907 1.1 lukem int columns, width, lines, items;
908 1.1 lukem char *p;
909 1.1 lukem
910 1.1 lukem width = items = 0;
911 1.1 lukem
912 1.1 lukem for (i = 0 ; i < sl->sl_cur ; i++) {
913 1.1 lukem w = strlen(sl->sl_str[i]);
914 1.1 lukem if (w > width)
915 1.1 lukem width = w;
916 1.1 lukem }
917 1.1 lukem width = (width + 8) &~ 7;
918 1.1 lukem
919 1.1 lukem columns = ttywidth / width;
920 1.1 lukem if (columns == 0)
921 1.1 lukem columns = 1;
922 1.1 lukem lines = (sl->sl_cur + columns - 1) / columns;
923 1.1 lukem for (i = 0; i < lines; i++) {
924 1.1 lukem for (j = 0; j < columns; j++) {
925 1.1 lukem p = sl->sl_str[j * lines + i];
926 1.1 lukem if (p)
927 1.24 lukem fputs(p, ttyout);
928 1.1 lukem if (j * lines + i + lines >= sl->sl_cur) {
929 1.24 lukem putc('\n', ttyout);
930 1.1 lukem break;
931 1.1 lukem }
932 1.1 lukem w = strlen(p);
933 1.1 lukem while (w < width) {
934 1.1 lukem w = (w + 8) &~ 7;
935 1.24 lukem (void)putc('\t', ttyout);
936 1.1 lukem }
937 1.1 lukem }
938 1.1 lukem }
939 1.3 lukem }
940 1.3 lukem
941 1.3 lukem /*
942 1.3 lukem * Update the global ttywidth value, using TIOCGWINSZ.
943 1.3 lukem */
944 1.3 lukem void
945 1.3 lukem setttywidth(a)
946 1.3 lukem int a;
947 1.3 lukem {
948 1.3 lukem struct winsize winsize;
949 1.75 lukem int oerrno = errno;
950 1.3 lukem
951 1.34 lukem if (ioctl(fileno(ttyout), TIOCGWINSZ, &winsize) != -1 &&
952 1.34 lukem winsize.ws_col != 0)
953 1.3 lukem ttywidth = winsize.ws_col;
954 1.3 lukem else
955 1.3 lukem ttywidth = 80;
956 1.53 lukem errno = oerrno;
957 1.3 lukem }
958 1.3 lukem
959 1.73 lukem /*
960 1.73 lukem * Change the rate limit up (SIGUSR1) or down (SIGUSR2)
961 1.73 lukem */
962 1.53 lukem void
963 1.76 lukem crankrate(sig)
964 1.76 lukem int sig;
965 1.53 lukem {
966 1.53 lukem
967 1.53 lukem switch (sig) {
968 1.53 lukem case SIGUSR1:
969 1.53 lukem if (rate_get)
970 1.53 lukem rate_get += rate_get_incr;
971 1.53 lukem if (rate_put)
972 1.53 lukem rate_put += rate_put_incr;
973 1.53 lukem break;
974 1.53 lukem case SIGUSR2:
975 1.53 lukem if (rate_get && rate_get > rate_get_incr)
976 1.53 lukem rate_get -= rate_get_incr;
977 1.53 lukem if (rate_put && rate_put > rate_put_incr)
978 1.53 lukem rate_put -= rate_put_incr;
979 1.53 lukem break;
980 1.53 lukem default:
981 1.53 lukem err(1, "crankrate invoked with unknown signal: %d", sig);
982 1.53 lukem }
983 1.53 lukem }
984 1.53 lukem
985 1.53 lukem
986 1.3 lukem /*
987 1.3 lukem * Set the SIGALRM interval timer for wait seconds, 0 to disable.
988 1.3 lukem */
989 1.3 lukem void
990 1.3 lukem alarmtimer(wait)
991 1.3 lukem int wait;
992 1.3 lukem {
993 1.3 lukem struct itimerval itv;
994 1.3 lukem
995 1.3 lukem itv.it_value.tv_sec = wait;
996 1.3 lukem itv.it_value.tv_usec = 0;
997 1.3 lukem itv.it_interval = itv.it_value;
998 1.3 lukem setitimer(ITIMER_REAL, &itv, NULL);
999 1.1 lukem }
1000 1.6 lukem
1001 1.6 lukem /*
1002 1.6 lukem * Setup or cleanup EditLine structures
1003 1.6 lukem */
1004 1.50 cgd #ifndef NO_EDITCOMPLETE
1005 1.6 lukem void
1006 1.6 lukem controlediting()
1007 1.6 lukem {
1008 1.6 lukem if (editing && el == NULL && hist == NULL) {
1009 1.16 christos HistEvent ev;
1010 1.30 lukem int editmode;
1011 1.16 christos
1012 1.24 lukem el = el_init(__progname, stdin, ttyout, stderr);
1013 1.23 christos /* init editline */
1014 1.6 lukem hist = history_init(); /* init the builtin history */
1015 1.23 christos history(hist, &ev, H_SETSIZE, 100);/* remember 100 events */
1016 1.6 lukem el_set(el, EL_HIST, history, hist); /* use history */
1017 1.6 lukem
1018 1.6 lukem el_set(el, EL_EDITOR, "emacs"); /* default editor is emacs */
1019 1.6 lukem el_set(el, EL_PROMPT, prompt); /* set the prompt function */
1020 1.6 lukem
1021 1.6 lukem /* add local file completion, bind to TAB */
1022 1.6 lukem el_set(el, EL_ADDFN, "ftp-complete",
1023 1.6 lukem "Context sensitive argument completion",
1024 1.6 lukem complete);
1025 1.6 lukem el_set(el, EL_BIND, "^I", "ftp-complete", NULL);
1026 1.6 lukem el_source(el, NULL); /* read ~/.editrc */
1027 1.30 lukem if ((el_get(el, EL_EDITMODE, &editmode) != -1) && editmode == 0)
1028 1.30 lukem editing = 0; /* the user doesn't want editing,
1029 1.30 lukem * so disable, and let statement
1030 1.30 lukem * below cleanup */
1031 1.30 lukem else
1032 1.30 lukem el_set(el, EL_SIGNAL, 1);
1033 1.30 lukem }
1034 1.30 lukem if (!editing) {
1035 1.6 lukem if (hist) {
1036 1.6 lukem history_end(hist);
1037 1.6 lukem hist = NULL;
1038 1.6 lukem }
1039 1.6 lukem if (el) {
1040 1.6 lukem el_end(el);
1041 1.6 lukem el = NULL;
1042 1.6 lukem }
1043 1.6 lukem }
1044 1.6 lukem }
1045 1.50 cgd #endif /* !NO_EDITCOMPLETE */
1046 1.27 thorpej
1047 1.27 thorpej /*
1048 1.53 lukem * Convert the string `arg' to an int, which may have an optional SI suffix
1049 1.53 lukem * (`b', `k', `m', `g'). Returns the number for success, -1 otherwise.
1050 1.27 thorpej */
1051 1.27 thorpej int
1052 1.53 lukem strsuftoi(arg)
1053 1.27 thorpej const char *arg;
1054 1.27 thorpej {
1055 1.27 thorpej char *cp;
1056 1.53 lukem long val;
1057 1.27 thorpej
1058 1.35 christos if (!isdigit((unsigned char)arg[0]))
1059 1.27 thorpej return (-1);
1060 1.27 thorpej
1061 1.27 thorpej val = strtol(arg, &cp, 10);
1062 1.27 thorpej if (cp != NULL) {
1063 1.53 lukem if (cp[0] != '\0' && cp[1] != '\0')
1064 1.27 thorpej return (-1);
1065 1.53 lukem switch (tolower((unsigned char)cp[0])) {
1066 1.53 lukem case '\0':
1067 1.53 lukem case 'b':
1068 1.53 lukem break;
1069 1.53 lukem case 'k':
1070 1.53 lukem val <<= 10;
1071 1.53 lukem break;
1072 1.53 lukem case 'm':
1073 1.53 lukem val <<= 20;
1074 1.53 lukem break;
1075 1.53 lukem case 'g':
1076 1.53 lukem val <<= 30;
1077 1.53 lukem break;
1078 1.53 lukem default:
1079 1.53 lukem return (-1);
1080 1.53 lukem }
1081 1.27 thorpej }
1082 1.53 lukem if (val < 0 || val > INT_MAX)
1083 1.27 thorpej return (-1);
1084 1.27 thorpej
1085 1.27 thorpej return (val);
1086 1.27 thorpej }
1087 1.27 thorpej
1088 1.27 thorpej /*
1089 1.27 thorpej * Set up socket buffer sizes before a connection is made.
1090 1.27 thorpej */
1091 1.27 thorpej void
1092 1.27 thorpej setupsockbufsize(sock)
1093 1.27 thorpej int sock;
1094 1.27 thorpej {
1095 1.27 thorpej
1096 1.58 lukem if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, (void *) &sndbuf_size,
1097 1.58 lukem sizeof(rcvbuf_size)) < 0)
1098 1.58 lukem warn("unable to set sndbuf size %d", sndbuf_size);
1099 1.58 lukem
1100 1.58 lukem if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, (void *) &rcvbuf_size,
1101 1.58 lukem sizeof(rcvbuf_size)) < 0)
1102 1.58 lukem warn("unable to set rcvbuf size %d", rcvbuf_size);
1103 1.43 lukem }
1104 1.43 lukem
1105 1.78 lukem /*
1106 1.78 lukem * Copy characters from src into dst, \ quoting characters that require it
1107 1.78 lukem */
1108 1.43 lukem void
1109 1.43 lukem ftpvis(dst, dstlen, src, srclen)
1110 1.43 lukem char *dst;
1111 1.43 lukem size_t dstlen;
1112 1.43 lukem const char *src;
1113 1.43 lukem size_t srclen;
1114 1.43 lukem {
1115 1.43 lukem int di, si;
1116 1.43 lukem
1117 1.43 lukem for (di = si = 0;
1118 1.43 lukem src[si] != '\0' && di < dstlen && si < srclen;
1119 1.43 lukem di++, si++) {
1120 1.43 lukem switch (src[si]) {
1121 1.43 lukem case '\\':
1122 1.43 lukem case ' ':
1123 1.43 lukem case '\t':
1124 1.43 lukem case '\r':
1125 1.44 lukem case '\n':
1126 1.43 lukem case '"':
1127 1.43 lukem dst[di++] = '\\';
1128 1.43 lukem if (di >= dstlen)
1129 1.43 lukem break;
1130 1.43 lukem /* FALLTHROUGH */
1131 1.43 lukem default:
1132 1.43 lukem dst[di] = src[si];
1133 1.43 lukem }
1134 1.43 lukem }
1135 1.43 lukem dst[di] = '\0';
1136 1.27 thorpej }
1137 1.55 lukem
1138 1.55 lukem /*
1139 1.55 lukem * Determine if given string is an IPv6 address or not.
1140 1.55 lukem * Return 1 for yes, 0 for no
1141 1.55 lukem */
1142 1.55 lukem int
1143 1.55 lukem isipv6addr(addr)
1144 1.55 lukem const char *addr;
1145 1.55 lukem {
1146 1.55 lukem int rv = 0;
1147 1.55 lukem #ifdef INET6
1148 1.78 lukem struct sockaddr_in6 su_sin6;
1149 1.55 lukem
1150 1.78 lukem rv = inet_pton(AF_INET6, addr, &su_sin6.sin6_addr);
1151 1.55 lukem if (debug)
1152 1.55 lukem fprintf(ttyout, "isipv6addr: got %d for %s\n", rv, addr);
1153 1.55 lukem #endif
1154 1.55 lukem return rv;
1155 1.55 lukem }
1156 1.55 lukem
1157 1.27 thorpej
1158 1.27 thorpej /*
1159 1.27 thorpej * Internal version of connect(2); sets socket buffer sizes first.
1160 1.27 thorpej */
1161 1.27 thorpej int
1162 1.27 thorpej xconnect(sock, name, namelen)
1163 1.27 thorpej int sock;
1164 1.27 thorpej const struct sockaddr *name;
1165 1.27 thorpej int namelen;
1166 1.27 thorpej {
1167 1.27 thorpej
1168 1.27 thorpej setupsockbufsize(sock);
1169 1.27 thorpej return (connect(sock, name, namelen));
1170 1.27 thorpej }
1171 1.27 thorpej
1172 1.27 thorpej /*
1173 1.27 thorpej * Internal version of listen(2); sets socket buffer sizes first.
1174 1.27 thorpej */
1175 1.27 thorpej int
1176 1.27 thorpej xlisten(sock, backlog)
1177 1.27 thorpej int sock, backlog;
1178 1.27 thorpej {
1179 1.27 thorpej
1180 1.27 thorpej setupsockbufsize(sock);
1181 1.27 thorpej return (listen(sock, backlog));
1182 1.31 lukem }
1183 1.31 lukem
1184 1.78 lukem /*
1185 1.78 lukem * malloc() with inbuilt error checking
1186 1.78 lukem */
1187 1.31 lukem void *
1188 1.31 lukem xmalloc(size)
1189 1.31 lukem size_t size;
1190 1.31 lukem {
1191 1.31 lukem void *p;
1192 1.31 lukem
1193 1.31 lukem p = malloc(size);
1194 1.31 lukem if (p == NULL)
1195 1.31 lukem err(1, "Unable to allocate %ld bytes of memory", (long)size);
1196 1.31 lukem return (p);
1197 1.31 lukem }
1198 1.31 lukem
1199 1.78 lukem /*
1200 1.78 lukem * strdup() with inbuilt error checking
1201 1.78 lukem */
1202 1.31 lukem char *
1203 1.31 lukem xstrdup(str)
1204 1.31 lukem const char *str;
1205 1.31 lukem {
1206 1.31 lukem char *s;
1207 1.31 lukem
1208 1.31 lukem if (str == NULL)
1209 1.31 lukem errx(1, "xstrdup() called with NULL argument");
1210 1.31 lukem s = strdup(str);
1211 1.31 lukem if (s == NULL)
1212 1.31 lukem err(1, "Unable to allocate memory for string copy");
1213 1.31 lukem return (s);
1214 1.33 lukem }
1215 1.33 lukem
1216 1.78 lukem /*
1217 1.78 lukem * Install a POSIX signal handler, allowing the invoker to set whether
1218 1.78 lukem * the signal should be restartable or not
1219 1.78 lukem */
1220 1.33 lukem sig_t
1221 1.71 lukem xsignal_restart(sig, func, restartable)
1222 1.33 lukem int sig;
1223 1.33 lukem void (*func) __P((int));
1224 1.71 lukem int restartable;
1225 1.33 lukem {
1226 1.33 lukem struct sigaction act, oact;
1227 1.71 lukem act.sa_handler = func;
1228 1.33 lukem
1229 1.33 lukem sigemptyset(&act.sa_mask);
1230 1.64 lukem #if defined(SA_RESTART) /* 4.4BSD, Posix(?), SVR4 */
1231 1.71 lukem act.sa_flags = restartable ? SA_RESTART : 0;
1232 1.64 lukem #elif defined(SA_INTERRUPT) /* SunOS 4.x */
1233 1.71 lukem act.sa_flags = restartable ? 0 : SA_INTERRUPT;
1234 1.64 lukem #else
1235 1.64 lukem #error "system must have SA_RESTART or SA_INTERRUPT"
1236 1.64 lukem #endif
1237 1.33 lukem if (sigaction(sig, &act, &oact) < 0)
1238 1.33 lukem return (SIG_ERR);
1239 1.33 lukem return (oact.sa_handler);
1240 1.71 lukem }
1241 1.71 lukem
1242 1.78 lukem /*
1243 1.78 lukem * Install a signal handler with the `restartable' flag set dependent upon
1244 1.78 lukem * which signal is being set. (This is a wrapper to xsignal_restart())
1245 1.78 lukem */
1246 1.71 lukem sig_t
1247 1.71 lukem xsignal(sig, func)
1248 1.71 lukem int sig;
1249 1.71 lukem void (*func) __P((int));
1250 1.71 lukem {
1251 1.71 lukem int restartable;
1252 1.71 lukem
1253 1.71 lukem /*
1254 1.71 lukem * Some signals print output or change the state of the process.
1255 1.71 lukem * There should be restartable, so that reads and writes are
1256 1.71 lukem * not affected. Some signals should cause program flow to change;
1257 1.71 lukem * these signals should not be restartable, so that the system call
1258 1.71 lukem * will return with EINTR, and the program will go do something
1259 1.75 lukem * different. If the signal handler calls longjmp() or siglongjmp(),
1260 1.75 lukem * it doesn't matter if it's restartable.
1261 1.71 lukem */
1262 1.71 lukem
1263 1.71 lukem switch(sig) {
1264 1.73 lukem #ifdef SIGINFO
1265 1.71 lukem case SIGINFO:
1266 1.73 lukem #endif
1267 1.73 lukem case SIGQUIT:
1268 1.71 lukem case SIGUSR1:
1269 1.71 lukem case SIGUSR2:
1270 1.71 lukem case SIGWINCH:
1271 1.71 lukem restartable = 1;
1272 1.71 lukem break;
1273 1.71 lukem
1274 1.71 lukem case SIGALRM:
1275 1.71 lukem case SIGINT:
1276 1.71 lukem case SIGPIPE:
1277 1.71 lukem restartable = 0;
1278 1.71 lukem break;
1279 1.71 lukem
1280 1.71 lukem default:
1281 1.71 lukem /*
1282 1.71 lukem * This is unpleasant, but I don't know what would be better.
1283 1.71 lukem * Right now, this "can't happen"
1284 1.71 lukem */
1285 1.71 lukem errx(1, "xsignal_restart called with signal %d\n",
1286 1.71 lukem sig);
1287 1.71 lukem }
1288 1.71 lukem
1289 1.71 lukem return(xsignal_restart(sig, func, restartable));
1290 1.27 thorpej }
1291