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