diff options
Diffstat (limited to 'contrib/less/filename.c')
-rw-r--r-- | contrib/less/filename.c | 54 |
1 files changed, 32 insertions, 22 deletions
diff --git a/contrib/less/filename.c b/contrib/less/filename.c index 0b56bdc..8b1c4c9 100644 --- a/contrib/less/filename.c +++ b/contrib/less/filename.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 1984-2002 Mark Nudelman + * Copyright (C) 1984-2005 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. @@ -195,7 +195,7 @@ shell_quote(s) newstr = p = (char *) ecalloc(len, sizeof(char)); if (use_quotes) { - sprintf(newstr, "%c%s%c", openquote, s, closequote); + SNPRINTF3(newstr, len, "%c%s%c", openquote, s, closequote); } else { while (*s != '\0') @@ -226,6 +226,7 @@ dirfile(dirname, filename) { char *pathname; char *qpathname; + int len; int f; if (dirname == NULL || *dirname == '\0') @@ -233,11 +234,11 @@ dirfile(dirname, filename) /* * Construct the full pathname. */ - pathname = (char *) calloc(strlen(dirname) + strlen(filename) + 2, - sizeof(char)); + len= strlen(dirname) + strlen(filename) + 2; + pathname = (char *) calloc(len, sizeof(char)); if (pathname == NULL) return (NULL); - sprintf(pathname, "%s%s%s", dirname, PATHNAME_SEP, filename); + SNPRINTF3(pathname, len, "%s%s%s", dirname, PATHNAME_SEP, filename); /* * Make sure the file exists. */ @@ -425,18 +426,23 @@ fcomplete(s) */ { char *slash; + int len; for (slash = s+strlen(s)-1; slash > s; slash--) if (*slash == *PATHNAME_SEP || *slash == '/') break; - fpat = (char *) ecalloc(strlen(s)+4, sizeof(char)); + len = strlen(s) + 4; + fpat = (char *) ecalloc(len, sizeof(char)); if (strchr(slash, '.') == NULL) - sprintf(fpat, "%s*.*", s); + SNPRINTF1(fpat, len, "%s*.*", s); else - sprintf(fpat, "%s*", s); + SNPRINTF1(fpat, len, "%s*", s); } #else - fpat = (char *) ecalloc(strlen(s)+2, sizeof(char)); - sprintf(fpat, "%s*", s); + { + int len = strlen(s) + 2; + fpat = (char *) ecalloc(len, sizeof(char)); + SNPRINTF1(fpat, len, "%s*", s); + } #endif qs = lglob(fpat); s = shell_unquote(qs); @@ -570,9 +576,9 @@ shellcmd(cmd) fd = popen(cmd, "r"); } else { - scmd = (char *) ecalloc(strlen(shell) + strlen(esccmd) + 5, - sizeof(char)); - sprintf(scmd, "%s %s %s", shell, shell_coption(), esccmd); + int len = strlen(shell) + strlen(esccmd) + 5; + scmd = (char *) ecalloc(len, sizeof(char)); + SNPRINTF3(scmd, len, "%s %s %s", shell, shell_coption(), esccmd); free(esccmd); fd = popen(scmd, "r"); free(scmd); @@ -680,7 +686,7 @@ lglob(filename) do { n = strlen(drive) + strlen(dir) + strlen(fnd.GLOB_NAME) + 1; pathname = (char *) ecalloc(n, sizeof(char)); - sprintf(pathname, "%s%s%s", drive, dir, fnd.GLOB_NAME); + SNPRINTF3(pathname, n, "%s%s%s", drive, dir, fnd.GLOB_NAME); qpathname = shell_quote(pathname); free(pathname); if (qpathname != NULL) @@ -725,6 +731,7 @@ lglob(filename) char *lessecho; char *cmd; char *esc; + int len; esc = get_meta_escape(); if (strlen(esc) == 0) @@ -741,8 +748,9 @@ lglob(filename) /* * Invoke lessecho, and read its output (a globbed list of filenames). */ - cmd = (char *) ecalloc(strlen(lessecho) + strlen(ofilename) + (7*strlen(metachars())) + 24, sizeof(char)); - sprintf(cmd, "%s -p0x%x -d0x%x -e%s ", lessecho, openquote, closequote, esc); + len = strlen(lessecho) + strlen(ofilename) + (7*strlen(metachars())) + 24; + cmd = (char *) ecalloc(len, sizeof(char)); + SNPRINTF4(cmd, len, "%s -p0x%x -d0x%x -e%s ", lessecho, openquote, closequote, esc); free(esc); for (s = metachars(); *s != '\0'; s++) sprintf(cmd + strlen(cmd), "-n0x%x ", *s); @@ -795,6 +803,7 @@ open_altfile(filename, pf, pfd) #else char *lessopen; char *cmd; + int len; FILE *fd; #if HAVE_FILENO int returnfd = 0; @@ -822,9 +831,9 @@ open_altfile(filename, pf, pfd) #endif } - cmd = (char *) ecalloc(strlen(lessopen) + strlen(filename) + 2, - sizeof(char)); - sprintf(cmd, lessopen, filename); + len = strlen(lessopen) + strlen(filename) + 2; + cmd = (char *) ecalloc(len, sizeof(char)); + SNPRINTF1(cmd, len, lessopen, filename); fd = shellcmd(cmd); free(cmd); if (fd == NULL) @@ -884,6 +893,7 @@ close_altfile(altfilename, filename, pipefd) char *lessclose; FILE *fd; char *cmd; + int len; if (secure) return; @@ -900,9 +910,9 @@ close_altfile(altfilename, filename, pipefd) } if ((lessclose = lgetenv("LESSCLOSE")) == NULL) return; - cmd = (char *) ecalloc(strlen(lessclose) + strlen(filename) + - strlen(altfilename) + 2, sizeof(char)); - sprintf(cmd, lessclose, filename, altfilename); + len = strlen(lessclose) + strlen(filename) + strlen(altfilename) + 2; + cmd = (char *) ecalloc(len, sizeof(char)); + SNPRINTF2(cmd, len, lessclose, filename, altfilename); fd = shellcmd(cmd); free(cmd); if (fd != NULL) |