Home | History | Annotate | Line # | Download | only in ftp
util.c revision 1.3
      1  1.3    lukem /*	$NetBSD: util.c,v 1.3 1997/02/01 10:45:08 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.3    lukem static char rcsid[] = "$NetBSD: util.c,v 1.3 1997/02/01 10:45:08 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.1    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.1    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.1    lukem 			printf("%s: bad port number-- %s\n", argv[1], argv[2]);
     89  1.1    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.1    lukem 		(void) strcpy(typename, "ascii"), type = TYPE_A;
    104  1.1    lukem 		curtype = TYPE_A;
    105  1.1    lukem 		(void) strcpy(formname, "non-print"), form = FORM_N;
    106  1.1    lukem 		(void) strcpy(modename, "stream"), mode = MODE_S;
    107  1.1    lukem 		(void) strcpy(structname, "file"), stru = STRU_F;
    108  1.1    lukem 		(void) strcpy(bytename, "8"), bytesize = 8;
    109  1.1    lukem 		if (autologin)
    110  1.1    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.1    lukem 			printf("Remote system type is %s.\n",
    129  1.1    lukem 				reply_string+4);
    130  1.1    lukem 			if (cp)
    131  1.1    lukem 				*cp = c;
    132  1.1    lukem 		}
    133  1.1    lukem 		if (!strncmp(reply_string, "215 UNIX Type: L8", 17)) {
    134  1.1    lukem 			if (proxy)
    135  1.1    lukem 				unix_proxy = 1;
    136  1.1    lukem 			else
    137  1.1    lukem 				unix_server = 1;
    138  1.1    lukem 			/*
    139  1.1    lukem 			 * Set type to 0 (not specified by user),
    140  1.1    lukem 			 * meaning binary by default, but don't bother
    141  1.1    lukem 			 * telling server.  We can use binary
    142  1.1    lukem 			 * for text files unless changed by the user.
    143  1.1    lukem 			 */
    144  1.1    lukem 			type = 0;
    145  1.1    lukem 			(void) strcpy(typename, "binary");
    146  1.1    lukem 			if (overbose)
    147  1.1    lukem 			    printf("Using %s mode to transfer files.\n",
    148  1.1    lukem 				typename);
    149  1.1    lukem 		} else {
    150  1.1    lukem 			if (proxy)
    151  1.1    lukem 				unix_proxy = 0;
    152  1.1    lukem 			else
    153  1.1    lukem 				unix_server = 0;
    154  1.1    lukem 			if (overbose &&
    155  1.1    lukem 			    !strncmp(reply_string, "215 TOPS20", 10))
    156  1.1    lukem 				printf("Remember to set tenex mode when "
    157  1.1    lukem 				    "transferring binary files from this "
    158  1.1    lukem 				    "machine.\n");
    159  1.1    lukem 		}
    160  1.1    lukem 		verbose = overbose;
    161  1.1    lukem 	}
    162  1.1    lukem }
    163  1.1    lukem 
    164  1.1    lukem /*
    165  1.1    lukem  * `Another' gets another argument, and stores the new argc and argv.
    166  1.1    lukem  * It reverts to the top level (via main.c's intr()) on EOF/error.
    167  1.1    lukem  *
    168  1.1    lukem  * Returns false if no new arguments have been added.
    169  1.1    lukem  */
    170  1.1    lukem int
    171  1.1    lukem another(pargc, pargv, prompt)
    172  1.1    lukem 	int *pargc;
    173  1.1    lukem 	char ***pargv;
    174  1.1    lukem 	const char *prompt;
    175  1.1    lukem {
    176  1.1    lukem 	int len = strlen(line), ret;
    177  1.1    lukem 
    178  1.1    lukem 	if (len >= sizeof(line) - 3) {
    179  1.1    lukem 		printf("sorry, arguments too long\n");
    180  1.1    lukem 		intr();
    181  1.1    lukem 	}
    182  1.1    lukem 	printf("(%s) ", prompt);
    183  1.1    lukem 	line[len++] = ' ';
    184  1.1    lukem 	if (fgets(&line[len], sizeof(line) - len, stdin) == NULL)
    185  1.1    lukem 		intr();
    186  1.1    lukem 	len += strlen(&line[len]);
    187  1.1    lukem 	if (len > 0 && line[len - 1] == '\n')
    188  1.1    lukem 		line[len - 1] = '\0';
    189  1.1    lukem 	makeargv();
    190  1.1    lukem 	ret = margc > *pargc;
    191  1.1    lukem 	*pargc = margc;
    192  1.1    lukem 	*pargv = margv;
    193  1.1    lukem 	return (ret);
    194  1.1    lukem }
    195  1.1    lukem 
    196  1.1    lukem char *
    197  1.1    lukem remglob(argv, doswitch)
    198  1.1    lukem         char *argv[];
    199  1.1    lukem         int doswitch;
    200  1.1    lukem {
    201  1.1    lukem         char temp[MAXPATHLEN];
    202  1.1    lukem         static char buf[MAXPATHLEN];
    203  1.1    lukem         static FILE *ftemp = NULL;
    204  1.1    lukem         static char **args;
    205  1.1    lukem         int oldverbose, oldhash, fd;
    206  1.1    lukem         char *cp, *mode;
    207  1.1    lukem 
    208  1.1    lukem         if (!mflag) {
    209  1.1    lukem                 if (!doglob) {
    210  1.1    lukem                         args = NULL;
    211  1.1    lukem                 }
    212  1.1    lukem                 else {
    213  1.1    lukem                         if (ftemp) {
    214  1.1    lukem                                 (void) fclose(ftemp);
    215  1.1    lukem                                 ftemp = NULL;
    216  1.1    lukem                         }
    217  1.1    lukem                 }
    218  1.1    lukem                 return (NULL);
    219  1.1    lukem         }
    220  1.1    lukem         if (!doglob) {
    221  1.1    lukem                 if (args == NULL)
    222  1.1    lukem                         args = argv;
    223  1.1    lukem                 if ((cp = *++args) == NULL)
    224  1.1    lukem                         args = NULL;
    225  1.1    lukem                 return (cp);
    226  1.1    lukem         }
    227  1.1    lukem         if (ftemp == NULL) {
    228  1.1    lukem                 (void) snprintf(temp, sizeof(temp), "%s%s", _PATH_TMP, TMPFILE)
    229  1.1    lukem ;
    230  1.1    lukem                 fd = mkstemp(temp);
    231  1.1    lukem                 if (fd < 0) {
    232  1.1    lukem                         warn("unable to create temporary file %s", temp);
    233  1.1    lukem                         return (NULL);
    234  1.1    lukem                 }
    235  1.1    lukem                 close(fd);
    236  1.1    lukem                 oldverbose = verbose, verbose = 0;
    237  1.1    lukem                 oldhash = hash, hash = 0;
    238  1.1    lukem                 if (doswitch) {
    239  1.1    lukem                         pswitch(!proxy);
    240  1.1    lukem                 }
    241  1.1    lukem                 for (mode = "w"; *++argv != NULL; mode = "a")
    242  1.1    lukem                         recvrequest ("NLST", temp, *argv, mode, 0);
    243  1.1    lukem                 if (doswitch) {
    244  1.1    lukem                         pswitch(!proxy);
    245  1.1    lukem                 }
    246  1.1    lukem                 verbose = oldverbose; hash = oldhash;
    247  1.1    lukem                 ftemp = fopen(temp, "r");
    248  1.1    lukem                 (void) unlink(temp);
    249  1.1    lukem                 if (ftemp == NULL) {
    250  1.1    lukem                         printf("can't find list of remote files, oops\n");
    251  1.1    lukem                         return (NULL);
    252  1.1    lukem                 }
    253  1.1    lukem         }
    254  1.1    lukem         if (fgets(buf, sizeof (buf), ftemp) == NULL) {
    255  1.1    lukem                 (void) fclose(ftemp), ftemp = NULL;
    256  1.1    lukem                 return (NULL);
    257  1.1    lukem         }
    258  1.1    lukem         if ((cp = strchr(buf, '\n')) != NULL)
    259  1.1    lukem                 *cp = '\0';
    260  1.1    lukem         return (buf);
    261  1.1    lukem }
    262  1.1    lukem 
    263  1.1    lukem int
    264  1.1    lukem confirm(cmd, file)
    265  1.1    lukem 	const char *cmd, *file;
    266  1.1    lukem {
    267  1.1    lukem 	char line[BUFSIZ];
    268  1.1    lukem 
    269  1.1    lukem 	if (!interactive || confirmrest)
    270  1.1    lukem 		return (1);
    271  1.1    lukem 	printf("%s %s? ", cmd, file);
    272  1.1    lukem 	(void) fflush(stdout);
    273  1.1    lukem 	if (fgets(line, sizeof(line), stdin) == NULL)
    274  1.1    lukem 		return (0);
    275  1.1    lukem 	switch (tolower(*line)) {
    276  1.1    lukem 		case 'n':
    277  1.1    lukem 			return (0);
    278  1.1    lukem 		case 'p':
    279  1.1    lukem 			interactive = 0;
    280  1.1    lukem 			printf("Interactive mode: off\n");
    281  1.1    lukem 			break;
    282  1.1    lukem 		case 'a':
    283  1.1    lukem 			confirmrest = 1;
    284  1.1    lukem 			printf("Prompting off for duration of %s\n", cmd);
    285  1.1    lukem 			break;
    286  1.1    lukem 	}
    287  1.1    lukem 	return (1);
    288  1.1    lukem }
    289  1.1    lukem 
    290  1.1    lukem /*
    291  1.1    lukem  * Glob a local file name specification with
    292  1.1    lukem  * the expectation of a single return value.
    293  1.1    lukem  * Can't control multiple values being expanded
    294  1.1    lukem  * from the expression, we return only the first.
    295  1.1    lukem  */
    296  1.1    lukem int
    297  1.1    lukem globulize(cpp)
    298  1.1    lukem 	char **cpp;
    299  1.1    lukem {
    300  1.1    lukem 	glob_t gl;
    301  1.1    lukem 	int flags;
    302  1.1    lukem 
    303  1.1    lukem 	if (!doglob)
    304  1.1    lukem 		return (1);
    305  1.1    lukem 
    306  1.1    lukem 	flags = GLOB_BRACE|GLOB_NOCHECK|GLOB_QUOTE|GLOB_TILDE;
    307  1.1    lukem 	memset(&gl, 0, sizeof(gl));
    308  1.1    lukem 	if (glob(*cpp, flags, NULL, &gl) ||
    309  1.1    lukem 	    gl.gl_pathc == 0) {
    310  1.1    lukem 		warnx("%s: not found", *cpp);
    311  1.1    lukem 		globfree(&gl);
    312  1.1    lukem 		return (0);
    313  1.1    lukem 	}
    314  1.1    lukem 	*cpp = strdup(gl.gl_pathv[0]);	/* XXX - wasted memory */
    315  1.1    lukem 	globfree(&gl);
    316  1.1    lukem 	return (1);
    317  1.1    lukem }
    318  1.1    lukem 
    319  1.1    lukem /*
    320  1.1    lukem  * determine size of remote file
    321  1.1    lukem  */
    322  1.1    lukem off_t
    323  1.1    lukem remotesize(file, noisy)
    324  1.1    lukem 	const char *file;
    325  1.1    lukem 	int noisy;
    326  1.1    lukem {
    327  1.1    lukem 	int overbose;
    328  1.1    lukem 	off_t size;
    329  1.1    lukem 
    330  1.1    lukem 	overbose = verbose;
    331  1.1    lukem 	size = -1;
    332  1.1    lukem 	if (debug == 0)
    333  1.1    lukem 		verbose = -1;
    334  1.1    lukem 	if (command("SIZE %s", file) == COMPLETE)
    335  1.1    lukem 		sscanf(reply_string, "%*s %qd", &size);
    336  1.1    lukem 	else if (noisy && debug == 0)
    337  1.1    lukem 		printf("%s\n", reply_string);
    338  1.1    lukem 	verbose = overbose;
    339  1.1    lukem 	return (size);
    340  1.1    lukem }
    341  1.1    lukem 
    342  1.1    lukem /*
    343  1.1    lukem  * determine last modification time (in GMT) of remote file
    344  1.1    lukem  */
    345  1.1    lukem time_t
    346  1.1    lukem remotemodtime(file, noisy)
    347  1.1    lukem 	const char *file;
    348  1.1    lukem 	int noisy;
    349  1.1    lukem {
    350  1.1    lukem 	int overbose;
    351  1.1    lukem 	time_t rtime;
    352  1.1    lukem 
    353  1.1    lukem 	overbose = verbose;
    354  1.1    lukem 	rtime = -1;
    355  1.1    lukem 	if (debug == 0)
    356  1.1    lukem 		verbose = -1;
    357  1.1    lukem 	if (command("MDTM %s", file) == COMPLETE) {
    358  1.1    lukem 		struct tm timebuf;
    359  1.1    lukem 		int yy, mo, day, hour, min, sec;
    360  1.1    lukem 		sscanf(reply_string, "%*s %04d%02d%02d%02d%02d%02d", &yy, &mo,
    361  1.1    lukem 			&day, &hour, &min, &sec);
    362  1.1    lukem 		memset(&timebuf, 0, sizeof(timebuf));
    363  1.1    lukem 		timebuf.tm_sec = sec;
    364  1.1    lukem 		timebuf.tm_min = min;
    365  1.1    lukem 		timebuf.tm_hour = hour;
    366  1.1    lukem 		timebuf.tm_mday = day;
    367  1.1    lukem 		timebuf.tm_mon = mo - 1;
    368  1.1    lukem 		timebuf.tm_year = yy - 1900;
    369  1.1    lukem 		timebuf.tm_isdst = -1;
    370  1.1    lukem 		rtime = mktime(&timebuf);
    371  1.1    lukem 		if (rtime == -1 && (noisy || debug != 0))
    372  1.1    lukem 			printf("Can't convert %s to a time\n", reply_string);
    373  1.1    lukem 		else
    374  1.1    lukem 			rtime += timebuf.tm_gmtoff;	/* conv. local -> GMT */
    375  1.1    lukem 	} else if (noisy && debug == 0)
    376  1.1    lukem 		printf("%s\n", reply_string);
    377  1.1    lukem 	verbose = overbose;
    378  1.1    lukem 	return (rtime);
    379  1.1    lukem }
    380  1.1    lukem 
    381  1.3    lukem void
    382  1.3    lukem updateprogressmeter()
    383  1.3    lukem {
    384  1.3    lukem 
    385  1.3    lukem 	progressmeter(0);
    386  1.3    lukem }
    387  1.3    lukem 
    388  1.1    lukem /*
    389  1.1    lukem  * Display a transfer progress bar if progress is non-zero.
    390  1.3    lukem  * SIGALRM is hijacked for use by this function.
    391  1.3    lukem  * - Before the transfer, set filesize to size of file (or -1 if unknown),
    392  1.3    lukem  *   and call with flag = -1. This starts the once per second timer,
    393  1.3    lukem  *   and a call to updateprogressmeter() upon SIGALRM.
    394  1.3    lukem  * - During the transfer, updateprogressmeter will call progressmeter
    395  1.3    lukem  *   with flag = 0
    396  1.3    lukem  * - After the transfer, call with flag = 1
    397  1.1    lukem  */
    398  1.1    lukem static struct timeval start;
    399  1.1    lukem 
    400  1.1    lukem void
    401  1.1    lukem progressmeter(flag)
    402  1.1    lukem 	int flag;
    403  1.1    lukem {
    404  1.3    lukem 	/*
    405  1.3    lukem 	 * List of order of magnitude prefixes.
    406  1.3    lukem 	 * The last is `P', as 2^64 = 16384 Petabytes
    407  1.3    lukem 	 */
    408  1.3    lukem 	static const char prefixes[] = " KMGTP";
    409  1.3    lukem 
    410  1.3    lukem 	static struct timeval lastupdate;
    411  1.3    lukem 	static off_t lastsize;
    412  1.3    lukem 	struct timeval now, td, wait;
    413  1.1    lukem 	off_t cursize, abbrevsize;
    414  1.1    lukem 	double elapsed;
    415  1.1    lukem 	int ratio, barlength, i, remaining;
    416  1.3    lukem 	char buf[256];
    417  1.1    lukem 
    418  1.3    lukem 	if (flag == -1) {
    419  1.1    lukem 		(void) gettimeofday(&start, (struct timezone *)0);
    420  1.3    lukem 		lastupdate = start;
    421  1.3    lukem 		lastsize = restart_point;
    422  1.3    lukem 	}
    423  1.3    lukem 	(void) gettimeofday(&now, (struct timezone *)0);
    424  1.2  thorpej 	if (!progress || filesize <= 0)
    425  1.1    lukem 		return;
    426  1.1    lukem 	cursize = bytes + restart_point;
    427  1.1    lukem 
    428  1.1    lukem 	ratio = cursize * 100 / filesize;
    429  1.1    lukem 	ratio = MAX(ratio, 0);
    430  1.1    lukem 	ratio = MIN(ratio, 100);
    431  1.3    lukem 	snprintf(buf, sizeof(buf), "\r%3d%% ", ratio);
    432  1.1    lukem 
    433  1.1    lukem 	barlength = ttywidth - 30;
    434  1.1    lukem 	if (barlength > 0) {
    435  1.1    lukem 		i = barlength * ratio / 100;
    436  1.3    lukem 		snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
    437  1.3    lukem 		    "|%.*s%*s|", i,
    438  1.1    lukem "*****************************************************************************"
    439  1.1    lukem "*****************************************************************************",
    440  1.1    lukem 		    barlength - i, "");
    441  1.1    lukem 	}
    442  1.1    lukem 
    443  1.1    lukem 	i = 0;
    444  1.1    lukem 	abbrevsize = cursize;
    445  1.1    lukem 	while (abbrevsize >= 100000 && i < sizeof(prefixes)) {
    446  1.1    lukem 		i++;
    447  1.1    lukem 		abbrevsize >>= 10;
    448  1.1    lukem 	}
    449  1.3    lukem 	snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
    450  1.3    lukem 	    " %5qd %c%c ", abbrevsize, prefixes[i],
    451  1.1    lukem 	    prefixes[i] == ' ' ? ' ' : 'B');
    452  1.1    lukem 
    453  1.3    lukem 	timersub(&now, &lastupdate, &wait);
    454  1.3    lukem 	if (cursize > lastsize) {
    455  1.3    lukem 		lastupdate = now;
    456  1.3    lukem 		lastsize = cursize;
    457  1.3    lukem 		if (wait.tv_sec >= STALLTIME) {	/* fudge out stalled time */
    458  1.3    lukem 			start.tv_sec += wait.tv_sec;
    459  1.3    lukem 			start.tv_usec += wait.tv_usec;
    460  1.3    lukem 		}
    461  1.3    lukem 		wait.tv_sec = 0;
    462  1.3    lukem 	}
    463  1.3    lukem 
    464  1.1    lukem 	timersub(&now, &start, &td);
    465  1.1    lukem 	elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
    466  1.3    lukem 
    467  1.1    lukem 	if (bytes <= 0 || elapsed <= 0.0) {
    468  1.3    lukem 		snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
    469  1.3    lukem 		    "   --:-- ETA");
    470  1.3    lukem 	} else if (wait.tv_sec >= STALLTIME) {
    471  1.3    lukem 		snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
    472  1.3    lukem 		    " - stalled -");
    473  1.1    lukem 	} else {
    474  1.1    lukem 		remaining = (int)((filesize - restart_point) /
    475  1.1    lukem 				  (bytes / elapsed) - elapsed);
    476  1.1    lukem 		i = remaining / 3600;
    477  1.1    lukem 		if (i)
    478  1.3    lukem 			snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
    479  1.3    lukem 			    "%2d:", i);
    480  1.1    lukem 		else
    481  1.3    lukem 			snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
    482  1.3    lukem 			    "   ");
    483  1.1    lukem 		i = remaining % 3600;
    484  1.3    lukem 		snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf),
    485  1.3    lukem 		    "%02d:%02d ETA", i / 60, i % 60);
    486  1.1    lukem 	}
    487  1.3    lukem 	(void)write(STDOUT_FILENO, buf, strlen(buf));
    488  1.1    lukem 
    489  1.3    lukem 	if (flag == -1) {
    490  1.3    lukem 		(void) signal(SIGALRM, updateprogressmeter);
    491  1.3    lukem 		alarmtimer(1);		/* set alarm timer for 1 Hz */
    492  1.3    lukem 	} else if (flag == 1) {
    493  1.3    lukem 		alarmtimer(0);
    494  1.1    lukem 		(void) putchar('\n');
    495  1.3    lukem 	}
    496  1.1    lukem 	fflush(stdout);
    497  1.1    lukem }
    498  1.1    lukem 
    499  1.1    lukem /*
    500  1.1    lukem  * Display transfer statistics.
    501  1.1    lukem  * Requires start to be initialised by progressmeter(-1),
    502  1.1    lukem  * direction to be defined by xfer routines, and filesize and bytes
    503  1.1    lukem  * to be updated by xfer routines
    504  1.1    lukem  * If siginfo is nonzero, an ETA is displayed, and the output goes to STDERR
    505  1.1    lukem  * instead of STDOUT.
    506  1.1    lukem  */
    507  1.1    lukem void
    508  1.1    lukem ptransfer(siginfo)
    509  1.1    lukem 	int siginfo;
    510  1.1    lukem {
    511  1.1    lukem 	struct timeval now, td;
    512  1.1    lukem 	double elapsed;
    513  1.1    lukem 	off_t bs;
    514  1.1    lukem 	int meg, remaining, hh;
    515  1.1    lukem 	char buf[100];
    516  1.1    lukem 
    517  1.1    lukem 	if (!verbose && !siginfo)
    518  1.1    lukem 		return;
    519  1.1    lukem 
    520  1.1    lukem 	(void) gettimeofday(&now, (struct timezone *)0);
    521  1.1    lukem 	timersub(&now, &start, &td);
    522  1.1    lukem 	elapsed = td.tv_sec + (td.tv_usec / 1000000.0);
    523  1.1    lukem 	bs = bytes / (elapsed == 0.0 ? 1 : elapsed);
    524  1.1    lukem 	meg = 0;
    525  1.1    lukem 	if (bs > (1024 * 1024))
    526  1.1    lukem 		meg = 1;
    527  1.1    lukem 	(void)snprintf(buf, sizeof(buf),
    528  1.1    lukem 	    "%qd byte%s %s in %.2f seconds (%.2f %sB/s)\n",
    529  1.1    lukem 	    bytes, bytes == 1 ? "" : "s", direction, elapsed,
    530  1.1    lukem 	    bs / (1024.0 * (meg ? 1024.0 : 1.0)), meg ? "M" : "K");
    531  1.1    lukem 	if (siginfo && bytes > 0 && elapsed > 0.0) {
    532  1.1    lukem 		remaining = (int)((filesize - restart_point) /
    533  1.1    lukem 				  (bytes / elapsed) - elapsed);
    534  1.1    lukem 		hh = remaining / 3600;
    535  1.1    lukem 		remaining %= 3600;
    536  1.1    lukem 		snprintf(buf + strlen(buf) - 1, sizeof(buf) - strlen(buf),
    537  1.1    lukem 		    "  ETA: %02d:%02d:%02d\n", hh, remaining / 60,
    538  1.1    lukem 		    remaining % 60);
    539  1.1    lukem 	}
    540  1.1    lukem 	(void)write(siginfo ? STDERR_FILENO : STDOUT_FILENO, buf, strlen(buf));
    541  1.1    lukem }
    542  1.1    lukem 
    543  1.1    lukem /*
    544  1.1    lukem  * List words in stringlist, vertically arranged
    545  1.1    lukem  */
    546  1.1    lukem void
    547  1.1    lukem list_vertical(sl)
    548  1.1    lukem 	StringList *sl;
    549  1.1    lukem {
    550  1.1    lukem 	int i, j, w;
    551  1.1    lukem 	int columns, width, lines, items;
    552  1.1    lukem 	char *p;
    553  1.1    lukem 
    554  1.1    lukem 	width = items = 0;
    555  1.1    lukem 
    556  1.1    lukem 	for (i = 0 ; i < sl->sl_cur ; i++) {
    557  1.1    lukem 		w = strlen(sl->sl_str[i]);
    558  1.1    lukem 		if (w > width)
    559  1.1    lukem 			width = w;
    560  1.1    lukem 	}
    561  1.1    lukem 	width = (width + 8) &~ 7;
    562  1.1    lukem 
    563  1.1    lukem 	columns = ttywidth / width;
    564  1.1    lukem 	if (columns == 0)
    565  1.1    lukem 		columns = 1;
    566  1.1    lukem 	lines = (sl->sl_cur + columns - 1) / columns;
    567  1.1    lukem 	for (i = 0; i < lines; i++) {
    568  1.1    lukem 		for (j = 0; j < columns; j++) {
    569  1.1    lukem 			p = sl->sl_str[j * lines + i];
    570  1.1    lukem 			if (p)
    571  1.1    lukem 				printf("%s", p);
    572  1.1    lukem 			if (j * lines + i + lines >= sl->sl_cur) {
    573  1.1    lukem 				printf("\n");
    574  1.1    lukem 				break;
    575  1.1    lukem 			}
    576  1.1    lukem 			w = strlen(p);
    577  1.1    lukem 			while (w < width) {
    578  1.1    lukem 				w = (w + 8) &~ 7;
    579  1.1    lukem 				(void) putchar('\t');
    580  1.1    lukem 			}
    581  1.1    lukem 		}
    582  1.1    lukem 	}
    583  1.3    lukem }
    584  1.3    lukem 
    585  1.3    lukem /*
    586  1.3    lukem  * Update the global ttywidth value, using TIOCGWINSZ.
    587  1.3    lukem  */
    588  1.3    lukem void
    589  1.3    lukem setttywidth(a)
    590  1.3    lukem 	int a;
    591  1.3    lukem {
    592  1.3    lukem 	struct winsize winsize;
    593  1.3    lukem 
    594  1.3    lukem 	if (ioctl(fileno(stdout), TIOCGWINSZ, &winsize) != -1)
    595  1.3    lukem 		ttywidth = winsize.ws_col;
    596  1.3    lukem 	else
    597  1.3    lukem 		ttywidth = 80;
    598  1.3    lukem }
    599  1.3    lukem 
    600  1.3    lukem /*
    601  1.3    lukem  * Set the SIGALRM interval timer for wait seconds, 0 to disable.
    602  1.3    lukem  */
    603  1.3    lukem 
    604  1.3    lukem void
    605  1.3    lukem alarmtimer(wait)
    606  1.3    lukem 	int wait;
    607  1.3    lukem {
    608  1.3    lukem 	struct itimerval itv;
    609  1.3    lukem 
    610  1.3    lukem 	itv.it_value.tv_sec = wait;
    611  1.3    lukem 	itv.it_value.tv_usec = 0;
    612  1.3    lukem 	itv.it_interval = itv.it_value;
    613  1.3    lukem 	setitimer(ITIMER_REAL, &itv, NULL);
    614  1.1    lukem }
    615