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