xref: /dragonfly/contrib/less/filename.c (revision f9993810)
1 /*
2  * Copyright (C) 1984-2022  Mark Nudelman
3  *
4  * You may distribute under the terms of either the GNU General Public
5  * License or the Less License, as specified in the README file.
6  *
7  * For more information, see the README file.
8  */
9 
10 
11 /*
12  * Routines to mess around with filenames (and files).
13  * Much of this is very OS dependent.
14  */
15 
16 #include "less.h"
17 #include "lglob.h"
18 #if MSDOS_COMPILER
19 #include <dos.h>
20 #if MSDOS_COMPILER==WIN32C && !defined(_MSC_VER)
21 #include <dir.h>
22 #endif
23 #if MSDOS_COMPILER==DJGPPC
24 #include <glob.h>
25 #include <dir.h>
26 #define _MAX_PATH      PATH_MAX
27 #endif
28 #endif
29 #ifdef _OSK
30 #include <rbf.h>
31 #ifndef _OSK_MWC32
32 #include <modes.h>
33 #endif
34 #endif
35 
36 #if HAVE_STAT
37 #include <sys/stat.h>
38 #ifndef S_ISDIR
39 #define S_ISDIR(m)      (((m) & S_IFMT) == S_IFDIR)
40 #endif
41 #ifndef S_ISREG
42 #define S_ISREG(m)      (((m) & S_IFMT) == S_IFREG)
43 #endif
44 #endif
45 
46 extern int force_open;
47 extern int secure;
48 extern int use_lessopen;
49 extern int ctldisp;
50 extern int utf_mode;
51 extern IFILE curr_ifile;
52 extern IFILE old_ifile;
53 #if SPACES_IN_FILENAMES
54 extern char openquote;
55 extern char closequote;
56 #endif
57 
58 /*
59  * Remove quotes around a filename.
60  */
61 	public char *
62 shell_unquote(str)
63 	char *str;
64 {
65 	char *name;
66 	char *p;
67 
68 	name = p = (char *) ecalloc(strlen(str)+1, sizeof(char));
69 	if (*str == openquote)
70 	{
71 		str++;
72 		while (*str != '\0')
73 		{
74 			if (*str == closequote)
75 			{
76 				if (str[1] != closequote)
77 					break;
78 				str++;
79 			}
80 			*p++ = *str++;
81 		}
82 	} else
83 	{
84 		char *esc = get_meta_escape();
85 		int esclen = (int) strlen(esc);
86 		while (*str != '\0')
87 		{
88 			if (esclen > 0 && strncmp(str, esc, esclen) == 0)
89 				str += esclen;
90 			*p++ = *str++;
91 		}
92 	}
93 	*p = '\0';
94 	return (name);
95 }
96 
97 /*
98  * Get the shell's escape character.
99  */
100 	public char *
101 get_meta_escape(VOID_PARAM)
102 {
103 	char *s;
104 
105 	s = lgetenv("LESSMETAESCAPE");
106 	if (s == NULL)
107 		s = DEF_METAESCAPE;
108 	return (s);
109 }
110 
111 /*
112  * Get the characters which the shell considers to be "metacharacters".
113  */
114 	static char *
115 metachars(VOID_PARAM)
116 {
117 	static char *mchars = NULL;
118 
119 	if (mchars == NULL)
120 	{
121 		mchars = lgetenv("LESSMETACHARS");
122 		if (mchars == NULL)
123 			mchars = DEF_METACHARS;
124 	}
125 	return (mchars);
126 }
127 
128 /*
129  * Is this a shell metacharacter?
130  */
131 	static int
132 metachar(c)
133 	char c;
134 {
135 	return (strchr(metachars(), c) != NULL);
136 }
137 
138 /*
139  * Insert a backslash before each metacharacter in a string.
140  */
141 	public char *
142 shell_quote(s)
143 	char *s;
144 {
145 	char *p;
146 	char *newstr;
147 	int len;
148 	char *esc = get_meta_escape();
149 	int esclen = (int) strlen(esc);
150 	int use_quotes = 0;
151 	int have_quotes = 0;
152 
153 	/*
154 	 * Determine how big a string we need to allocate.
155 	 */
156 	len = 1; /* Trailing null byte */
157 	for (p = s;  *p != '\0';  p++)
158 	{
159 		len++;
160 		if (*p == openquote || *p == closequote)
161 			have_quotes = 1;
162 		if (metachar(*p))
163 		{
164 			if (esclen == 0)
165 			{
166 				/*
167 				 * We've got a metachar, but this shell
168 				 * doesn't support escape chars.  Use quotes.
169 				 */
170 				use_quotes = 1;
171 			} else
172 			{
173 				/*
174 				 * Allow space for the escape char.
175 				 */
176 				len += esclen;
177 			}
178 		}
179 	}
180 	if (use_quotes)
181 	{
182 		if (have_quotes)
183 			/*
184 			 * We can't quote a string that contains quotes.
185 			 */
186 			return (NULL);
187 		len = (int) strlen(s) + 3;
188 	}
189 	/*
190 	 * Allocate and construct the new string.
191 	 */
192 	newstr = p = (char *) ecalloc(len, sizeof(char));
193 	if (use_quotes)
194 	{
195 		SNPRINTF3(newstr, len, "%c%s%c", openquote, s, closequote);
196 	} else
197 	{
198 		while (*s != '\0')
199 		{
200 			if (metachar(*s))
201 			{
202 				/*
203 				 * Add the escape char.
204 				 */
205 				strcpy(p, esc);
206 				p += esclen;
207 			}
208 			*p++ = *s++;
209 		}
210 		*p = '\0';
211 	}
212 	return (newstr);
213 }
214 
215 /*
216  * Return a pathname that points to a specified file in a specified directory.
217  * Return NULL if the file does not exist in the directory.
218  */
219 	public char *
220 dirfile(dirname, filename, must_exist)
221 	char *dirname;
222 	char *filename;
223 	int must_exist;
224 {
225 	char *pathname;
226 	int len;
227 	int f;
228 
229 	if (dirname == NULL || *dirname == '\0')
230 		return (NULL);
231 	/*
232 	 * Construct the full pathname.
233 	 */
234 	len = (int) (strlen(dirname) + strlen(filename) + 2);
235 	pathname = (char *) calloc(len, sizeof(char));
236 	if (pathname == NULL)
237 		return (NULL);
238 	SNPRINTF3(pathname, len, "%s%s%s", dirname, PATHNAME_SEP, filename);
239 	if (must_exist)
240 	{
241 		/*
242 		 * Make sure the file exists.
243 		 */
244 		f = open(pathname, OPEN_READ);
245 		if (f < 0)
246 		{
247 			free(pathname);
248 			pathname = NULL;
249 		} else
250 		{
251 			close(f);
252 		}
253 	}
254 	return (pathname);
255 }
256 
257 /*
258  * Return the full pathname of the given file in the "home directory".
259  */
260 	public char *
261 homefile(filename)
262 	char *filename;
263 {
264 	char *pathname;
265 
266 	/* Try $HOME/filename. */
267 	pathname = dirfile(lgetenv("HOME"), filename, 1);
268 	if (pathname != NULL)
269 		return (pathname);
270 #if OS2
271 	/* Try $INIT/filename. */
272 	pathname = dirfile(lgetenv("INIT"), filename, 1);
273 	if (pathname != NULL)
274 		return (pathname);
275 #endif
276 #if MSDOS_COMPILER || OS2
277 	/* Look for the file anywhere on search path. */
278 	pathname = (char *) ecalloc(_MAX_PATH, sizeof(char));
279 #if MSDOS_COMPILER==DJGPPC
280 	{
281 		char *res = searchpath(filename);
282 		if (res == 0)
283 			*pathname = '\0';
284 		else
285 			strcpy(pathname, res);
286 	}
287 #else
288 	_searchenv(filename, "PATH", pathname);
289 #endif
290 	if (*pathname != '\0')
291 		return (pathname);
292 	free(pathname);
293 #endif
294 	return (NULL);
295 }
296 
297 /*
298  * Expand a string, substituting any "%" with the current filename,
299  * and any "#" with the previous filename.
300  * But a string of N "%"s is just replaced with N-1 "%"s.
301  * Likewise for a string of N "#"s.
302  * {{ This is a lot of work just to support % and #. }}
303  */
304 	public char *
305 fexpand(s)
306 	char *s;
307 {
308 	char *fr, *to;
309 	int n;
310 	char *e;
311 	IFILE ifile;
312 
313 #define fchar_ifile(c) \
314 	((c) == '%' ? curr_ifile : \
315 	 (c) == '#' ? old_ifile : NULL_IFILE)
316 
317 	/*
318 	 * Make one pass to see how big a buffer we
319 	 * need to allocate for the expanded string.
320 	 */
321 	n = 0;
322 	for (fr = s;  *fr != '\0';  fr++)
323 	{
324 		switch (*fr)
325 		{
326 		case '%':
327 		case '#':
328 			if (fr > s && fr[-1] == *fr)
329 			{
330 				/*
331 				 * Second (or later) char in a string
332 				 * of identical chars.  Treat as normal.
333 				 */
334 				n++;
335 			} else if (fr[1] != *fr)
336 			{
337 				/*
338 				 * Single char (not repeated).  Treat specially.
339 				 */
340 				ifile = fchar_ifile(*fr);
341 				if (ifile == NULL_IFILE)
342 					n++;
343 				else
344 					n += (int) strlen(get_filename(ifile));
345 			}
346 			/*
347 			 * Else it is the first char in a string of
348 			 * identical chars.  Just discard it.
349 			 */
350 			break;
351 		default:
352 			n++;
353 			break;
354 		}
355 	}
356 
357 	e = (char *) ecalloc(n+1, sizeof(char));
358 
359 	/*
360 	 * Now copy the string, expanding any "%" or "#".
361 	 */
362 	to = e;
363 	for (fr = s;  *fr != '\0';  fr++)
364 	{
365 		switch (*fr)
366 		{
367 		case '%':
368 		case '#':
369 			if (fr > s && fr[-1] == *fr)
370 			{
371 				*to++ = *fr;
372 			} else if (fr[1] != *fr)
373 			{
374 				ifile = fchar_ifile(*fr);
375 				if (ifile == NULL_IFILE)
376 					*to++ = *fr;
377 				else
378 				{
379 					strcpy(to, get_filename(ifile));
380 					to += strlen(to);
381 				}
382 			}
383 			break;
384 		default:
385 			*to++ = *fr;
386 			break;
387 		}
388 	}
389 	*to = '\0';
390 	return (e);
391 }
392 
393 
394 #if TAB_COMPLETE_FILENAME
395 
396 /*
397  * Return a blank-separated list of filenames which "complete"
398  * the given string.
399  */
400 	public char *
401 fcomplete(s)
402 	char *s;
403 {
404 	char *fpat;
405 	char *qs;
406 
407 	if (secure)
408 		return (NULL);
409 	/*
410 	 * Complete the filename "s" by globbing "s*".
411 	 */
412 #if MSDOS_COMPILER && (MSDOS_COMPILER == MSOFTC || MSDOS_COMPILER == BORLANDC)
413 	/*
414 	 * But in DOS, we have to glob "s*.*".
415 	 * But if the final component of the filename already has
416 	 * a dot in it, just do "s*".
417 	 * (Thus, "FILE" is globbed as "FILE*.*",
418 	 *  but "FILE.A" is globbed as "FILE.A*").
419 	 */
420 	{
421 		char *slash;
422 		int len;
423 		for (slash = s+strlen(s)-1;  slash > s;  slash--)
424 			if (*slash == *PATHNAME_SEP || *slash == '/')
425 				break;
426 		len = (int) strlen(s) + 4;
427 		fpat = (char *) ecalloc(len, sizeof(char));
428 		if (strchr(slash, '.') == NULL)
429 			SNPRINTF1(fpat, len, "%s*.*", s);
430 		else
431 			SNPRINTF1(fpat, len, "%s*", s);
432 	}
433 #else
434 	{
435 	int len = (int) strlen(s) + 2;
436 	fpat = (char *) ecalloc(len, sizeof(char));
437 	SNPRINTF1(fpat, len, "%s*", s);
438 	}
439 #endif
440 	qs = lglob(fpat);
441 	s = shell_unquote(qs);
442 	if (strcmp(s,fpat) == 0)
443 	{
444 		/*
445 		 * The filename didn't expand.
446 		 */
447 		free(qs);
448 		qs = NULL;
449 	}
450 	free(s);
451 	free(fpat);
452 	return (qs);
453 }
454 #endif
455 
456 /*
457  * Try to determine if a file is "binary".
458  * This is just a guess, and we need not try too hard to make it accurate.
459  */
460 	public int
461 bin_file(f)
462 	int f;
463 {
464 	int n;
465 	int bin_count = 0;
466 	char data[256];
467 	char* p;
468 	char* edata;
469 
470 	if (!seekable(f))
471 		return (0);
472 	if (lseek(f, (off_t)0, SEEK_SET) == BAD_LSEEK)
473 		return (0);
474 	n = read(f, data, sizeof(data));
475 	if (n <= 0)
476 		return (0);
477 	edata = &data[n];
478 	for (p = data;  p < edata;  )
479 	{
480 		if (utf_mode && !is_utf8_well_formed(p, edata-p))
481 		{
482 			bin_count++;
483 			utf_skip_to_lead(&p, edata);
484 		} else
485 		{
486 			LWCHAR c = step_char(&p, +1, edata);
487 			struct ansi_state *pansi;
488 			if (ctldisp == OPT_ONPLUS && (pansi = ansi_start(c)) != NULL)
489 			{
490 				skip_ansi(pansi, &p, edata);
491 				ansi_done(pansi);
492 			} else if (binary_char(c))
493 				bin_count++;
494 		}
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
507 seek_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 #if HAVE_POPEN
519 /*
520  * Read a string from a file.
521  * Return a pointer to the string in memory.
522  */
523 	static char *
524 readfd(fd)
525 	FILE *fd;
526 {
527 	int len;
528 	int ch;
529 	char *buf;
530 	char *p;
531 
532 	/*
533 	 * Make a guess about how many chars in the string
534 	 * and allocate a buffer to hold it.
535 	 */
536 	len = 100;
537 	buf = (char *) ecalloc(len, sizeof(char));
538 	for (p = buf;  ;  p++)
539 	{
540 		if ((ch = getc(fd)) == '\n' || ch == EOF)
541 			break;
542 		if (p - buf >= len-1)
543 		{
544 			/*
545 			 * The string is too big to fit in the buffer we have.
546 			 * Allocate a new buffer, twice as big.
547 			 */
548 			len *= 2;
549 			*p = '\0';
550 			p = (char *) ecalloc(len, sizeof(char));
551 			strcpy(p, buf);
552 			free(buf);
553 			buf = p;
554 			p = buf + strlen(buf);
555 		}
556 		*p = ch;
557 	}
558 	*p = '\0';
559 	return (buf);
560 }
561 
562 /*
563  * Execute a shell command.
564  * Return a pointer to a pipe connected to the shell command's standard output.
565  */
566 	static FILE *
567 shellcmd(cmd)
568 	char *cmd;
569 {
570 	FILE *fd;
571 
572 #if HAVE_SHELL
573 	char *shell;
574 
575 	shell = lgetenv("SHELL");
576 	if (!isnullenv(shell))
577 	{
578 		char *scmd;
579 		char *esccmd;
580 
581 		/*
582 		 * Read the output of <$SHELL -c cmd>.
583 		 * Escape any metacharacters in the command.
584 		 */
585 		esccmd = shell_quote(cmd);
586 		if (esccmd == NULL)
587 		{
588 			fd = popen(cmd, "r");
589 		} else
590 		{
591 			int len = (int) (strlen(shell) + strlen(esccmd) + 5);
592 			scmd = (char *) ecalloc(len, sizeof(char));
593 			SNPRINTF3(scmd, len, "%s %s %s", shell, shell_coption(), esccmd);
594 			free(esccmd);
595 			fd = popen(scmd, "r");
596 			free(scmd);
597 		}
598 	} else
599 #endif
600 	{
601 		fd = popen(cmd, "r");
602 	}
603 	/*
604 	 * Redirection in `popen' might have messed with the
605 	 * standard devices.  Restore binary input mode.
606 	 */
607 	SET_BINARY(0);
608 	return (fd);
609 }
610 
611 #endif /* HAVE_POPEN */
612 
613 
614 /*
615  * Expand a filename, doing any system-specific metacharacter substitutions.
616  */
617 	public char *
618 lglob(filename)
619 	char *filename;
620 {
621 	char *gfilename;
622 
623 	filename = fexpand(filename);
624 	if (secure)
625 		return (filename);
626 
627 #ifdef DECL_GLOB_LIST
628 {
629 	/*
630 	 * The globbing function returns a list of names.
631 	 */
632 	int length;
633 	char *p;
634 	char *qfilename;
635 	DECL_GLOB_LIST(list)
636 
637 	GLOB_LIST(filename, list);
638 	if (GLOB_LIST_FAILED(list))
639 	{
640 		return (filename);
641 	}
642 	length = 1; /* Room for trailing null byte */
643 	for (SCAN_GLOB_LIST(list, p))
644 	{
645 		INIT_GLOB_LIST(list, p);
646 		qfilename = shell_quote(p);
647 		if (qfilename != NULL)
648 		{
649 			length += strlen(qfilename) + 1;
650 			free(qfilename);
651 		}
652 	}
653 	gfilename = (char *) ecalloc(length, sizeof(char));
654 	for (SCAN_GLOB_LIST(list, p))
655 	{
656 		INIT_GLOB_LIST(list, p);
657 		qfilename = shell_quote(p);
658 		if (qfilename != NULL)
659 		{
660 			sprintf(gfilename + strlen(gfilename), "%s ", qfilename);
661 			free(qfilename);
662 		}
663 	}
664 	/*
665 	 * Overwrite the final trailing space with a null terminator.
666 	 */
667 	*--p = '\0';
668 	GLOB_LIST_DONE(list);
669 }
670 #else
671 #ifdef DECL_GLOB_NAME
672 {
673 	/*
674 	 * The globbing function returns a single name, and
675 	 * is called multiple times to walk thru all names.
676 	 */
677 	char *p;
678 	int len;
679 	int n;
680 	char *pfilename;
681 	char *qfilename;
682 	DECL_GLOB_NAME(fnd,drive,dir,fname,ext,handle)
683 
684 	GLOB_FIRST_NAME(filename, &fnd, handle);
685 	if (GLOB_FIRST_FAILED(handle))
686 	{
687 		return (filename);
688 	}
689 
690 	_splitpath(filename, drive, dir, fname, ext);
691 	len = 100;
692 	gfilename = (char *) ecalloc(len, sizeof(char));
693 	p = gfilename;
694 	do {
695 		n = (int) (strlen(drive) + strlen(dir) + strlen(fnd.GLOB_NAME) + 1);
696 		pfilename = (char *) ecalloc(n, sizeof(char));
697 		SNPRINTF3(pfilename, n, "%s%s%s", drive, dir, fnd.GLOB_NAME);
698 		qfilename = shell_quote(pfilename);
699 		free(pfilename);
700 		if (qfilename != NULL)
701 		{
702 			n = (int) strlen(qfilename);
703 			while (p - gfilename + n + 2 >= len)
704 			{
705 				/*
706 				 * No room in current buffer.
707 				 * Allocate a bigger one.
708 				 */
709 				len *= 2;
710 				*p = '\0';
711 				p = (char *) ecalloc(len, sizeof(char));
712 				strcpy(p, gfilename);
713 				free(gfilename);
714 				gfilename = p;
715 				p = gfilename + strlen(gfilename);
716 			}
717 			strcpy(p, qfilename);
718 			free(qfilename);
719 			p += n;
720 			*p++ = ' ';
721 		}
722 	} while (GLOB_NEXT_NAME(handle, &fnd) == 0);
723 
724 	/*
725 	 * Overwrite the final trailing space with a null terminator.
726 	 */
727 	*--p = '\0';
728 	GLOB_NAME_DONE(handle);
729 }
730 #else
731 #if HAVE_POPEN
732 {
733 	/*
734 	 * We get the shell to glob the filename for us by passing
735 	 * an "echo" command to the shell and reading its output.
736 	 */
737 	FILE *fd;
738 	char *s;
739 	char *lessecho;
740 	char *cmd;
741 	char *esc;
742 	int len;
743 
744 	esc = get_meta_escape();
745 	if (strlen(esc) == 0)
746 		esc = "-";
747 	esc = shell_quote(esc);
748 	if (esc == NULL)
749 	{
750 		return (filename);
751 	}
752 	lessecho = lgetenv("LESSECHO");
753 	if (isnullenv(lessecho))
754 		lessecho = "lessecho";
755 	/*
756 	 * Invoke lessecho, and read its output (a globbed list of filenames).
757 	 */
758 	len = (int) (strlen(lessecho) + strlen(filename) + (7*strlen(metachars())) + 24);
759 	cmd = (char *) ecalloc(len, sizeof(char));
760 	SNPRINTF4(cmd, len, "%s -p0x%x -d0x%x -e%s ", lessecho,
761 		(unsigned char) openquote, (unsigned char) closequote, esc);
762 	free(esc);
763 	for (s = metachars();  *s != '\0';  s++)
764 		sprintf(cmd + strlen(cmd), "-n0x%x ", (unsigned char) *s);
765 	sprintf(cmd + strlen(cmd), "-- %s", filename);
766 	fd = shellcmd(cmd);
767 	free(cmd);
768 	if (fd == NULL)
769 	{
770 		/*
771 		 * Cannot create the pipe.
772 		 * Just return the original (fexpanded) filename.
773 		 */
774 		return (filename);
775 	}
776 	gfilename = readfd(fd);
777 	pclose(fd);
778 	if (*gfilename == '\0')
779 	{
780 		free(gfilename);
781 		return (filename);
782 	}
783 }
784 #else
785 	/*
786 	 * No globbing functions at all.  Just use the fexpanded filename.
787 	 */
788 	gfilename = save(filename);
789 #endif
790 #endif
791 #endif
792 	free(filename);
793 	return (gfilename);
794 }
795 
796 /*
797  * Return canonical pathname.
798  */
799 	public char *
800 lrealpath(path)
801 	char *path;
802 {
803 #if HAVE_REALPATH
804 	char rpath[PATH_MAX];
805 	if (realpath(path, rpath) != NULL)
806 		return (save(rpath));
807 #endif
808 	return (save(path));
809 }
810 
811 #if HAVE_POPEN
812 /*
813  * Return number of %s escapes in a string.
814  * Return a large number if there are any other % escapes besides %s.
815  */
816 	static int
817 num_pct_s(lessopen)
818 	char *lessopen;
819 {
820 	int num = 0;
821 
822 	while (*lessopen != '\0')
823 	{
824 		if (*lessopen == '%')
825 		{
826 			if (lessopen[1] == '%')
827 				++lessopen;
828 			else if (lessopen[1] == 's')
829 				++num;
830 			else
831 				return (999);
832 		}
833 		++lessopen;
834 	}
835 	return (num);
836 }
837 #endif
838 
839 /*
840  * See if we should open a "replacement file"
841  * instead of the file we're about to open.
842  */
843 	public char *
844 open_altfile(filename, pf, pfd)
845 	char *filename;
846 	int *pf;
847 	void **pfd;
848 {
849 #if !HAVE_POPEN
850 	return (NULL);
851 #else
852 	char *lessopen;
853 	char *qfilename;
854 	char *cmd;
855 	int len;
856 	FILE *fd;
857 #if HAVE_FILENO
858 	int returnfd = 0;
859 #endif
860 
861 	if (!use_lessopen || secure)
862 		return (NULL);
863 	ch_ungetchar(-1);
864 	if ((lessopen = lgetenv("LESSOPEN")) == NULL)
865 		return (NULL);
866 	while (*lessopen == '|')
867 	{
868 		/*
869 		 * If LESSOPEN starts with a |, it indicates
870 		 * a "pipe preprocessor".
871 		 */
872 #if !HAVE_FILENO
873 		error("LESSOPEN pipe is not supported", NULL_PARG);
874 		return (NULL);
875 #else
876 		lessopen++;
877 		returnfd++;
878 #endif
879 	}
880 	if (*lessopen == '-')
881 	{
882 		/*
883 		 * Lessopen preprocessor will accept "-" as a filename.
884 		 */
885 		lessopen++;
886 	} else
887 	{
888 		if (strcmp(filename, "-") == 0)
889 			return (NULL);
890 	}
891 	if (num_pct_s(lessopen) != 1)
892 	{
893 		error("LESSOPEN ignored: must contain exactly one %%s", NULL_PARG);
894 		return (NULL);
895 	}
896 
897 	qfilename = shell_quote(filename);
898 	len = (int) (strlen(lessopen) + strlen(qfilename) + 2);
899 	cmd = (char *) ecalloc(len, sizeof(char));
900 	SNPRINTF1(cmd, len, lessopen, qfilename);
901 	free(qfilename);
902 	fd = shellcmd(cmd);
903 	free(cmd);
904 	if (fd == NULL)
905 	{
906 		/*
907 		 * Cannot create the pipe.
908 		 */
909 		return (NULL);
910 	}
911 #if HAVE_FILENO
912 	if (returnfd)
913 	{
914 		char c;
915 		int f;
916 
917 		/*
918 		 * The alt file is a pipe. Read one char
919 		 * to see if the pipe will produce any data.
920 		 * If it does, push the char back on the pipe.
921 		 */
922 		f = fileno(fd);
923 		SET_BINARY(f);
924 		if (read(f, &c, 1) != 1)
925 		{
926 			/*
927 			 * Pipe is empty.
928 			 * If more than 1 pipe char was specified,
929 			 * the exit status tells whether the file itself
930 			 * is empty, or if there is no alt file.
931 			 * If only one pipe char, just assume no alt file.
932 			 */
933 			int status = pclose(fd);
934 			if (returnfd > 1 && status == 0) {
935 				/* File is empty. */
936 				*pfd = NULL;
937 				*pf = -1;
938 				return (save(FAKE_EMPTYFILE));
939 			}
940 			/* No alt file. */
941 			return (NULL);
942 		}
943 		/* Alt pipe contains data, so use it. */
944 		ch_ungetchar(c);
945 		*pfd = (void *) fd;
946 		*pf = f;
947 		return (save("-"));
948 	}
949 #endif
950 	/* The alt file is a regular file. Read its name from LESSOPEN. */
951 	cmd = readfd(fd);
952 	pclose(fd);
953 	if (*cmd == '\0')
954 	{
955 		/*
956 		 * Pipe is empty.  This means there is no alt file.
957 		 */
958 		free(cmd);
959 		return (NULL);
960 	}
961 	return (cmd);
962 #endif /* HAVE_POPEN */
963 }
964 
965 /*
966  * Close a replacement file.
967  */
968 	public void
969 close_altfile(altfilename, filename)
970 	char *altfilename;
971 	char *filename;
972 {
973 #if HAVE_POPEN
974 	char *lessclose;
975 	char *qfilename;
976 	char *qaltfilename;
977 	FILE *fd;
978 	char *cmd;
979 	int len;
980 
981 	if (secure)
982 		return;
983 	ch_ungetchar(-1);
984 	if ((lessclose = lgetenv("LESSCLOSE")) == NULL)
985 		return;
986 	if (num_pct_s(lessclose) > 2)
987 	{
988 		error("LESSCLOSE ignored; must contain no more than 2 %%s", NULL_PARG);
989 		return;
990 	}
991 	qfilename = shell_quote(filename);
992 	qaltfilename = shell_quote(altfilename);
993 	len = (int) (strlen(lessclose) + strlen(qfilename) + strlen(qaltfilename) + 2);
994 	cmd = (char *) ecalloc(len, sizeof(char));
995 	SNPRINTF2(cmd, len, lessclose, qfilename, qaltfilename);
996 	free(qaltfilename);
997 	free(qfilename);
998 	fd = shellcmd(cmd);
999 	free(cmd);
1000 	if (fd != NULL)
1001 		pclose(fd);
1002 #endif
1003 }
1004 
1005 /*
1006  * Is the specified file a directory?
1007  */
1008 	public int
1009 is_dir(filename)
1010 	char *filename;
1011 {
1012 	int isdir = 0;
1013 
1014 #if HAVE_STAT
1015 {
1016 	int r;
1017 	struct stat statbuf;
1018 
1019 	r = stat(filename, &statbuf);
1020 	isdir = (r >= 0 && S_ISDIR(statbuf.st_mode));
1021 }
1022 #else
1023 #ifdef _OSK
1024 {
1025 	int f;
1026 
1027 	f = open(filename, S_IREAD | S_IFDIR);
1028 	if (f >= 0)
1029 		close(f);
1030 	isdir = (f >= 0);
1031 }
1032 #endif
1033 #endif
1034 	return (isdir);
1035 }
1036 
1037 /*
1038  * Returns NULL if the file can be opened and
1039  * is an ordinary file, otherwise an error message
1040  * (if it cannot be opened or is a directory, etc.)
1041  */
1042 	public char *
1043 bad_file(filename)
1044 	char *filename;
1045 {
1046 	char *m = NULL;
1047 
1048 	if (!force_open && is_dir(filename))
1049 	{
1050 		static char is_a_dir[] = " is a directory";
1051 
1052 		m = (char *) ecalloc(strlen(filename) + sizeof(is_a_dir),
1053 			sizeof(char));
1054 		strcpy(m, filename);
1055 		strcat(m, is_a_dir);
1056 	} else
1057 	{
1058 #if HAVE_STAT
1059 		int r;
1060 		struct stat statbuf;
1061 
1062 		r = stat(filename, &statbuf);
1063 		if (r < 0)
1064 		{
1065 			m = errno_message(filename);
1066 		} else if (force_open)
1067 		{
1068 			m = NULL;
1069 		} else if (!S_ISREG(statbuf.st_mode))
1070 		{
1071 			static char not_reg[] = " is not a regular file (use -f to see it)";
1072 			m = (char *) ecalloc(strlen(filename) + sizeof(not_reg),
1073 				sizeof(char));
1074 			strcpy(m, filename);
1075 			strcat(m, not_reg);
1076 		}
1077 #endif
1078 	}
1079 	return (m);
1080 }
1081 
1082 /*
1083  * Return the size of a file, as cheaply as possible.
1084  * In Unix, we can stat the file.
1085  */
1086 	public POSITION
1087 filesize(f)
1088 	int f;
1089 {
1090 #if HAVE_STAT
1091 	struct stat statbuf;
1092 
1093 	if (fstat(f, &statbuf) >= 0)
1094 		return ((POSITION) statbuf.st_size);
1095 #else
1096 #ifdef _OSK
1097 	long size;
1098 
1099 	if ((size = (long) _gs_size(f)) >= 0)
1100 		return ((POSITION) size);
1101 #endif
1102 #endif
1103 	return (seek_filesize(f));
1104 }
1105 
1106 /*
1107  *
1108  */
1109 	public char *
1110 shell_coption(VOID_PARAM)
1111 {
1112 	return ("-c");
1113 }
1114 
1115 /*
1116  * Return last component of a pathname.
1117  */
1118 	public char *
1119 last_component(name)
1120 	char *name;
1121 {
1122 	char *slash;
1123 
1124 	for (slash = name + strlen(name);  slash > name; )
1125 	{
1126 		--slash;
1127 		if (*slash == *PATHNAME_SEP || *slash == '/')
1128 			return (slash + 1);
1129 	}
1130 	return (name);
1131 }
1132