filename.c revision 191930
131110Swpaul/*
250479Speter * Copyright (C) 1984-2008  Mark Nudelman
331110Swpaul *
4108470Sschweikh * You may distribute under the terms of either the GNU General Public
531110Swpaul * License or the Less License, as specified in the README file.
631110Swpaul *
731110Swpaul * For more information about less, or for information on how to
831110Swpaul * contact the author, see the README file.
931110Swpaul */
1031110Swpaul
1131110Swpaul
1231110Swpaul/*
1331110Swpaul * Routines to mess around with filenames (and files).
1431110Swpaul * Much of this is very OS dependent.
1531110Swpaul */
1631110Swpaul
17286892Sasomers#include "less.h"
18286892Sasomers#include "lglob.h"
19286892Sasomers#if MSDOS_COMPILER
2031110Swpaul#include <dos.h>
2131110Swpaul#if MSDOS_COMPILER==WIN32C && !defined(_MSC_VER)
2231110Swpaul#include <dir.h>
2331110Swpaul#endif
2431110Swpaul#if MSDOS_COMPILER==DJGPPC
2531110Swpaul#include <glob.h>
2631110Swpaul#include <dir.h>
2731110Swpaul#define _MAX_PATH	PATH_MAX
2831110Swpaul#endif
2931110Swpaul#endif
3031110Swpaul#ifdef _OSK
3131110Swpaul#include <rbf.h>
3231110Swpaul#ifndef _OSK_MWC32
3331110Swpaul#include <modes.h>
3431110Swpaul#endif
3531110Swpaul#endif
3631110Swpaul#if OS2
3731110Swpaul#include <signal.h>
3831110Swpaul#endif
3931110Swpaul
4031110Swpaul#if HAVE_STAT
4131110Swpaul#include <sys/stat.h>
4231110Swpaul#ifndef S_ISDIR
4331110Swpaul#define	S_ISDIR(m)	(((m) & S_IFMT) == S_IFDIR)
4431110Swpaul#endif
4531110Swpaul#ifndef S_ISREG
4631110Swpaul#define	S_ISREG(m)	(((m) & S_IFMT) == S_IFREG)
4731110Swpaul#endif
4831110Swpaul#endif
4931110Swpaul
5031110Swpaul
5131110Swpaulextern int force_open;
5231110Swpaulextern int secure;
5331110Swpaulextern int use_lessopen;
5431110Swpaulextern int ctldisp;
5531110Swpaulextern int utf_mode;
5631110Swpaulextern IFILE curr_ifile;
5731110Swpaulextern IFILE old_ifile;
5831110Swpaul#if SPACES_IN_FILENAMES
5931110Swpaulextern char openquote;
6031110Swpaulextern char closequote;
6131110Swpaul#endif
6231110Swpaul
6331110Swpaul/*
6431110Swpaul * Remove quotes around a filename.
6531110Swpaul */
6631110Swpaul	public char *
6731110Swpaulshell_unquote(str)
6831110Swpaul	char *str;
6931110Swpaul{
7031110Swpaul	char *name;
7131110Swpaul	char *p;
7231110Swpaul
7331110Swpaul	name = p = (char *) ecalloc(strlen(str)+1, sizeof(char));
7431110Swpaul	if (*str == openquote)
7531110Swpaul	{
7631110Swpaul		str++;
7731110Swpaul		while (*str != '\0')
7831110Swpaul		{
7931110Swpaul			if (*str == closequote)
8031110Swpaul			{
8131110Swpaul				if (str[1] != closequote)
8231110Swpaul					break;
8331110Swpaul				str++;
8431110Swpaul			}
8531110Swpaul			*p++ = *str++;
8631110Swpaul		}
8731110Swpaul	} else
8831110Swpaul	{
8931110Swpaul		char *esc = get_meta_escape();
9031110Swpaul		int esclen = strlen(esc);
9131110Swpaul		while (*str != '\0')
9231110Swpaul		{
9331110Swpaul			if (esclen > 0 && strncmp(str, esc, esclen) == 0)
9431110Swpaul				str += esclen;
9531110Swpaul			*p++ = *str++;
9631110Swpaul		}
9731110Swpaul	}
9831110Swpaul	*p = '\0';
9931110Swpaul	return (name);
10031182Swpaul}
10131110Swpaul
10231110Swpaul/*
10331110Swpaul * Get the shell's escape character.
10431110Swpaul */
10531110Swpaul	public char *
10631110Swpaulget_meta_escape()
10731110Swpaul{
10831110Swpaul	char *s;
10931110Swpaul
11031110Swpaul	s = lgetenv("LESSMETAESCAPE");
11131182Swpaul	if (s == NULL)
11231110Swpaul		s = DEF_METAESCAPE;
11331110Swpaul	return (s);
11431110Swpaul}
11531110Swpaul
11631110Swpaul/*
11731110Swpaul * Get the characters which the shell considers to be "metacharacters".
11831110Swpaul */
11931110Swpaul	static char *
12031110Swpaulmetachars()
12131110Swpaul{
12231110Swpaul	static char *mchars = NULL;
12331110Swpaul
12431110Swpaul	if (mchars == NULL)
12531110Swpaul	{
12631110Swpaul		mchars = lgetenv("LESSMETACHARS");
12731110Swpaul		if (mchars == NULL)
12831110Swpaul			mchars = DEF_METACHARS;
12931110Swpaul	}
13031110Swpaul	return (mchars);
13131110Swpaul}
13231110Swpaul
13331110Swpaul/*
13431110Swpaul * Is this a shell metacharacter?
13531110Swpaul */
13631110Swpaul	static int
13731110Swpaulmetachar(c)
13831110Swpaul	char c;
13931110Swpaul{
14031110Swpaul	return (strchr(metachars(), c) != NULL);
14131110Swpaul}
14231110Swpaul
14331110Swpaul/*
14431110Swpaul * Insert a backslash before each metacharacter in a string.
14531110Swpaul */
14631110Swpaul	public char *
14731110Swpaulshell_quote(s)
14831110Swpaul	char *s;
14931110Swpaul{
15031110Swpaul	char *p;
15131110Swpaul	char *newstr;
15231110Swpaul	int len;
15331110Swpaul	char *esc = get_meta_escape();
15431110Swpaul	int esclen = strlen(esc);
15531110Swpaul	int use_quotes = 0;
15631110Swpaul	int have_quotes = 0;
15731110Swpaul
15831110Swpaul	/*
15931110Swpaul	 * Determine how big a string we need to allocate.
16031110Swpaul	 */
16131110Swpaul	len = 1; /* Trailing null byte */
16231110Swpaul	for (p = s;  *p != '\0';  p++)
16331110Swpaul	{
16431110Swpaul		len++;
16531110Swpaul		if (*p == openquote || *p == closequote)
16631110Swpaul			have_quotes = 1;
16731110Swpaul		if (metachar(*p))
16831110Swpaul		{
16931110Swpaul			if (esclen == 0)
17031110Swpaul			{
17131110Swpaul				/*
17231110Swpaul				 * We've got a metachar, but this shell
17331110Swpaul				 * doesn't support escape chars.  Use quotes.
17431110Swpaul				 */
17531110Swpaul				use_quotes = 1;
17631110Swpaul			} else
17731110Swpaul			{
17831110Swpaul				/*
17931110Swpaul				 * Allow space for the escape char.
18031110Swpaul				 */
18131110Swpaul				len += esclen;
18231110Swpaul			}
18331110Swpaul		}
184108470Sschweikh	}
18531110Swpaul	if (use_quotes)
18631110Swpaul	{
18731110Swpaul		if (have_quotes)
18831110Swpaul			/*
18931110Swpaul			 * We can't quote a string that contains quotes.
19031110Swpaul			 */
19131110Swpaul			return (NULL);
19231110Swpaul		len = strlen(s) + 3;
19331110Swpaul	}
19431110Swpaul	/*
19531110Swpaul	 * Allocate and construct the new string.
19631110Swpaul	 */
19731110Swpaul	newstr = p = (char *) ecalloc(len, sizeof(char));
19831110Swpaul	if (use_quotes)
19931110Swpaul	{
20031110Swpaul		SNPRINTF3(newstr, len, "%c%s%c", openquote, s, closequote);
20131110Swpaul	} else
20231110Swpaul	{
20331110Swpaul		while (*s != '\0')
20431110Swpaul		{
20531110Swpaul			if (metachar(*s))
20631110Swpaul			{
20731110Swpaul				/*
20831110Swpaul				 * Add the escape char.
20931110Swpaul				 */
21031110Swpaul				strcpy(p, esc);
21131110Swpaul				p += esclen;
21231110Swpaul			}
21331110Swpaul			*p++ = *s++;
21431110Swpaul		}
21531110Swpaul		*p = '\0';
21631110Swpaul	}
21731110Swpaul	return (newstr);
21831110Swpaul}
21931110Swpaul
22031110Swpaul/*
22131110Swpaul * Return a pathname that points to a specified file in a specified directory.
22231110Swpaul * Return NULL if the file does not exist in the directory.
22331110Swpaul */
22431110Swpaul	static char *
22531110Swpauldirfile(dirname, filename)
22631110Swpaul	char *dirname;
22731110Swpaul	char *filename;
22831110Swpaul{
22931110Swpaul	char *pathname;
23031110Swpaul	char *qpathname;
23131110Swpaul	int len;
23231110Swpaul	int f;
23331110Swpaul
23431110Swpaul	if (dirname == NULL || *dirname == '\0')
23531110Swpaul		return (NULL);
23631110Swpaul	/*
23731110Swpaul	 * Construct the full pathname.
238289677Seadler	 */
23931110Swpaul	len= strlen(dirname) + strlen(filename) + 2;
24031110Swpaul	pathname = (char *) calloc(len, sizeof(char));
24131110Swpaul	if (pathname == NULL)
24231110Swpaul		return (NULL);
24331110Swpaul	SNPRINTF3(pathname, len, "%s%s%s", dirname, PATHNAME_SEP, filename);
24431110Swpaul	/*
24531110Swpaul	 * Make sure the file exists.
24631110Swpaul	 */
24731110Swpaul	qpathname = shell_unquote(pathname);
24831110Swpaul	f = open(qpathname, OPEN_READ);
24931110Swpaul	if (f < 0)
25031110Swpaul	{
25131110Swpaul		free(pathname);
25231110Swpaul		pathname = NULL;
25331110Swpaul	} else
25431110Swpaul	{
25531110Swpaul		close(f);
25631110Swpaul	}
25731110Swpaul	free(qpathname);
25831110Swpaul	return (pathname);
25931110Swpaul}
26031110Swpaul
26131110Swpaul/*
26231110Swpaul * Return the full pathname of the given file in the "home directory".
26331110Swpaul */
26431110Swpaul	public char *
26531110Swpaulhomefile(filename)
26631110Swpaul	char *filename;
26731110Swpaul{
26831110Swpaul	register char *pathname;
26931110Swpaul
27031110Swpaul	/*
27131110Swpaul	 * Try $HOME/filename.
27231110Swpaul	 */
27331110Swpaul	pathname = dirfile(lgetenv("HOME"), filename);
27431110Swpaul	if (pathname != NULL)
27531110Swpaul		return (pathname);
27631110Swpaul#if OS2
27731110Swpaul	/*
27831110Swpaul	 * Try $INIT/filename.
27931110Swpaul	 */
28031110Swpaul	pathname = dirfile(lgetenv("INIT"), filename);
28131110Swpaul	if (pathname != NULL)
28231110Swpaul		return (pathname);
28331110Swpaul#endif
28431110Swpaul#if MSDOS_COMPILER || OS2
28531110Swpaul	/*
28631110Swpaul	 * Look for the file anywhere on search path.
28731110Swpaul	 */
28831110Swpaul	pathname = (char *) calloc(_MAX_PATH, sizeof(char));
28931110Swpaul#if MSDOS_COMPILER==DJGPPC
29031110Swpaul	{
29131110Swpaul		char *res = searchpath(filename);
29231110Swpaul		if (res == 0)
29331110Swpaul			*pathname = '\0';
29431110Swpaul		else
29531110Swpaul			strcpy(pathname, res);
29631110Swpaul	}
29731110Swpaul#else
29831110Swpaul	_searchenv(filename, "PATH", pathname);
29931110Swpaul#endif
30031110Swpaul	if (*pathname != '\0')
30131110Swpaul		return (pathname);
302201051Smarck	free(pathname);
303108470Sschweikh#endif
30431110Swpaul	return (NULL);
30531110Swpaul}
30631110Swpaul
30731110Swpaul/*
30831110Swpaul * Expand a string, substituting any "%" with the current filename,
30931110Swpaul * and any "#" with the previous filename.
31031110Swpaul * But a string of N "%"s is just replaced with N-1 "%"s.
31131110Swpaul * Likewise for a string of N "#"s.
31231110Swpaul * {{ This is a lot of work just to support % and #. }}
31331110Swpaul */
31431110Swpaul	public char *
31531110Swpaulfexpand(s)
31631110Swpaul	char *s;
31731110Swpaul{
31831110Swpaul	register char *fr, *to;
31931110Swpaul	register int n;
32031110Swpaul	register char *e;
32131110Swpaul	IFILE ifile;
32231110Swpaul
32331110Swpaul#define	fchar_ifile(c) \
32431110Swpaul	((c) == '%' ? curr_ifile : \
32531110Swpaul	 (c) == '#' ? old_ifile : NULL_IFILE)
32631110Swpaul
32731110Swpaul	/*
32831110Swpaul	 * Make one pass to see how big a buffer we
32931110Swpaul	 * need to allocate for the expanded string.
33031110Swpaul	 */
33131110Swpaul	n = 0;
33231110Swpaul	for (fr = s;  *fr != '\0';  fr++)
33331110Swpaul	{
33431110Swpaul		switch (*fr)
33531110Swpaul		{
33631110Swpaul		case '%':
33731110Swpaul		case '#':
33831110Swpaul			if (fr > s && fr[-1] == *fr)
33931110Swpaul			{
34031110Swpaul				/*
34131110Swpaul				 * Second (or later) char in a string
34231110Swpaul				 * of identical chars.  Treat as normal.
34331110Swpaul				 */
34431110Swpaul				n++;
34531110Swpaul			} else if (fr[1] != *fr)
34631110Swpaul			{
34731110Swpaul				/*
34831110Swpaul				 * Single char (not repeated).  Treat specially.
34931110Swpaul				 */
35031110Swpaul				ifile = fchar_ifile(*fr);
35131110Swpaul				if (ifile == NULL_IFILE)
35231110Swpaul					n++;
35331110Swpaul				else
35431110Swpaul					n += strlen(get_filename(ifile));
35531110Swpaul			}
35631110Swpaul			/*
35731110Swpaul			 * Else it is the first char in a string of
35831110Swpaul			 * identical chars.  Just discard it.
35931110Swpaul			 */
360108470Sschweikh			break;
36131110Swpaul		default:
36231110Swpaul			n++;
36331110Swpaul			break;
36431110Swpaul		}
36531110Swpaul	}
36631110Swpaul
36731110Swpaul	e = (char *) ecalloc(n+1, sizeof(char));
36831110Swpaul
36931110Swpaul	/*
37031110Swpaul	 * Now copy the string, expanding any "%" or "#".
37131110Swpaul	 */
37231110Swpaul	to = e;
373108470Sschweikh	for (fr = s;  *fr != '\0';  fr++)
37431110Swpaul	{
37531110Swpaul		switch (*fr)
37631110Swpaul		{
37731110Swpaul		case '%':
37831110Swpaul		case '#':
37931110Swpaul			if (fr > s && fr[-1] == *fr)
38031110Swpaul			{
38131110Swpaul				*to++ = *fr;
38231110Swpaul			} else if (fr[1] != *fr)
38331110Swpaul			{
38431110Swpaul				ifile = fchar_ifile(*fr);
38531110Swpaul				if (ifile == NULL_IFILE)
38631110Swpaul					*to++ = *fr;
38731110Swpaul				else
388				{
389					strcpy(to, get_filename(ifile));
390					to += strlen(to);
391				}
392			}
393			break;
394		default:
395			*to++ = *fr;
396			break;
397		}
398	}
399	*to = '\0';
400	return (e);
401}
402
403#if TAB_COMPLETE_FILENAME
404
405/*
406 * Return a blank-separated list of filenames which "complete"
407 * the given string.
408 */
409	public char *
410fcomplete(s)
411	char *s;
412{
413	char *fpat;
414	char *qs;
415
416	if (secure)
417		return (NULL);
418	/*
419	 * Complete the filename "s" by globbing "s*".
420	 */
421#if MSDOS_COMPILER && (MSDOS_COMPILER == MSOFTC || MSDOS_COMPILER == BORLANDC)
422	/*
423	 * But in DOS, we have to glob "s*.*".
424	 * But if the final component of the filename already has
425	 * a dot in it, just do "s*".
426	 * (Thus, "FILE" is globbed as "FILE*.*",
427	 *  but "FILE.A" is globbed as "FILE.A*").
428	 */
429	{
430		char *slash;
431		int len;
432		for (slash = s+strlen(s)-1;  slash > s;  slash--)
433			if (*slash == *PATHNAME_SEP || *slash == '/')
434				break;
435		len = strlen(s) + 4;
436		fpat = (char *) ecalloc(len, sizeof(char));
437		if (strchr(slash, '.') == NULL)
438			SNPRINTF1(fpat, len, "%s*.*", s);
439		else
440			SNPRINTF1(fpat, len, "%s*", s);
441	}
442#else
443	{
444	int len = strlen(s) + 2;
445	fpat = (char *) ecalloc(len, sizeof(char));
446	SNPRINTF1(fpat, len, "%s*", s);
447	}
448#endif
449	qs = lglob(fpat);
450	s = shell_unquote(qs);
451	if (strcmp(s,fpat) == 0)
452	{
453		/*
454		 * The filename didn't expand.
455		 */
456		free(qs);
457		qs = NULL;
458	}
459	free(s);
460	free(fpat);
461	return (qs);
462}
463#endif
464
465/*
466 * Try to determine if a file is "binary".
467 * This is just a guess, and we need not try too hard to make it accurate.
468 */
469	public int
470bin_file(f)
471	int f;
472{
473	int n;
474	int bin_count = 0;
475	char data[256];
476	char* p;
477	char* pend;
478
479	if (!seekable(f))
480		return (0);
481	if (lseek(f, (off_t)0, SEEK_SET) == BAD_LSEEK)
482		return (0);
483	n = read(f, data, sizeof(data));
484	pend = &data[n];
485	for (p = data;  p < pend;  )
486	{
487		LWCHAR c = step_char(&p, +1, pend);
488		if (ctldisp == OPT_ONPLUS && IS_CSI_START(c))
489		{
490			do {
491				c = step_char(&p, +1, pend);
492			} while (p < pend && is_ansi_middle(c));
493		} else if (binary_char(c))
494			bin_count++;
495	}
496	/*
497	 * Call it a binary file if there are more than 5 binary characters
498	 * in the first 256 bytes of the file.
499	 */
500	return (bin_count > 5);
501}
502
503/*
504 * Try to determine the size of a file by seeking to the end.
505 */
506	static POSITION
507seek_filesize(f)
508	int f;
509{
510	off_t spos;
511
512	spos = lseek(f, (off_t)0, SEEK_END);
513	if (spos == BAD_LSEEK)
514		return (NULL_POSITION);
515	return ((POSITION) spos);
516}
517
518/*
519 * Read a string from a file.
520 * Return a pointer to the string in memory.
521 */
522	static char *
523readfd(fd)
524	FILE *fd;
525{
526	int len;
527	int ch;
528	char *buf;
529	char *p;
530
531	/*
532	 * Make a guess about how many chars in the string
533	 * and allocate a buffer to hold it.
534	 */
535	len = 100;
536	buf = (char *) ecalloc(len, sizeof(char));
537	for (p = buf;  ;  p++)
538	{
539		if ((ch = getc(fd)) == '\n' || ch == EOF)
540			break;
541		if (p - buf >= len-1)
542		{
543			/*
544			 * The string is too big to fit in the buffer we have.
545			 * Allocate a new buffer, twice as big.
546			 */
547			len *= 2;
548			*p = '\0';
549			p = (char *) ecalloc(len, sizeof(char));
550			strcpy(p, buf);
551			free(buf);
552			buf = p;
553			p = buf + strlen(buf);
554		}
555		*p = ch;
556	}
557	*p = '\0';
558	return (buf);
559}
560
561
562
563#if HAVE_POPEN
564
565FILE *popen();
566
567/*
568 * Execute a shell command.
569 * Return a pointer to a pipe connected to the shell command's standard output.
570 */
571	static FILE *
572shellcmd(cmd)
573	char *cmd;
574{
575	FILE *fd;
576
577#if HAVE_SHELL
578	char *shell;
579
580	shell = lgetenv("SHELL");
581	if (shell != NULL && *shell != '\0')
582	{
583		char *scmd;
584		char *esccmd;
585
586		/*
587		 * Read the output of <$SHELL -c cmd>.
588		 * Escape any metacharacters in the command.
589		 */
590		esccmd = shell_quote(cmd);
591		if (esccmd == NULL)
592		{
593			fd = popen(cmd, "r");
594		} else
595		{
596			int len = strlen(shell) + strlen(esccmd) + 5;
597			scmd = (char *) ecalloc(len, sizeof(char));
598			SNPRINTF3(scmd, len, "%s %s %s", shell, shell_coption(), esccmd);
599			free(esccmd);
600			fd = popen(scmd, "r");
601			free(scmd);
602		}
603	} else
604#endif
605	{
606		fd = popen(cmd, "r");
607	}
608	/*
609	 * Redirection in `popen' might have messed with the
610	 * standard devices.  Restore binary input mode.
611	 */
612	SET_BINARY(0);
613	return (fd);
614}
615
616#endif /* HAVE_POPEN */
617
618
619/*
620 * Expand a filename, doing any system-specific metacharacter substitutions.
621 */
622	public char *
623lglob(filename)
624	char *filename;
625{
626	char *gfilename;
627	char *ofilename;
628
629	ofilename = fexpand(filename);
630	if (secure)
631		return (ofilename);
632	filename = shell_unquote(ofilename);
633
634#ifdef DECL_GLOB_LIST
635{
636	/*
637	 * The globbing function returns a list of names.
638	 */
639	int length;
640	char *p;
641	char *qfilename;
642	DECL_GLOB_LIST(list)
643
644	GLOB_LIST(filename, list);
645	if (GLOB_LIST_FAILED(list))
646	{
647		free(filename);
648		return (ofilename);
649	}
650	length = 1; /* Room for trailing null byte */
651	for (SCAN_GLOB_LIST(list, p))
652	{
653		INIT_GLOB_LIST(list, p);
654		qfilename = shell_quote(p);
655		if (qfilename != NULL)
656		{
657	  		length += strlen(qfilename) + 1;
658			free(qfilename);
659		}
660	}
661	gfilename = (char *) ecalloc(length, sizeof(char));
662	for (SCAN_GLOB_LIST(list, p))
663	{
664		INIT_GLOB_LIST(list, p);
665		qfilename = shell_quote(p);
666		if (qfilename != NULL)
667		{
668			sprintf(gfilename + strlen(gfilename), "%s ", qfilename);
669			free(qfilename);
670		}
671	}
672	/*
673	 * Overwrite the final trailing space with a null terminator.
674	 */
675	*--p = '\0';
676	GLOB_LIST_DONE(list);
677}
678#else
679#ifdef DECL_GLOB_NAME
680{
681	/*
682	 * The globbing function returns a single name, and
683	 * is called multiple times to walk thru all names.
684	 */
685	register char *p;
686	register int len;
687	register int n;
688	char *pathname;
689	char *qpathname;
690	DECL_GLOB_NAME(fnd,drive,dir,fname,ext,handle)
691
692	GLOB_FIRST_NAME(filename, &fnd, handle);
693	if (GLOB_FIRST_FAILED(handle))
694	{
695		free(filename);
696		return (ofilename);
697	}
698
699	_splitpath(filename, drive, dir, fname, ext);
700	len = 100;
701	gfilename = (char *) ecalloc(len, sizeof(char));
702	p = gfilename;
703	do {
704		n = strlen(drive) + strlen(dir) + strlen(fnd.GLOB_NAME) + 1;
705		pathname = (char *) ecalloc(n, sizeof(char));
706		SNPRINTF3(pathname, n, "%s%s%s", drive, dir, fnd.GLOB_NAME);
707		qpathname = shell_quote(pathname);
708		free(pathname);
709		if (qpathname != NULL)
710		{
711			n = strlen(qpathname);
712			while (p - gfilename + n + 2 >= len)
713			{
714				/*
715				 * No room in current buffer.
716				 * Allocate a bigger one.
717				 */
718				len *= 2;
719				*p = '\0';
720				p = (char *) ecalloc(len, sizeof(char));
721				strcpy(p, gfilename);
722				free(gfilename);
723				gfilename = p;
724				p = gfilename + strlen(gfilename);
725			}
726			strcpy(p, qpathname);
727			free(qpathname);
728			p += n;
729			*p++ = ' ';
730		}
731	} while (GLOB_NEXT_NAME(handle, &fnd) == 0);
732
733	/*
734	 * Overwrite the final trailing space with a null terminator.
735	 */
736	*--p = '\0';
737	GLOB_NAME_DONE(handle);
738}
739#else
740#if HAVE_POPEN
741{
742	/*
743	 * We get the shell to glob the filename for us by passing
744	 * an "echo" command to the shell and reading its output.
745	 */
746	FILE *fd;
747	char *s;
748	char *lessecho;
749	char *cmd;
750	char *esc;
751	int len;
752
753	esc = get_meta_escape();
754	if (strlen(esc) == 0)
755		esc = "-";
756	esc = shell_quote(esc);
757	if (esc == NULL)
758	{
759		free(filename);
760		return (ofilename);
761	}
762	lessecho = lgetenv("LESSECHO");
763	if (lessecho == NULL || *lessecho == '\0')
764		lessecho = "lessecho";
765	/*
766	 * Invoke lessecho, and read its output (a globbed list of filenames).
767	 */
768	len = strlen(lessecho) + strlen(ofilename) + (7*strlen(metachars())) + 24;
769	cmd = (char *) ecalloc(len, sizeof(char));
770	SNPRINTF4(cmd, len, "%s -p0x%x -d0x%x -e%s ", lessecho, openquote, closequote, esc);
771	free(esc);
772	for (s = metachars();  *s != '\0';  s++)
773		sprintf(cmd + strlen(cmd), "-n0x%x ", *s);
774	sprintf(cmd + strlen(cmd), "-- %s", ofilename);
775	fd = shellcmd(cmd);
776	free(cmd);
777	if (fd == NULL)
778	{
779		/*
780		 * Cannot create the pipe.
781		 * Just return the original (fexpanded) filename.
782		 */
783		free(filename);
784		return (ofilename);
785	}
786	gfilename = readfd(fd);
787	pclose(fd);
788	if (*gfilename == '\0')
789	{
790		free(gfilename);
791		free(filename);
792		return (ofilename);
793	}
794}
795#else
796	/*
797	 * No globbing functions at all.  Just use the fexpanded filename.
798	 */
799	gfilename = save(filename);
800#endif
801#endif
802#endif
803	free(filename);
804	free(ofilename);
805	return (gfilename);
806}
807
808/*
809 * See if we should open a "replacement file"
810 * instead of the file we're about to open.
811 */
812	public char *
813open_altfile(filename, pf, pfd)
814	char *filename;
815	int *pf;
816	void **pfd;
817{
818#if !HAVE_POPEN
819	return (NULL);
820#else
821	char *lessopen;
822	char *cmd;
823	int len;
824	FILE *fd;
825#if HAVE_FILENO
826	int returnfd = 0;
827#endif
828
829	if (!use_lessopen || secure)
830		return (NULL);
831	ch_ungetchar(-1);
832	if ((lessopen = lgetenv("LESSOPEN")) == NULL)
833		return (NULL);
834	if (*lessopen == '|')
835	{
836		/*
837		 * If LESSOPEN starts with a |, it indicates
838		 * a "pipe preprocessor".
839		 */
840#if !HAVE_FILENO
841		error("LESSOPEN pipe is not supported", NULL_PARG);
842		return (NULL);
843#else
844		lessopen++;
845		returnfd = 1;
846		if (*lessopen == '-') {
847			/*
848			 * Lessopen preprocessor will accept "-" as a filename.
849			 */
850			lessopen++;
851		} else {
852			if (strcmp(filename, "-") == 0)
853				return (NULL);
854		}
855#endif
856	}
857
858	len = strlen(lessopen) + strlen(filename) + 2;
859	cmd = (char *) ecalloc(len, sizeof(char));
860	SNPRINTF1(cmd, len, lessopen, filename);
861	fd = shellcmd(cmd);
862	free(cmd);
863	if (fd == NULL)
864	{
865		/*
866		 * Cannot create the pipe.
867		 */
868		return (NULL);
869	}
870#if HAVE_FILENO
871	if (returnfd)
872	{
873		int f;
874		char c;
875
876		/*
877		 * Read one char to see if the pipe will produce any data.
878		 * If it does, push the char back on the pipe.
879		 */
880		f = fileno(fd);
881		SET_BINARY(f);
882		if (read(f, &c, 1) != 1)
883		{
884			/*
885			 * Pipe is empty.  This means there is no alt file.
886			 */
887			pclose(fd);
888			return (NULL);
889		}
890		ch_ungetchar(c);
891		*pfd = (void *) fd;
892		*pf = f;
893		return (save("-"));
894	}
895#endif
896	cmd = readfd(fd);
897	pclose(fd);
898	if (*cmd == '\0')
899		/*
900		 * Pipe is empty.  This means there is no alt file.
901		 */
902		return (NULL);
903	return (cmd);
904#endif /* HAVE_POPEN */
905}
906
907/*
908 * Close a replacement file.
909 */
910	public void
911close_altfile(altfilename, filename, pipefd)
912	char *altfilename;
913	char *filename;
914	void *pipefd;
915{
916#if HAVE_POPEN
917	char *lessclose;
918	FILE *fd;
919	char *cmd;
920	int len;
921
922	if (secure)
923		return;
924	if (pipefd != NULL)
925	{
926#if OS2
927		/*
928		 * The pclose function of OS/2 emx sometimes fails.
929		 * Send SIGINT to the piped process before closing it.
930		 */
931		kill(((FILE*)pipefd)->_pid, SIGINT);
932#endif
933		pclose((FILE*) pipefd);
934	}
935	if ((lessclose = lgetenv("LESSCLOSE")) == NULL)
936	     	return;
937	len = strlen(lessclose) + strlen(filename) + strlen(altfilename) + 2;
938	cmd = (char *) ecalloc(len, sizeof(char));
939	SNPRINTF2(cmd, len, lessclose, filename, altfilename);
940	fd = shellcmd(cmd);
941	free(cmd);
942	if (fd != NULL)
943		pclose(fd);
944#endif
945}
946
947/*
948 * Is the specified file a directory?
949 */
950	public int
951is_dir(filename)
952	char *filename;
953{
954	int isdir = 0;
955
956	filename = shell_unquote(filename);
957#if HAVE_STAT
958{
959	int r;
960	struct stat statbuf;
961
962	r = stat(filename, &statbuf);
963	isdir = (r >= 0 && S_ISDIR(statbuf.st_mode));
964}
965#else
966#ifdef _OSK
967{
968	register int f;
969
970	f = open(filename, S_IREAD | S_IFDIR);
971	if (f >= 0)
972		close(f);
973	isdir = (f >= 0);
974}
975#endif
976#endif
977	free(filename);
978	return (isdir);
979}
980
981/*
982 * Returns NULL if the file can be opened and
983 * is an ordinary file, otherwise an error message
984 * (if it cannot be opened or is a directory, etc.)
985 */
986	public char *
987bad_file(filename)
988	char *filename;
989{
990	register char *m = NULL;
991
992	filename = shell_unquote(filename);
993	if (!force_open && is_dir(filename))
994	{
995		static char is_a_dir[] = " is a directory";
996
997		m = (char *) ecalloc(strlen(filename) + sizeof(is_a_dir),
998			sizeof(char));
999		strcpy(m, filename);
1000		strcat(m, is_a_dir);
1001	} else
1002	{
1003#if HAVE_STAT
1004		int r;
1005		struct stat statbuf;
1006
1007		r = stat(filename, &statbuf);
1008		if (r < 0)
1009		{
1010			m = errno_message(filename);
1011		} else if (force_open)
1012		{
1013			m = NULL;
1014		} else if (!S_ISREG(statbuf.st_mode))
1015		{
1016			static char not_reg[] = " is not a regular file (use -f to see it)";
1017			m = (char *) ecalloc(strlen(filename) + sizeof(not_reg),
1018				sizeof(char));
1019			strcpy(m, filename);
1020			strcat(m, not_reg);
1021		}
1022#endif
1023	}
1024	free(filename);
1025	return (m);
1026}
1027
1028/*
1029 * Return the size of a file, as cheaply as possible.
1030 * In Unix, we can stat the file.
1031 */
1032	public POSITION
1033filesize(f)
1034	int f;
1035{
1036#if HAVE_STAT
1037	struct stat statbuf;
1038
1039	if (fstat(f, &statbuf) >= 0)
1040		return ((POSITION) statbuf.st_size);
1041#else
1042#ifdef _OSK
1043	long size;
1044
1045	if ((size = (long) _gs_size(f)) >= 0)
1046		return ((POSITION) size);
1047#endif
1048#endif
1049	return (seek_filesize(f));
1050}
1051
1052/*
1053 *
1054 */
1055	public char *
1056shell_coption()
1057{
1058	return ("-c");
1059}
1060