1 /*
2 * Copyright (C) 1984-2012 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 #if OS2
36 #include <signal.h>
37 #endif
38
39 #if HAVE_STAT
40 #include <sys/stat.h>
41 #ifndef S_ISDIR
42 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
43 #endif
44 #ifndef S_ISREG
45 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
46 #endif
47 #endif
48
49
50 extern int force_open;
51 extern int secure;
52 extern int use_lessopen;
53 extern int ctldisp;
54 extern int utf_mode;
55 extern IFILE curr_ifile;
56 extern IFILE old_ifile;
57 #if SPACES_IN_FILENAMES
58 extern char openquote;
59 extern char closequote;
60 #endif
61
62 /*
63 * Remove quotes around a filename.
64 */
65 public char *
shell_unquote(str)66 shell_unquote(str)
67 char *str;
68 {
69 char *name;
70 char *p;
71
72 name = p = (char *) ecalloc(strlen(str)+1, sizeof(char));
73 if (*str == openquote)
74 {
75 str++;
76 while (*str != '\0')
77 {
78 if (*str == closequote)
79 {
80 if (str[1] != closequote)
81 break;
82 str++;
83 }
84 *p++ = *str++;
85 }
86 } else
87 {
88 char *esc = get_meta_escape();
89 int esclen = strlen(esc);
90 while (*str != '\0')
91 {
92 if (esclen > 0 && strncmp(str, esc, esclen) == 0)
93 str += esclen;
94 *p++ = *str++;
95 }
96 }
97 *p = '\0';
98 return (name);
99 }
100
101 /*
102 * Get the shell's escape character.
103 */
104 public char *
get_meta_escape()105 get_meta_escape()
106 {
107 char *s;
108
109 s = lgetenv("LESSMETAESCAPE");
110 if (s == NULL)
111 s = DEF_METAESCAPE;
112 return (s);
113 }
114
115 /*
116 * Get the characters which the shell considers to be "metacharacters".
117 */
118 static char *
metachars()119 metachars()
120 {
121 static char *mchars = NULL;
122
123 if (mchars == NULL)
124 {
125 mchars = lgetenv("LESSMETACHARS");
126 if (mchars == NULL)
127 mchars = DEF_METACHARS;
128 }
129 return (mchars);
130 }
131
132 /*
133 * Is this a shell metacharacter?
134 */
135 static int
metachar(c)136 metachar(c)
137 char c;
138 {
139 return (strchr(metachars(), c) != NULL);
140 }
141
142 /*
143 * Insert a backslash before each metacharacter in a string.
144 */
145 public char *
shell_quote(s)146 shell_quote(s)
147 char *s;
148 {
149 char *p;
150 char *newstr;
151 int len;
152 char *esc = get_meta_escape();
153 int esclen = strlen(esc);
154 int use_quotes = 0;
155 int have_quotes = 0;
156
157 /*
158 * Determine how big a string we need to allocate.
159 */
160 len = 1; /* Trailing null byte */
161 for (p = s; *p != '\0'; p++)
162 {
163 len++;
164 if (*p == openquote || *p == closequote)
165 have_quotes = 1;
166 if (metachar(*p))
167 {
168 if (esclen == 0)
169 {
170 /*
171 * We've got a metachar, but this shell
172 * doesn't support escape chars. Use quotes.
173 */
174 use_quotes = 1;
175 } else
176 {
177 /*
178 * Allow space for the escape char.
179 */
180 len += esclen;
181 }
182 }
183 }
184 if (use_quotes)
185 {
186 if (have_quotes)
187 /*
188 * We can't quote a string that contains quotes.
189 */
190 return (NULL);
191 len = strlen(s) + 3;
192 }
193 /*
194 * Allocate and construct the new string.
195 */
196 newstr = p = (char *) ecalloc(len, sizeof(char));
197 if (use_quotes)
198 {
199 SNPRINTF3(newstr, len, "%c%s%c", openquote, s, closequote);
200 } else
201 {
202 while (*s != '\0')
203 {
204 if (metachar(*s))
205 {
206 /*
207 * Add the escape char.
208 */
209 strcpy(p, esc);
210 p += esclen;
211 }
212 *p++ = *s++;
213 }
214 *p = '\0';
215 }
216 return (newstr);
217 }
218
219 /*
220 * Return a pathname that points to a specified file in a specified directory.
221 * Return NULL if the file does not exist in the directory.
222 */
223 static char *
dirfile(dirname,filename)224 dirfile(dirname, filename)
225 char *dirname;
226 char *filename;
227 {
228 char *pathname;
229 char *qpathname;
230 int len;
231 int f;
232
233 if (dirname == NULL || *dirname == '\0')
234 return (NULL);
235 /*
236 * Construct the full pathname.
237 */
238 len= strlen(dirname) + strlen(filename) + 2;
239 pathname = (char *) calloc(len, sizeof(char));
240 if (pathname == NULL)
241 return (NULL);
242 SNPRINTF3(pathname, len, "%s%s%s", dirname, PATHNAME_SEP, filename);
243 /*
244 * Make sure the file exists.
245 */
246 qpathname = shell_unquote(pathname);
247 f = open(qpathname, OPEN_READ);
248 if (f < 0)
249 {
250 free(pathname);
251 pathname = NULL;
252 } else
253 {
254 close(f);
255 }
256 free(qpathname);
257 return (pathname);
258 }
259
260 /*
261 * Return the full pathname of the given file in the "home directory".
262 */
263 public char *
homefile(filename)264 homefile(filename)
265 char *filename;
266 {
267 register char *pathname;
268
269 /*
270 * Try $HOME/filename.
271 */
272 pathname = dirfile(lgetenv("HOME"), filename);
273 if (pathname != NULL)
274 return (pathname);
275 #if OS2
276 /*
277 * Try $INIT/filename.
278 */
279 pathname = dirfile(lgetenv("INIT"), filename);
280 if (pathname != NULL)
281 return (pathname);
282 #endif
283 #if MSDOS_COMPILER || OS2
284 /*
285 * Look for the file anywhere on search path.
286 */
287 pathname = (char *) calloc(_MAX_PATH, sizeof(char));
288 #if MSDOS_COMPILER==DJGPPC
289 {
290 char *res = searchpath(filename);
291 if (res == 0)
292 *pathname = '\0';
293 else
294 strcpy(pathname, res);
295 }
296 #else
297 _searchenv(filename, "PATH", pathname);
298 #endif
299 if (*pathname != '\0')
300 return (pathname);
301 free(pathname);
302 #endif
303 return (NULL);
304 }
305
306 /*
307 * Expand a string, substituting any "%" with the current filename,
308 * and any "#" with the previous filename.
309 * But a string of N "%"s is just replaced with N-1 "%"s.
310 * Likewise for a string of N "#"s.
311 * {{ This is a lot of work just to support % and #. }}
312 */
313 public char *
fexpand(s)314 fexpand(s)
315 char *s;
316 {
317 register char *fr, *to;
318 register int n;
319 register char *e;
320 IFILE ifile;
321
322 #define fchar_ifile(c) \
323 ((c) == '%' ? curr_ifile : \
324 (c) == '#' ? old_ifile : NULL_IFILE)
325
326 /*
327 * Make one pass to see how big a buffer we
328 * need to allocate for the expanded string.
329 */
330 n = 0;
331 for (fr = s; *fr != '\0'; fr++)
332 {
333 switch (*fr)
334 {
335 case '%':
336 case '#':
337 if (fr > s && fr[-1] == *fr)
338 {
339 /*
340 * Second (or later) char in a string
341 * of identical chars. Treat as normal.
342 */
343 n++;
344 } else if (fr[1] != *fr)
345 {
346 /*
347 * Single char (not repeated). Treat specially.
348 */
349 ifile = fchar_ifile(*fr);
350 if (ifile == NULL_IFILE)
351 n++;
352 else
353 n += strlen(get_filename(ifile));
354 }
355 /*
356 * Else it is the first char in a string of
357 * identical chars. Just discard it.
358 */
359 break;
360 default:
361 n++;
362 break;
363 }
364 }
365
366 e = (char *) ecalloc(n+1, sizeof(char));
367
368 /*
369 * Now copy the string, expanding any "%" or "#".
370 */
371 to = e;
372 for (fr = s; *fr != '\0'; fr++)
373 {
374 switch (*fr)
375 {
376 case '%':
377 case '#':
378 if (fr > s && fr[-1] == *fr)
379 {
380 *to++ = *fr;
381 } else if (fr[1] != *fr)
382 {
383 ifile = fchar_ifile(*fr);
384 if (ifile == NULL_IFILE)
385 *to++ = *fr;
386 else
387 {
388 strcpy(to, get_filename(ifile));
389 to += strlen(to);
390 }
391 }
392 break;
393 default:
394 *to++ = *fr;
395 break;
396 }
397 }
398 *to = '\0';
399 return (e);
400 }
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 *
fcomplete(s)410 fcomplete(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
bin_file(f)470 bin_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
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 /*
519 * Read a string from a file.
520 * Return a pointer to the string in memory.
521 */
522 static char *
readfd(fd)523 readfd(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
565 FILE *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 *
shellcmd(cmd)572 shellcmd(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 *
lglob(filename)623 lglob(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 * Return number of %s escapes in a string.
810 * Return a large number if there are any other % escapes besides %s.
811 */
812 static int
num_pct_s(lessopen)813 num_pct_s(lessopen)
814 char *lessopen;
815 {
816 int num;
817
818 for (num = 0;; num++)
819 {
820 lessopen = strchr(lessopen, '%');
821 if (lessopen == NULL)
822 break;
823 if (*++lessopen != 's')
824 return (999);
825 }
826 return (num);
827 }
828
829 /*
830 * See if we should open a "replacement file"
831 * instead of the file we're about to open.
832 */
833 public char *
open_altfile(filename,pf,pfd)834 open_altfile(filename, pf, pfd)
835 char *filename;
836 int *pf;
837 void **pfd;
838 {
839 #if !HAVE_POPEN
840 return (NULL);
841 #else
842 char *lessopen;
843 char *cmd;
844 int len;
845 FILE *fd;
846 #if HAVE_FILENO
847 int returnfd = 0;
848 #endif
849
850 if (!use_lessopen || secure)
851 return (NULL);
852 ch_ungetchar(-1);
853 if ((lessopen = lgetenv("LESSOPEN")) == NULL)
854 return (NULL);
855 while (*lessopen == '|')
856 {
857 /*
858 * If LESSOPEN starts with a |, it indicates
859 * a "pipe preprocessor".
860 */
861 #if !HAVE_FILENO
862 error("LESSOPEN pipe is not supported", NULL_PARG);
863 return (NULL);
864 #else
865 lessopen++;
866 returnfd++;
867 #endif
868 }
869 if (*lessopen == '-') {
870 /*
871 * Lessopen preprocessor will accept "-" as a filename.
872 */
873 lessopen++;
874 } else {
875 if (strcmp(filename, "-") == 0)
876 return (NULL);
877 }
878 if (num_pct_s(lessopen) > 1)
879 {
880 error("Invalid LESSOPEN variable", NULL_PARG);
881 return (NULL);
882 }
883
884 len = strlen(lessopen) + strlen(filename) + 2;
885 cmd = (char *) ecalloc(len, sizeof(char));
886 SNPRINTF1(cmd, len, lessopen, filename);
887 fd = shellcmd(cmd);
888 free(cmd);
889 if (fd == NULL)
890 {
891 /*
892 * Cannot create the pipe.
893 */
894 return (NULL);
895 }
896 #if HAVE_FILENO
897 if (returnfd)
898 {
899 int f;
900 char c;
901
902 /*
903 * Read one char to see if the pipe will produce any data.
904 * If it does, push the char back on the pipe.
905 */
906 f = fileno(fd);
907 SET_BINARY(f);
908 if (read(f, &c, 1) != 1)
909 {
910 /*
911 * Pipe is empty.
912 * If more than 1 pipe char was specified,
913 * the exit status tells whether the file itself
914 * is empty, or if there is no alt file.
915 * If only one pipe char, just assume no alt file.
916 */
917 int status = pclose(fd);
918 if (returnfd > 1 && status == 0) {
919 *pfd = NULL;
920 *pf = -1;
921 return (save(FAKE_EMPTYFILE));
922 }
923 return (NULL);
924 }
925 ch_ungetchar(c);
926 *pfd = (void *) fd;
927 *pf = f;
928 return (save("-"));
929 }
930 #endif
931 cmd = readfd(fd);
932 pclose(fd);
933 if (*cmd == '\0')
934 /*
935 * Pipe is empty. This means there is no alt file.
936 */
937 return (NULL);
938 return (cmd);
939 #endif /* HAVE_POPEN */
940 }
941
942 /*
943 * Close a replacement file.
944 */
945 public void
close_altfile(altfilename,filename,pipefd)946 close_altfile(altfilename, filename, pipefd)
947 char *altfilename;
948 char *filename;
949 void *pipefd;
950 {
951 #if HAVE_POPEN
952 char *lessclose;
953 FILE *fd;
954 char *cmd;
955 int len;
956
957 if (secure)
958 return;
959 if (pipefd != NULL)
960 {
961 #if OS2
962 /*
963 * The pclose function of OS/2 emx sometimes fails.
964 * Send SIGINT to the piped process before closing it.
965 */
966 kill(((FILE*)pipefd)->_pid, SIGINT);
967 #endif
968 pclose((FILE*) pipefd);
969 }
970 if ((lessclose = lgetenv("LESSCLOSE")) == NULL)
971 return;
972 if (num_pct_s(lessclose) > 2)
973 {
974 error("Invalid LESSCLOSE variable");
975 return;
976 }
977 len = strlen(lessclose) + strlen(filename) + strlen(altfilename) + 2;
978 cmd = (char *) ecalloc(len, sizeof(char));
979 SNPRINTF2(cmd, len, lessclose, filename, altfilename);
980 fd = shellcmd(cmd);
981 free(cmd);
982 if (fd != NULL)
983 pclose(fd);
984 #endif
985 }
986
987 /*
988 * Is the specified file a directory?
989 */
990 public int
is_dir(filename)991 is_dir(filename)
992 char *filename;
993 {
994 int isdir = 0;
995
996 filename = shell_unquote(filename);
997 #if HAVE_STAT
998 {
999 int r;
1000 struct stat statbuf;
1001
1002 r = stat(filename, &statbuf);
1003 isdir = (r >= 0 && S_ISDIR(statbuf.st_mode));
1004 }
1005 #else
1006 #ifdef _OSK
1007 {
1008 register int f;
1009
1010 f = open(filename, S_IREAD | S_IFDIR);
1011 if (f >= 0)
1012 close(f);
1013 isdir = (f >= 0);
1014 }
1015 #endif
1016 #endif
1017 free(filename);
1018 return (isdir);
1019 }
1020
1021 /*
1022 * Returns NULL if the file can be opened and
1023 * is an ordinary file, otherwise an error message
1024 * (if it cannot be opened or is a directory, etc.)
1025 */
1026 public char *
bad_file(filename)1027 bad_file(filename)
1028 char *filename;
1029 {
1030 register char *m = NULL;
1031
1032 filename = shell_unquote(filename);
1033 if (!force_open && is_dir(filename))
1034 {
1035 static char is_a_dir[] = " is a directory";
1036
1037 m = (char *) ecalloc(strlen(filename) + sizeof(is_a_dir),
1038 sizeof(char));
1039 strcpy(m, filename);
1040 strcat(m, is_a_dir);
1041 } else
1042 {
1043 #if HAVE_STAT
1044 int r;
1045 struct stat statbuf;
1046
1047 r = stat(filename, &statbuf);
1048 if (r < 0)
1049 {
1050 m = errno_message(filename);
1051 } else if (force_open)
1052 {
1053 m = NULL;
1054 } else if (!S_ISREG(statbuf.st_mode))
1055 {
1056 static char not_reg[] = " is not a regular file (use -f to see it)";
1057 m = (char *) ecalloc(strlen(filename) + sizeof(not_reg),
1058 sizeof(char));
1059 strcpy(m, filename);
1060 strcat(m, not_reg);
1061 }
1062 #endif
1063 }
1064 free(filename);
1065 return (m);
1066 }
1067
1068 /*
1069 * Return the size of a file, as cheaply as possible.
1070 * In Unix, we can stat the file.
1071 */
1072 public POSITION
filesize(f)1073 filesize(f)
1074 int f;
1075 {
1076 #if HAVE_STAT
1077 struct stat statbuf;
1078
1079 if (fstat(f, &statbuf) >= 0)
1080 return ((POSITION) statbuf.st_size);
1081 #else
1082 #ifdef _OSK
1083 long size;
1084
1085 if ((size = (long) _gs_size(f)) >= 0)
1086 return ((POSITION) size);
1087 #endif
1088 #endif
1089 return (seek_filesize(f));
1090 }
1091
1092 /*
1093 *
1094 */
1095 public char *
shell_coption()1096 shell_coption()
1097 {
1098 return ("-c");
1099 }
1100
1101 /*
1102 * Return last component of a pathname.
1103 */
1104 public char *
last_component(name)1105 last_component(name)
1106 char *name;
1107 {
1108 char *slash;
1109
1110 for (slash = name + strlen(name); slash > name; )
1111 {
1112 --slash;
1113 if (*slash == *PATHNAME_SEP || *slash == '/')
1114 return (slash + 1);
1115 }
1116 return (name);
1117 }
1118
1119