]> git.cworth.org Git - apitrace/blob - thirdparty/less/filename.c
Bundle less (version 444) for Windows.
[apitrace] / thirdparty / less / filename.c
1 /*
2  * Copyright (C) 1984-2011  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(__BORLANDC__)
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 #if HAVE_STAT
41 #include <sys/stat.h>
42 #ifndef S_ISDIR
43 #define S_ISDIR(m)      (((m) & S_IFMT) == S_IFDIR)
44 #endif
45 #ifndef S_ISREG
46 #define S_ISREG(m)      (((m) & S_IFMT) == S_IFREG)
47 #endif
48 #endif
49
50
51 extern int force_open;
52 extern int secure;
53 extern int use_lessopen;
54 extern int ctldisp;
55 extern int utf_mode;
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 *
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 *
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 *
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
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 *
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                 SNPRINTF3(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                                 strcpy(p, esc);
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 *
225 dirfile(dirname, filename)
226         char *dirname;
227         char *filename;
228 {
229         char *pathname;
230         char *qpathname;
231         int len;
232         int f;
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         SNPRINTF3(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 *
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                         strcpy(pathname, res);
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 /*
308  * Expand a string, substituting any "%" with the current filename,
309  * and any "#" with the previous filename.
310  * But a string of N "%"s is just replaced with N-1 "%"s.
311  * Likewise for a string of N "#"s.
312  * {{ This is a lot of work just to support % and #. }}
313  */
314         public char *
315 fexpand(s)
316         char *s;
317 {
318         register char *fr, *to;
319         register int n;
320         register char *e;
321         IFILE ifile;
322
323 #define fchar_ifile(c) \
324         ((c) == '%' ? curr_ifile : \
325          (c) == '#' ? old_ifile : NULL_IFILE)
326
327         /*
328          * Make one pass to see how big a buffer we 
329          * need to allocate for the expanded string.
330          */
331         n = 0;
332         for (fr = s;  *fr != '\0';  fr++)
333         {
334                 switch (*fr)
335                 {
336                 case '%':
337                 case '#':
338                         if (fr > s && fr[-1] == *fr)
339                         {
340                                 /*
341                                  * Second (or later) char in a string
342                                  * of identical chars.  Treat as normal.
343                                  */
344                                 n++;
345                         } else if (fr[1] != *fr)
346                         {
347                                 /*
348                                  * Single char (not repeated).  Treat specially.
349                                  */
350                                 ifile = fchar_ifile(*fr);
351                                 if (ifile == NULL_IFILE)
352                                         n++;
353                                 else
354                                         n += strlen(get_filename(ifile));
355                         }
356                         /*
357                          * Else it is the first char in a string of
358                          * identical chars.  Just discard it.
359                          */
360                         break;
361                 default:
362                         n++;
363                         break;
364                 }
365         }
366
367         e = (char *) ecalloc(n+1, sizeof(char));
368
369         /*
370          * Now copy the string, expanding any "%" or "#".
371          */
372         to = e;
373         for (fr = s;  *fr != '\0';  fr++)
374         {
375                 switch (*fr)
376                 {
377                 case '%':
378                 case '#':
379                         if (fr > s && fr[-1] == *fr)
380                         {
381                                 *to++ = *fr;
382                         } else if (fr[1] != *fr)
383                         {
384                                 ifile = fchar_ifile(*fr);
385                                 if (ifile == NULL_IFILE)
386                                         *to++ = *fr;
387                                 else
388                                 {
389                                         strcpy(to, get_filename(ifile));
390                                         to += strlen(to);
391                                 }
392                         }
393                         break;
394                 default:
395                         *to++ = *fr;
396                         break;
397                 }
398         }
399         *to = '\0';
400         return (e);
401 }
402
403
404 #if TAB_COMPLETE_FILENAME
405
406 /*
407  * Return a blank-separated list of filenames which "complete"
408  * the given string.
409  */
410         public char *
411 fcomplete(s)
412         char *s;
413 {
414         char *fpat;
415         char *qs;
416
417         if (secure)
418                 return (NULL);
419         /*
420          * Complete the filename "s" by globbing "s*".
421          */
422 #if MSDOS_COMPILER && (MSDOS_COMPILER == MSOFTC || MSDOS_COMPILER == BORLANDC)
423         /*
424          * But in DOS, we have to glob "s*.*".
425          * But if the final component of the filename already has
426          * a dot in it, just do "s*".  
427          * (Thus, "FILE" is globbed as "FILE*.*", 
428          *  but "FILE.A" is globbed as "FILE.A*").
429          */
430         {
431                 char *slash;
432                 int len;
433                 for (slash = s+strlen(s)-1;  slash > s;  slash--)
434                         if (*slash == *PATHNAME_SEP || *slash == '/')
435                                 break;
436                 len = strlen(s) + 4;
437                 fpat = (char *) ecalloc(len, sizeof(char));
438                 if (strchr(slash, '.') == NULL)
439                         SNPRINTF1(fpat, len, "%s*.*", s);
440                 else
441                         SNPRINTF1(fpat, len, "%s*", s);
442         }
443 #else
444         {
445         int len = strlen(s) + 2;
446         fpat = (char *) ecalloc(len, sizeof(char));
447         SNPRINTF1(fpat, len, "%s*", s);
448         }
449 #endif
450         qs = lglob(fpat);
451         s = shell_unquote(qs);
452         if (strcmp(s,fpat) == 0)
453         {
454                 /*
455                  * The filename didn't expand.
456                  */
457                 free(qs);
458                 qs = NULL;
459         }
460         free(s);
461         free(fpat);
462         return (qs);
463 }
464 #endif
465
466 /*
467  * Try to determine if a file is "binary".
468  * This is just a guess, and we need not try too hard to make it accurate.
469  */
470         public int
471 bin_file(f)
472         int f;
473 {
474         int n;
475         int bin_count = 0;
476         char data[256];
477         char* p;
478         char* pend;
479
480         if (!seekable(f))
481                 return (0);
482         if (lseek(f, (off_t)0, SEEK_SET) == BAD_LSEEK)
483                 return (0);
484         n = read(f, data, sizeof(data));
485         pend = &data[n];
486         for (p = data;  p < pend;  )
487         {
488                 LWCHAR c = step_char(&p, +1, pend);
489                 if (ctldisp == OPT_ONPLUS && IS_CSI_START(c))
490                 {
491                         do {
492                                 c = step_char(&p, +1, pend);
493                         } while (p < pend && is_ansi_middle(c));
494                 } else if (binary_char(c))
495                         bin_count++;
496         }
497         /*
498          * Call it a binary file if there are more than 5 binary characters
499          * in the first 256 bytes of the file.
500          */
501         return (bin_count > 5);
502 }
503
504 /*
505  * Try to determine the size of a file by seeking to the end.
506  */
507         static POSITION
508 seek_filesize(f)
509         int f;
510 {
511         off_t spos;
512
513         spos = lseek(f, (off_t)0, SEEK_END);
514         if (spos == BAD_LSEEK)
515                 return (NULL_POSITION);
516         return ((POSITION) spos);
517 }
518
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
564 #if HAVE_POPEN
565
566 #if MSDOS_COMPILER && MSDOS_COMPILER!=WIN32C
567 FILE *popen();
568 #endif
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 *
575 shellcmd(cmd)
576         char *cmd;
577 {
578         FILE *fd;
579
580 #if HAVE_SHELL
581         char *shell;
582
583         shell = lgetenv("SHELL");
584         if (shell != NULL && *shell != '\0')
585         {
586                 char *scmd;
587                 char *esccmd;
588
589                 /*
590                  * Read the output of <$SHELL -c cmd>.  
591                  * Escape any metacharacters in the command.
592                  */
593                 esccmd = shell_quote(cmd);
594                 if (esccmd == NULL)
595                 {
596                         fd = popen(cmd, "r");
597                 } else
598                 {
599                         int len = strlen(shell) + strlen(esccmd) + 5;
600                         scmd = (char *) ecalloc(len, sizeof(char));
601                         SNPRINTF3(scmd, len, "%s %s %s", shell, shell_coption(), esccmd);
602                         free(esccmd);
603                         fd = popen(scmd, "r");
604                         free(scmd);
605                 }
606         } else
607 #endif
608         {
609                 fd = popen(cmd, "r");
610         }
611         /*
612          * Redirection in `popen' might have messed with the
613          * standard devices.  Restore binary input mode.
614          */
615         SET_BINARY(0);
616         return (fd);
617 }
618
619 #endif /* HAVE_POPEN */
620
621
622 /*
623  * Expand a filename, doing any system-specific metacharacter substitutions.
624  */
625         public char *
626 lglob(filename)
627         char *filename;
628 {
629         char *gfilename;
630         char *ofilename;
631
632         ofilename = fexpand(filename);
633         if (secure)
634                 return (ofilename);
635         filename = shell_unquote(ofilename);
636
637 #ifdef DECL_GLOB_LIST
638 {
639         /*
640          * The globbing function returns a list of names.
641          */
642         int length;
643         char *p;
644         char *qfilename;
645         DECL_GLOB_LIST(list)
646
647         GLOB_LIST(filename, list);
648         if (GLOB_LIST_FAILED(list))
649         {
650                 free(filename);
651                 return (ofilename);
652         }
653         length = 1; /* Room for trailing null byte */
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                         length += strlen(qfilename) + 1;
661                         free(qfilename);
662                 }
663         }
664         gfilename = (char *) ecalloc(length, sizeof(char));
665         for (SCAN_GLOB_LIST(list, p))
666         {
667                 INIT_GLOB_LIST(list, p);
668                 qfilename = shell_quote(p);
669                 if (qfilename != NULL)
670                 {
671                         sprintf(gfilename + strlen(gfilename), "%s ", qfilename);
672                         free(qfilename);
673                 }
674         }
675         /*
676          * Overwrite the final trailing space with a null terminator.
677          */
678         *--p = '\0';
679         GLOB_LIST_DONE(list);
680 }
681 #else
682 #ifdef DECL_GLOB_NAME
683 {
684         /*
685          * The globbing function returns a single name, and
686          * is called multiple times to walk thru all names.
687          */
688         register char *p;
689         register int len;
690         register int n;
691         char *pathname;
692         char *qpathname;
693         DECL_GLOB_NAME(fnd,drive,dir,fname,ext,handle)
694         
695         GLOB_FIRST_NAME(filename, &fnd, handle);
696         if (GLOB_FIRST_FAILED(handle))
697         {
698                 free(filename);
699                 return (ofilename);
700         }
701
702         _splitpath(filename, drive, dir, fname, ext);
703         len = 100;
704         gfilename = (char *) ecalloc(len, sizeof(char));
705         p = gfilename;
706         do {
707                 n = strlen(drive) + strlen(dir) + strlen(fnd.GLOB_NAME) + 1;
708                 pathname = (char *) ecalloc(n, sizeof(char));
709                 SNPRINTF3(pathname, n, "%s%s%s", drive, dir, fnd.GLOB_NAME);
710                 qpathname = shell_quote(pathname);
711                 free(pathname);
712                 if (qpathname != NULL)
713                 {
714                         n = strlen(qpathname);
715                         while (p - gfilename + n + 2 >= len)
716                         {
717                                 /*
718                                  * No room in current buffer.
719                                  * Allocate a bigger one.
720                                  */
721                                 len *= 2;
722                                 *p = '\0';
723                                 p = (char *) ecalloc(len, sizeof(char));
724                                 strcpy(p, gfilename);
725                                 free(gfilename);
726                                 gfilename = p;
727                                 p = gfilename + strlen(gfilename);
728                         }
729                         strcpy(p, qpathname);
730                         free(qpathname);
731                         p += n;
732                         *p++ = ' ';
733                 }
734         } while (GLOB_NEXT_NAME(handle, &fnd) == 0);
735
736         /*
737          * Overwrite the final trailing space with a null terminator.
738          */
739         *--p = '\0';
740         GLOB_NAME_DONE(handle);
741 }
742 #else
743 #if HAVE_POPEN
744 {
745         /*
746          * We get the shell to glob the filename for us by passing
747          * an "echo" command to the shell and reading its output.
748          */
749         FILE *fd;
750         char *s;
751         char *lessecho;
752         char *cmd;
753         char *esc;
754         int len;
755
756         esc = get_meta_escape();
757         if (strlen(esc) == 0)
758                 esc = "-";
759         esc = shell_quote(esc);
760         if (esc == NULL)
761         {
762                 free(filename);
763                 return (ofilename);
764         }
765         lessecho = lgetenv("LESSECHO");
766         if (lessecho == NULL || *lessecho == '\0')
767                 lessecho = "lessecho";
768         /*
769          * Invoke lessecho, and read its output (a globbed list of filenames).
770          */
771         len = strlen(lessecho) + strlen(ofilename) + (7*strlen(metachars())) + 24;
772         cmd = (char *) ecalloc(len, sizeof(char));
773         SNPRINTF4(cmd, len, "%s -p0x%x -d0x%x -e%s ", lessecho, openquote, closequote, esc);
774         free(esc);
775         for (s = metachars();  *s != '\0';  s++)
776                 sprintf(cmd + strlen(cmd), "-n0x%x ", *s);
777         sprintf(cmd + strlen(cmd), "-- %s", ofilename);
778         fd = shellcmd(cmd);
779         free(cmd);
780         if (fd == NULL)
781         {
782                 /*
783                  * Cannot create the pipe.
784                  * Just return the original (fexpanded) filename.
785                  */
786                 free(filename);
787                 return (ofilename);
788         }
789         gfilename = readfd(fd);
790         pclose(fd);
791         if (*gfilename == '\0')
792         {
793                 free(gfilename);
794                 free(filename);
795                 return (ofilename);
796         }
797 }
798 #else
799         /*
800          * No globbing functions at all.  Just use the fexpanded filename.
801          */
802         gfilename = save(filename);
803 #endif
804 #endif
805 #endif
806         free(filename);
807         free(ofilename);
808         return (gfilename);
809 }
810
811 /*
812  * See if we should open a "replacement file" 
813  * instead of the file we're about to open.
814  */
815         public char *
816 open_altfile(filename, pf, pfd)
817         char *filename;
818         int *pf;
819         void **pfd;
820 {
821 #if !HAVE_POPEN
822         return (NULL);
823 #else
824         char *lessopen;
825         char *cmd;
826         int len;
827         FILE *fd;
828 #if HAVE_FILENO
829         int returnfd = 0;
830 #endif
831         
832         if (!use_lessopen || secure)
833                 return (NULL);
834         ch_ungetchar(-1);
835         if ((lessopen = lgetenv("LESSOPEN")) == NULL)
836                 return (NULL);
837         if (*lessopen == '|')
838         {
839                 /*
840                  * If LESSOPEN starts with a |, it indicates 
841                  * a "pipe preprocessor".
842                  */
843 #if !HAVE_FILENO
844                 error("LESSOPEN pipe is not supported", NULL_PARG);
845                 return (NULL);
846 #else
847                 lessopen++;
848                 returnfd = 1;
849 #endif
850         }
851         if (*lessopen == '-') {
852                 /*
853                  * Lessopen preprocessor will accept "-" as a filename.
854                  */
855                 lessopen++;
856         } else {
857                 if (strcmp(filename, "-") == 0)
858                         return (NULL);
859         }
860
861         len = strlen(lessopen) + strlen(filename) + 2;
862         cmd = (char *) ecalloc(len, sizeof(char));
863         SNPRINTF1(cmd, len, lessopen, filename);
864         fd = shellcmd(cmd);
865         free(cmd);
866         if (fd == NULL)
867         {
868                 /*
869                  * Cannot create the pipe.
870                  */
871                 return (NULL);
872         }
873 #if HAVE_FILENO
874         if (returnfd)
875         {
876                 int f;
877                 char c;
878
879                 /*
880                  * Read one char to see if the pipe will produce any data.
881                  * If it does, push the char back on the pipe.
882                  */
883                 f = fileno(fd);
884                 SET_BINARY(f);
885                 if (read(f, &c, 1) != 1)
886                 {
887                         /*
888                          * Pipe is empty.  This means there is no alt file.
889                          */
890                         pclose(fd);
891                         return (NULL);
892                 }
893                 ch_ungetchar(c);
894                 *pfd = (void *) fd;
895                 *pf = f;
896                 return (save("-"));
897         }
898 #endif
899         cmd = readfd(fd);
900         pclose(fd);
901         if (*cmd == '\0')
902                 /*
903                  * Pipe is empty.  This means there is no alt file.
904                  */
905                 return (NULL);
906         return (cmd);
907 #endif /* HAVE_POPEN */
908 }
909
910 /*
911  * Close a replacement file.
912  */
913         public void
914 close_altfile(altfilename, filename, pipefd)
915         char *altfilename;
916         char *filename;
917         void *pipefd;
918 {
919 #if HAVE_POPEN
920         char *lessclose;
921         FILE *fd;
922         char *cmd;
923         int len;
924         
925         if (secure)
926                 return;
927         if (pipefd != NULL)
928         {
929 #if OS2
930                 /*
931                  * The pclose function of OS/2 emx sometimes fails.
932                  * Send SIGINT to the piped process before closing it.
933                  */
934                 kill(((FILE*)pipefd)->_pid, SIGINT);
935 #endif
936                 pclose((FILE*) pipefd);
937         }
938         if ((lessclose = lgetenv("LESSCLOSE")) == NULL)
939                 return;
940         len = strlen(lessclose) + strlen(filename) + strlen(altfilename) + 2;
941         cmd = (char *) ecalloc(len, sizeof(char));
942         SNPRINTF2(cmd, len, lessclose, filename, altfilename);
943         fd = shellcmd(cmd);
944         free(cmd);
945         if (fd != NULL)
946                 pclose(fd);
947 #endif
948 }
949                 
950 /*
951  * Is the specified file a directory?
952  */
953         public int
954 is_dir(filename)
955         char *filename;
956 {
957         int isdir = 0;
958
959         filename = shell_unquote(filename);
960 #if HAVE_STAT
961 {
962         int r;
963         struct stat statbuf;
964
965         r = stat(filename, &statbuf);
966         isdir = (r >= 0 && S_ISDIR(statbuf.st_mode));
967 }
968 #else
969 #ifdef _OSK
970 {
971         register int f;
972
973         f = open(filename, S_IREAD | S_IFDIR);
974         if (f >= 0)
975                 close(f);
976         isdir = (f >= 0);
977 }
978 #endif
979 #endif
980         free(filename);
981         return (isdir);
982 }
983
984 /*
985  * Returns NULL if the file can be opened and
986  * is an ordinary file, otherwise an error message
987  * (if it cannot be opened or is a directory, etc.)
988  */
989         public char *
990 bad_file(filename)
991         char *filename;
992 {
993         register char *m = NULL;
994
995         filename = shell_unquote(filename);
996         if (!force_open && is_dir(filename))
997         {
998                 static char is_a_dir[] = " is a directory";
999
1000                 m = (char *) ecalloc(strlen(filename) + sizeof(is_a_dir), 
1001                         sizeof(char));
1002                 strcpy(m, filename);
1003                 strcat(m, is_a_dir);
1004         } else
1005         {
1006 #if HAVE_STAT
1007                 int r;
1008                 struct stat statbuf;
1009
1010                 r = stat(filename, &statbuf);
1011                 if (r < 0)
1012                 {
1013                         m = errno_message(filename);
1014                 } else if (force_open)
1015                 {
1016                         m = NULL;
1017                 } else if (!S_ISREG(statbuf.st_mode))
1018                 {
1019                         static char not_reg[] = " is not a regular file (use -f to see it)";
1020                         m = (char *) ecalloc(strlen(filename) + sizeof(not_reg),
1021                                 sizeof(char));
1022                         strcpy(m, filename);
1023                         strcat(m, not_reg);
1024                 }
1025 #endif
1026         }
1027         free(filename);
1028         return (m);
1029 }
1030
1031 /*
1032  * Return the size of a file, as cheaply as possible.
1033  * In Unix, we can stat the file.
1034  */
1035         public POSITION
1036 filesize(f)
1037         int f;
1038 {
1039 #if HAVE_STAT
1040         struct stat statbuf;
1041
1042         if (fstat(f, &statbuf) >= 0)
1043                 return ((POSITION) statbuf.st_size);
1044 #else
1045 #ifdef _OSK
1046         long size;
1047
1048         if ((size = (long) _gs_size(f)) >= 0)
1049                 return ((POSITION) size);
1050 #endif
1051 #endif
1052         return (seek_filesize(f));
1053 }
1054
1055 /*
1056  * 
1057  */
1058         public char *
1059 shell_coption()
1060 {
1061         return ("-c");
1062 }
1063
1064 /*
1065  * Return last component of a pathname.
1066  */
1067         public char *
1068 last_component(name)
1069         char *name;
1070 {
1071         char *slash;
1072
1073         for (slash = name + strlen(name);  slash > name; )
1074         {
1075                 --slash;
1076                 if (*slash == *PATHNAME_SEP || *slash == '/')
1077                         return (slash + 1);
1078         }
1079         return (name);
1080 }
1081