summaryrefslogtreecommitdiff
path: root/usr.bin
diff options
context:
space:
mode:
authorTodd C. Miller <millert@cvs.openbsd.org>2004-08-23 17:56:21 +0000
committerTodd C. Miller <millert@cvs.openbsd.org>2004-08-23 17:56:21 +0000
commit9c9d2339d1028d158c66fb7b82749108af808852 (patch)
tree86e9e702179e384262dfe56afc77d3af1d75d71f /usr.bin
parent374e45ee0438eee8c62b6409b14da6f9f3d3da46 (diff)
Do %s replacement for LESSOPEN and LESSCLOSE ourselves instead
of using snprintf(). Based on a patch from Brent Graveland. Ok tdeval@ and otto@
Diffstat (limited to 'usr.bin')
-rw-r--r--usr.bin/less/filename.c40
1 files changed, 32 insertions, 8 deletions
diff --git a/usr.bin/less/filename.c b/usr.bin/less/filename.c
index a46df09e77e..4e76843c95b 100644
--- a/usr.bin/less/filename.c
+++ b/usr.bin/less/filename.c
@@ -824,9 +824,10 @@ open_altfile(filename, pf, pfd)
return (NULL);
#else
char *lessopen;
- char *cmd;
+ char *cmd, *cp;
FILE *fd;
- size_t len;
+ size_t i, len;
+ int found;
#if HAVE_FILENO
int returnfd = 0;
#endif
@@ -853,9 +854,18 @@ open_altfile(filename, pf, pfd)
#endif
}
- len = strlen(lessopen) + strlen(filename) + 2;
+ /* strlen(filename) is guaranteed to be > 0 */
+ len = strlen(lessopen) + strlen(filename);
cmd = (char *) ecalloc(len, sizeof(char));
- snprintf(cmd, len, lessopen, filename);
+ for (cp = cmd, i = 0, found = 0; i < strlen(lessopen); i++) {
+ if (!found && lessopen[i] == '%' && lessopen[i + 1] == 's') {
+ found = 1;
+ strlcat(cmd, filename, len);
+ cp += strlen(filename);
+ i++;
+ } else
+ *cp++ = lessopen[i];
+ }
fd = shellcmd(cmd);
free(cmd);
if (fd == NULL)
@@ -914,8 +924,9 @@ close_altfile(altfilename, filename, pipefd)
#if HAVE_POPEN
char *lessclose;
FILE *fd;
- char *cmd;
- size_t len;
+ char *cmd, *cp;
+ size_t i, len;
+ int found;
if (secure)
return;
@@ -932,9 +943,22 @@ close_altfile(altfilename, filename, pipefd)
}
if ((lessclose = lgetenv("LESSCLOSE")) == NULL)
return;
- len = strlen(lessclose) + strlen(filename) + strlen(altfilename) + 2;
+ /* strlen(filename) is guaranteed to be > 0 */
+ len = strlen(lessclose) + strlen(filename) + strlen(altfilename);
cmd = (char *) ecalloc(len, sizeof(char));
- snprintf(cmd, len, lessclose, filename, altfilename);
+ for (cp = cmd, i = 0, found = 0; i < strlen(lessclose); i++) {
+ if (found < 2 && lessclose[i] == '%' && lessclose[i + 1] == 's') {
+ if (++found == 1) {
+ strlcat(cmd, filename, len);
+ cp += strlen(filename);
+ } else {
+ strlcat(cmd, altfilename, len);
+ cp += strlen(altfilename);
+ }
+ i++;
+ } else
+ *cp++ = lessclose[i];
+ }
fd = shellcmd(cmd);
free(cmd);
if (fd != NULL)