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