util.c revision 1.8 1 /* $NetBSD: util.c,v 1.8 1997/05/12 11:41:13 lukem Exp $ */
2
3 /*
4 * Copyright (c) 1985, 1989, 1993, 1994
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #ifndef lint
37 static char rcsid[] = "$NetBSD: util.c,v 1.8 1997/05/12 11:41:13 lukem Exp $";
38 #endif /* not lint */
39
40 /*
41 * FTP User Program -- Misc support routines
42 */
43 #include <sys/ioctl.h>
44 #include <sys/time.h>
45 #include <arpa/ftp.h>
46
47 #include <ctype.h>
48 #include <err.h>
49 #include <fcntl.h>
50 #include <glob.h>
51 #include <pwd.h>
52 #include <stdio.h>
53 #include <string.h>
54 #include <time.h>
55 #include <unistd.h>
56
57 #include "ftp_var.h"
58 #include "pathnames.h"
59
60 /*
61 * Connect to peer server and
62 * auto-login, if possible.
63 */
64 void
65 setpeer(argc, argv)
66 int argc;
67 char *argv[];
68 {
69 char *host;
70 short port;
71
72 if (connected) {
73 printf("Already connected to %s, use close first.\n",
74 hostname);
75 code = -1;
76 return;
77 }
78 if (argc < 2)
79 (void)another(&argc, &argv, "to");
80 if (argc < 2 || argc > 3) {
81 printf("usage: %s host-name [port]\n", argv[0]);
82 code = -1;
83 return;
84 }
85 port = ftpport;
86 if (argc > 2) {
87 port = atoi(argv[2]);
88 if (port <= 0) {
89 printf("%s: bad port number '%s'.\n", argv[1], argv[2]);
90 printf("usage: %s host-name [port]\n", argv[0]);
91 code = -1;
92 return;
93 }
94 port = htons(port);
95 }
96 host = hookup(argv[1], port);
97 if (host) {
98 int overbose;
99
100 connected = 1;
101 /*
102 * Set up defaults for FTP.
103 */
104 (void)strcpy(typename, "ascii"), type = TYPE_A;
105 curtype = TYPE_A;
106 (void)strcpy(formname, "non-print"), form = FORM_N;
107 (void)strcpy(modename, "stream"), mode = MODE_S;
108 (void)strcpy(structname, "file"), stru = STRU_F;
109 (void)strcpy(bytename, "8"), bytesize = 8;
110 if (autologin)
111 (void)login(argv[1], NULL, NULL);
112
113 overbose = verbose;
114 if (debug == 0)
115 verbose = -1;
116 if (command("SYST") == COMPLETE && overbose) {
117 char *cp, c;
118 c = 0;
119 cp = strchr(reply_string+4, ' ');
120 if (cp == NULL)
121 cp = strchr(reply_string+4, '\r');
122 if (cp) {
123 if (cp[-1] == '.')
124 cp--;
125 c = *cp;
126 *cp = '\0';
127 }
128
129 printf("Remote system type is %s.\n", reply_string + 4);
130 if (cp)
131 *cp = c;
132 }
133 if (!strncmp(reply_string, "215 UNIX Type: L8", 17)) {
134 if (proxy)
135 unix_proxy = 1;
136 else
137 unix_server = 1;
138 /*
139 * Set type to 0 (not specified by user),
140 * meaning binary by default, but don't bother
141 * telling server. We can use binary
142 * for text files unless changed by the user.
143 */
144 type = 0;
145 (void)strcpy(typename, "binary");
146 if (overbose)
147 printf("Using %s mode to transfer files.\n",
148 typename);
149 } else {
150 if (proxy)
151 unix_proxy = 0;
152 else
153 unix_server = 0;
154 if (overbose &&
155 !strncmp(reply_string, "215 TOPS20", 10))
156 puts(
157 "Remember to set tenex mode when transferring binary files from this machine.");
158 }
159 verbose = overbose;
160 }
161 }
162
163
164 /*
165 * login to remote host, using given username & password if supplied
166 */
167 int
168 login(host, user, pass)
169 const char *host;
170 char *user, *pass;
171 {
172 char tmp[80];
173 char *acct;
174 char anonpass[MAXLOGNAME + 1 + MAXHOSTNAMELEN]; /* "user@hostname" */
175 char hostname[MAXHOSTNAMELEN];
176 int n, aflag = 0;
177
178 acct = NULL;
179 if (user == NULL) {
180 if (ruserpass(host, &user, &pass, &acct) < 0) {
181 code = -1;
182 return (0);
183 }
184 }
185
186 /*
187 * Set up arguments for an anonymous FTP session, if necessary.
188 */
189 if ((user == NULL || pass == NULL) && anonftp) {
190 memset(anonpass, 0, sizeof(anonpass));
191 memset(hostname, 0, sizeof(hostname));
192
193 /*
194 * Set up anonymous login password.
195 */
196 user = getlogin();
197 gethostname(hostname, MAXHOSTNAMELEN);
198 #ifndef DONT_CHEAT_ANONPASS
199 /*
200 * Every anonymous FTP server I've encountered
201 * will accept the string "username@", and will
202 * append the hostname itself. We do this by default
203 * since many servers are picky about not having
204 * a FQDN in the anonymous password. - thorpej (at) netbsd.org
205 */
206 snprintf(anonpass, sizeof(anonpass) - 1, "%s@",
207 user);
208 #else
209 snprintf(anonpass, sizeof(anonpass) - 1, "%s@%s",
210 user, hp->h_name);
211 #endif
212 pass = anonpass;
213 user = "anonymous";
214 }
215
216 while (user == NULL) {
217 char *myname = getlogin();
218
219 if (myname == NULL) {
220 struct passwd *pp = getpwuid(getuid());
221
222 if (pp != NULL)
223 myname = pp->pw_name;
224 }
225 if (myname)
226 printf("Name (%s:%s): ", host, myname);
227 else
228 printf("Name (%s): ", host);
229 (void)fgets(tmp, sizeof(tmp) - 1, stdin);
230 tmp[strlen(tmp) - 1] = '\0';
231 if (*tmp == '\0')
232 user = myname;
233 else
234 user = tmp;
235 }
236 n = command("USER %s", user);
237 if (n == CONTINUE) {
238 if (pass == NULL)
239 pass = getpass("Password:");
240 n = command("PASS %s", pass);
241 }
242 if (n == CONTINUE) {
243 aflag++;
244 if (acct == NULL)
245 acct = getpass("Account:");
246 n = command("ACCT %s", acct);
247 }
248 if ((n != COMPLETE) ||
249 (!aflag && acct != NULL && command("ACCT %s", acct) != COMPLETE)) {
250 warnx("Login failed.");
251 return (0);
252 }
253 if (proxy)
254 return (1);
255 connected = -1;
256 for (n = 0; n < macnum; ++n) {
257 if (!strcmp("init", macros[n].mac_name)) {
258 (void)strcpy(line, "$init");
259 makeargv();
260 domacro(margc, margv);
261 break;
262 }
263 }
264 return (1);
265 }
266
267 /*
268 * `another' gets another argument, and stores the new argc and argv.
269 * It reverts to the top level (via main.c's intr()) on EOF/error.
270 *
271 * Returns false if no new arguments have been added.
272 */
273 int
274 another(pargc, pargv, prompt)
275 int *pargc;
276 char ***pargv;
277 const char *prompt;
278 {
279 int len = strlen(line), ret;
280
281 if (len >= sizeof(line) - 3) {
282 puts("sorry, arguments too long.");
283 intr();
284 }
285 printf("(%s) ", prompt);
286 line[len++] = ' ';
287 if (fgets(&line[len], sizeof(line) - len, stdin) == NULL)
288 intr();
289 len += strlen(&line[len]);
290 if (len > 0 && line[len - 1] == '\n')
291 line[len - 1] = '\0';
292 makeargv();
293 ret = margc > *pargc;
294 *pargc = margc;
295 *pargv = margv;
296 return (ret);
297 }
298
299 /*
300 * glob files given in argv[] from the remote server.
301 * if errbuf isn't NULL, store error messages there instead
302 * of writing to the screen.
303 */
304 char *
305 remglob(argv, doswitch, errbuf)
306 char *argv[];
307 int doswitch;
308 char **errbuf;
309 {
310 char temp[MAXPATHLEN];
311 static char buf[MAXPATHLEN];
312 static FILE *ftemp = NULL;
313 static char **args;
314 int oldverbose, oldhash, fd;
315 char *cp, *mode;
316
317 if (!mflag) {
318 if (!doglob)
319 args = NULL;
320 else {
321 if (ftemp) {
322 (void)fclose(ftemp);
323 ftemp = NULL;
324 }
325 }
326 return (NULL);
327 }
328 if (!doglob) {
329 if (args == NULL)
330 args = argv;
331 if ((cp = *++args) == NULL)
332 args = NULL;
333 return (cp);
334 }
335 if (ftemp == NULL) {
336 (void)snprintf(temp, sizeof(temp), "%s%s", _PATH_TMP, TMPFILE);
337 if ((fd = mkstemp(temp)) < 0) {
338 warn("unable to create temporary file %s", temp);
339 return (NULL);
340 }
341 close(fd);
342 oldverbose = verbose;
343 verbose = (errbuf != NULL) ? -1 : 0;
344 oldhash = hash;
345 hash = 0;
346 if (doswitch)
347 pswitch(!proxy);
348 for (mode = "w"; *++argv != NULL; mode = "a")
349 recvrequest("NLST", temp, *argv, mode, 0);
350 if ((code / 100) != COMPLETE) {
351 if (errbuf != NULL)
352 *errbuf = reply_string;
353 }
354 if (doswitch)
355 pswitch(!proxy);
356 verbose = oldverbose;
357 hash = oldhash;
358 ftemp = fopen(temp, "r");
359 (void)unlink(temp);
360 if (ftemp == NULL) {
361 if (errbuf == NULL)
362 puts("can't find list of remote files, oops.");
363 else
364 *errbuf =
365 "can't find list of remote files, oops.";
366 return (NULL);
367 }
368 }
369 if (fgets(buf, sizeof(buf), ftemp) == NULL) {
370 (void)fclose(ftemp);
371 ftemp = NULL;
372 return (NULL);
373 }
374 if ((cp = strchr(buf, '\n')) != NULL)
375 *cp = '\0';
376 return (buf);
377 }
378
379 int
380 confirm(cmd, file)
381 const char *cmd, *file;
382 {
383 char line[BUFSIZ];
384
385 if (!interactive || confirmrest)
386 return (1);
387 printf("%s %s? ", cmd, file);
388 (void)fflush(stdout);
389 if (fgets(line, sizeof(line), stdin) == NULL)
390 return (0);
391 switch (tolower(*line)) {
392 case 'n':
393 return (0);
394 case 'p':
395 interactive = 0;
396 puts("Interactive mode: off.");
397 break;
398 case 'a':
399 confirmrest = 1;
400 printf("Prompting off for duration of %s.\n", cmd);
401 break;
402 }
403 return (1);
404 }
405
406 /*
407 * Glob a local file name specification with
408 * the expectation of a single return value.
409 * Can't control multiple values being expanded
410 * from the expression, we return only the first.
411 */
412 int
413 globulize(cpp)
414 char **cpp;
415 {
416 glob_t gl;
417 int flags;
418
419 if (!doglob)
420 return (1);
421
422 flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE;
423 memset(&gl, 0, sizeof(gl));
424 if (glob(*cpp, flags, NULL, &gl) ||
425 gl.gl_pathc == 0) {
426 warnx("%s: not found", *cpp);
427 globfree(&gl);
428 return (0);
429 }
430 *cpp = strdup(gl.gl_pathv[0]); /* XXX - wasted memory */
431 globfree(&gl);
432 return (1);
433 }
434
435 /*
436 * determine size of remote file
437 */
438 off_t
439 remotesize(file, noisy)
440 const char *file;
441 int noisy;
442 {
443 int overbose;
444 off_t size;
445
446 overbose = verbose;
447 size = -1;
448 if (debug == 0)
449 verbose = -1;
450 if (command("SIZE %s", file) == COMPLETE)
451 sscanf(reply_string, "%*s %qd", &size);
452 else if (noisy && debug == 0)
453 puts(reply_string);
454 verbose = overbose;
455 return (size);
456 }
457
458 /*
459 * determine last modification time (in GMT) of remote file
460 */
461 time_t
462 remotemodtime(file, noisy)
463 const char *file;
464 int noisy;
465 {
466 int overbose;
467 time_t rtime;
468
469 overbose = verbose;
470 rtime = -1;
471 if (debug == 0)
472 verbose = -1;
473 if (command("MDTM %s", file) == COMPLETE) {
474 struct tm timebuf;
475 int yy, mo, day, hour, min, sec;
476 sscanf(reply_string, "%*s %04d%02d%02d%02d%02d%02d", &yy, &mo,
477 &day, &hour, &min, &sec);
478 memset(&timebuf, 0, sizeof(timebuf));
479 timebuf.tm_sec = sec;
480 timebuf.tm_min = min;
481 timebuf.tm_hour = hour;
482 timebuf.tm_mday = day;
483 timebuf.tm_mon = mo - 1;
484 timebuf.tm_year = yy - 1900;
485 timebuf.tm_isdst = -1;
486 rtime = mktime(&timebuf);
487 if (rtime == -1 && (noisy || debug != 0))
488 printf("Can't convert %s to a time.\n", reply_string);
489 else
490 rtime += timebuf.tm_gmtoff; /* conv. local -> GMT */
491 } else if (noisy && debug == 0)
492 puts(reply_string);
493 verbose = overbose;
494 return (rtime);
495 }
496
497 void
498 updateprogressmeter()
499 {
500
501 progressmeter(0);
502 }
503
504 /*
505 * Display a transfer progress bar if progress is non-zero.
506 * SIGALRM is hijacked for use by this function.
507 * - Before the transfer, set filesize to size of file (or -1 if unknown),
508 * and call with flag = -1. This starts the once per second timer,
509 * and a call to updateprogressmeter() upon SIGALRM.
510 * - During the transfer, updateprogressmeter will call progressmeter
511 * with flag = 0
512 * - After the transfer, call with flag = 1
513 */
514 static struct timeval start;
515
516 void
517 progressmeter(flag)
518 int flag;
519 {
520 /*
521 * List of order of magnitude prefixes.
522 * The last is `P', as 2^64 = 16384 Petabytes
523 */
524 static const char prefixes[] = " KMGTP";
525
526 static struct timeval lastupdate;
527 static off_t lastsize;
528 struct timeval now, td, wait;
529 off_t cursize, abbrevsize;
530 double elapsed;
531 int ratio, barlength, i, remaining;
532 char buf[256];
533
534 if (flag == -1) {
535 (void)gettimeofday(&start, (struct timezone *)0);
536 lastupdate = start;
537 lastsize = restart_point;
538 }
539 (void)gettimeofday(&now, (struct timezone *)0);
540 if (!progress || filesize <= 0)
541 return;
542 cursize = bytes + restart_point;
543
544 ratio = cursize * 100 / filesize;
545 ratio = MAX(ratio, 0);
546 ratio = MIN(ratio, 100);
547 snprintf(buf, sizeof(buf), "\r%3d%% ", ratio);
548
549 barlength = ttywidth - 30;
550 if (barlength > 0) {
551 i = barlength * ratio / 100;
552 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
553 "|%.*s%*s|", i,
554 "*****************************************************************************"
555 "*****************************************************************************",
556 barlength - i, "");
557 }
558
559 i = 0;
560 abbrevsize = cursize;
561 while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
562 i++;
563 abbrevsize >>= 10;
564 }
565 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
566 " %5qd %c%c ", abbrevsize, prefixes[i],
567 prefixes[i] == ' ' ? ' ' : 'B');
568
569 timersub(&now, &lastupdate, &wait);
570 if (cursize > lastsize) {
571 lastupdate = now;
572 lastsize = cursize;
573 if (wait.tv_sec >= STALLTIME) { /* fudge out stalled time */
574 start.tv_sec += wait.tv_sec;
575 start.tv_usec += wait.tv_usec;
576 }
577 wait.tv_sec = 0;
578 }
579
580 timersub(&now, &start, &td);
581 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
582
583 if (bytes <= 0 || elapsed <= 0.0 || cursize > filesize) {
584 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
585 " --:-- ETA");
586 } else if (wait.tv_sec >= STALLTIME) {
587 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
588 " - stalled -");
589 } else {
590 remaining = (int)((filesize - restart_point) /
591 (bytes / elapsed) - elapsed);
592 i = remaining / 3600;
593 if (i)
594 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
595 "%2d:", i);
596 else
597 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
598 " ");
599 i = remaining % 3600;
600 snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
601 "%02d:%02d ETA", i / 60, i % 60);
602 }
603 (void)write(STDOUT_FILENO, buf, strlen(buf));
604
605 if (flag == -1) {
606 (void)signal(SIGALRM, updateprogressmeter);
607 alarmtimer(1); /* set alarm timer for 1 Hz */
608 } else if (flag == 1) {
609 alarmtimer(0);
610 (void)putchar('\n');
611 }
612 fflush(stdout);
613 }
614
615 /*
616 * Display transfer statistics.
617 * Requires start to be initialised by progressmeter(-1),
618 * direction to be defined by xfer routines, and filesize and bytes
619 * to be updated by xfer routines
620 * If siginfo is nonzero, an ETA is displayed, and the output goes to STDERR
621 * instead of STDOUT.
622 */
623 void
624 ptransfer(siginfo)
625 int siginfo;
626 {
627 struct timeval now, td;
628 double elapsed;
629 off_t bs;
630 int meg, remaining, hh;
631 char buf[100];
632
633 if (!verbose && !siginfo)
634 return;
635
636 (void)gettimeofday(&now, (struct timezone *)0);
637 timersub(&now, &start, &td);
638 elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
639 bs = bytes / (elapsed == 0.0 ? 1 : elapsed);
640 meg = 0;
641 if (bs > (1024 * 1024))
642 meg = 1;
643 (void)snprintf(buf, sizeof(buf),
644 "%qd byte%s %s in %.2f seconds (%.2f %sB/s)\n",
645 bytes, bytes == 1 ? "" : "s", direction, elapsed,
646 bs / (1024.0 * (meg ? 1024.0 : 1.0)), meg ? "M" : "K");
647 if (siginfo && bytes > 0 && elapsed > 0.0 && filesize >= 0
648 && bytes + restart_point <= filesize) {
649 remaining = (int)((filesize - restart_point) /
650 (bytes / elapsed) - elapsed);
651 hh = remaining / 3600;
652 remaining %= 3600;
653 /* "buf+len(buf) -1" to overwrite \n */
654 snprintf(buf + strlen(buf) - 1, sizeof(buf) - strlen(buf),
655 " ETA: %02d:%02d:%02d\n", hh, remaining / 60,
656 remaining % 60);
657 }
658 (void)write(siginfo ? STDERR_FILENO : STDOUT_FILENO, buf, strlen(buf));
659 }
660
661 /*
662 * List words in stringlist, vertically arranged
663 */
664 void
665 list_vertical(sl)
666 StringList *sl;
667 {
668 int i, j, w;
669 int columns, width, lines, items;
670 char *p;
671
672 width = items = 0;
673
674 for (i = 0 ; i < sl->sl_cur ; i++) {
675 w = strlen(sl->sl_str[i]);
676 if (w > width)
677 width = w;
678 }
679 width = (width + 8) &~ 7;
680
681 columns = ttywidth / width;
682 if (columns == 0)
683 columns = 1;
684 lines = (sl->sl_cur + columns - 1) / columns;
685 for (i = 0; i < lines; i++) {
686 for (j = 0; j < columns; j++) {
687 p = sl->sl_str[j * lines + i];
688 if (p)
689 fputs(p, stdout);
690 if (j * lines + i + lines >= sl->sl_cur) {
691 putchar('\n');
692 break;
693 }
694 w = strlen(p);
695 while (w < width) {
696 w = (w + 8) &~ 7;
697 (void)putchar('\t');
698 }
699 }
700 }
701 }
702
703 /*
704 * Update the global ttywidth value, using TIOCGWINSZ.
705 */
706 void
707 setttywidth(a)
708 int a;
709 {
710 struct winsize winsize;
711
712 if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
713 ttywidth = winsize.ws_col;
714 else
715 ttywidth = 80;
716 }
717
718 /*
719 * Set the SIGALRM interval timer for wait seconds, 0 to disable.
720 */
721 void
722 alarmtimer(wait)
723 int wait;
724 {
725 struct itimerval itv;
726
727 itv.it_value.tv_sec = wait;
728 itv.it_value.tv_usec = 0;
729 itv.it_interval = itv.it_value;
730 setitimer(ITIMER_REAL, &itv, NULL);
731 }
732
733 /*
734 * Setup or cleanup EditLine structures
735 */
736 #ifndef SMALL
737 void
738 controlediting()
739 {
740 if (editing && el == NULL && hist == NULL) {
741 el = el_init(__progname, stdin, stdout); /* init editline */
742 hist = history_init(); /* init the builtin history */
743 history(hist, H_EVENT, 100); /* remember 100 events */
744 el_set(el, EL_HIST, history, hist); /* use history */
745
746 el_set(el, EL_EDITOR, "emacs"); /* default editor is emacs */
747 el_set(el, EL_PROMPT, prompt); /* set the prompt function */
748
749 /* add local file completion, bind to TAB */
750 el_set(el, EL_ADDFN, "ftp-complete",
751 "Context sensitive argument completion",
752 complete);
753 el_set(el, EL_BIND, "^I", "ftp-complete", NULL);
754
755 el_source(el, NULL); /* read ~/.editrc */
756 el_set(el, EL_SIGNAL, 1);
757 } else if (!editing) {
758 if (hist) {
759 history_end(hist);
760 hist = NULL;
761 }
762 if (el) {
763 el_end(el);
764 el = NULL;
765 }
766 }
767 }
768 #endif /* !SMALL */
769