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