1 /*
2 * Copyright (C) 1984-2021 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 *
shell_unquote(str)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 *
get_meta_escape(VOID_PARAM)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 *
metachars(VOID_PARAM)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
metachar(c)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 *
shell_quote(s)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 *
dirfile(dirname,filename,must_exist)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 *
homefile(filename)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 *
fexpand(s)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 *
fcomplete(s)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
bin_file(f)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-data))
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
seek_filesize(f)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 *
readfd(fd)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 *
shellcmd(cmd)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 *
lglob(filename)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, openquote, closequote, esc);
761 free(esc);
762 for (s = metachars(); *s != '\0'; s++)
763 sprintf(cmd + strlen(cmd), "-n0x%x ", *s);
764 sprintf(cmd + strlen(cmd), "-- %s", filename);
765 fd = shellcmd(cmd);
766 free(cmd);
767 if (fd == NULL)
768 {
769 /*
770 * Cannot create the pipe.
771 * Just return the original (fexpanded) filename.
772 */
773 return (filename);
774 }
775 gfilename = readfd(fd);
776 pclose(fd);
777 if (*gfilename == '\0')
778 {
779 free(gfilename);
780 return (filename);
781 }
782 }
783 #else
784 /*
785 * No globbing functions at all. Just use the fexpanded filename.
786 */
787 gfilename = save(filename);
788 #endif
789 #endif
790 #endif
791 free(filename);
792 return (gfilename);
793 }
794
795 /*
796 * Return canonical pathname.
797 */
798 public char *
lrealpath(path)799 lrealpath(path)
800 char *path;
801 {
802 #if HAVE_REALPATH
803 char rpath[PATH_MAX];
804 if (realpath(path, rpath) != NULL)
805 return (save(rpath));
806 #endif
807 return (save(path));
808 }
809
810 #if HAVE_POPEN
811 /*
812 * Return number of %s escapes in a string.
813 * Return a large number if there are any other % escapes besides %s.
814 */
815 static int
num_pct_s(lessopen)816 num_pct_s(lessopen)
817 char *lessopen;
818 {
819 int num = 0;
820
821 while (*lessopen != '\0')
822 {
823 if (*lessopen == '%')
824 {
825 if (lessopen[1] == '%')
826 ++lessopen;
827 else if (lessopen[1] == 's')
828 ++num;
829 else
830 return (999);
831 }
832 ++lessopen;
833 }
834 return (num);
835 }
836 #endif
837
838 /*
839 * See if we should open a "replacement file"
840 * instead of the file we're about to open.
841 */
842 public char *
open_altfile(filename,pf,pfd)843 open_altfile(filename, pf, pfd)
844 char *filename;
845 int *pf;
846 void **pfd;
847 {
848 #if !HAVE_POPEN
849 return (NULL);
850 #else
851 char *lessopen;
852 char *qfilename;
853 char *cmd;
854 int len;
855 FILE *fd;
856 #if HAVE_FILENO
857 int returnfd = 0;
858 #endif
859
860 if (!use_lessopen || secure)
861 return (NULL);
862 ch_ungetchar(-1);
863 if ((lessopen = lgetenv("LESSOPEN")) == NULL)
864 return (NULL);
865 while (*lessopen == '|')
866 {
867 /*
868 * If LESSOPEN starts with a |, it indicates
869 * a "pipe preprocessor".
870 */
871 #if !HAVE_FILENO
872 error("LESSOPEN pipe is not supported", NULL_PARG);
873 return (NULL);
874 #else
875 lessopen++;
876 returnfd++;
877 #endif
878 }
879 if (*lessopen == '-')
880 {
881 /*
882 * Lessopen preprocessor will accept "-" as a filename.
883 */
884 lessopen++;
885 } else
886 {
887 if (strcmp(filename, "-") == 0)
888 return (NULL);
889 }
890 if (num_pct_s(lessopen) != 1)
891 {
892 error("LESSOPEN ignored: must contain exactly one %%s", NULL_PARG);
893 return (NULL);
894 }
895
896 qfilename = shell_quote(filename);
897 len = (int) (strlen(lessopen) + strlen(qfilename) + 2);
898 cmd = (char *) ecalloc(len, sizeof(char));
899 SNPRINTF1(cmd, len, lessopen, qfilename);
900 free(qfilename);
901 fd = shellcmd(cmd);
902 free(cmd);
903 if (fd == NULL)
904 {
905 /*
906 * Cannot create the pipe.
907 */
908 return (NULL);
909 }
910 #if HAVE_FILENO
911 if (returnfd)
912 {
913 char c;
914 int f;
915
916 /*
917 * The alt file is a pipe. Read one char
918 * to see if the pipe will produce any data.
919 * If it does, push the char back on the pipe.
920 */
921 f = fileno(fd);
922 SET_BINARY(f);
923 if (read(f, &c, 1) != 1)
924 {
925 /*
926 * Pipe is empty.
927 * If more than 1 pipe char was specified,
928 * the exit status tells whether the file itself
929 * is empty, or if there is no alt file.
930 * If only one pipe char, just assume no alt file.
931 */
932 int status = pclose(fd);
933 if (returnfd > 1 && status == 0) {
934 /* File is empty. */
935 *pfd = NULL;
936 *pf = -1;
937 return (save(FAKE_EMPTYFILE));
938 }
939 /* No alt file. */
940 return (NULL);
941 }
942 /* Alt pipe contains data, so use it. */
943 ch_ungetchar(c);
944 *pfd = (void *) fd;
945 *pf = f;
946 return (save("-"));
947 }
948 #endif
949 /* The alt file is a regular file. Read its name from LESSOPEN. */
950 cmd = readfd(fd);
951 pclose(fd);
952 if (*cmd == '\0')
953 {
954 /*
955 * Pipe is empty. This means there is no alt file.
956 */
957 free(cmd);
958 return (NULL);
959 }
960 return (cmd);
961 #endif /* HAVE_POPEN */
962 }
963
964 /*
965 * Close a replacement file.
966 */
967 public void
close_altfile(altfilename,filename)968 close_altfile(altfilename, filename)
969 char *altfilename;
970 char *filename;
971 {
972 #if HAVE_POPEN
973 char *lessclose;
974 FILE *fd;
975 char *cmd;
976 int len;
977
978 if (secure)
979 return;
980 ch_ungetchar(-1);
981 if ((lessclose = lgetenv("LESSCLOSE")) == NULL)
982 return;
983 if (num_pct_s(lessclose) > 2)
984 {
985 error("LESSCLOSE ignored; must contain no more than 2 %%s", NULL_PARG);
986 return;
987 }
988 len = (int) (strlen(lessclose) + strlen(filename) + strlen(altfilename) + 2);
989 cmd = (char *) ecalloc(len, sizeof(char));
990 SNPRINTF2(cmd, len, lessclose, filename, altfilename);
991 fd = shellcmd(cmd);
992 free(cmd);
993 if (fd != NULL)
994 pclose(fd);
995 #endif
996 }
997
998 /*
999 * Is the specified file a directory?
1000 */
1001 public int
is_dir(filename)1002 is_dir(filename)
1003 char *filename;
1004 {
1005 int isdir = 0;
1006
1007 #if HAVE_STAT
1008 {
1009 int r;
1010 struct stat statbuf;
1011
1012 r = stat(filename, &statbuf);
1013 isdir = (r >= 0 && S_ISDIR(statbuf.st_mode));
1014 }
1015 #else
1016 #ifdef _OSK
1017 {
1018 int f;
1019
1020 f = open(filename, S_IREAD | S_IFDIR);
1021 if (f >= 0)
1022 close(f);
1023 isdir = (f >= 0);
1024 }
1025 #endif
1026 #endif
1027 return (isdir);
1028 }
1029
1030 /*
1031 * Returns NULL if the file can be opened and
1032 * is an ordinary file, otherwise an error message
1033 * (if it cannot be opened or is a directory, etc.)
1034 */
1035 public char *
bad_file(filename)1036 bad_file(filename)
1037 char *filename;
1038 {
1039 char *m = NULL;
1040
1041 if (!force_open && is_dir(filename))
1042 {
1043 static char is_a_dir[] = " is a directory";
1044
1045 m = (char *) ecalloc(strlen(filename) + sizeof(is_a_dir),
1046 sizeof(char));
1047 strcpy(m, filename);
1048 strcat(m, is_a_dir);
1049 } else
1050 {
1051 #if HAVE_STAT
1052 int r;
1053 struct stat statbuf;
1054
1055 r = stat(filename, &statbuf);
1056 if (r < 0)
1057 {
1058 m = errno_message(filename);
1059 } else if (force_open)
1060 {
1061 m = NULL;
1062 } else if (!S_ISREG(statbuf.st_mode))
1063 {
1064 static char not_reg[] = " is not a regular file (use -f to see it)";
1065 m = (char *) ecalloc(strlen(filename) + sizeof(not_reg),
1066 sizeof(char));
1067 strcpy(m, filename);
1068 strcat(m, not_reg);
1069 }
1070 #endif
1071 }
1072 return (m);
1073 }
1074
1075 /*
1076 * Return the size of a file, as cheaply as possible.
1077 * In Unix, we can stat the file.
1078 */
1079 public POSITION
filesize(f)1080 filesize(f)
1081 int f;
1082 {
1083 #if HAVE_STAT
1084 struct stat statbuf;
1085
1086 if (fstat(f, &statbuf) >= 0)
1087 return ((POSITION) statbuf.st_size);
1088 #else
1089 #ifdef _OSK
1090 long size;
1091
1092 if ((size = (long) _gs_size(f)) >= 0)
1093 return ((POSITION) size);
1094 #endif
1095 #endif
1096 return (seek_filesize(f));
1097 }
1098
1099 /*
1100 *
1101 */
1102 public char *
shell_coption(VOID_PARAM)1103 shell_coption(VOID_PARAM)
1104 {
1105 return ("-c");
1106 }
1107
1108 /*
1109 * Return last component of a pathname.
1110 */
1111 public char *
last_component(name)1112 last_component(name)
1113 char *name;
1114 {
1115 char *slash;
1116
1117 for (slash = name + strlen(name); slash > name; )
1118 {
1119 --slash;
1120 if (*slash == *PATHNAME_SEP || *slash == '/')
1121 return (slash + 1);
1122 }
1123 return (name);
1124 }
1125