diff options
author | delphij <delphij@FreeBSD.org> | 2012-06-26 23:17:33 +0000 |
---|---|---|
committer | delphij <delphij@FreeBSD.org> | 2012-06-26 23:17:33 +0000 |
commit | 922d40fed4b7f6bb0aafe327600825f9680ce0e7 (patch) | |
tree | 1acc67942c59e279c426e0a72ee0ad94cb0ed3ba /contrib/less/filename.c | |
parent | bd43c5e6d640101f1bd6cca7e83586ae41e1a065 (diff) | |
download | FreeBSD-src-922d40fed4b7f6bb0aafe327600825f9680ce0e7.zip FreeBSD-src-922d40fed4b7f6bb0aafe327600825f9680ce0e7.tar.gz |
MFV: less v449.
Diffstat (limited to 'contrib/less/filename.c')
-rw-r--r-- | contrib/less/filename.c | 65 |
1 files changed, 52 insertions, 13 deletions
diff --git a/contrib/less/filename.c b/contrib/less/filename.c index 0bbe82a..e99f81a 100644 --- a/contrib/less/filename.c +++ b/contrib/less/filename.c @@ -1,12 +1,11 @@ -/* - * Copyright (C) 1984-2011 Mark Nudelman - * - * You may distribute under the terms of either the GNU General Public - * License or the Less License, as specified in the README file. - * - * For more information about less, or for information on how to - * contact the author, see the README file. - */ +/*
+ * Copyright (C) 1984-2012 Mark Nudelman
+ *
+ * You may distribute under the terms of either the GNU General Public
+ * License or the Less License, as specified in the README file.
+ *
+ * For more information, see the README file.
+ */
/* @@ -807,6 +806,27 @@ lglob(filename) } /* + * Return number of %s escapes in a string. + * Return a large number if there are any other % escapes besides %s. + */ + static int +num_pct_s(lessopen) + char *lessopen; +{ + int num; + + for (num = 0;; num++) + { + lessopen = strchr(lessopen, '%'); + if (lessopen == NULL) + break; + if (*++lessopen != 's') + return (999); + } + return (num); +} + +/* * See if we should open a "replacement file" * instead of the file we're about to open. */ @@ -832,7 +852,7 @@ open_altfile(filename, pf, pfd) ch_ungetchar(-1); if ((lessopen = lgetenv("LESSOPEN")) == NULL) return (NULL); - if (*lessopen == '|') + while (*lessopen == '|') { /* * If LESSOPEN starts with a |, it indicates @@ -843,7 +863,7 @@ open_altfile(filename, pf, pfd) return (NULL); #else lessopen++; - returnfd = 1; + returnfd++; #endif } if (*lessopen == '-') { @@ -855,6 +875,11 @@ open_altfile(filename, pf, pfd) if (strcmp(filename, "-") == 0) return (NULL); } + if (num_pct_s(lessopen) > 1) + { + error("Invalid LESSOPEN variable", NULL_PARG); + return (NULL); + } len = strlen(lessopen) + strlen(filename) + 2; cmd = (char *) ecalloc(len, sizeof(char)); @@ -883,9 +908,18 @@ open_altfile(filename, pf, pfd) if (read(f, &c, 1) != 1) { /* - * Pipe is empty. This means there is no alt file. + * Pipe is empty. + * If more than 1 pipe char was specified, + * the exit status tells whether the file itself + * is empty, or if there is no alt file. + * If only one pipe char, just assume no alt file. */ - pclose(fd); + int status = pclose(fd); + if (returnfd > 1 && status == 0) { + *pfd = NULL; + *pf = -1; + return (save(FAKE_EMPTYFILE)); + } return (NULL); } ch_ungetchar(c); @@ -935,6 +969,11 @@ close_altfile(altfilename, filename, pipefd) } if ((lessclose = lgetenv("LESSCLOSE")) == NULL) return; + if (num_pct_s(lessclose) > 2) + { + error("Invalid LESSCLOSE variable"); + return; + } len = strlen(lessclose) + strlen(filename) + strlen(altfilename) + 2; cmd = (char *) ecalloc(len, sizeof(char)); SNPRINTF2(cmd, len, lessclose, filename, altfilename); |