2000-05-22 09:53:22 +00:00
|
|
|
/*
|
2007-06-04 01:42:54 +00:00
|
|
|
* Copyright (C) 1984-2007 Mark Nudelman
|
2000-05-22 09:53:22 +00:00
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Routines to mess around with filenames (and files).
|
|
|
|
* Much of this is very OS dependent.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "less.h"
|
|
|
|
#include "lglob.h"
|
|
|
|
#if MSDOS_COMPILER
|
|
|
|
#include <dos.h>
|
|
|
|
#if MSDOS_COMPILER==WIN32C && !defined(_MSC_VER)
|
|
|
|
#include <dir.h>
|
|
|
|
#endif
|
|
|
|
#if MSDOS_COMPILER==DJGPPC
|
|
|
|
#include <glob.h>
|
|
|
|
#include <dir.h>
|
|
|
|
#define _MAX_PATH PATH_MAX
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#ifdef _OSK
|
|
|
|
#include <rbf.h>
|
|
|
|
#ifndef _OSK_MWC32
|
|
|
|
#include <modes.h>
|
|
|
|
#endif
|
|
|
|
#endif
|
2002-01-07 20:29:38 +00:00
|
|
|
#if OS2
|
|
|
|
#include <signal.h>
|
|
|
|
#endif
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
#if HAVE_STAT
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#ifndef S_ISDIR
|
|
|
|
#define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
|
|
|
|
#endif
|
|
|
|
#ifndef S_ISREG
|
|
|
|
#define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
|
extern int force_open;
|
|
|
|
extern int secure;
|
2004-04-17 07:16:34 +00:00
|
|
|
extern int use_lessopen;
|
2007-06-04 01:42:54 +00:00
|
|
|
extern int ctldisp;
|
2000-05-22 09:53:22 +00:00
|
|
|
extern IFILE curr_ifile;
|
|
|
|
extern IFILE old_ifile;
|
|
|
|
#if SPACES_IN_FILENAMES
|
|
|
|
extern char openquote;
|
|
|
|
extern char closequote;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Remove quotes around a filename.
|
|
|
|
*/
|
|
|
|
public char *
|
2004-04-17 07:16:34 +00:00
|
|
|
shell_unquote(str)
|
2000-05-22 09:53:22 +00:00
|
|
|
char *str;
|
|
|
|
{
|
|
|
|
char *name;
|
|
|
|
char *p;
|
|
|
|
|
2004-04-17 07:16:34 +00:00
|
|
|
name = p = (char *) ecalloc(strlen(str)+1, sizeof(char));
|
|
|
|
if (*str == openquote)
|
|
|
|
{
|
|
|
|
str++;
|
|
|
|
while (*str != '\0')
|
|
|
|
{
|
|
|
|
if (*str == closequote)
|
|
|
|
{
|
|
|
|
if (str[1] != closequote)
|
|
|
|
break;
|
|
|
|
str++;
|
|
|
|
}
|
|
|
|
*p++ = *str++;
|
|
|
|
}
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
char *esc = get_meta_escape();
|
|
|
|
int esclen = strlen(esc);
|
|
|
|
while (*str != '\0')
|
|
|
|
{
|
|
|
|
if (esclen > 0 && strncmp(str, esc, esclen) == 0)
|
|
|
|
str += esclen;
|
|
|
|
*p++ = *str++;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*p = '\0';
|
2000-05-22 09:53:22 +00:00
|
|
|
return (name);
|
2004-04-17 07:16:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the shell's escape character.
|
|
|
|
*/
|
|
|
|
public char *
|
|
|
|
get_meta_escape()
|
|
|
|
{
|
|
|
|
char *s;
|
|
|
|
|
|
|
|
s = lgetenv("LESSMETAESCAPE");
|
|
|
|
if (s == NULL)
|
|
|
|
s = DEF_METAESCAPE;
|
|
|
|
return (s);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Get the characters which the shell considers to be "metacharacters".
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
metachars()
|
|
|
|
{
|
|
|
|
static char *mchars = NULL;
|
|
|
|
|
|
|
|
if (mchars == NULL)
|
|
|
|
{
|
|
|
|
mchars = lgetenv("LESSMETACHARS");
|
|
|
|
if (mchars == NULL)
|
|
|
|
mchars = DEF_METACHARS;
|
|
|
|
}
|
|
|
|
return (mchars);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Is this a shell metacharacter?
|
|
|
|
*/
|
|
|
|
static int
|
|
|
|
metachar(c)
|
|
|
|
char c;
|
|
|
|
{
|
|
|
|
return (strchr(metachars(), c) != NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Insert a backslash before each metacharacter in a string.
|
|
|
|
*/
|
|
|
|
public char *
|
|
|
|
shell_quote(s)
|
|
|
|
char *s;
|
|
|
|
{
|
|
|
|
char *p;
|
|
|
|
char *newstr;
|
|
|
|
int len;
|
|
|
|
char *esc = get_meta_escape();
|
|
|
|
int esclen = strlen(esc);
|
|
|
|
int use_quotes = 0;
|
|
|
|
int have_quotes = 0;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Determine how big a string we need to allocate.
|
|
|
|
*/
|
|
|
|
len = 1; /* Trailing null byte */
|
|
|
|
for (p = s; *p != '\0'; p++)
|
|
|
|
{
|
|
|
|
len++;
|
|
|
|
if (*p == openquote || *p == closequote)
|
|
|
|
have_quotes = 1;
|
|
|
|
if (metachar(*p))
|
|
|
|
{
|
|
|
|
if (esclen == 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We've got a metachar, but this shell
|
|
|
|
* doesn't support escape chars. Use quotes.
|
|
|
|
*/
|
|
|
|
use_quotes = 1;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Allow space for the escape char.
|
|
|
|
*/
|
|
|
|
len += esclen;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (use_quotes)
|
|
|
|
{
|
|
|
|
if (have_quotes)
|
|
|
|
/*
|
|
|
|
* We can't quote a string that contains quotes.
|
|
|
|
*/
|
|
|
|
return (NULL);
|
|
|
|
len = strlen(s) + 3;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Allocate and construct the new string.
|
|
|
|
*/
|
|
|
|
newstr = p = (char *) ecalloc(len, sizeof(char));
|
|
|
|
if (use_quotes)
|
|
|
|
{
|
2006-08-20 15:49:51 +00:00
|
|
|
SNPRINTF3(newstr, len, "%c%s%c", openquote, s, closequote);
|
2004-04-17 07:16:34 +00:00
|
|
|
} else
|
|
|
|
{
|
|
|
|
while (*s != '\0')
|
|
|
|
{
|
|
|
|
if (metachar(*s))
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Add the escape char.
|
|
|
|
*/
|
|
|
|
strcpy(p, esc);
|
|
|
|
p += esclen;
|
|
|
|
}
|
|
|
|
*p++ = *s++;
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
}
|
|
|
|
return (newstr);
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return a pathname that points to a specified file in a specified directory.
|
|
|
|
* Return NULL if the file does not exist in the directory.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
dirfile(dirname, filename)
|
|
|
|
char *dirname;
|
|
|
|
char *filename;
|
|
|
|
{
|
|
|
|
char *pathname;
|
|
|
|
char *qpathname;
|
2006-08-20 15:49:51 +00:00
|
|
|
int len;
|
2000-05-22 09:53:22 +00:00
|
|
|
int f;
|
|
|
|
|
|
|
|
if (dirname == NULL || *dirname == '\0')
|
|
|
|
return (NULL);
|
|
|
|
/*
|
|
|
|
* Construct the full pathname.
|
|
|
|
*/
|
2006-08-20 15:49:51 +00:00
|
|
|
len= strlen(dirname) + strlen(filename) + 2;
|
|
|
|
pathname = (char *) calloc(len, sizeof(char));
|
2000-05-22 09:53:22 +00:00
|
|
|
if (pathname == NULL)
|
|
|
|
return (NULL);
|
2006-08-20 15:49:51 +00:00
|
|
|
SNPRINTF3(pathname, len, "%s%s%s", dirname, PATHNAME_SEP, filename);
|
2000-05-22 09:53:22 +00:00
|
|
|
/*
|
|
|
|
* Make sure the file exists.
|
|
|
|
*/
|
2004-04-17 07:16:34 +00:00
|
|
|
qpathname = shell_unquote(pathname);
|
2000-05-22 09:53:22 +00:00
|
|
|
f = open(qpathname, OPEN_READ);
|
|
|
|
if (f < 0)
|
|
|
|
{
|
|
|
|
free(pathname);
|
|
|
|
pathname = NULL;
|
|
|
|
} else
|
|
|
|
{
|
|
|
|
close(f);
|
|
|
|
}
|
|
|
|
free(qpathname);
|
|
|
|
return (pathname);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the full pathname of the given file in the "home directory".
|
|
|
|
*/
|
|
|
|
public char *
|
|
|
|
homefile(filename)
|
|
|
|
char *filename;
|
|
|
|
{
|
|
|
|
register char *pathname;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try $HOME/filename.
|
|
|
|
*/
|
|
|
|
pathname = dirfile(lgetenv("HOME"), filename);
|
|
|
|
if (pathname != NULL)
|
|
|
|
return (pathname);
|
|
|
|
#if OS2
|
|
|
|
/*
|
|
|
|
* Try $INIT/filename.
|
|
|
|
*/
|
|
|
|
pathname = dirfile(lgetenv("INIT"), filename);
|
|
|
|
if (pathname != NULL)
|
|
|
|
return (pathname);
|
|
|
|
#endif
|
|
|
|
#if MSDOS_COMPILER || OS2
|
|
|
|
/*
|
|
|
|
* Look for the file anywhere on search path.
|
|
|
|
*/
|
|
|
|
pathname = (char *) calloc(_MAX_PATH, sizeof(char));
|
|
|
|
#if MSDOS_COMPILER==DJGPPC
|
|
|
|
{
|
|
|
|
char *res = searchpath(filename);
|
|
|
|
if (res == 0)
|
|
|
|
*pathname = '\0';
|
|
|
|
else
|
|
|
|
strcpy(pathname, res);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
_searchenv(filename, "PATH", pathname);
|
|
|
|
#endif
|
|
|
|
if (*pathname != '\0')
|
|
|
|
return (pathname);
|
|
|
|
free(pathname);
|
|
|
|
#endif
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Expand a string, substituting any "%" with the current filename,
|
|
|
|
* and any "#" with the previous filename.
|
|
|
|
* But a string of N "%"s is just replaced with N-1 "%"s.
|
|
|
|
* Likewise for a string of N "#"s.
|
|
|
|
* {{ This is a lot of work just to support % and #. }}
|
|
|
|
*/
|
|
|
|
public char *
|
|
|
|
fexpand(s)
|
|
|
|
char *s;
|
|
|
|
{
|
|
|
|
register char *fr, *to;
|
|
|
|
register int n;
|
|
|
|
register char *e;
|
|
|
|
IFILE ifile;
|
|
|
|
|
|
|
|
#define fchar_ifile(c) \
|
|
|
|
((c) == '%' ? curr_ifile : \
|
|
|
|
(c) == '#' ? old_ifile : NULL_IFILE)
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make one pass to see how big a buffer we
|
|
|
|
* need to allocate for the expanded string.
|
|
|
|
*/
|
|
|
|
n = 0;
|
|
|
|
for (fr = s; *fr != '\0'; fr++)
|
|
|
|
{
|
|
|
|
switch (*fr)
|
|
|
|
{
|
|
|
|
case '%':
|
|
|
|
case '#':
|
|
|
|
if (fr > s && fr[-1] == *fr)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Second (or later) char in a string
|
|
|
|
* of identical chars. Treat as normal.
|
|
|
|
*/
|
|
|
|
n++;
|
|
|
|
} else if (fr[1] != *fr)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Single char (not repeated). Treat specially.
|
|
|
|
*/
|
|
|
|
ifile = fchar_ifile(*fr);
|
|
|
|
if (ifile == NULL_IFILE)
|
|
|
|
n++;
|
|
|
|
else
|
|
|
|
n += strlen(get_filename(ifile));
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Else it is the first char in a string of
|
|
|
|
* identical chars. Just discard it.
|
|
|
|
*/
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
n++;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
e = (char *) ecalloc(n+1, sizeof(char));
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Now copy the string, expanding any "%" or "#".
|
|
|
|
*/
|
|
|
|
to = e;
|
|
|
|
for (fr = s; *fr != '\0'; fr++)
|
|
|
|
{
|
|
|
|
switch (*fr)
|
|
|
|
{
|
|
|
|
case '%':
|
|
|
|
case '#':
|
|
|
|
if (fr > s && fr[-1] == *fr)
|
|
|
|
{
|
|
|
|
*to++ = *fr;
|
|
|
|
} else if (fr[1] != *fr)
|
|
|
|
{
|
|
|
|
ifile = fchar_ifile(*fr);
|
|
|
|
if (ifile == NULL_IFILE)
|
|
|
|
*to++ = *fr;
|
|
|
|
else
|
|
|
|
{
|
|
|
|
strcpy(to, get_filename(ifile));
|
|
|
|
to += strlen(to);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
*to++ = *fr;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
*to = '\0';
|
|
|
|
return (e);
|
|
|
|
}
|
|
|
|
|
|
|
|
#if TAB_COMPLETE_FILENAME
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return a blank-separated list of filenames which "complete"
|
|
|
|
* the given string.
|
|
|
|
*/
|
|
|
|
public char *
|
|
|
|
fcomplete(s)
|
|
|
|
char *s;
|
|
|
|
{
|
|
|
|
char *fpat;
|
2004-04-17 07:16:34 +00:00
|
|
|
char *qs;
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
if (secure)
|
|
|
|
return (NULL);
|
|
|
|
/*
|
|
|
|
* Complete the filename "s" by globbing "s*".
|
|
|
|
*/
|
|
|
|
#if MSDOS_COMPILER && (MSDOS_COMPILER == MSOFTC || MSDOS_COMPILER == BORLANDC)
|
|
|
|
/*
|
|
|
|
* But in DOS, we have to glob "s*.*".
|
|
|
|
* But if the final component of the filename already has
|
|
|
|
* a dot in it, just do "s*".
|
|
|
|
* (Thus, "FILE" is globbed as "FILE*.*",
|
|
|
|
* but "FILE.A" is globbed as "FILE.A*").
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
char *slash;
|
2006-08-20 15:49:51 +00:00
|
|
|
int len;
|
2000-05-22 09:53:22 +00:00
|
|
|
for (slash = s+strlen(s)-1; slash > s; slash--)
|
|
|
|
if (*slash == *PATHNAME_SEP || *slash == '/')
|
|
|
|
break;
|
2006-08-20 15:49:51 +00:00
|
|
|
len = strlen(s) + 4;
|
|
|
|
fpat = (char *) ecalloc(len, sizeof(char));
|
2000-05-22 09:53:22 +00:00
|
|
|
if (strchr(slash, '.') == NULL)
|
2006-08-20 15:49:51 +00:00
|
|
|
SNPRINTF1(fpat, len, "%s*.*", s);
|
2000-05-22 09:53:22 +00:00
|
|
|
else
|
2006-08-20 15:49:51 +00:00
|
|
|
SNPRINTF1(fpat, len, "%s*", s);
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
#else
|
2006-08-20 15:49:51 +00:00
|
|
|
{
|
|
|
|
int len = strlen(s) + 2;
|
|
|
|
fpat = (char *) ecalloc(len, sizeof(char));
|
|
|
|
SNPRINTF1(fpat, len, "%s*", s);
|
|
|
|
}
|
2000-05-22 09:53:22 +00:00
|
|
|
#endif
|
2004-04-17 07:16:34 +00:00
|
|
|
qs = lglob(fpat);
|
|
|
|
s = shell_unquote(qs);
|
2000-05-22 09:53:22 +00:00
|
|
|
if (strcmp(s,fpat) == 0)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The filename didn't expand.
|
|
|
|
*/
|
2004-04-17 07:16:34 +00:00
|
|
|
free(qs);
|
|
|
|
qs = NULL;
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
2004-04-17 07:16:34 +00:00
|
|
|
free(s);
|
2000-05-22 09:53:22 +00:00
|
|
|
free(fpat);
|
2004-04-17 07:16:34 +00:00
|
|
|
return (qs);
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to determine if a file is "binary".
|
|
|
|
* This is just a guess, and we need not try too hard to make it accurate.
|
|
|
|
*/
|
|
|
|
public int
|
|
|
|
bin_file(f)
|
|
|
|
int f;
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
int n;
|
2007-06-04 01:42:54 +00:00
|
|
|
int bin_count = 0;
|
|
|
|
unsigned char data[256];
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
if (!seekable(f))
|
|
|
|
return (0);
|
|
|
|
if (lseek(f, (off_t)0, 0) == BAD_LSEEK)
|
|
|
|
return (0);
|
|
|
|
n = read(f, data, sizeof(data));
|
|
|
|
for (i = 0; i < n; i++)
|
2007-06-04 01:42:54 +00:00
|
|
|
{
|
|
|
|
char c = data[i];
|
|
|
|
if (ctldisp == OPT_ONPLUS && c == ESC)
|
|
|
|
{
|
|
|
|
while (++i < n && is_ansi_middle(data[i]))
|
|
|
|
continue;
|
|
|
|
} else if (binary_char(c))
|
|
|
|
bin_count++;
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Call it a binary file if there are more than 5 binary characters
|
|
|
|
* in the first 256 bytes of the file.
|
|
|
|
*/
|
|
|
|
return (bin_count > 5);
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Try to determine the size of a file by seeking to the end.
|
|
|
|
*/
|
|
|
|
static POSITION
|
|
|
|
seek_filesize(f)
|
|
|
|
int f;
|
|
|
|
{
|
|
|
|
off_t spos;
|
|
|
|
|
|
|
|
spos = lseek(f, (off_t)0, 2);
|
|
|
|
if (spos == BAD_LSEEK)
|
|
|
|
return (NULL_POSITION);
|
|
|
|
return ((POSITION) spos);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read a string from a file.
|
|
|
|
* Return a pointer to the string in memory.
|
|
|
|
*/
|
|
|
|
static char *
|
|
|
|
readfd(fd)
|
|
|
|
FILE *fd;
|
|
|
|
{
|
|
|
|
int len;
|
|
|
|
int ch;
|
|
|
|
char *buf;
|
|
|
|
char *p;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Make a guess about how many chars in the string
|
|
|
|
* and allocate a buffer to hold it.
|
|
|
|
*/
|
|
|
|
len = 100;
|
|
|
|
buf = (char *) ecalloc(len, sizeof(char));
|
|
|
|
for (p = buf; ; p++)
|
|
|
|
{
|
|
|
|
if ((ch = getc(fd)) == '\n' || ch == EOF)
|
|
|
|
break;
|
|
|
|
if (p - buf >= len-1)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The string is too big to fit in the buffer we have.
|
|
|
|
* Allocate a new buffer, twice as big.
|
|
|
|
*/
|
|
|
|
len *= 2;
|
|
|
|
*p = '\0';
|
|
|
|
p = (char *) ecalloc(len, sizeof(char));
|
|
|
|
strcpy(p, buf);
|
|
|
|
free(buf);
|
|
|
|
buf = p;
|
|
|
|
p = buf + strlen(buf);
|
|
|
|
}
|
|
|
|
*p = ch;
|
|
|
|
}
|
|
|
|
*p = '\0';
|
|
|
|
return (buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#if HAVE_POPEN
|
|
|
|
|
|
|
|
FILE *popen();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Execute a shell command.
|
|
|
|
* Return a pointer to a pipe connected to the shell command's standard output.
|
|
|
|
*/
|
|
|
|
static FILE *
|
|
|
|
shellcmd(cmd)
|
|
|
|
char *cmd;
|
|
|
|
{
|
|
|
|
FILE *fd;
|
|
|
|
|
|
|
|
#if HAVE_SHELL
|
|
|
|
char *shell;
|
|
|
|
|
|
|
|
shell = lgetenv("SHELL");
|
|
|
|
if (shell != NULL && *shell != '\0')
|
|
|
|
{
|
|
|
|
char *scmd;
|
|
|
|
char *esccmd;
|
|
|
|
|
|
|
|
/*
|
2004-04-17 07:16:34 +00:00
|
|
|
* Read the output of <$SHELL -c cmd>.
|
|
|
|
* Escape any metacharacters in the command.
|
2000-05-22 09:53:22 +00:00
|
|
|
*/
|
2004-04-17 07:16:34 +00:00
|
|
|
esccmd = shell_quote(cmd);
|
|
|
|
if (esccmd == NULL)
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2004-04-17 07:16:34 +00:00
|
|
|
fd = popen(cmd, "r");
|
2000-05-22 09:53:22 +00:00
|
|
|
} else
|
|
|
|
{
|
2006-08-20 15:49:51 +00:00
|
|
|
int len = strlen(shell) + strlen(esccmd) + 5;
|
|
|
|
scmd = (char *) ecalloc(len, sizeof(char));
|
|
|
|
SNPRINTF3(scmd, len, "%s %s %s", shell, shell_coption(), esccmd);
|
2000-05-22 09:53:22 +00:00
|
|
|
free(esccmd);
|
2004-04-17 07:16:34 +00:00
|
|
|
fd = popen(scmd, "r");
|
|
|
|
free(scmd);
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
} else
|
|
|
|
#endif
|
|
|
|
{
|
|
|
|
fd = popen(cmd, "r");
|
|
|
|
}
|
2004-04-17 07:16:34 +00:00
|
|
|
/*
|
|
|
|
* Redirection in `popen' might have messed with the
|
|
|
|
* standard devices. Restore binary input mode.
|
|
|
|
*/
|
|
|
|
SET_BINARY(0);
|
2000-05-22 09:53:22 +00:00
|
|
|
return (fd);
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* HAVE_POPEN */
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Expand a filename, doing any system-specific metacharacter substitutions.
|
|
|
|
*/
|
|
|
|
public char *
|
|
|
|
lglob(filename)
|
|
|
|
char *filename;
|
|
|
|
{
|
|
|
|
char *gfilename;
|
|
|
|
char *ofilename;
|
|
|
|
|
|
|
|
ofilename = fexpand(filename);
|
|
|
|
if (secure)
|
|
|
|
return (ofilename);
|
2004-04-17 07:16:34 +00:00
|
|
|
filename = shell_unquote(ofilename);
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
#ifdef DECL_GLOB_LIST
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The globbing function returns a list of names.
|
|
|
|
*/
|
|
|
|
int length;
|
|
|
|
char *p;
|
2004-04-17 07:16:34 +00:00
|
|
|
char *qfilename;
|
2000-05-22 09:53:22 +00:00
|
|
|
DECL_GLOB_LIST(list)
|
|
|
|
|
|
|
|
GLOB_LIST(filename, list);
|
|
|
|
if (GLOB_LIST_FAILED(list))
|
|
|
|
{
|
|
|
|
free(filename);
|
|
|
|
return (ofilename);
|
|
|
|
}
|
|
|
|
length = 1; /* Room for trailing null byte */
|
|
|
|
for (SCAN_GLOB_LIST(list, p))
|
|
|
|
{
|
|
|
|
INIT_GLOB_LIST(list, p);
|
2004-04-17 07:16:34 +00:00
|
|
|
qfilename = shell_quote(p);
|
|
|
|
if (qfilename != NULL)
|
|
|
|
{
|
|
|
|
length += strlen(qfilename) + 1;
|
|
|
|
free(qfilename);
|
|
|
|
}
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
gfilename = (char *) ecalloc(length, sizeof(char));
|
|
|
|
for (SCAN_GLOB_LIST(list, p))
|
|
|
|
{
|
|
|
|
INIT_GLOB_LIST(list, p);
|
2004-04-17 07:16:34 +00:00
|
|
|
qfilename = shell_quote(p);
|
|
|
|
if (qfilename != NULL)
|
|
|
|
{
|
|
|
|
sprintf(gfilename + strlen(gfilename), "%s ", qfilename);
|
|
|
|
free(qfilename);
|
|
|
|
}
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
/*
|
|
|
|
* Overwrite the final trailing space with a null terminator.
|
|
|
|
*/
|
|
|
|
*--p = '\0';
|
|
|
|
GLOB_LIST_DONE(list);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#ifdef DECL_GLOB_NAME
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* The globbing function returns a single name, and
|
|
|
|
* is called multiple times to walk thru all names.
|
|
|
|
*/
|
|
|
|
register char *p;
|
|
|
|
register int len;
|
|
|
|
register int n;
|
2004-04-17 07:16:34 +00:00
|
|
|
char *pathname;
|
|
|
|
char *qpathname;
|
2000-05-22 09:53:22 +00:00
|
|
|
DECL_GLOB_NAME(fnd,drive,dir,fname,ext,handle)
|
|
|
|
|
|
|
|
GLOB_FIRST_NAME(filename, &fnd, handle);
|
|
|
|
if (GLOB_FIRST_FAILED(handle))
|
|
|
|
{
|
|
|
|
free(filename);
|
|
|
|
return (ofilename);
|
|
|
|
}
|
|
|
|
|
|
|
|
_splitpath(filename, drive, dir, fname, ext);
|
|
|
|
len = 100;
|
|
|
|
gfilename = (char *) ecalloc(len, sizeof(char));
|
|
|
|
p = gfilename;
|
|
|
|
do {
|
|
|
|
n = strlen(drive) + strlen(dir) + strlen(fnd.GLOB_NAME) + 1;
|
2004-04-17 07:16:34 +00:00
|
|
|
pathname = (char *) ecalloc(n, sizeof(char));
|
2006-08-20 15:49:51 +00:00
|
|
|
SNPRINTF3(pathname, n, "%s%s%s", drive, dir, fnd.GLOB_NAME);
|
2004-04-17 07:16:34 +00:00
|
|
|
qpathname = shell_quote(pathname);
|
|
|
|
free(pathname);
|
|
|
|
if (qpathname != NULL)
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2004-04-17 07:16:34 +00:00
|
|
|
n = strlen(qpathname);
|
|
|
|
while (p - gfilename + n + 2 >= len)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* No room in current buffer.
|
|
|
|
* Allocate a bigger one.
|
|
|
|
*/
|
|
|
|
len *= 2;
|
|
|
|
*p = '\0';
|
|
|
|
p = (char *) ecalloc(len, sizeof(char));
|
|
|
|
strcpy(p, gfilename);
|
|
|
|
free(gfilename);
|
|
|
|
gfilename = p;
|
|
|
|
p = gfilename + strlen(gfilename);
|
|
|
|
}
|
|
|
|
strcpy(p, qpathname);
|
|
|
|
free(qpathname);
|
|
|
|
p += n;
|
|
|
|
*p++ = ' ';
|
2000-05-22 09:53:22 +00:00
|
|
|
}
|
|
|
|
} while (GLOB_NEXT_NAME(handle, &fnd) == 0);
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Overwrite the final trailing space with a null terminator.
|
|
|
|
*/
|
|
|
|
*--p = '\0';
|
|
|
|
GLOB_NAME_DONE(handle);
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#if HAVE_POPEN
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* We get the shell to glob the filename for us by passing
|
|
|
|
* an "echo" command to the shell and reading its output.
|
|
|
|
*/
|
|
|
|
FILE *fd;
|
|
|
|
char *s;
|
|
|
|
char *lessecho;
|
|
|
|
char *cmd;
|
2004-04-17 07:16:34 +00:00
|
|
|
char *esc;
|
2006-08-20 15:49:51 +00:00
|
|
|
int len;
|
2000-05-22 09:53:22 +00:00
|
|
|
|
2004-04-17 07:16:34 +00:00
|
|
|
esc = get_meta_escape();
|
|
|
|
if (strlen(esc) == 0)
|
|
|
|
esc = "-";
|
|
|
|
esc = shell_quote(esc);
|
|
|
|
if (esc == NULL)
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
|
|
|
free(filename);
|
|
|
|
return (ofilename);
|
|
|
|
}
|
2004-04-17 07:16:34 +00:00
|
|
|
lessecho = lgetenv("LESSECHO");
|
|
|
|
if (lessecho == NULL || *lessecho == '\0')
|
|
|
|
lessecho = "lessecho";
|
2000-05-22 09:53:22 +00:00
|
|
|
/*
|
|
|
|
* Invoke lessecho, and read its output (a globbed list of filenames).
|
|
|
|
*/
|
2006-08-20 15:49:51 +00:00
|
|
|
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);
|
2004-04-17 07:16:34 +00:00
|
|
|
free(esc);
|
|
|
|
for (s = metachars(); *s != '\0'; s++)
|
|
|
|
sprintf(cmd + strlen(cmd), "-n0x%x ", *s);
|
|
|
|
sprintf(cmd + strlen(cmd), "-- %s", ofilename);
|
2000-05-22 09:53:22 +00:00
|
|
|
fd = shellcmd(cmd);
|
|
|
|
free(cmd);
|
|
|
|
if (fd == NULL)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Cannot create the pipe.
|
|
|
|
* Just return the original (fexpanded) filename.
|
|
|
|
*/
|
|
|
|
free(filename);
|
|
|
|
return (ofilename);
|
|
|
|
}
|
|
|
|
gfilename = readfd(fd);
|
|
|
|
pclose(fd);
|
|
|
|
if (*gfilename == '\0')
|
|
|
|
{
|
|
|
|
free(gfilename);
|
|
|
|
free(filename);
|
|
|
|
return (ofilename);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
/*
|
|
|
|
* No globbing functions at all. Just use the fexpanded filename.
|
|
|
|
*/
|
|
|
|
gfilename = save(filename);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
free(filename);
|
|
|
|
free(ofilename);
|
|
|
|
return (gfilename);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* See if we should open a "replacement file"
|
|
|
|
* instead of the file we're about to open.
|
|
|
|
*/
|
|
|
|
public char *
|
|
|
|
open_altfile(filename, pf, pfd)
|
|
|
|
char *filename;
|
|
|
|
int *pf;
|
|
|
|
void **pfd;
|
|
|
|
{
|
|
|
|
#if !HAVE_POPEN
|
|
|
|
return (NULL);
|
|
|
|
#else
|
|
|
|
char *lessopen;
|
|
|
|
char *cmd;
|
2006-08-20 15:49:51 +00:00
|
|
|
int len;
|
2000-05-22 09:53:22 +00:00
|
|
|
FILE *fd;
|
|
|
|
#if HAVE_FILENO
|
|
|
|
int returnfd = 0;
|
|
|
|
#endif
|
|
|
|
|
2004-04-17 07:16:34 +00:00
|
|
|
if (!use_lessopen || secure)
|
2000-05-22 09:53:22 +00:00
|
|
|
return (NULL);
|
|
|
|
ch_ungetchar(-1);
|
|
|
|
if ((lessopen = lgetenv("LESSOPEN")) == NULL)
|
|
|
|
return (NULL);
|
|
|
|
if (strcmp(filename, "-") == 0)
|
|
|
|
return (NULL);
|
|
|
|
if (*lessopen == '|')
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* If LESSOPEN starts with a |, it indicates
|
|
|
|
* a "pipe preprocessor".
|
|
|
|
*/
|
|
|
|
#if HAVE_FILENO
|
|
|
|
lessopen++;
|
|
|
|
returnfd = 1;
|
|
|
|
#else
|
|
|
|
error("LESSOPEN pipe is not supported", NULL_PARG);
|
|
|
|
return (NULL);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2006-08-20 15:49:51 +00:00
|
|
|
len = strlen(lessopen) + strlen(filename) + 2;
|
|
|
|
cmd = (char *) ecalloc(len, sizeof(char));
|
|
|
|
SNPRINTF1(cmd, len, lessopen, filename);
|
2000-05-22 09:53:22 +00:00
|
|
|
fd = shellcmd(cmd);
|
|
|
|
free(cmd);
|
|
|
|
if (fd == NULL)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Cannot create the pipe.
|
|
|
|
*/
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
#if HAVE_FILENO
|
|
|
|
if (returnfd)
|
|
|
|
{
|
|
|
|
int f;
|
|
|
|
char c;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Read one char to see if the pipe will produce any data.
|
|
|
|
* If it does, push the char back on the pipe.
|
|
|
|
*/
|
|
|
|
f = fileno(fd);
|
|
|
|
SET_BINARY(f);
|
|
|
|
if (read(f, &c, 1) != 1)
|
|
|
|
{
|
|
|
|
/*
|
|
|
|
* Pipe is empty. This means there is no alt file.
|
|
|
|
*/
|
|
|
|
pclose(fd);
|
|
|
|
return (NULL);
|
|
|
|
}
|
|
|
|
ch_ungetchar(c);
|
|
|
|
*pfd = (void *) fd;
|
|
|
|
*pf = f;
|
|
|
|
return (save("-"));
|
|
|
|
}
|
|
|
|
#endif
|
2004-04-17 07:16:34 +00:00
|
|
|
cmd = readfd(fd);
|
2000-05-22 09:53:22 +00:00
|
|
|
pclose(fd);
|
2004-04-17 07:16:34 +00:00
|
|
|
if (*cmd == '\0')
|
2000-05-22 09:53:22 +00:00
|
|
|
/*
|
|
|
|
* Pipe is empty. This means there is no alt file.
|
|
|
|
*/
|
|
|
|
return (NULL);
|
2004-04-17 07:16:34 +00:00
|
|
|
return (cmd);
|
2000-05-22 09:53:22 +00:00
|
|
|
#endif /* HAVE_POPEN */
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Close a replacement file.
|
|
|
|
*/
|
|
|
|
public void
|
|
|
|
close_altfile(altfilename, filename, pipefd)
|
|
|
|
char *altfilename;
|
|
|
|
char *filename;
|
|
|
|
void *pipefd;
|
|
|
|
{
|
|
|
|
#if HAVE_POPEN
|
|
|
|
char *lessclose;
|
|
|
|
FILE *fd;
|
|
|
|
char *cmd;
|
2006-08-20 15:49:51 +00:00
|
|
|
int len;
|
2000-05-22 09:53:22 +00:00
|
|
|
|
|
|
|
if (secure)
|
|
|
|
return;
|
|
|
|
if (pipefd != NULL)
|
2002-01-07 20:29:38 +00:00
|
|
|
{
|
|
|
|
#if OS2
|
|
|
|
/*
|
|
|
|
* The pclose function of OS/2 emx sometimes fails.
|
|
|
|
* Send SIGINT to the piped process before closing it.
|
|
|
|
*/
|
|
|
|
kill(((FILE*)pipefd)->_pid, SIGINT);
|
|
|
|
#endif
|
2000-05-22 09:53:22 +00:00
|
|
|
pclose((FILE*) pipefd);
|
2002-01-07 20:29:38 +00:00
|
|
|
}
|
2000-05-22 09:53:22 +00:00
|
|
|
if ((lessclose = lgetenv("LESSCLOSE")) == NULL)
|
|
|
|
return;
|
2006-08-20 15:49:51 +00:00
|
|
|
len = strlen(lessclose) + strlen(filename) + strlen(altfilename) + 2;
|
|
|
|
cmd = (char *) ecalloc(len, sizeof(char));
|
|
|
|
SNPRINTF2(cmd, len, lessclose, filename, altfilename);
|
2000-05-22 09:53:22 +00:00
|
|
|
fd = shellcmd(cmd);
|
|
|
|
free(cmd);
|
|
|
|
if (fd != NULL)
|
|
|
|
pclose(fd);
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Is the specified file a directory?
|
|
|
|
*/
|
|
|
|
public int
|
|
|
|
is_dir(filename)
|
|
|
|
char *filename;
|
|
|
|
{
|
|
|
|
int isdir = 0;
|
|
|
|
|
2004-04-17 07:16:34 +00:00
|
|
|
filename = shell_unquote(filename);
|
2000-05-22 09:53:22 +00:00
|
|
|
#if HAVE_STAT
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
struct stat statbuf;
|
|
|
|
|
|
|
|
r = stat(filename, &statbuf);
|
|
|
|
isdir = (r >= 0 && S_ISDIR(statbuf.st_mode));
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
#ifdef _OSK
|
|
|
|
{
|
|
|
|
register int f;
|
|
|
|
|
|
|
|
f = open(filename, S_IREAD | S_IFDIR);
|
|
|
|
if (f >= 0)
|
|
|
|
close(f);
|
|
|
|
isdir = (f >= 0);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
free(filename);
|
|
|
|
return (isdir);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns NULL if the file can be opened and
|
|
|
|
* is an ordinary file, otherwise an error message
|
|
|
|
* (if it cannot be opened or is a directory, etc.)
|
|
|
|
*/
|
|
|
|
public char *
|
|
|
|
bad_file(filename)
|
|
|
|
char *filename;
|
|
|
|
{
|
|
|
|
register char *m = NULL;
|
|
|
|
|
2004-04-17 07:16:34 +00:00
|
|
|
filename = shell_unquote(filename);
|
2007-06-04 01:42:54 +00:00
|
|
|
if (!force_open && is_dir(filename))
|
2000-05-22 09:53:22 +00:00
|
|
|
{
|
2004-04-17 07:16:34 +00:00
|
|
|
static char is_a_dir[] = " is a directory";
|
2000-05-22 09:53:22 +00:00
|
|
|
|
2004-04-17 07:16:34 +00:00
|
|
|
m = (char *) ecalloc(strlen(filename) + sizeof(is_a_dir),
|
2000-05-22 09:53:22 +00:00
|
|
|
sizeof(char));
|
|
|
|
strcpy(m, filename);
|
2004-04-17 07:16:34 +00:00
|
|
|
strcat(m, is_a_dir);
|
2000-05-22 09:53:22 +00:00
|
|
|
} else
|
|
|
|
{
|
|
|
|
#if HAVE_STAT
|
|
|
|
int r;
|
|
|
|
struct stat statbuf;
|
|
|
|
|
|
|
|
r = stat(filename, &statbuf);
|
|
|
|
if (r < 0)
|
|
|
|
{
|
|
|
|
m = errno_message(filename);
|
|
|
|
} else if (force_open)
|
|
|
|
{
|
|
|
|
m = NULL;
|
|
|
|
} else if (!S_ISREG(statbuf.st_mode))
|
|
|
|
{
|
|
|
|
static char not_reg[] = " is not a regular file (use -f to see it)";
|
|
|
|
m = (char *) ecalloc(strlen(filename) + sizeof(not_reg),
|
|
|
|
sizeof(char));
|
|
|
|
strcpy(m, filename);
|
|
|
|
strcat(m, not_reg);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
free(filename);
|
|
|
|
return (m);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Return the size of a file, as cheaply as possible.
|
|
|
|
* In Unix, we can stat the file.
|
|
|
|
*/
|
|
|
|
public POSITION
|
|
|
|
filesize(f)
|
|
|
|
int f;
|
|
|
|
{
|
|
|
|
#if HAVE_STAT
|
|
|
|
struct stat statbuf;
|
|
|
|
|
|
|
|
if (fstat(f, &statbuf) >= 0)
|
|
|
|
return ((POSITION) statbuf.st_size);
|
|
|
|
#else
|
|
|
|
#ifdef _OSK
|
|
|
|
long size;
|
|
|
|
|
|
|
|
if ((size = (long) _gs_size(f)) >= 0)
|
|
|
|
return ((POSITION) size);
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
return (seek_filesize(f));
|
|
|
|
}
|
|
|
|
|
2004-04-17 07:16:34 +00:00
|
|
|
/*
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
public char *
|
|
|
|
shell_coption()
|
|
|
|
{
|
|
|
|
return ("-c");
|
|
|
|
}
|