diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 1998-02-17 23:22:57 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 1998-02-17 23:22:57 +0000 |
commit | 921d6e7e0ac5e0aaff789114a7d2d6e6ff55f05b (patch) | |
tree | 4d501c4a140e71a1b8f3ac7fdd574c154d3c7d71 /usr.bin | |
parent | 4bf25be6f082822cb2e8f1578e000cb51110b1ff (diff) |
Add a new option "-o filename" for autofetch mode. Allows use of
"-" to mean stdout. This replaces the old (unreliable) hueristic
of checking if stdout is a tty and if not make the output go to stdout.
Diffstat (limited to 'usr.bin')
-rw-r--r-- | usr.bin/ftp/extern.h | 4 | ||||
-rw-r--r-- | usr.bin/ftp/fetch.c | 41 | ||||
-rw-r--r-- | usr.bin/ftp/ftp.1 | 12 | ||||
-rw-r--r-- | usr.bin/ftp/main.c | 21 |
4 files changed, 45 insertions, 33 deletions
diff --git a/usr.bin/ftp/extern.h b/usr.bin/ftp/extern.h index 1cfa300cb07..abee8241188 100644 --- a/usr.bin/ftp/extern.h +++ b/usr.bin/ftp/extern.h @@ -1,4 +1,4 @@ -/* $OpenBSD: extern.h,v 1.16 1997/09/04 04:37:14 millert Exp $ */ +/* $OpenBSD: extern.h,v 1.17 1998/02/17 23:22:53 millert Exp $ */ /* $NetBSD: extern.h,v 1.17 1997/08/18 10:20:19 lukem Exp $ */ /*- @@ -45,7 +45,7 @@ void abortsend __P((int)); void account __P((int, char **)); void alarmtimer __P((int)); int another __P((int *, char ***, const char *)); -int auto_fetch __P((int, char **, int)); +int auto_fetch __P((int, char **, char *)); void blkfree __P((char **)); void cd __P((int, char **)); void cdup __P((int, char **)); diff --git a/usr.bin/ftp/fetch.c b/usr.bin/ftp/fetch.c index 06973bc5797..ea12fe841c0 100644 --- a/usr.bin/ftp/fetch.c +++ b/usr.bin/ftp/fetch.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fetch.c,v 1.16 1997/09/11 01:55:15 millert Exp $ */ +/* $OpenBSD: fetch.c,v 1.17 1998/02/17 23:22:54 millert Exp $ */ /* $NetBSD: fetch.c,v 1.14 1997/08/18 10:20:20 lukem Exp $ */ /*- @@ -38,7 +38,7 @@ */ #ifndef lint -static char rcsid[] = "$OpenBSD: fetch.c,v 1.16 1997/09/11 01:55:15 millert Exp $"; +static char rcsid[] = "$OpenBSD: fetch.c,v 1.17 1998/02/17 23:22:54 millert Exp $"; #endif /* not lint */ /* @@ -56,6 +56,7 @@ static char rcsid[] = "$OpenBSD: fetch.c,v 1.16 1997/09/11 01:55:15 millert Exp #include <ctype.h> #include <err.h> +#include <libgen.h> #include <netdb.h> #include <fcntl.h> #include <signal.h> @@ -66,7 +67,7 @@ static char rcsid[] = "$OpenBSD: fetch.c,v 1.16 1997/09/11 01:55:15 millert Exp #include "ftp_var.h" -static int url_get __P((const char *, const char *, int)); +static int url_get __P((const char *, const char *, const char *)); void aborthttp __P((int)); @@ -86,10 +87,10 @@ jmp_buf httpabort; * Returns -1 on failure, 0 on success */ static int -url_get(origline, proxyenv, fd) +url_get(origline, proxyenv, outfile) const char *origline; const char *proxyenv; - int fd; + const char *outfile; { struct sockaddr_in sin; int i, out, isftpurl; @@ -138,11 +139,11 @@ url_get(origline, proxyenv, fd) goto cleanup_url_get; } - savefile = strrchr(path, '/'); /* find savefile */ - if (savefile != NULL) - savefile++; + if (outfile) + savefile = outfile; else - savefile = path; + savefile = basename(path); + if (EMPTYSTRING(savefile)) { if (isftpurl) goto noftpautologin; @@ -302,15 +303,15 @@ url_get(origline, proxyenv, fd) } else filesize = -1; - /* Open the output file. */ - if (fd == -1) { + /* Open the output file. */ + if (strcmp(savefile, "-") != 0) { out = open(savefile, O_CREAT | O_WRONLY | O_TRUNC, 0666); if (out < 0) { warn("Can't open %s", savefile); goto cleanup_url_get; } } else - out = fd; + out = fileno(stdout); /* Trap signals */ oldintr = NULL; @@ -361,7 +362,7 @@ url_get(origline, proxyenv, fd) (void)signal(SIGINT, oldintr); close(s); - if (fd != -1) + if (out != fileno(stdout)) close(out); if (proxy) free(proxy); @@ -414,10 +415,10 @@ aborthttp(notused) * Otherwise, 0 is returned if all files retrieved successfully. */ int -auto_fetch(argc, argv, fd) +auto_fetch(argc, argv, outfile) int argc; char *argv[]; - int fd; + char *outfile; { static char lasthost[MAXHOSTNAMELEN]; char *xargv[5]; @@ -461,7 +462,7 @@ auto_fetch(argc, argv, fd) * Try HTTP URL-style arguments first. */ if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0) { - if (url_get(line, httpproxy, fd) == -1) + if (url_get(line, httpproxy, outfile) == -1) rval = argpos + 1; continue; } @@ -474,7 +475,7 @@ auto_fetch(argc, argv, fd) host = line; if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) { if (ftpproxy) { - if (url_get(line, ftpproxy, fd) == -1) + if (url_get(line, ftpproxy, outfile) == -1) rval = argpos + 1; continue; } @@ -638,11 +639,11 @@ parsed_url: mget(xargc, xargv); interactive = ointeractive; } else { - if (fd != -1) { - xargv[2] = "-"; + if (outfile != NULL) { + xargv[2] = outfile; + xargv[3] = NULL; xargc++; } - xargv[3] = NULL; get(xargc, xargv); } diff --git a/usr.bin/ftp/ftp.1 b/usr.bin/ftp/ftp.1 index dd9f5258818..624d243ea1b 100644 --- a/usr.bin/ftp/ftp.1 +++ b/usr.bin/ftp/ftp.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: ftp.1,v 1.16 1997/12/17 16:03:01 millert Exp $ +.\" $OpenBSD: ftp.1,v 1.17 1998/02/17 23:22:55 millert Exp $ .\" $NetBSD: ftp.1,v 1.22 1997/08/18 10:20:22 lukem Exp $ .\" .\" Copyright (c) 1985, 1989, 1990, 1993 @@ -51,6 +51,7 @@ file transfer program .Op Fl g .Op Fl i .Op Fl n +.Op Fl o Ar output .Op Fl p .Op Fl P Ar port .Op Fl r Ar seconds @@ -119,6 +120,15 @@ If no entry exists, will prompt for the remote machine login name (default is the user identity on the local machine), and, if necessary, prompt for a password and an account with which to login. +.It Fl o Ar output +When fetching a single file or url, save the contents in +.Ar output . +To make the contents go to +.Ar stdout , +use +.Qq - +for +.Ar output . .It Fl p Enable passive mode operation for use behind connection filtering firewalls. This option has been deprecated as diff --git a/usr.bin/ftp/main.c b/usr.bin/ftp/main.c index b09fc7aaba0..e25927d7ceb 100644 --- a/usr.bin/ftp/main.c +++ b/usr.bin/ftp/main.c @@ -1,4 +1,4 @@ -/* $OpenBSD: main.c,v 1.37 1997/12/17 16:03:05 millert Exp $ */ +/* $OpenBSD: main.c,v 1.38 1998/02/17 23:22:56 millert Exp $ */ /* $NetBSD: main.c,v 1.24 1997/08/18 10:20:26 lukem Exp $ */ /* @@ -44,7 +44,7 @@ static char copyright[] = #if 0 static char sccsid[] = "@(#)main.c 8.6 (Berkeley) 10/9/94"; #else -static char rcsid[] = "$OpenBSD: main.c,v 1.37 1997/12/17 16:03:05 millert Exp $"; +static char rcsid[] = "$OpenBSD: main.c,v 1.38 1998/02/17 23:22:56 millert Exp $"; #endif #endif /* not lint */ @@ -77,8 +77,8 @@ main(argc, argv) long port; struct passwd *pw = NULL; char *cp, *ep, homedir[MAXPATHLEN]; + char *outfile = NULL; int dumb_terminal = 0; - int outfd = -1; sp = getservbyname("ftp", "tcp"); if (sp == 0) @@ -170,12 +170,7 @@ main(argc, argv) if (isatty(fileno(ttyout)) && !dumb_terminal && foregroundproc()) progress = 1; /* progress bar on if tty is usable */ - if (!isatty(fileno(ttyout))) { - outfd = fileno(stdout); - ttyout = stderr; - } - - while ((ch = getopt(argc, argv, "AadeginpPr:tvV")) != -1) { + while ((ch = getopt(argc, argv, "Aadegino:pPr:tvV")) != -1) { switch (ch) { case 'A': activefallback = 0; @@ -209,6 +204,12 @@ main(argc, argv) autologin = 0; break; + case 'o': + outfile = optarg; + if (strcmp(outfile, "-") == 0) + ttyout = stderr; + break; + case 'p': passivemode = 1; activefallback = 0; @@ -277,7 +278,7 @@ main(argc, argv) if (argc > 0) { if (strchr(argv[0], ':') != NULL) { anonftp = 1; /* Handle "automatic" transfers. */ - rval = auto_fetch(argc, argv, outfd); + rval = auto_fetch(argc, argv, outfile); if (rval >= 0) /* -1 == connected and cd-ed */ exit(rval); } else { |