* preserve modtime if size is 0 [bin/3040, Enami Tsugutomo]

* in autofetch mode, don't retry a file if the connection failed - skip it
  and move to the next file. [bin/3051, Matt Green]
* only print error messages from SIZE or MDTM if the user used 'size' or
  'modt' (respectively). prevents extraneous warnings during normal transfers
  when connected to a site which doesn't support these (behaviour prior to
  last commit), but still allows error feedback to specific user requests
  for this info (which the last commit broke).
* 'account': only accept one optional argument
* if invoked as pftp, default to passive mode on (from FreeBSD)
* remove leading '0 ' in progress bar - looked ugly
* use warn instead of perror
* use strncpy when src isn't known to have safe length
* remglob(): use mkstemp() to prevent symlink games, and don't override
  _PATH_TMP, use it as the prefix to the temp file
This commit is contained in:
lukem 1996-12-29 04:05:29 +00:00
parent ca69bed024
commit e699917c51
5 changed files with 57 additions and 61 deletions

View File

@ -1,4 +1,4 @@
/* $NetBSD: cmds.c,v 1.12 1996/12/25 16:00:38 christos Exp $ */ /* $NetBSD: cmds.c,v 1.13 1996/12/29 04:05:29 lukem Exp $ */
/* /*
* Copyright (c) 1985, 1989, 1993, 1994 * Copyright (c) 1985, 1989, 1993, 1994
@ -37,7 +37,7 @@
#if 0 #if 0
static char sccsid[] = "@(#)cmds.c 8.6 (Berkeley) 10/9/94"; static char sccsid[] = "@(#)cmds.c 8.6 (Berkeley) 10/9/94";
#else #else
static char rcsid[] = "$NetBSD: cmds.c,v 1.12 1996/12/25 16:00:38 christos Exp $"; static char rcsid[] = "$NetBSD: cmds.c,v 1.13 1996/12/29 04:05:29 lukem Exp $";
#endif #endif
#endif /* not lint */ #endif /* not lint */
@ -658,7 +658,7 @@ usage:
if (ret == 0) { if (ret == 0) {
time_t mtime; time_t mtime;
mtime = remotemodtime(argv[1]); mtime = remotemodtime(argv[1], 0);
if (mtime == -1) if (mtime == -1)
return(0); return(0);
if (stbuf.st_mtime >= mtime) if (stbuf.st_mtime >= mtime)
@ -760,7 +760,7 @@ remglob(argv, doswitch)
char *argv[]; char *argv[];
int doswitch; int doswitch;
{ {
char temp[16]; char temp[MAXPATHLEN];
static char buf[MAXPATHLEN]; static char buf[MAXPATHLEN];
static FILE *ftemp = NULL; static FILE *ftemp = NULL;
static char **args; static char **args;
@ -787,14 +787,10 @@ remglob(argv, doswitch)
return (cp); return (cp);
} }
if (ftemp == NULL) { if (ftemp == NULL) {
(void) strcpy(temp, _PATH_TMP); (void) snprintf(temp, sizeof(temp), "%s%s", _PATH_TMP, TMPFILE);
(void) mktemp(temp); fd = mkstemp(temp);
/* create a zero-length version of the file so that
* people can't play symlink games.
*/
fd = open (temp, O_CREAT | O_EXCL | O_WRONLY, 600);
if (fd < 0) { if (fd < 0) {
printf("Temporary file %s already exists\n", temp); warn("unable to create temporary file %s", temp);
return (NULL); return (NULL);
} }
close(fd); close(fd);
@ -1335,7 +1331,7 @@ shell(argc, argv)
{ {
pid_t pid; pid_t pid;
sig_t old1, old2; sig_t old1, old2;
char shellnam[40], *shell, *namep; char shellnam[MAXPATHLEN], *shell, *namep;
union wait status; union wait status;
old1 = signal (SIGINT, SIG_IGN); old1 = signal (SIGINT, SIG_IGN);
@ -1351,8 +1347,8 @@ shell(argc, argv)
namep = strrchr(shell, '/'); namep = strrchr(shell, '/');
if (namep == NULL) if (namep == NULL)
namep = shell; namep = shell;
(void) strcpy(shellnam, "-"); shellnam[0] = '-';
(void) strcat(shellnam, ++namep); (void) strncpy(shellnam + 1, ++namep, sizeof(shellnam) - 1);
if (strcmp(namep, "sh") != 0) if (strcmp(namep, "sh") != 0)
shellnam[0] = '+'; shellnam[0] = '+';
if (debug) { if (debug) {
@ -1558,13 +1554,14 @@ quote1(initial, argc, argv)
int i, len; int i, len;
char buf[BUFSIZ]; /* must be >= sizeof(line) */ char buf[BUFSIZ]; /* must be >= sizeof(line) */
(void) strcpy(buf, initial); (void) strncpy(buf, initial, sizeof(buf));
if (argc > 1) { if (argc > 1) {
len = strlen(buf); len = strlen(buf);
len += strlen(strcpy(&buf[len], argv[1])); len += strlen(strncpy(&buf[len], argv[1], sizeof(buf) - len));
for (i = 2; i < argc; i++) { for (i = 2; i < argc; i++) {
buf[len++] = ' '; buf[len++] = ' ';
len += strlen(strcpy(&buf[len], argv[i])); len += strlen(strncpy(&buf[len], argv[i],
sizeof(buf) - len));
} }
} }
if (command(buf) == PRELIM) { if (command(buf) == PRELIM) {
@ -1740,23 +1737,17 @@ account(argc, argv)
int argc; int argc;
char *argv[]; char *argv[];
{ {
char acct[50], *ap; char *ap;
if (argc > 1) { if (argc > 2) {
++argv; printf("usage: %s [password]\n", argv[0]);
--argc; code = -1;
(void) strncpy(acct, *argv, 49); return;
acct[49] = '\0';
while (argc > 1) {
--argc;
++argv;
(void) strncat(acct, *argv, 49-strlen(acct));
}
ap = acct;
} }
else { else if (argc == 2)
ap = argv[1];
else
ap = getpass("Account:"); ap = getpass("Account:");
}
(void) command("ACCT %s", ap); (void) command("ACCT %s", ap);
} }
@ -2245,8 +2236,9 @@ macdef(argc, argv)
* determine size of remote file * determine size of remote file
*/ */
off_t off_t
remotesize(file) remotesize(file, noisy)
const char *file; const char *file;
int noisy;
{ {
int overbose; int overbose;
off_t size; off_t size;
@ -2257,7 +2249,7 @@ remotesize(file)
verbose = -1; verbose = -1;
if (command("SIZE %s", file) == COMPLETE) if (command("SIZE %s", file) == COMPLETE)
sscanf(reply_string, "%*s %qd", &size); sscanf(reply_string, "%*s %qd", &size);
else if (debug != 0) else if (noisy && debug == 0)
printf("%s\n", reply_string); printf("%s\n", reply_string);
verbose = overbose; verbose = overbose;
return (size); return (size);
@ -2278,7 +2270,7 @@ sizecmd(argc, argv)
code = -1; code = -1;
return; return;
} }
size = remotesize(argv[1]); size = remotesize(argv[1], 1);
if (size != -1) if (size != -1)
printf("%s\t%qd\n", argv[1], size); printf("%s\t%qd\n", argv[1], size);
code = size; code = size;
@ -2288,8 +2280,9 @@ sizecmd(argc, argv)
* determine last modification time (in GMT) of remote file * determine last modification time (in GMT) of remote file
*/ */
time_t time_t
remotemodtime(file) remotemodtime(file, noisy)
const char *file; const char *file;
int noisy;
{ {
int overbose; int overbose;
time_t rtime; time_t rtime;
@ -2312,11 +2305,11 @@ remotemodtime(file)
timebuf.tm_year = yy - 1900; timebuf.tm_year = yy - 1900;
timebuf.tm_isdst = -1; timebuf.tm_isdst = -1;
rtime = mktime(&timebuf); rtime = mktime(&timebuf);
if (rtime == -1) if (rtime == -1 && (noisy || debug != 0))
printf("Can't convert %s to a time\n", reply_string); printf("Can't convert %s to a time\n", reply_string);
else else
rtime += timebuf.tm_gmtoff; /* conv. local -> GMT */ rtime += timebuf.tm_gmtoff; /* conv. local -> GMT */
} else if (debug != 0) } else if (noisy && debug == 0)
printf("%s\n", reply_string); printf("%s\n", reply_string);
verbose = overbose; verbose = overbose;
return(rtime); return(rtime);
@ -2337,7 +2330,7 @@ modtime(argc, argv)
code = -1; code = -1;
return; return;
} }
mtime = remotemodtime(argv[1]); mtime = remotemodtime(argv[1], 1);
if (mtime != -1) if (mtime != -1)
printf("%s\t%s", argv[1], asctime(localtime(&mtime))); printf("%s\t%s", argv[1], asctime(localtime(&mtime)));
code = mtime; code = mtime;

View File

@ -1,4 +1,4 @@
/* $NetBSD: extern.h,v 1.7 1996/12/06 02:06:48 lukem Exp $ */ /* $NetBSD: extern.h,v 1.8 1996/12/29 04:05:30 lukem Exp $ */
/*- /*-
* Copyright (c) 1994 The Regents of the University of California. * Copyright (c) 1994 The Regents of the University of California.
@ -106,8 +106,8 @@ void recvrequest __P((const char *, const char *, const char *,
const char *, int)); const char *, int));
void reget __P((int, char **)); void reget __P((int, char **));
char *remglob __P((char **, int)); char *remglob __P((char **, int));
off_t remotesize __P((const char *)); off_t remotesize __P((const char *, int));
time_t remotemodtime __P((const char *)); time_t remotemodtime __P((const char *, int));
void removedir __P((int, char **)); void removedir __P((int, char **));
void renamefile __P((int, char **)); void renamefile __P((int, char **));
void reset __P((int, char **)); void reset __P((int, char **));

View File

@ -1,4 +1,4 @@
/* $NetBSD: ftp.c,v 1.18 1996/12/25 16:02:06 christos Exp $ */ /* $NetBSD: ftp.c,v 1.19 1996/12/29 04:05:31 lukem Exp $ */
/* /*
* Copyright (c) 1985, 1989, 1993, 1994 * Copyright (c) 1985, 1989, 1993, 1994
@ -37,7 +37,7 @@
#if 0 #if 0
static char sccsid[] = "@(#)ftp.c 8.6 (Berkeley) 10/27/94"; static char sccsid[] = "@(#)ftp.c 8.6 (Berkeley) 10/27/94";
#else #else
static char rcsid[] = "$NetBSD: ftp.c,v 1.18 1996/12/25 16:02:06 christos Exp $"; static char rcsid[] = "$NetBSD: ftp.c,v 1.19 1996/12/29 04:05:31 lukem Exp $";
#endif #endif
#endif /* not lint */ #endif /* not lint */
@ -875,7 +875,7 @@ recvrequest(cmd, local, remote, lmode, printnames)
} else { } else {
if (curtype != type) if (curtype != type)
changetype(type, 0); changetype(type, 0);
filesize = remotesize(remote); filesize = remotesize(remote, 0);
} }
if (initconn()) { if (initconn()) {
(void) signal(SIGINT, oldintr); (void) signal(SIGINT, oldintr);
@ -1061,10 +1061,11 @@ break2:
(void) signal(SIGPIPE, oldintp); (void) signal(SIGPIPE, oldintp);
(void) fclose(din); (void) fclose(din);
(void) getreply(0); (void) getreply(0);
if (bytes > 0 && is_retr) { if (bytes >= 0 && is_retr) {
ptransfer(0); if (bytes > 0)
ptransfer(0);
if (preserve && (closefunc == fclose)) { if (preserve && (closefunc == fclose)) {
mtime = remotemodtime(remote); mtime = remotemodtime(remote, 0);
if (mtime != -1) { if (mtime != -1) {
(void) gettimeofday(&tval[0], (void) gettimeofday(&tval[0],
(struct timezone *)0); (struct timezone *)0);
@ -1124,13 +1125,13 @@ initconn()
if (passivemode) { if (passivemode) {
data = socket(AF_INET, SOCK_STREAM, 0); data = socket(AF_INET, SOCK_STREAM, 0);
if (data < 0) { if (data < 0) {
perror("ftp: socket"); warn("socket");
return(1); return(1);
} }
if ((options & SO_DEBUG) && if ((options & SO_DEBUG) &&
setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on, setsockopt(data, SOL_SOCKET, SO_DEBUG, (char *)&on,
sizeof (on)) < 0) sizeof (on)) < 0)
perror("ftp: setsockopt (ignored)"); warn("setsockopt (ignored)");
if (command("PASV") != COMPLETE) { if (command("PASV") != COMPLETE) {
printf("Passive mode refused.\n"); printf("Passive mode refused.\n");
goto bad; goto bad;
@ -1164,14 +1165,14 @@ initconn()
if (connect(data, (struct sockaddr *)&data_addr, if (connect(data, (struct sockaddr *)&data_addr,
sizeof(data_addr)) < 0) { sizeof(data_addr)) < 0) {
perror("ftp: connect"); warn("connect");
goto bad; goto bad;
} }
#ifdef IP_TOS #ifdef IP_TOS
on = IPTOS_THROUGHPUT; on = IPTOS_THROUGHPUT;
if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on, if (setsockopt(data, IPPROTO_IP, IP_TOS, (char *)&on,
sizeof(int)) < 0) sizeof(int)) < 0)
perror("ftp: setsockopt TOS (ignored)"); warn("setsockopt TOS (ignored)");
#endif #endif
return(0); return(0);
} }
@ -1303,9 +1304,9 @@ progressmeter(flag)
ratio = cursize * 100 / filesize; ratio = cursize * 100 / filesize;
ratio = MAX(ratio, 0); ratio = MAX(ratio, 0);
ratio = MIN(ratio, 100); ratio = MIN(ratio, 100);
printf("\r%3d%% 0 ", ratio); printf("\r%3d%% ", ratio);
barlength = ttywidth - 30; barlength = ttywidth - 32;
if (barlength > 0) { if (barlength > 0) {
i = barlength * ratio / 100; i = barlength * ratio / 100;
printf("%.*s%*s", i, printf("%.*s%*s", i,

View File

@ -1,4 +1,4 @@
/* $NetBSD: main.c,v 1.14 1996/12/06 02:06:52 lukem Exp $ */ /* $NetBSD: main.c,v 1.15 1996/12/29 04:05:32 lukem Exp $ */
/* /*
* Copyright (c) 1985, 1989, 1993, 1994 * Copyright (c) 1985, 1989, 1993, 1994
@ -43,7 +43,7 @@ static char copyright[] =
#if 0 #if 0
static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 10/9/94"; static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 10/9/94";
#else #else
static char rcsid[] = "$NetBSD: main.c,v 1.14 1996/12/06 02:06:52 lukem Exp $"; static char rcsid[] = "$NetBSD: main.c,v 1.15 1996/12/29 04:05:32 lukem Exp $";
#endif #endif
#endif /* not lint */ #endif /* not lint */
@ -93,6 +93,11 @@ main(argc, argv)
preserve = 1; preserve = 1;
mark = HASHBYTES; mark = HASHBYTES;
cp = strrchr(argv[0], '/');
cp = (cp == NULL) ? argv[0] : cp + 1;
if (strcmp(cp, "pftp") == 0)
passivemode = 1;
while ((ch = getopt(argc, argv, "adginpP:tv")) != EOF) { while ((ch = getopt(argc, argv, "adginpP:tv")) != EOF) {
switch (ch) { switch (ch) {
case 'a': case 'a':
@ -586,7 +591,7 @@ auto_fetch(argc, argv)
/* /*
* Loop through as long as there's files to fetch. * Loop through as long as there's files to fetch.
*/ */
while (argc > 0 && strchr(argv[0], ':') != NULL) { for ( ; argc > 0 && strchr(argv[0], ':') != NULL ; --argc, argv++) {
host = dir = file = portnum = NULL; host = dir = file = portnum = NULL;
/* /*
@ -673,8 +678,6 @@ auto_fetch(argc, argv)
disconnect(0, NULL); disconnect(0, NULL);
free(host); free(host);
--argc, ++argv;
} }
return (rval); return (rval);

View File

@ -1,4 +1,4 @@
/* $NetBSD: pathnames.h,v 1.5 1995/09/08 01:06:40 tls Exp $ */ /* $NetBSD: pathnames.h,v 1.6 1996/12/29 04:05:32 lukem Exp $ */
/* /*
* Copyright (c) 1989, 1993 * Copyright (c) 1989, 1993
@ -37,5 +37,4 @@
#include <paths.h> #include <paths.h>
#undef _PATH_TMP #define TMPFILE "ftpXXXXXX"
#define _PATH_TMP "/tmp/ftpXXXXXX"