summaryrefslogtreecommitdiff
path: root/filename.c
diff options
context:
space:
mode:
Diffstat (limited to 'filename.c')
-rw-r--r--filename.c122
1 files changed, 48 insertions, 74 deletions
diff --git a/filename.c b/filename.c
index a153207ba6359..983a02b0e062e 100644
--- a/filename.c
+++ b/filename.c
@@ -32,9 +32,6 @@
#include <modes.h>
#endif
#endif
-#if OS2
-#include <signal.h>
-#endif
#if HAVE_STAT
#include <sys/stat.h>
@@ -46,7 +43,6 @@
#endif
#endif
-
extern int force_open;
extern int secure;
extern int use_lessopen;
@@ -226,7 +222,6 @@ dirfile(dirname, filename)
char *filename;
{
char *pathname;
- char *qpathname;
int len;
int f;
@@ -243,8 +238,7 @@ dirfile(dirname, filename)
/*
* Make sure the file exists.
*/
- qpathname = shell_unquote(pathname);
- f = open(qpathname, OPEN_READ);
+ f = open(pathname, OPEN_READ);
if (f < 0)
{
free(pathname);
@@ -253,7 +247,6 @@ dirfile(dirname, filename)
{
close(f);
}
- free(qpathname);
return (pathname);
}
@@ -474,7 +467,7 @@ bin_file(f)
int bin_count = 0;
char data[256];
char* p;
- char* pend;
+ char* edata;
if (!seekable(f))
return (0);
@@ -483,21 +476,19 @@ bin_file(f)
n = read(f, data, sizeof(data));
if (n <= 0)
return (0);
- if (utf_mode)
+ edata = &data[n];
+ for (p = data; p < edata; )
{
- bin_count = utf_bin_count(data, n);
- } else
- {
- pend = &data[n];
- for (p = data; p < pend; )
+ if (utf_mode && !is_utf8_well_formed(p, edata-data))
+ {
+ bin_count++;
+ utf_skip_to_lead(&p, edata);
+ } else
{
- LWCHAR c = step_char(&p, +1, pend);
+ LWCHAR c = step_char(&p, +1, edata);
if (ctldisp == OPT_ONPLUS && IS_CSI_START(c))
- {
- do {
- c = step_char(&p, +1, pend);
- } while (p < pend && is_ansi_middle(c));
- } else if (binary_char(c))
+ skip_ansi(&p, edata);
+ else if (binary_char(c))
bin_count++;
}
}
@@ -632,12 +623,10 @@ lglob(filename)
char *filename;
{
char *gfilename;
- char *ofilename;
- ofilename = fexpand(filename);
+ filename = fexpand(filename);
if (secure)
- return (ofilename);
- filename = shell_unquote(ofilename);
+ return (filename);
#ifdef DECL_GLOB_LIST
{
@@ -652,8 +641,7 @@ lglob(filename)
GLOB_LIST(filename, list);
if (GLOB_LIST_FAILED(list))
{
- free(filename);
- return (ofilename);
+ return (filename);
}
length = 1; /* Room for trailing null byte */
for (SCAN_GLOB_LIST(list, p))
@@ -693,15 +681,14 @@ lglob(filename)
char *p;
int len;
int n;
- char *pathname;
- char *qpathname;
+ char *pfilename;
+ char *qfilename;
DECL_GLOB_NAME(fnd,drive,dir,fname,ext,handle)
GLOB_FIRST_NAME(filename, &fnd, handle);
if (GLOB_FIRST_FAILED(handle))
{
- free(filename);
- return (ofilename);
+ return (filename);
}
_splitpath(filename, drive, dir, fname, ext);
@@ -710,13 +697,13 @@ lglob(filename)
p = gfilename;
do {
n = (int) (strlen(drive) + strlen(dir) + strlen(fnd.GLOB_NAME) + 1);
- pathname = (char *) ecalloc(n, sizeof(char));
- SNPRINTF3(pathname, n, "%s%s%s", drive, dir, fnd.GLOB_NAME);
- qpathname = shell_quote(pathname);
- free(pathname);
- if (qpathname != NULL)
+ pfilename = (char *) ecalloc(n, sizeof(char));
+ SNPRINTF3(pfilename, n, "%s%s%s", drive, dir, fnd.GLOB_NAME);
+ qfilename = shell_quote(pfilename);
+ free(pfilename);
+ if (qfilename != NULL)
{
- n = (int) strlen(qpathname);
+ n = (int) strlen(qfilename);
while (p - gfilename + n + 2 >= len)
{
/*
@@ -731,8 +718,8 @@ lglob(filename)
gfilename = p;
p = gfilename + strlen(gfilename);
}
- strcpy(p, qpathname);
- free(qpathname);
+ strcpy(p, qfilename);
+ free(qfilename);
p += n;
*p++ = ' ';
}
@@ -764,8 +751,7 @@ lglob(filename)
esc = shell_quote(esc);
if (esc == NULL)
{
- free(filename);
- return (ofilename);
+ return (filename);
}
lessecho = lgetenv("LESSECHO");
if (lessecho == NULL || *lessecho == '\0')
@@ -773,13 +759,13 @@ lglob(filename)
/*
* Invoke lessecho, and read its output (a globbed list of filenames).
*/
- len = (int) (strlen(lessecho) + strlen(ofilename) + (7*strlen(metachars())) + 24);
+ len = (int) (strlen(lessecho) + strlen(filename) + (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);
- sprintf(cmd + strlen(cmd), "-- %s", ofilename);
+ sprintf(cmd + strlen(cmd), "-- %s", filename);
fd = shellcmd(cmd);
free(cmd);
if (fd == NULL)
@@ -788,16 +774,14 @@ lglob(filename)
* Cannot create the pipe.
* Just return the original (fexpanded) filename.
*/
- free(filename);
- return (ofilename);
+ return (filename);
}
gfilename = readfd(fd);
pclose(fd);
if (*gfilename == '\0')
{
free(gfilename);
- free(filename);
- return (ofilename);
+ return (save(filename));
}
}
#else
@@ -809,7 +793,6 @@ lglob(filename)
#endif
#endif
free(filename);
- free(ofilename);
return (gfilename);
}
@@ -853,6 +836,7 @@ open_altfile(filename, pf, pfd)
return (NULL);
#else
char *lessopen;
+ char *qfilename;
char *cmd;
int len;
FILE *fd;
@@ -879,24 +863,28 @@ open_altfile(filename, pf, pfd)
returnfd++;
#endif
}
- if (*lessopen == '-') {
+ if (*lessopen == '-')
+ {
/*
* Lessopen preprocessor will accept "-" as a filename.
*/
lessopen++;
- } else {
+ } else
+ {
if (strcmp(filename, "-") == 0)
return (NULL);
}
- if (num_pct_s(lessopen) > 1)
+ if (num_pct_s(lessopen) != 1)
{
- error("Invalid LESSOPEN variable", NULL_PARG);
+ error("LESSOPEN ignored: must contain exactly one %%s", NULL_PARG);
return (NULL);
}
- len = (int) (strlen(lessopen) + strlen(filename) + 2);
+ qfilename = shell_quote(filename);
+ len = (int) (strlen(lessopen) + strlen(qfilename) + 2);
cmd = (char *) ecalloc(len, sizeof(char));
- SNPRINTF1(cmd, len, lessopen, filename);
+ SNPRINTF1(cmd, len, lessopen, qfilename);
+ free(qfilename);
fd = shellcmd(cmd);
free(cmd);
if (fd == NULL)
@@ -909,11 +897,12 @@ open_altfile(filename, pf, pfd)
#if HAVE_FILENO
if (returnfd)
{
- int f;
char c;
+ int f;
/*
- * Read one char to see if the pipe will produce any data.
+ * The first time we open the file, 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);
@@ -956,10 +945,9 @@ open_altfile(filename, pf, pfd)
* Close a replacement file.
*/
public void
-close_altfile(altfilename, filename, pipefd)
+close_altfile(altfilename, filename)
char *altfilename;
char *filename;
- void *pipefd;
{
#if HAVE_POPEN
char *lessclose;
@@ -969,22 +957,12 @@ close_altfile(altfilename, filename, pipefd)
if (secure)
return;
- if (pipefd != NULL)
- {
-#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
- pclose((FILE*) pipefd);
- }
+ ch_ungetchar(-1);
if ((lessclose = lgetenv("LESSCLOSE")) == NULL)
return;
if (num_pct_s(lessclose) > 2)
{
- error("Invalid LESSCLOSE variable", NULL_PARG);
+ error("LESSCLOSE ignored; must contain no more than 2 %%s", NULL_PARG);
return;
}
len = (int) (strlen(lessclose) + strlen(filename) + strlen(altfilename) + 2);
@@ -1006,7 +984,6 @@ is_dir(filename)
{
int isdir = 0;
- filename = shell_unquote(filename);
#if HAVE_STAT
{
int r;
@@ -1027,7 +1004,6 @@ is_dir(filename)
}
#endif
#endif
- free(filename);
return (isdir);
}
@@ -1042,7 +1018,6 @@ bad_file(filename)
{
char *m = NULL;
- filename = shell_unquote(filename);
if (!force_open && is_dir(filename))
{
static char is_a_dir[] = " is a directory";
@@ -1074,7 +1049,6 @@ bad_file(filename)
}
#endif
}
- free(filename);
return (m);
}