util.c revision 1.16.2.3 1 1.16.2.3 cgd /* $NetBSD: util.c,v 1.16.2.3 1998/11/10 18:49:11 cgd Exp $ */
2 1.16.2.3 cgd
3 1.16.2.3 cgd /*-
4 1.16.2.3 cgd * Copyright (c) 1998 The NetBSD Foundation, Inc.
5 1.16.2.3 cgd * All rights reserved.
6 1.16.2.3 cgd *
7 1.16.2.3 cgd * This code is derived from software contributed to The NetBSD Foundation
8 1.16.2.3 cgd * by Jason R. Thorpe of the Numerical Aerospace Simulation Facility,
9 1.16.2.3 cgd * NASA Ames Research Center.
10 1.16.2.3 cgd *
11 1.16.2.3 cgd * Redistribution and use in source and binary forms, with or without
12 1.16.2.3 cgd * modification, are permitted provided that the following conditions
13 1.16.2.3 cgd * are met:
14 1.16.2.3 cgd * 1. Redistributions of source code must retain the above copyright
15 1.16.2.3 cgd * notice, this list of conditions and the following disclaimer.
16 1.16.2.3 cgd * 2. Redistributions in binary form must reproduce the above copyright
17 1.16.2.3 cgd * notice, this list of conditions and the following disclaimer in the
18 1.16.2.3 cgd * documentation and/or other materials provided with the distribution.
19 1.16.2.3 cgd * 3. All advertising materials mentioning features or use of this software
20 1.16.2.3 cgd * must display the following acknowledgement:
21 1.16.2.3 cgd * This product includes software developed by the NetBSD
22 1.16.2.3 cgd * Foundation, Inc. and its contributors.
23 1.16.2.3 cgd * 4. Neither the name of The NetBSD Foundation nor the names of its
24 1.16.2.3 cgd * contributors may be used to endorse or promote products derived
25 1.16.2.3 cgd * from this software without specific prior written permission.
26 1.16.2.3 cgd *
27 1.16.2.3 cgd * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
28 1.16.2.3 cgd * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
29 1.16.2.3 cgd * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
30 1.16.2.3 cgd * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
31 1.16.2.3 cgd * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
32 1.16.2.3 cgd * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
33 1.16.2.3 cgd * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
34 1.16.2.3 cgd * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
35 1.16.2.3 cgd * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
36 1.16.2.3 cgd * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
37 1.16.2.3 cgd * POSSIBILITY OF SUCH DAMAGE.
38 1.16.2.3 cgd */
39 1.1 lukem
40 1.1 lukem /*
41 1.1 lukem * Copyright (c) 1985, 1989, 1993, 1994
42 1.1 lukem * The Regents of the University of California. All rights reserved.
43 1.1 lukem *
44 1.1 lukem * Redistribution and use in source and binary forms, with or without
45 1.1 lukem * modification, are permitted provided that the following conditions
46 1.1 lukem * are met:
47 1.1 lukem * 1. Redistributions of source code must retain the above copyright
48 1.1 lukem * notice, this list of conditions and the following disclaimer.
49 1.1 lukem * 2. Redistributions in binary form must reproduce the above copyright
50 1.1 lukem * notice, this list of conditions and the following disclaimer in the
51 1.1 lukem * documentation and/or other materials provided with the distribution.
52 1.1 lukem * 3. All advertising materials mentioning features or use of this software
53 1.1 lukem * must display the following acknowledgement:
54 1.1 lukem * This product includes software developed by the University of
55 1.1 lukem * California, Berkeley and its contributors.
56 1.1 lukem * 4. Neither the name of the University nor the names of its contributors
57 1.1 lukem * may be used to endorse or promote products derived from this software
58 1.1 lukem * without specific prior written permission.
59 1.1 lukem *
60 1.1 lukem * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
61 1.1 lukem * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
62 1.1 lukem * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
63 1.1 lukem * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
64 1.1 lukem * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
65 1.1 lukem * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
66 1.1 lukem * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
67 1.1 lukem * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
68 1.1 lukem * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
69 1.1 lukem * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
70 1.1 lukem * SUCH DAMAGE.
71 1.1 lukem */
72 1.1 lukem
73 1.10 lukem #include <sys/cdefs.h>
74 1.1 lukem #ifndef lint
75 1.16.2.3 cgd __RCSID("$NetBSD: util.c,v 1.16.2.3 1998/11/10 18:49:11 cgd Exp $");
76 1.1 lukem #endif /* not lint */
77 1.1 lukem
78 1.1 lukem /*
79 1.1 lukem * FTP User Program -- Misc support routines
80 1.1 lukem */
81 1.16.2.3 cgd #include <sys/types.h>
82 1.16.2.3 cgd #include <sys/socket.h>
83 1.1 lukem #include <sys/ioctl.h>
84 1.1 lukem #include <sys/time.h>
85 1.1 lukem #include <arpa/ftp.h>
86 1.1 lukem
87 1.1 lukem #include <ctype.h>
88 1.1 lukem #include <err.h>
89 1.1 lukem #include <fcntl.h>
90 1.1 lukem #include <glob.h>
91 1.16.2.3 cgd #include <termios.h>
92 1.16.2.3 cgd #include <signal.h>
93 1.10 lukem #include <limits.h>
94 1.7 lukem #include <pwd.h>
95 1.1 lukem #include <stdio.h>
96 1.10 lukem #include <stdlib.h>
97 1.1 lukem #include <string.h>
98 1.1 lukem #include <time.h>
99 1.16.2.1 mellon #include <tzfile.h>
100 1.1 lukem #include <unistd.h>
101 1.1 lukem
102 1.1 lukem #include "ftp_var.h"
103 1.1 lukem #include "pathnames.h"
104 1.1 lukem
105 1.1 lukem /*
106 1.1 lukem * Connect to peer server and
107 1.1 lukem * auto-login, if possible.
108 1.1 lukem */
109 1.1 lukem void
110 1.1 lukem setpeer(argc, argv)
111 1.1 lukem int argc;
112 1.1 lukem char *argv[];
113 1.1 lukem {
114 1.1 lukem char *host;
115 1.16.2.3 cgd in_port_t port;
116 1.1 lukem
117 1.1 lukem if (connected) {
118 1.16.2.3 cgd fprintf(ttyout, "Already connected to %s, use close first.\n",
119 1.5 lukem hostname);
120 1.1 lukem code = -1;
121 1.1 lukem return;
122 1.1 lukem }
123 1.1 lukem if (argc < 2)
124 1.5 lukem (void)another(&argc, &argv, "to");
125 1.1 lukem if (argc < 2 || argc > 3) {
126 1.16.2.3 cgd fprintf(ttyout, "usage: %s host-name [port]\n", argv[0]);
127 1.1 lukem code = -1;
128 1.1 lukem return;
129 1.1 lukem }
130 1.12 lukem if (gatemode)
131 1.12 lukem port = gateport;
132 1.12 lukem else
133 1.12 lukem port = ftpport;
134 1.1 lukem if (argc > 2) {
135 1.10 lukem char *ep;
136 1.10 lukem long nport;
137 1.10 lukem
138 1.10 lukem nport = strtol(argv[2], &ep, 10);
139 1.16.2.3 cgd if (nport < 1 || nport > MAX_IN_PORT_T || *ep != '\0') {
140 1.16.2.3 cgd fprintf(ttyout, "%s: bad port number '%s'.\n",
141 1.16.2.3 cgd argv[1], argv[2]);
142 1.16.2.3 cgd fprintf(ttyout, "usage: %s host-name [port]\n",
143 1.16.2.3 cgd argv[0]);
144 1.1 lukem code = -1;
145 1.1 lukem return;
146 1.1 lukem }
147 1.16.2.3 cgd port = htons((in_port_t)nport);
148 1.1 lukem }
149 1.12 lukem
150 1.12 lukem if (gatemode) {
151 1.12 lukem if (gateserver == NULL || *gateserver == '\0')
152 1.12 lukem errx(1, "gateserver not defined (shouldn't happen)");
153 1.12 lukem host = hookup(gateserver, port);
154 1.12 lukem } else
155 1.12 lukem host = hookup(argv[1], port);
156 1.12 lukem
157 1.1 lukem if (host) {
158 1.1 lukem int overbose;
159 1.1 lukem
160 1.12 lukem if (gatemode) {
161 1.12 lukem if (command("PASSERVE %s", argv[1]) != COMPLETE)
162 1.12 lukem return;
163 1.12 lukem if (verbose)
164 1.16.2.3 cgd fprintf(ttyout,
165 1.16.2.3 cgd "Connected via pass-through server %s\n",
166 1.12 lukem gateserver);
167 1.12 lukem }
168 1.12 lukem
169 1.1 lukem connected = 1;
170 1.1 lukem /*
171 1.1 lukem * Set up defaults for FTP.
172 1.1 lukem */
173 1.5 lukem (void)strcpy(typename, "ascii"), type = TYPE_A;
174 1.1 lukem curtype = TYPE_A;
175 1.5 lukem (void)strcpy(formname, "non-print"), form = FORM_N;
176 1.5 lukem (void)strcpy(modename, "stream"), mode = MODE_S;
177 1.5 lukem (void)strcpy(structname, "file"), stru = STRU_F;
178 1.5 lukem (void)strcpy(bytename, "8"), bytesize = 8;
179 1.1 lukem if (autologin)
180 1.16.2.3 cgd (void)ftp_login(argv[1], NULL, NULL);
181 1.1 lukem
182 1.1 lukem overbose = verbose;
183 1.1 lukem if (debug == 0)
184 1.1 lukem verbose = -1;
185 1.1 lukem if (command("SYST") == COMPLETE && overbose) {
186 1.1 lukem char *cp, c;
187 1.1 lukem c = 0;
188 1.16.2.3 cgd cp = strchr(reply_string + 4, ' ');
189 1.1 lukem if (cp == NULL)
190 1.16.2.3 cgd cp = strchr(reply_string + 4, '\r');
191 1.1 lukem if (cp) {
192 1.1 lukem if (cp[-1] == '.')
193 1.1 lukem cp--;
194 1.1 lukem c = *cp;
195 1.1 lukem *cp = '\0';
196 1.1 lukem }
197 1.1 lukem
198 1.16.2.3 cgd fprintf(ttyout, "Remote system type is %s.\n",
199 1.16.2.3 cgd reply_string + 4);
200 1.1 lukem if (cp)
201 1.1 lukem *cp = c;
202 1.1 lukem }
203 1.1 lukem if (!strncmp(reply_string, "215 UNIX Type: L8", 17)) {
204 1.1 lukem if (proxy)
205 1.1 lukem unix_proxy = 1;
206 1.1 lukem else
207 1.1 lukem unix_server = 1;
208 1.1 lukem /*
209 1.1 lukem * Set type to 0 (not specified by user),
210 1.1 lukem * meaning binary by default, but don't bother
211 1.1 lukem * telling server. We can use binary
212 1.1 lukem * for text files unless changed by the user.
213 1.1 lukem */
214 1.1 lukem type = 0;
215 1.5 lukem (void)strcpy(typename, "binary");
216 1.1 lukem if (overbose)
217 1.16.2.3 cgd fprintf(ttyout,
218 1.16.2.3 cgd "Using %s mode to transfer files.\n",
219 1.1 lukem typename);
220 1.1 lukem } else {
221 1.1 lukem if (proxy)
222 1.1 lukem unix_proxy = 0;
223 1.1 lukem else
224 1.1 lukem unix_server = 0;
225 1.1 lukem if (overbose &&
226 1.1 lukem !strncmp(reply_string, "215 TOPS20", 10))
227 1.16.2.3 cgd fputs(
228 1.16.2.3 cgd "Remember to set tenex mode when transferring binary files from this machine.\n",
229 1.16.2.3 cgd ttyout);
230 1.1 lukem }
231 1.1 lukem verbose = overbose;
232 1.1 lukem }
233 1.7 lukem }
234 1.7 lukem
235 1.7 lukem /*
236 1.7 lukem * login to remote host, using given username & password if supplied
237 1.7 lukem */
238 1.7 lukem int
239 1.16.2.3 cgd ftp_login(host, user, pass)
240 1.7 lukem const char *host;
241 1.16.2.3 cgd const char *user, *pass;
242 1.7 lukem {
243 1.7 lukem char tmp[80];
244 1.16.2.3 cgd const char *acct;
245 1.16.2.3 cgd char anonpass[MAXLOGNAME + 2]; /* "user@" */
246 1.14 lukem struct passwd *pw;
247 1.7 lukem int n, aflag = 0;
248 1.7 lukem
249 1.7 lukem acct = NULL;
250 1.7 lukem if (user == NULL) {
251 1.7 lukem if (ruserpass(host, &user, &pass, &acct) < 0) {
252 1.7 lukem code = -1;
253 1.7 lukem return (0);
254 1.7 lukem }
255 1.7 lukem }
256 1.7 lukem
257 1.7 lukem /*
258 1.7 lukem * Set up arguments for an anonymous FTP session, if necessary.
259 1.7 lukem */
260 1.7 lukem if ((user == NULL || pass == NULL) && anonftp) {
261 1.7 lukem memset(anonpass, 0, sizeof(anonpass));
262 1.7 lukem
263 1.7 lukem /*
264 1.7 lukem * Set up anonymous login password.
265 1.7 lukem */
266 1.16.2.3 cgd if ((pass = getenv("FTPANONPASS")) == NULL) {
267 1.16.2.3 cgd if ((pass = getlogin()) == NULL) {
268 1.16.2.3 cgd if ((pw = getpwuid(getuid())) == NULL)
269 1.16.2.3 cgd pass = "anonymous";
270 1.16.2.3 cgd else
271 1.16.2.3 cgd pass = pw->pw_name;
272 1.16.2.3 cgd }
273 1.16.2.3 cgd /*
274 1.16.2.3 cgd * Every anonymous FTP server I've encountered
275 1.16.2.3 cgd * will accept the string "username@", and will
276 1.16.2.3 cgd * append the hostname itself. We do this by default
277 1.16.2.3 cgd * since many servers are picky about not having
278 1.16.2.3 cgd * a FQDN in the anonymous password.
279 1.16.2.3 cgd * - thorpej (at) netbsd.org
280 1.16.2.3 cgd */
281 1.16.2.3 cgd snprintf(anonpass, sizeof(anonpass) - 1, "%s@", pass);
282 1.16.2.3 cgd pass = anonpass;
283 1.14 lukem }
284 1.9 lukem user = "anonymous"; /* as per RFC 1635 */
285 1.7 lukem }
286 1.7 lukem
287 1.7 lukem while (user == NULL) {
288 1.16.2.3 cgd const char *myname = getlogin();
289 1.7 lukem
290 1.14 lukem if (myname == NULL && (pw = getpwuid(getuid())) != NULL)
291 1.14 lukem myname = pw->pw_name;
292 1.7 lukem if (myname)
293 1.16.2.3 cgd fprintf(ttyout, "Name (%s:%s): ", host, myname);
294 1.7 lukem else
295 1.16.2.3 cgd fprintf(ttyout, "Name (%s): ", host);
296 1.16.2.3 cgd *tmp = '\0';
297 1.7 lukem (void)fgets(tmp, sizeof(tmp) - 1, stdin);
298 1.7 lukem tmp[strlen(tmp) - 1] = '\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.7 lukem n = command("USER %s", user);
305 1.7 lukem if (n == CONTINUE) {
306 1.7 lukem if (pass == NULL)
307 1.7 lukem pass = getpass("Password:");
308 1.7 lukem n = command("PASS %s", pass);
309 1.7 lukem }
310 1.7 lukem if (n == CONTINUE) {
311 1.7 lukem aflag++;
312 1.7 lukem if (acct == NULL)
313 1.7 lukem acct = getpass("Account:");
314 1.7 lukem n = command("ACCT %s", acct);
315 1.7 lukem }
316 1.7 lukem if ((n != COMPLETE) ||
317 1.7 lukem (!aflag && acct != NULL && command("ACCT %s", acct) != COMPLETE)) {
318 1.7 lukem warnx("Login failed.");
319 1.7 lukem return (0);
320 1.7 lukem }
321 1.7 lukem if (proxy)
322 1.7 lukem return (1);
323 1.7 lukem connected = -1;
324 1.7 lukem for (n = 0; n < macnum; ++n) {
325 1.7 lukem if (!strcmp("init", macros[n].mac_name)) {
326 1.7 lukem (void)strcpy(line, "$init");
327 1.7 lukem makeargv();
328 1.7 lukem domacro(margc, margv);
329 1.7 lukem break;
330 1.7 lukem }
331 1.7 lukem }
332 1.7 lukem return (1);
333 1.1 lukem }
334 1.1 lukem
335 1.1 lukem /*
336 1.5 lukem * `another' gets another argument, and stores the new argc and argv.
337 1.1 lukem * It reverts to the top level (via main.c's intr()) on EOF/error.
338 1.1 lukem *
339 1.1 lukem * Returns false if no new arguments have been added.
340 1.1 lukem */
341 1.1 lukem int
342 1.1 lukem another(pargc, pargv, prompt)
343 1.1 lukem int *pargc;
344 1.1 lukem char ***pargv;
345 1.1 lukem const char *prompt;
346 1.1 lukem {
347 1.1 lukem int len = strlen(line), ret;
348 1.1 lukem
349 1.1 lukem if (len >= sizeof(line) - 3) {
350 1.16.2.3 cgd fputs("sorry, arguments too long.\n", ttyout);
351 1.1 lukem intr();
352 1.1 lukem }
353 1.16.2.3 cgd fprintf(ttyout, "(%s) ", prompt);
354 1.1 lukem line[len++] = ' ';
355 1.1 lukem if (fgets(&line[len], sizeof(line) - len, stdin) == NULL)
356 1.1 lukem intr();
357 1.1 lukem len += strlen(&line[len]);
358 1.1 lukem if (len > 0 && line[len - 1] == '\n')
359 1.1 lukem line[len - 1] = '\0';
360 1.1 lukem makeargv();
361 1.1 lukem ret = margc > *pargc;
362 1.1 lukem *pargc = margc;
363 1.1 lukem *pargv = margv;
364 1.1 lukem return (ret);
365 1.1 lukem }
366 1.1 lukem
367 1.5 lukem /*
368 1.5 lukem * glob files given in argv[] from the remote server.
369 1.5 lukem * if errbuf isn't NULL, store error messages there instead
370 1.5 lukem * of writing to the screen.
371 1.5 lukem */
372 1.1 lukem char *
373 1.5 lukem remglob(argv, doswitch, errbuf)
374 1.1 lukem char *argv[];
375 1.1 lukem int doswitch;
376 1.5 lukem char **errbuf;
377 1.1 lukem {
378 1.1 lukem char temp[MAXPATHLEN];
379 1.1 lukem static char buf[MAXPATHLEN];
380 1.1 lukem static FILE *ftemp = NULL;
381 1.1 lukem static char **args;
382 1.1 lukem int oldverbose, oldhash, fd;
383 1.1 lukem char *cp, *mode;
384 1.1 lukem
385 1.1 lukem if (!mflag) {
386 1.5 lukem if (!doglob)
387 1.1 lukem args = NULL;
388 1.1 lukem else {
389 1.1 lukem if (ftemp) {
390 1.5 lukem (void)fclose(ftemp);
391 1.1 lukem ftemp = NULL;
392 1.1 lukem }
393 1.1 lukem }
394 1.1 lukem return (NULL);
395 1.1 lukem }
396 1.1 lukem if (!doglob) {
397 1.1 lukem if (args == NULL)
398 1.1 lukem args = argv;
399 1.1 lukem if ((cp = *++args) == NULL)
400 1.1 lukem args = NULL;
401 1.1 lukem return (cp);
402 1.1 lukem }
403 1.1 lukem if (ftemp == NULL) {
404 1.13 lukem (void)snprintf(temp, sizeof(temp), "%s/%s", tmpdir, TMPFILE);
405 1.5 lukem if ((fd = mkstemp(temp)) < 0) {
406 1.1 lukem warn("unable to create temporary file %s", temp);
407 1.1 lukem return (NULL);
408 1.1 lukem }
409 1.1 lukem close(fd);
410 1.5 lukem oldverbose = verbose;
411 1.5 lukem verbose = (errbuf != NULL) ? -1 : 0;
412 1.5 lukem oldhash = hash;
413 1.5 lukem hash = 0;
414 1.5 lukem if (doswitch)
415 1.1 lukem pswitch(!proxy);
416 1.1 lukem for (mode = "w"; *++argv != NULL; mode = "a")
417 1.12 lukem recvrequest("NLST", temp, *argv, mode, 0, 0);
418 1.5 lukem if ((code / 100) != COMPLETE) {
419 1.5 lukem if (errbuf != NULL)
420 1.5 lukem *errbuf = reply_string;
421 1.5 lukem }
422 1.5 lukem if (doswitch)
423 1.1 lukem pswitch(!proxy);
424 1.5 lukem verbose = oldverbose;
425 1.5 lukem hash = oldhash;
426 1.1 lukem ftemp = fopen(temp, "r");
427 1.5 lukem (void)unlink(temp);
428 1.1 lukem if (ftemp == NULL) {
429 1.5 lukem if (errbuf == NULL)
430 1.16.2.3 cgd fputs(
431 1.16.2.3 cgd "can't find list of remote files, oops.\n",
432 1.16.2.3 cgd ttyout);
433 1.5 lukem else
434 1.5 lukem *errbuf =
435 1.5 lukem "can't find list of remote files, oops.";
436 1.1 lukem return (NULL);
437 1.1 lukem }
438 1.1 lukem }
439 1.5 lukem if (fgets(buf, sizeof(buf), ftemp) == NULL) {
440 1.5 lukem (void)fclose(ftemp);
441 1.5 lukem ftemp = NULL;
442 1.1 lukem return (NULL);
443 1.1 lukem }
444 1.1 lukem if ((cp = strchr(buf, '\n')) != NULL)
445 1.1 lukem *cp = '\0';
446 1.1 lukem return (buf);
447 1.1 lukem }
448 1.1 lukem
449 1.1 lukem int
450 1.1 lukem confirm(cmd, file)
451 1.1 lukem const char *cmd, *file;
452 1.1 lukem {
453 1.1 lukem char line[BUFSIZ];
454 1.1 lukem
455 1.1 lukem if (!interactive || confirmrest)
456 1.1 lukem return (1);
457 1.16.2.3 cgd fprintf(ttyout, "%s %s? ", cmd, file);
458 1.16.2.3 cgd (void)fflush(ttyout);
459 1.1 lukem if (fgets(line, sizeof(line), stdin) == NULL)
460 1.1 lukem return (0);
461 1.1 lukem switch (tolower(*line)) {
462 1.1 lukem case 'n':
463 1.1 lukem return (0);
464 1.1 lukem case 'p':
465 1.1 lukem interactive = 0;
466 1.16.2.3 cgd fputs("Interactive mode: off.\n", ttyout);
467 1.1 lukem break;
468 1.1 lukem case 'a':
469 1.1 lukem confirmrest = 1;
470 1.16.2.3 cgd fprintf(ttyout, "Prompting off for duration of %s.\n",
471 1.16.2.3 cgd cmd);
472 1.1 lukem break;
473 1.1 lukem }
474 1.1 lukem return (1);
475 1.1 lukem }
476 1.1 lukem
477 1.1 lukem /*
478 1.1 lukem * Glob a local file name specification with
479 1.1 lukem * the expectation of a single return value.
480 1.1 lukem * Can't control multiple values being expanded
481 1.1 lukem * from the expression, we return only the first.
482 1.1 lukem */
483 1.1 lukem int
484 1.1 lukem globulize(cpp)
485 1.1 lukem char **cpp;
486 1.1 lukem {
487 1.1 lukem glob_t gl;
488 1.1 lukem int flags;
489 1.1 lukem
490 1.1 lukem if (!doglob)
491 1.1 lukem return (1);
492 1.1 lukem
493 1.16.2.3 cgd flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_TILDE;
494 1.1 lukem memset(&gl, 0, sizeof(gl));
495 1.1 lukem if (glob(*cpp, flags, NULL, &gl) ||
496 1.1 lukem gl.gl_pathc == 0) {
497 1.1 lukem warnx("%s: not found", *cpp);
498 1.1 lukem globfree(&gl);
499 1.1 lukem return (0);
500 1.1 lukem }
501 1.12 lukem /* XXX: caller should check if *cpp changed, and
502 1.12 lukem * free(*cpp) if that is the case
503 1.12 lukem */
504 1.16.2.3 cgd *cpp = xstrdup(gl.gl_pathv[0]);
505 1.1 lukem globfree(&gl);
506 1.1 lukem return (1);
507 1.1 lukem }
508 1.1 lukem
509 1.1 lukem /*
510 1.1 lukem * determine size of remote file
511 1.1 lukem */
512 1.1 lukem off_t
513 1.1 lukem remotesize(file, noisy)
514 1.1 lukem const char *file;
515 1.1 lukem int noisy;
516 1.1 lukem {
517 1.1 lukem int overbose;
518 1.1 lukem off_t size;
519 1.1 lukem
520 1.1 lukem overbose = verbose;
521 1.1 lukem size = -1;
522 1.1 lukem if (debug == 0)
523 1.1 lukem verbose = -1;
524 1.11 lukem if (command("SIZE %s", file) == COMPLETE) {
525 1.11 lukem char *cp, *ep;
526 1.11 lukem
527 1.11 lukem cp = strchr(reply_string, ' ');
528 1.11 lukem if (cp != NULL) {
529 1.11 lukem cp++;
530 1.16.2.3 cgd #ifndef NO_QUAD
531 1.11 lukem size = strtoq(cp, &ep, 10);
532 1.16.2.3 cgd #else
533 1.16.2.3 cgd size = strtol(cp, &ep, 10);
534 1.16.2.3 cgd #endif
535 1.16.2.3 cgd if (*ep != '\0' && !isspace((unsigned char)*ep))
536 1.11 lukem size = -1;
537 1.11 lukem }
538 1.16.2.3 cgd } else if (noisy && debug == 0) {
539 1.16.2.3 cgd fputs(reply_string, ttyout);
540 1.16.2.3 cgd putc('\n', ttyout);
541 1.16.2.3 cgd }
542 1.1 lukem verbose = overbose;
543 1.1 lukem return (size);
544 1.1 lukem }
545 1.1 lukem
546 1.1 lukem /*
547 1.1 lukem * determine last modification time (in GMT) of remote file
548 1.1 lukem */
549 1.1 lukem time_t
550 1.1 lukem remotemodtime(file, noisy)
551 1.1 lukem const char *file;
552 1.1 lukem int noisy;
553 1.1 lukem {
554 1.1 lukem int overbose;
555 1.1 lukem time_t rtime;
556 1.14 lukem int ocode;
557 1.1 lukem
558 1.1 lukem overbose = verbose;
559 1.14 lukem ocode = code;
560 1.1 lukem rtime = -1;
561 1.1 lukem if (debug == 0)
562 1.1 lukem verbose = -1;
563 1.1 lukem if (command("MDTM %s", file) == COMPLETE) {
564 1.1 lukem struct tm timebuf;
565 1.1 lukem int yy, mo, day, hour, min, sec;
566 1.1 lukem sscanf(reply_string, "%*s %04d%02d%02d%02d%02d%02d", &yy, &mo,
567 1.1 lukem &day, &hour, &min, &sec);
568 1.1 lukem memset(&timebuf, 0, sizeof(timebuf));
569 1.1 lukem timebuf.tm_sec = sec;
570 1.1 lukem timebuf.tm_min = min;
571 1.1 lukem timebuf.tm_hour = hour;
572 1.1 lukem timebuf.tm_mday = day;
573 1.1 lukem timebuf.tm_mon = mo - 1;
574 1.16.2.3 cgd timebuf.tm_year = yy - TM_YEAR_BASE;
575 1.1 lukem timebuf.tm_isdst = -1;
576 1.1 lukem rtime = mktime(&timebuf);
577 1.1 lukem if (rtime == -1 && (noisy || debug != 0))
578 1.16.2.3 cgd fprintf(ttyout, "Can't convert %s to a time.\n",
579 1.16.2.3 cgd reply_string);
580 1.1 lukem else
581 1.16.2.3 cgd #ifndef __SVR4
582 1.1 lukem rtime += timebuf.tm_gmtoff; /* conv. local -> GMT */
583 1.16.2.3 cgd #else
584 1.16.2.3 cgd rtime -= timezone;
585 1.16.2.3 cgd #endif
586 1.16.2.3 cgd } else if (noisy && debug == 0) {
587 1.16.2.3 cgd fputs(reply_string, ttyout);
588 1.16.2.3 cgd putc('\n', ttyout);
589 1.16.2.3 cgd }
590 1.1 lukem verbose = overbose;
591 1.14 lukem if (rtime == -1)
592 1.14 lukem code = ocode;
593 1.1 lukem return (rtime);
594 1.1 lukem }
595 1.1 lukem
596 1.16.2.2 mellon #ifndef SMALL
597 1.10 lukem
598 1.16.2.3 cgd /*
599 1.16.2.3 cgd * return non-zero if we're the current foreground process
600 1.16.2.3 cgd */
601 1.16.2.3 cgd int
602 1.16.2.3 cgd foregroundproc()
603 1.3 lukem {
604 1.9 lukem static pid_t pgrp = -1;
605 1.9 lukem int ctty_pgrp;
606 1.3 lukem
607 1.9 lukem if (pgrp == -1)
608 1.9 lukem pgrp = getpgrp();
609 1.9 lukem
610 1.16.2.3 cgd return ((ioctl(fileno(ttyout), TIOCGPGRP, &ctty_pgrp) != -1 &&
611 1.16.2.3 cgd ctty_pgrp == (int)pgrp));
612 1.16.2.3 cgd }
613 1.16.2.3 cgd
614 1.16.2.3 cgd
615 1.16.2.3 cgd static void updateprogressmeter __P((int));
616 1.16.2.3 cgd
617 1.16.2.3 cgd static void
618 1.16.2.3 cgd updateprogressmeter(dummy)
619 1.16.2.3 cgd int dummy;
620 1.16.2.3 cgd {
621 1.16.2.3 cgd
622 1.9 lukem /*
623 1.9 lukem * print progress bar only if we are foreground process.
624 1.9 lukem */
625 1.16.2.3 cgd if (foregroundproc())
626 1.9 lukem progressmeter(0);
627 1.3 lukem }
628 1.16.2.2 mellon #endif /* SMALL */
629 1.3 lukem
630 1.1 lukem /*
631 1.1 lukem * Display a transfer progress bar if progress is non-zero.
632 1.3 lukem * SIGALRM is hijacked for use by this function.
633 1.3 lukem * - Before the transfer, set filesize to size of file (or -1 if unknown),
634 1.3 lukem * and call with flag = -1. This starts the once per second timer,
635 1.3 lukem * and a call to updateprogressmeter() upon SIGALRM.
636 1.3 lukem * - During the transfer, updateprogressmeter will call progressmeter
637 1.3 lukem * with flag = 0
638 1.3 lukem * - After the transfer, call with flag = 1
639 1.1 lukem */
640 1.1 lukem static struct timeval start;
641 1.16.2.3 cgd static struct timeval lastupdate;
642 1.1 lukem
643 1.1 lukem void
644 1.1 lukem progressmeter(flag)
645 1.1 lukem int flag;
646 1.1 lukem {
647 1.16.2.2 mellon #ifndef SMALL
648 1.3 lukem /*
649 1.3 lukem * List of order of magnitude prefixes.
650 1.3 lukem * The last is `P', as 2^64 = 16384 Petabytes
651 1.3 lukem */
652 1.16.2.3 cgd static const char prefixes[] = "BKMGTP";
653 1.3 lukem
654 1.3 lukem static off_t lastsize;
655 1.3 lukem struct timeval now, td, wait;
656 1.16.2.3 cgd off_t cursize, abbrevsize, bytespersec;
657 1.16.2.1 mellon double elapsed;
658 1.16.2.1 mellon int ratio, barlength, i, len, remaining;
659 1.3 lukem char buf[256];
660 1.1 lukem
661 1.15 mycroft len = 0;
662 1.15 mycroft
663 1.3 lukem if (flag == -1) {
664 1.16.2.3 cgd (void)gettimeofday(&start, NULL);
665 1.3 lukem lastupdate = start;
666 1.3 lukem lastsize = restart_point;
667 1.3 lukem }
668 1.16.2.3 cgd (void)gettimeofday(&now, NULL);
669 1.2 thorpej if (!progress || filesize <= 0)
670 1.1 lukem return;
671 1.1 lukem cursize = bytes + restart_point;
672 1.1 lukem
673 1.16.2.3 cgd ratio = (int)((double)cursize * 100.0 / (double)filesize);
674 1.1 lukem ratio = MAX(ratio, 0);
675 1.1 lukem ratio = MIN(ratio, 100);
676 1.15 mycroft len += snprintf(buf + len, sizeof(buf) - len, "\r%3d%% ", ratio);
677 1.1 lukem
678 1.16.2.3 cgd barlength = ttywidth - 42;
679 1.1 lukem if (barlength > 0) {
680 1.1 lukem i = barlength * ratio / 100;
681 1.15 mycroft len += snprintf(buf + len, sizeof(buf) - len,
682 1.3 lukem "|%.*s%*s|", i,
683 1.1 lukem "*****************************************************************************"
684 1.1 lukem "*****************************************************************************",
685 1.1 lukem barlength - i, "");
686 1.1 lukem }
687 1.1 lukem
688 1.1 lukem abbrevsize = cursize;
689 1.16.2.3 cgd for (i = 0; abbrevsize >= 100000 && i < sizeof(prefixes); i++)
690 1.1 lukem abbrevsize >>= 10;
691 1.15 mycroft len += snprintf(buf + len, sizeof(buf) - len,
692 1.16.2.3 cgd #ifndef NO_QUAD
693 1.16.2.3 cgd " %5qd %c%c ", (long long)abbrevsize,
694 1.16.2.3 cgd #else
695 1.16.2.3 cgd " %5ld %c%c ", (long)abbrevsize,
696 1.16.2.3 cgd #endif
697 1.16.2.3 cgd i == 0 ? ' ' : prefixes[i],
698 1.16.2.3 cgd i == 0 ? ' ' : 'B');
699 1.1 lukem
700 1.3 lukem timersub(&now, &lastupdate, &wait);
701 1.3 lukem if (cursize > lastsize) {
702 1.3 lukem lastupdate = now;
703 1.3 lukem lastsize = cursize;
704 1.3 lukem if (wait.tv_sec >= STALLTIME) { /* fudge out stalled time */
705 1.3 lukem start.tv_sec += wait.tv_sec;
706 1.3 lukem start.tv_usec += wait.tv_usec;
707 1.3 lukem }
708 1.3 lukem wait.tv_sec = 0;
709 1.3 lukem }
710 1.3 lukem
711 1.1 lukem timersub(&now, &start, &td);
712 1.1 lukem elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
713 1.3 lukem
714 1.16.2.3 cgd bytespersec = 0;
715 1.16.2.3 cgd if (bytes > 0) {
716 1.16.2.3 cgd bytespersec = bytes;
717 1.16.2.3 cgd if (elapsed > 0.0)
718 1.16.2.3 cgd bytespersec /= elapsed;
719 1.16.2.3 cgd }
720 1.16.2.3 cgd for (i = 0; bytespersec >= 100000 && i < sizeof(prefixes); i++)
721 1.16.2.3 cgd bytespersec >>= 10;
722 1.16.2.3 cgd len += snprintf(buf + len, sizeof(buf) - len,
723 1.16.2.3 cgd #ifndef NO_QUAD
724 1.16.2.3 cgd " %5qd %c%s ", (long long)bytespersec,
725 1.16.2.3 cgd #else
726 1.16.2.3 cgd " %5ld %c%s ", (long)bytespersec,
727 1.16.2.3 cgd #endif
728 1.16.2.3 cgd prefixes[i],
729 1.16.2.3 cgd i == 0 ? "/s " : "B/s");
730 1.16.2.3 cgd
731 1.8 lukem if (bytes <= 0 || elapsed <= 0.0 || cursize > filesize) {
732 1.15 mycroft len += snprintf(buf + len, sizeof(buf) - len,
733 1.3 lukem " --:-- ETA");
734 1.3 lukem } else if (wait.tv_sec >= STALLTIME) {
735 1.15 mycroft len += snprintf(buf + len, sizeof(buf) - len,
736 1.3 lukem " - stalled -");
737 1.1 lukem } else {
738 1.16.2.1 mellon remaining = (int)
739 1.16.2.1 mellon ((filesize - restart_point) / (bytes / elapsed) - elapsed);
740 1.16.2.1 mellon if (remaining >= 100 * SECSPERHOUR)
741 1.15 mycroft len += snprintf(buf + len, sizeof(buf) - len,
742 1.15 mycroft " --:-- ETA");
743 1.15 mycroft else {
744 1.16.2.1 mellon i = remaining / SECSPERHOUR;
745 1.15 mycroft if (i)
746 1.15 mycroft len += snprintf(buf + len, sizeof(buf) - len,
747 1.15 mycroft "%2d:", i);
748 1.15 mycroft else
749 1.15 mycroft len += snprintf(buf + len, sizeof(buf) - len,
750 1.15 mycroft " ");
751 1.16.2.1 mellon i = remaining % SECSPERHOUR;
752 1.15 mycroft len += snprintf(buf + len, sizeof(buf) - len,
753 1.15 mycroft "%02d:%02d ETA", i / 60, i % 60);
754 1.15 mycroft }
755 1.1 lukem }
756 1.16.2.3 cgd (void)write(fileno(ttyout), buf, len);
757 1.1 lukem
758 1.3 lukem if (flag == -1) {
759 1.16.2.3 cgd (void)xsignal(SIGALRM, updateprogressmeter);
760 1.3 lukem alarmtimer(1); /* set alarm timer for 1 Hz */
761 1.3 lukem } else if (flag == 1) {
762 1.16.2.3 cgd (void)xsignal(SIGALRM, SIG_DFL);
763 1.3 lukem alarmtimer(0);
764 1.16.2.3 cgd (void)putc('\n', ttyout);
765 1.3 lukem }
766 1.16.2.3 cgd fflush(ttyout);
767 1.16.2.2 mellon #endif /* SMALL */
768 1.1 lukem }
769 1.1 lukem
770 1.1 lukem /*
771 1.1 lukem * Display transfer statistics.
772 1.1 lukem * Requires start to be initialised by progressmeter(-1),
773 1.1 lukem * direction to be defined by xfer routines, and filesize and bytes
774 1.1 lukem * to be updated by xfer routines
775 1.16.2.3 cgd * If siginfo is nonzero, an ETA is displayed, and the output goes to stderr
776 1.16.2.3 cgd * instead of ttyout.
777 1.1 lukem */
778 1.1 lukem void
779 1.1 lukem ptransfer(siginfo)
780 1.1 lukem int siginfo;
781 1.1 lukem {
782 1.16.2.2 mellon #ifndef SMALL
783 1.16.2.3 cgd struct timeval now, td, wait;
784 1.1 lukem double elapsed;
785 1.16.2.3 cgd off_t bytespersec;
786 1.16.2.1 mellon int meg, remaining, hh, len;
787 1.1 lukem char buf[100];
788 1.1 lukem
789 1.1 lukem if (!verbose && !siginfo)
790 1.1 lukem return;
791 1.1 lukem
792 1.16.2.3 cgd (void)gettimeofday(&now, NULL);
793 1.1 lukem timersub(&now, &start, &td);
794 1.1 lukem elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
795 1.16.2.3 cgd bytespersec = 0;
796 1.1 lukem meg = 0;
797 1.16.2.3 cgd if (bytes > 0) {
798 1.16.2.3 cgd bytespersec = bytes;
799 1.16.2.3 cgd if (elapsed > 0.0)
800 1.16.2.3 cgd bytespersec /= elapsed;
801 1.16.2.3 cgd if (bytespersec > (1024 * 1024))
802 1.16.2.3 cgd meg = 1;
803 1.16.2.3 cgd }
804 1.16.2.1 mellon len = 0;
805 1.16.2.1 mellon len += snprintf(buf + len, sizeof(buf) - len,
806 1.16.2.3 cgd #ifndef NO_QUAD
807 1.16.2.3 cgd "%qd byte%s %s in ", (long long)bytes,
808 1.16.2.3 cgd #else
809 1.16.2.3 cgd "%ld byte%s %s in ", (long)bytes,
810 1.16.2.3 cgd #endif
811 1.16.2.3 cgd bytes == 1 ? "" : "s", direction);
812 1.16.2.3 cgd remaining = (int)elapsed;
813 1.16.2.3 cgd if (remaining > SECSPERDAY) {
814 1.16.2.3 cgd int days;
815 1.16.2.3 cgd
816 1.16.2.3 cgd days = remaining / SECSPERDAY;
817 1.16.2.3 cgd remaining %= SECSPERDAY;
818 1.16.2.3 cgd len += snprintf(buf + len, sizeof(buf) - len,
819 1.16.2.3 cgd "%d day%s ", days, days == 1 ? "" : "s");
820 1.16.2.3 cgd }
821 1.16.2.3 cgd hh = remaining / SECSPERHOUR;
822 1.16.2.3 cgd remaining %= SECSPERHOUR;
823 1.16.2.3 cgd if (hh)
824 1.16.2.3 cgd len += snprintf(buf + len, sizeof(buf) - len, "%2d:", hh);
825 1.16.2.3 cgd len += snprintf(buf + len, sizeof(buf) - len,
826 1.16.2.3 cgd "%02d:%02d (%.2f %sB/s)", remaining / 60, remaining % 60,
827 1.16.2.3 cgd bytespersec / (1024.0 * (meg ? 1024.0 : 1.0)),
828 1.16.2.3 cgd meg ? "M" : "K");
829 1.16.2.3 cgd
830 1.8 lukem if (siginfo && bytes > 0 && elapsed > 0.0 && filesize >= 0
831 1.8 lukem && bytes + restart_point <= filesize) {
832 1.1 lukem remaining = (int)((filesize - restart_point) /
833 1.1 lukem (bytes / elapsed) - elapsed);
834 1.16.2.1 mellon hh = remaining / SECSPERHOUR;
835 1.16.2.1 mellon remaining %= SECSPERHOUR;
836 1.16.2.3 cgd len += snprintf(buf + len, sizeof(buf) - len, " ETA: ");
837 1.16.2.3 cgd if (hh)
838 1.16.2.3 cgd len += snprintf(buf + len, sizeof(buf) - len, "%2d:",
839 1.16.2.3 cgd hh);
840 1.16.2.1 mellon len += snprintf(buf + len, sizeof(buf) - len,
841 1.16.2.3 cgd "%02d:%02d", remaining / 60, remaining % 60);
842 1.16.2.3 cgd timersub(&now, &lastupdate, &wait);
843 1.16.2.3 cgd if (wait.tv_sec >= STALLTIME)
844 1.16.2.3 cgd len += snprintf(buf + len, sizeof(buf) - len,
845 1.16.2.3 cgd " (stalled)");
846 1.1 lukem }
847 1.16.2.3 cgd len += snprintf(buf + len, sizeof(buf) - len, "\n");
848 1.16.2.3 cgd (void)write(siginfo ? STDERR_FILENO : fileno(ttyout), buf, len);
849 1.16.2.2 mellon #endif /* SMALL */
850 1.1 lukem }
851 1.1 lukem
852 1.1 lukem /*
853 1.1 lukem * List words in stringlist, vertically arranged
854 1.1 lukem */
855 1.1 lukem void
856 1.1 lukem list_vertical(sl)
857 1.1 lukem StringList *sl;
858 1.1 lukem {
859 1.1 lukem int i, j, w;
860 1.1 lukem int columns, width, lines, items;
861 1.1 lukem char *p;
862 1.1 lukem
863 1.1 lukem width = items = 0;
864 1.1 lukem
865 1.1 lukem for (i = 0 ; i < sl->sl_cur ; i++) {
866 1.1 lukem w = strlen(sl->sl_str[i]);
867 1.1 lukem if (w > width)
868 1.1 lukem width = w;
869 1.1 lukem }
870 1.1 lukem width = (width + 8) &~ 7;
871 1.1 lukem
872 1.1 lukem columns = ttywidth / width;
873 1.1 lukem if (columns == 0)
874 1.1 lukem columns = 1;
875 1.1 lukem lines = (sl->sl_cur + columns - 1) / columns;
876 1.1 lukem for (i = 0; i < lines; i++) {
877 1.1 lukem for (j = 0; j < columns; j++) {
878 1.1 lukem p = sl->sl_str[j * lines + i];
879 1.1 lukem if (p)
880 1.16.2.3 cgd fputs(p, ttyout);
881 1.1 lukem if (j * lines + i + lines >= sl->sl_cur) {
882 1.16.2.3 cgd putc('\n', ttyout);
883 1.1 lukem break;
884 1.1 lukem }
885 1.1 lukem w = strlen(p);
886 1.1 lukem while (w < width) {
887 1.1 lukem w = (w + 8) &~ 7;
888 1.16.2.3 cgd (void)putc('\t', ttyout);
889 1.1 lukem }
890 1.1 lukem }
891 1.1 lukem }
892 1.3 lukem }
893 1.3 lukem
894 1.3 lukem /*
895 1.3 lukem * Update the global ttywidth value, using TIOCGWINSZ.
896 1.3 lukem */
897 1.3 lukem void
898 1.3 lukem setttywidth(a)
899 1.3 lukem int a;
900 1.3 lukem {
901 1.3 lukem struct winsize winsize;
902 1.3 lukem
903 1.16.2.3 cgd if (ioctl(fileno(ttyout), TIOCGWINSZ, &winsize) != -1 &&
904 1.16.2.3 cgd winsize.ws_col != 0)
905 1.3 lukem ttywidth = winsize.ws_col;
906 1.3 lukem else
907 1.3 lukem ttywidth = 80;
908 1.3 lukem }
909 1.3 lukem
910 1.3 lukem /*
911 1.3 lukem * Set the SIGALRM interval timer for wait seconds, 0 to disable.
912 1.3 lukem */
913 1.3 lukem void
914 1.3 lukem alarmtimer(wait)
915 1.3 lukem int wait;
916 1.3 lukem {
917 1.3 lukem struct itimerval itv;
918 1.3 lukem
919 1.3 lukem itv.it_value.tv_sec = wait;
920 1.3 lukem itv.it_value.tv_usec = 0;
921 1.3 lukem itv.it_interval = itv.it_value;
922 1.3 lukem setitimer(ITIMER_REAL, &itv, NULL);
923 1.1 lukem }
924 1.6 lukem
925 1.6 lukem /*
926 1.6 lukem * Setup or cleanup EditLine structures
927 1.6 lukem */
928 1.6 lukem #ifndef SMALL
929 1.6 lukem void
930 1.6 lukem controlediting()
931 1.6 lukem {
932 1.6 lukem if (editing && el == NULL && hist == NULL) {
933 1.16 christos HistEvent ev;
934 1.16.2.3 cgd int editmode;
935 1.16 christos
936 1.16.2.3 cgd #ifdef EL_EDITMODE /* hack */
937 1.16.2.3 cgd el = el_init(__progname, stdin, ttyout, stderr);
938 1.16.2.3 cgd #else
939 1.16.2.3 cgd el = el_init(__progname, stdin, ttyout);
940 1.16.2.3 cgd #endif /* EL_EDITMODE */
941 1.16.2.3 cgd /* init editline */
942 1.6 lukem hist = history_init(); /* init the builtin history */
943 1.16.2.3 cgd #ifdef H_SETSIZE
944 1.16.2.3 cgd history(hist, &ev, H_SETSIZE, 100);/* remember 100 events */
945 1.16.2.3 cgd #endif
946 1.6 lukem el_set(el, EL_HIST, history, hist); /* use history */
947 1.6 lukem
948 1.6 lukem el_set(el, EL_EDITOR, "emacs"); /* default editor is emacs */
949 1.6 lukem el_set(el, EL_PROMPT, prompt); /* set the prompt function */
950 1.6 lukem
951 1.6 lukem /* add local file completion, bind to TAB */
952 1.6 lukem el_set(el, EL_ADDFN, "ftp-complete",
953 1.6 lukem "Context sensitive argument completion",
954 1.6 lukem complete);
955 1.6 lukem el_set(el, EL_BIND, "^I", "ftp-complete", NULL);
956 1.6 lukem el_source(el, NULL); /* read ~/.editrc */
957 1.16.2.3 cgd #ifdef EL_EDITMODE
958 1.16.2.3 cgd if ((el_get(el, EL_EDITMODE, &editmode) != -1) && editmode == 0)
959 1.16.2.3 cgd editing = 0; /* the user doesn't want editing,
960 1.16.2.3 cgd * so disable, and let statement
961 1.16.2.3 cgd * below cleanup */
962 1.16.2.3 cgd else
963 1.16.2.3 cgd el_set(el, EL_SIGNAL, 1);
964 1.16.2.3 cgd #else
965 1.16.2.3 cgd if (editmode == 0) {
966 1.16.2.3 cgd editing = 0;
967 1.16.2.3 cgd } else {
968 1.16.2.3 cgd el_set(el, EL_SIGNAL, 1);
969 1.16.2.3 cgd }
970 1.16.2.3 cgd #endif
971 1.16.2.3 cgd }
972 1.16.2.3 cgd if (!editing) {
973 1.6 lukem if (hist) {
974 1.6 lukem history_end(hist);
975 1.6 lukem hist = NULL;
976 1.6 lukem }
977 1.6 lukem if (el) {
978 1.6 lukem el_end(el);
979 1.6 lukem el = NULL;
980 1.6 lukem }
981 1.6 lukem }
982 1.6 lukem }
983 1.6 lukem #endif /* !SMALL */
984 1.16.2.3 cgd
985 1.16.2.3 cgd /*
986 1.16.2.3 cgd * Parse the specified socket buffer size.
987 1.16.2.3 cgd */
988 1.16.2.3 cgd int
989 1.16.2.3 cgd getsockbufsize(arg)
990 1.16.2.3 cgd const char *arg;
991 1.16.2.3 cgd {
992 1.16.2.3 cgd char *cp;
993 1.16.2.3 cgd int val;
994 1.16.2.3 cgd
995 1.16.2.3 cgd if (!isdigit((unsigned char)arg[0]))
996 1.16.2.3 cgd return (-1);
997 1.16.2.3 cgd
998 1.16.2.3 cgd val = strtol(arg, &cp, 10);
999 1.16.2.3 cgd if (cp != NULL) {
1000 1.16.2.3 cgd if (cp[1] != '\0')
1001 1.16.2.3 cgd return (-1);
1002 1.16.2.3 cgd if (cp[0] == 'k')
1003 1.16.2.3 cgd val *= 1024;
1004 1.16.2.3 cgd if (cp[0] == 'm')
1005 1.16.2.3 cgd val *= 1024 * 1024;
1006 1.16.2.3 cgd }
1007 1.16.2.3 cgd
1008 1.16.2.3 cgd if (val < 0)
1009 1.16.2.3 cgd return (-1);
1010 1.16.2.3 cgd
1011 1.16.2.3 cgd return (val);
1012 1.16.2.3 cgd }
1013 1.16.2.3 cgd
1014 1.16.2.3 cgd /*
1015 1.16.2.3 cgd * Set up socket buffer sizes before a connection is made.
1016 1.16.2.3 cgd */
1017 1.16.2.3 cgd void
1018 1.16.2.3 cgd setupsockbufsize(sock)
1019 1.16.2.3 cgd int sock;
1020 1.16.2.3 cgd {
1021 1.16.2.3 cgd static int sndbuf_default, rcvbuf_default;
1022 1.16.2.3 cgd int len, size;
1023 1.16.2.3 cgd
1024 1.16.2.3 cgd /*
1025 1.16.2.3 cgd * Get the default socket buffer sizes if we don't already
1026 1.16.2.3 cgd * have them. It doesn't matter which socket we do this
1027 1.16.2.3 cgd * to, because on the first call no socket buffer sizes
1028 1.16.2.3 cgd * will have been modified, so we are guaranteed to get
1029 1.16.2.3 cgd * the system defaults.
1030 1.16.2.3 cgd */
1031 1.16.2.3 cgd if (sndbuf_default == 0) {
1032 1.16.2.3 cgd len = sizeof(sndbuf_default);
1033 1.16.2.3 cgd if (getsockopt(sock, SOL_SOCKET, SO_SNDBUF, &sndbuf_default,
1034 1.16.2.3 cgd &len) < 0)
1035 1.16.2.3 cgd err(1, "unable to get default sndbuf size");
1036 1.16.2.3 cgd len = sizeof(rcvbuf_default);
1037 1.16.2.3 cgd if (getsockopt(sock, SOL_SOCKET, SO_RCVBUF, &rcvbuf_default,
1038 1.16.2.3 cgd &len) < 0)
1039 1.16.2.3 cgd err(1, "unable to get default rcvbuf size");
1040 1.16.2.3 cgd
1041 1.16.2.3 cgd }
1042 1.16.2.3 cgd
1043 1.16.2.3 cgd size = sndbuf_size ? sndbuf_size : sndbuf_default;
1044 1.16.2.3 cgd if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &size, sizeof(size)) < 0)
1045 1.16.2.3 cgd warn("unable to set sndbuf size %d", size);
1046 1.16.2.3 cgd
1047 1.16.2.3 cgd size = rcvbuf_size ? rcvbuf_size : rcvbuf_default;
1048 1.16.2.3 cgd if (setsockopt(sock, SOL_SOCKET, SO_RCVBUF, &size, sizeof(size)) < 0)
1049 1.16.2.3 cgd warn("unable to set rcvbuf size %d", size);
1050 1.16.2.3 cgd }
1051 1.16.2.3 cgd
1052 1.16.2.3 cgd /*
1053 1.16.2.3 cgd * If the socket buffer sizes were not set manually (i.e. came from a
1054 1.16.2.3 cgd * configuration file), reset them so the right thing will happen on
1055 1.16.2.3 cgd * subsequent connections.
1056 1.16.2.3 cgd */
1057 1.16.2.3 cgd void
1058 1.16.2.3 cgd resetsockbufsize()
1059 1.16.2.3 cgd {
1060 1.16.2.3 cgd
1061 1.16.2.3 cgd if (sndbuf_manual == 0)
1062 1.16.2.3 cgd sndbuf_size = 0;
1063 1.16.2.3 cgd if (rcvbuf_manual == 0)
1064 1.16.2.3 cgd rcvbuf_size = 0;
1065 1.16.2.3 cgd }
1066 1.16.2.3 cgd
1067 1.16.2.3 cgd /*
1068 1.16.2.3 cgd * Internal version of connect(2); sets socket buffer sizes first.
1069 1.16.2.3 cgd */
1070 1.16.2.3 cgd int
1071 1.16.2.3 cgd xconnect(sock, name, namelen)
1072 1.16.2.3 cgd int sock;
1073 1.16.2.3 cgd const struct sockaddr *name;
1074 1.16.2.3 cgd int namelen;
1075 1.16.2.3 cgd {
1076 1.16.2.3 cgd
1077 1.16.2.3 cgd setupsockbufsize(sock);
1078 1.16.2.3 cgd return (connect(sock, name, namelen));
1079 1.16.2.3 cgd }
1080 1.16.2.3 cgd
1081 1.16.2.3 cgd /*
1082 1.16.2.3 cgd * Internal version of listen(2); sets socket buffer sizes first.
1083 1.16.2.3 cgd */
1084 1.16.2.3 cgd int
1085 1.16.2.3 cgd xlisten(sock, backlog)
1086 1.16.2.3 cgd int sock, backlog;
1087 1.16.2.3 cgd {
1088 1.16.2.3 cgd
1089 1.16.2.3 cgd setupsockbufsize(sock);
1090 1.16.2.3 cgd return (listen(sock, backlog));
1091 1.16.2.3 cgd }
1092 1.16.2.3 cgd
1093 1.16.2.3 cgd void *
1094 1.16.2.3 cgd xmalloc(size)
1095 1.16.2.3 cgd size_t size;
1096 1.16.2.3 cgd {
1097 1.16.2.3 cgd void *p;
1098 1.16.2.3 cgd
1099 1.16.2.3 cgd p = malloc(size);
1100 1.16.2.3 cgd if (p == NULL)
1101 1.16.2.3 cgd err(1, "Unable to allocate %ld bytes of memory", (long)size);
1102 1.16.2.3 cgd return (p);
1103 1.16.2.3 cgd }
1104 1.16.2.3 cgd
1105 1.16.2.3 cgd char *
1106 1.16.2.3 cgd xstrdup(str)
1107 1.16.2.3 cgd const char *str;
1108 1.16.2.3 cgd {
1109 1.16.2.3 cgd char *s;
1110 1.16.2.3 cgd
1111 1.16.2.3 cgd if (str == NULL)
1112 1.16.2.3 cgd errx(1, "xstrdup() called with NULL argument");
1113 1.16.2.3 cgd s = strdup(str);
1114 1.16.2.3 cgd if (s == NULL)
1115 1.16.2.3 cgd err(1, "Unable to allocate memory for string copy");
1116 1.16.2.3 cgd return (s);
1117 1.16.2.3 cgd }
1118 1.16.2.3 cgd
1119 1.16.2.3 cgd sig_t
1120 1.16.2.3 cgd xsignal(sig, func)
1121 1.16.2.3 cgd int sig;
1122 1.16.2.3 cgd void (*func) __P((int));
1123 1.16.2.3 cgd {
1124 1.16.2.3 cgd struct sigaction act, oact;
1125 1.16.2.3 cgd
1126 1.16.2.3 cgd act.sa_handler = func;
1127 1.16.2.3 cgd sigemptyset(&act.sa_mask);
1128 1.16.2.3 cgd act.sa_flags = SA_RESTART;
1129 1.16.2.3 cgd if (sigaction(sig, &act, &oact) < 0)
1130 1.16.2.3 cgd return (SIG_ERR);
1131 1.16.2.3 cgd return (oact.sa_handler);
1132 1.16.2.3 cgd }
1133