version 1.1.1.3, 2011/09/16 17:47:04 |
version 1.1.1.4, 2014/04/25 13:33:46 |
|
|
/* |
/* |
* Copyright (C) 1984-2011 Mark Nudelman |
* Copyright (C) 1984-2012 Mark Nudelman |
* |
* |
* You may distribute under the terms of either the GNU General Public |
* You may distribute under the terms of either the GNU General Public |
* License or the Less License, as specified in the README file. |
* License or the Less License, as specified in the README file. |
* |
* |
* For more information about less, or for information on how to |
* For more information, see the README file. |
* contact the author, see the README file. |
|
*/ |
*/ |
|
|
|
|
|
|
/* |
/* |
* Add the escape char. |
* Add the escape char. |
*/ |
*/ |
strcpy(p, esc); |
strlcpy(p, esc, newstr + len - p); |
p += esclen; |
p += esclen; |
} |
} |
*p++ = *s++; |
*p++ = *s++; |
|
|
{ |
{ |
char *pathname; |
char *pathname; |
char *qpathname; |
char *qpathname; |
int len; |
size_t len; |
int f; |
int f; |
|
|
if (dirname == NULL || *dirname == '\0') |
if (dirname == NULL || *dirname == '\0') |
|
|
return (pathname); |
return (pathname); |
} |
} |
|
|
|
#if USERFILE |
/* |
/* |
* Return the full pathname of the given file in the "home directory". |
* Return the full pathname of the given file in the "home directory". |
*/ |
*/ |
|
|
if (res == 0) |
if (res == 0) |
*pathname = '\0'; |
*pathname = '\0'; |
else |
else |
strcpy(pathname, res); |
strlcpy(pathname, res, _MAX_PATH); |
} |
} |
#else |
#else |
_searchenv(filename, "PATH", pathname); |
_searchenv(filename, "PATH", pathname); |
|
|
#endif |
#endif |
return (NULL); |
return (NULL); |
} |
} |
|
#endif /* USERFILE */ |
|
|
/* |
/* |
* Expand a string, substituting any "%" with the current filename, |
* Expand a string, substituting any "%" with the current filename, |
|
|
*to++ = *fr; |
*to++ = *fr; |
else |
else |
{ |
{ |
strcpy(to, get_filename(ifile)); |
strlcpy(to, get_filename(ifile), |
|
e + n + 1 - to); |
to += strlen(to); |
to += strlen(to); |
} |
} |
} |
} |
|
|
{ |
{ |
char *fpat; |
char *fpat; |
char *qs; |
char *qs; |
|
size_t len; |
|
|
if (secure) |
if (secure) |
return (NULL); |
return (NULL); |
|
|
*/ |
*/ |
{ |
{ |
char *slash; |
char *slash; |
int len; |
|
for (slash = s+strlen(s)-1; slash > s; slash--) |
for (slash = s+strlen(s)-1; slash > s; slash--) |
if (*slash == *PATHNAME_SEP || *slash == '/') |
if (*slash == *PATHNAME_SEP || *slash == '/') |
break; |
break; |
|
|
} |
} |
#else |
#else |
{ |
{ |
int len = strlen(s) + 2; |
len = strlen(s) + 2; |
fpat = (char *) ecalloc(len, sizeof(char)); |
fpat = (char *) ecalloc(len, sizeof(char)); |
SNPRINTF1(fpat, len, "%s*", s); |
SNPRINTF1(fpat, len, "%s*", s); |
} |
} |
|
|
len *= 2; |
len *= 2; |
*p = '\0'; |
*p = '\0'; |
p = (char *) ecalloc(len, sizeof(char)); |
p = (char *) ecalloc(len, sizeof(char)); |
strcpy(p, buf); |
strlcpy(p, buf, len); |
free(buf); |
free(buf); |
buf = p; |
buf = p; |
p = buf + strlen(buf); |
p = buf + strlen(buf); |
|
|
fd = popen(cmd, "r"); |
fd = popen(cmd, "r"); |
} else |
} else |
{ |
{ |
int len = strlen(shell) + strlen(esccmd) + 5; |
size_t len = strlen(shell) + strlen(esccmd) + 5; |
scmd = (char *) ecalloc(len, sizeof(char)); |
scmd = (char *) ecalloc(len, sizeof(char)); |
SNPRINTF3(scmd, len, "%s %s %s", shell, shell_coption(), esccmd); |
SNPRINTF3(scmd, len, "%s %s %s", shell, shell_coption(), esccmd); |
free(esccmd); |
free(esccmd); |
|
|
#endif /* HAVE_POPEN */ |
#endif /* HAVE_POPEN */ |
|
|
|
|
|
#if !SMALL |
/* |
/* |
* Expand a filename, doing any system-specific metacharacter substitutions. |
* Expand a filename, doing any system-specific metacharacter substitutions. |
*/ |
*/ |
|
|
qfilename = shell_quote(p); |
qfilename = shell_quote(p); |
if (qfilename != NULL) |
if (qfilename != NULL) |
{ |
{ |
sprintf(gfilename + strlen(gfilename), "%s ", qfilename); |
snprintf(gfilename + strlen(gfilename), |
|
length - strlen(gfilename), "%s ", qfilename); |
free(qfilename); |
free(qfilename); |
} |
} |
} |
} |
/* |
/* |
* Overwrite the final trailing space with a null terminator. |
* Overwrite the final trailing space with a null terminator. |
*/ |
*/ |
*--p = '\0'; |
if (gfilename[0] != '\0' && gfilename[strlen(gfilename) - 1] == ' ') |
|
gfilename[strlen(gfilename) - 1] = '\0'; |
GLOB_LIST_DONE(list); |
GLOB_LIST_DONE(list); |
} |
} |
#else |
#else |
|
|
len *= 2; |
len *= 2; |
*p = '\0'; |
*p = '\0'; |
p = (char *) ecalloc(len, sizeof(char)); |
p = (char *) ecalloc(len, sizeof(char)); |
strcpy(p, gfilename); |
strlcpy(p, gfilename, len); |
free(gfilename); |
free(gfilename); |
gfilename = p; |
gfilename = p; |
p = gfilename + strlen(gfilename); |
p = gfilename + strlen(gfilename); |
} |
} |
strcpy(p, qpathname); |
strlcpy(p, qpathname, gfilename + len - p); |
free(qpathname); |
free(qpathname); |
p += n; |
p += n; |
*p++ = ' '; |
*p++ = ' '; |
|
|
char *lessecho; |
char *lessecho; |
char *cmd; |
char *cmd; |
char *esc; |
char *esc; |
int len; |
size_t len; |
|
|
esc = get_meta_escape(); |
esc = get_meta_escape(); |
if (strlen(esc) == 0) |
if (strlen(esc) == 0) |
|
|
SNPRINTF4(cmd, len, "%s -p0x%x -d0x%x -e%s ", lessecho, openquote, closequote, esc); |
SNPRINTF4(cmd, len, "%s -p0x%x -d0x%x -e%s ", lessecho, openquote, closequote, esc); |
free(esc); |
free(esc); |
for (s = metachars(); *s != '\0'; s++) |
for (s = metachars(); *s != '\0'; s++) |
sprintf(cmd + strlen(cmd), "-n0x%x ", *s); |
snprintf(cmd + strlen(cmd), len - strlen(cmd), "-n0x%x ", *s); |
sprintf(cmd + strlen(cmd), "-- %s", ofilename); |
snprintf(cmd + strlen(cmd), len - strlen(cmd), "-- %s", ofilename); |
fd = shellcmd(cmd); |
fd = shellcmd(cmd); |
free(cmd); |
free(cmd); |
if (fd == NULL) |
if (fd == NULL) |
|
|
free(ofilename); |
free(ofilename); |
return (gfilename); |
return (gfilename); |
} |
} |
|
#endif /* !SMALL */ |
|
|
/* |
/* |
|
* 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" |
* See if we should open a "replacement file" |
* instead of the file we're about to open. |
* instead of the file we're about to open. |
*/ |
*/ |
|
|
#else |
#else |
char *lessopen; |
char *lessopen; |
char *cmd; |
char *cmd; |
int len; |
size_t len; |
FILE *fd; |
FILE *fd; |
#if HAVE_FILENO |
#if HAVE_FILENO |
int returnfd = 0; |
int returnfd = 0; |
|
|
ch_ungetchar(-1); |
ch_ungetchar(-1); |
if ((lessopen = lgetenv("LESSOPEN")) == NULL) |
if ((lessopen = lgetenv("LESSOPEN")) == NULL) |
return (NULL); |
return (NULL); |
if (*lessopen == '|') |
while (*lessopen == '|') |
{ |
{ |
/* |
/* |
* If LESSOPEN starts with a |, it indicates |
* If LESSOPEN starts with a |, it indicates |
|
|
return (NULL); |
return (NULL); |
#else |
#else |
lessopen++; |
lessopen++; |
returnfd = 1; |
returnfd++; |
#endif |
#endif |
} |
} |
if (*lessopen == '-') { |
if (*lessopen == '-') { |
|
|
if (strcmp(filename, "-") == 0) |
if (strcmp(filename, "-") == 0) |
return (NULL); |
return (NULL); |
} |
} |
|
if (num_pct_s(lessopen) > 1) |
|
{ |
|
error("Invalid LESSOPEN variable", NULL_PARG); |
|
return (NULL); |
|
} |
|
|
len = strlen(lessopen) + strlen(filename) + 2; |
len = strlen(lessopen) + strlen(filename) + 2; |
cmd = (char *) ecalloc(len, sizeof(char)); |
cmd = (char *) ecalloc(len, sizeof(char)); |
|
|
if (read(f, &c, 1) != 1) |
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); |
return (NULL); |
} |
} |
ch_ungetchar(c); |
ch_ungetchar(c); |
|
|
char *lessclose; |
char *lessclose; |
FILE *fd; |
FILE *fd; |
char *cmd; |
char *cmd; |
int len; |
size_t len; |
|
|
if (secure) |
if (secure) |
return; |
return; |
|
|
} |
} |
if ((lessclose = lgetenv("LESSCLOSE")) == NULL) |
if ((lessclose = lgetenv("LESSCLOSE")) == NULL) |
return; |
return; |
|
if (num_pct_s(lessclose) > 2) |
|
{ |
|
error("Invalid LESSCLOSE variable"); |
|
return; |
|
} |
len = strlen(lessclose) + strlen(filename) + strlen(altfilename) + 2; |
len = strlen(lessclose) + strlen(filename) + strlen(altfilename) + 2; |
cmd = (char *) ecalloc(len, sizeof(char)); |
cmd = (char *) ecalloc(len, sizeof(char)); |
SNPRINTF2(cmd, len, lessclose, filename, altfilename); |
SNPRINTF2(cmd, len, lessclose, filename, altfilename); |
|
|
char *filename; |
char *filename; |
{ |
{ |
register char *m = NULL; |
register char *m = NULL; |
|
size_t len; |
|
|
filename = shell_unquote(filename); |
filename = shell_unquote(filename); |
if (!force_open && is_dir(filename)) |
if (!force_open && is_dir(filename)) |
{ |
{ |
static char is_a_dir[] = " is a directory"; |
static char is_a_dir[] = " is a directory"; |
|
|
m = (char *) ecalloc(strlen(filename) + sizeof(is_a_dir), |
len = strlen(filename) + sizeof(is_a_dir); |
sizeof(char)); |
m = (char *) ecalloc(len, sizeof(char)); |
strcpy(m, filename); |
strlcpy(m, filename, len); |
strcat(m, is_a_dir); |
strlcat(m, is_a_dir, len); |
} else |
} else |
{ |
{ |
#if HAVE_STAT |
#if HAVE_STAT |
|
|
} else if (!S_ISREG(statbuf.st_mode)) |
} else if (!S_ISREG(statbuf.st_mode)) |
{ |
{ |
static char not_reg[] = " is not a regular file (use -f to see it)"; |
static char not_reg[] = " is not a regular file (use -f to see it)"; |
m = (char *) ecalloc(strlen(filename) + sizeof(not_reg), |
len = strlen(filename) + sizeof(not_reg); |
sizeof(char)); |
m = (char *) ecalloc(len, sizeof(char)); |
strcpy(m, filename); |
strlcpy(m, filename, len); |
strcat(m, not_reg); |
strlcat(m, not_reg, len); |
} |
} |
#endif |
#endif |
} |
} |