b1351fd65da16b37b030b4584e5dfb0970772454
[dragonfly.git] / contrib / less / filename.c
1 /*
2  * Copyright (C) 1984-2007  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 #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 #if TAB_COMPLETE_FILENAME
404
405 /*
406  * Return a blank-separated list of filenames which "complete"
407  * the given string.
408  */
409         public char *
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
470 bin_file(f)
471         int f;
472 {
473         int i;
474         int n;
475         int bin_count = 0;
476         unsigned char data[256];
477
478         if (!seekable(f))
479                 return (0);
480         if (lseek(f, (off_t)0, SEEK_SET) == BAD_LSEEK)
481                 return (0);
482         n = read(f, data, sizeof(data));
483         for (i = 0;  i < n;  i++)
484         {
485                 char c = data[i];
486                 if (ctldisp == OPT_ONPLUS && IS_CSI_START(c))
487                 {
488                         while (++i < n && is_ansi_middle(data[i]))
489                                 continue;
490                 } else if (binary_char(c))
491                         bin_count++;
492         }
493         /*
494          * Call it a binary file if there are more than 5 binary characters
495          * in the first 256 bytes of the file.
496          */
497         return (bin_count > 5);
498 }
499
500 /*
501  * Try to determine the size of a file by seeking to the end.
502  */
503         static POSITION
504 seek_filesize(f)
505         int f;
506 {
507         off_t spos;
508
509         spos = lseek(f, (off_t)0, SEEK_END);
510         if (spos == BAD_LSEEK)
511                 return (NULL_POSITION);
512         return ((POSITION) spos);
513 }
514
515 /*
516  * Read a string from a file.
517  * Return a pointer to the string in memory.
518  */
519         static char *
520 readfd(fd)
521         FILE *fd;
522 {
523         int len;
524         int ch;
525         char *buf;
526         char *p;
527         
528         /* 
529          * Make a guess about how many chars in the string
530          * and allocate a buffer to hold it.
531          */
532         len = 100;
533         buf = (char *) ecalloc(len, sizeof(char));
534         for (p = buf;  ;  p++)
535         {
536                 if ((ch = getc(fd)) == '\n' || ch == EOF)
537                         break;
538                 if (p - buf >= len-1)
539                 {
540                         /*
541                          * The string is too big to fit in the buffer we have.
542                          * Allocate a new buffer, twice as big.
543                          */
544                         len *= 2;
545                         *p = '\0';
546                         p = (char *) ecalloc(len, sizeof(char));
547                         strcpy(p, buf);
548                         free(buf);
549                         buf = p;
550                         p = buf + strlen(buf);
551                 }
552                 *p = ch;
553         }
554         *p = '\0';
555         return (buf);
556 }
557
558
559
560 #if HAVE_POPEN
561
562 FILE *popen();
563
564 /*
565  * Execute a shell command.
566  * Return a pointer to a pipe connected to the shell command's standard output.
567  */
568         static FILE *
569 shellcmd(cmd)
570         char *cmd;
571 {
572         FILE *fd;
573
574 #if HAVE_SHELL
575         char *shell;
576
577         shell = lgetenv("SHELL");
578         if (shell != NULL && *shell != '\0')
579         {
580                 char *scmd;
581                 char *esccmd;
582
583                 /*
584                  * Read the output of <$SHELL -c cmd>.  
585                  * Escape any metacharacters in the command.
586                  */
587                 esccmd = shell_quote(cmd);
588                 if (esccmd == NULL)
589                 {
590                         fd = popen(cmd, "r");
591                 } else
592                 {
593                         int len = strlen(shell) + strlen(esccmd) + 5;
594                         scmd = (char *) ecalloc(len, sizeof(char));
595                         SNPRINTF3(scmd, len, "%s %s %s", shell, shell_coption(), esccmd);
596                         free(esccmd);
597                         fd = popen(scmd, "r");
598                         free(scmd);
599                 }
600         } else
601 #endif
602         {
603                 fd = popen(cmd, "r");
604         }
605         /*
606          * Redirection in `popen' might have messed with the
607          * standard devices.  Restore binary input mode.
608          */
609         SET_BINARY(0);
610         return (fd);
611 }
612
613 #endif /* HAVE_POPEN */
614
615
616 /*
617  * Expand a filename, doing any system-specific metacharacter substitutions.
618  */
619         public char *
620 lglob(filename)
621         char *filename;
622 {
623         char *gfilename;
624         char *ofilename;
625
626         ofilename = fexpand(filename);
627         if (secure)
628                 return (ofilename);
629         filename = shell_unquote(ofilename);
630
631 #ifdef DECL_GLOB_LIST
632 {
633         /*
634          * The globbing function returns a list of names.
635          */
636         int length;
637         char *p;
638         char *qfilename;
639         DECL_GLOB_LIST(list)
640
641         GLOB_LIST(filename, list);
642         if (GLOB_LIST_FAILED(list))
643         {
644                 free(filename);
645                 return (ofilename);
646         }
647         length = 1; /* Room for trailing null byte */
648         for (SCAN_GLOB_LIST(list, p))
649         {
650                 INIT_GLOB_LIST(list, p);
651                 qfilename = shell_quote(p);
652                 if (qfilename != NULL)
653                 {
654                         length += strlen(qfilename) + 1;
655                         free(qfilename);
656                 }
657         }
658         gfilename = (char *) ecalloc(length, sizeof(char));
659         for (SCAN_GLOB_LIST(list, p))
660         {
661                 INIT_GLOB_LIST(list, p);
662                 qfilename = shell_quote(p);
663                 if (qfilename != NULL)
664                 {
665                         sprintf(gfilename + strlen(gfilename), "%s ", qfilename);
666                         free(qfilename);
667                 }
668         }
669         /*
670          * Overwrite the final trailing space with a null terminator.
671          */
672         *--p = '\0';
673         GLOB_LIST_DONE(list);
674 }
675 #else
676 #ifdef DECL_GLOB_NAME
677 {
678         /*
679          * The globbing function returns a single name, and
680          * is called multiple times to walk thru all names.
681          */
682         register char *p;
683         register int len;
684         register int n;
685         char *pathname;
686         char *qpathname;
687         DECL_GLOB_NAME(fnd,drive,dir,fname,ext,handle)
688         
689         GLOB_FIRST_NAME(filename, &fnd, handle);
690         if (GLOB_FIRST_FAILED(handle))
691         {
692                 free(filename);
693                 return (ofilename);
694         }
695
696         _splitpath(filename, drive, dir, fname, ext);
697         len = 100;
698         gfilename = (char *) ecalloc(len, sizeof(char));
699         p = gfilename;
700         do {
701                 n = strlen(drive) + strlen(dir) + strlen(fnd.GLOB_NAME) + 1;
702                 pathname = (char *) ecalloc(n, sizeof(char));
703                 SNPRINTF3(pathname, n, "%s%s%s", drive, dir, fnd.GLOB_NAME);
704                 qpathname = shell_quote(pathname);
705                 free(pathname);
706                 if (qpathname != NULL)
707                 {
708                         n = strlen(qpathname);
709                         while (p - gfilename + n + 2 >= len)
710                         {
711                                 /*
712                                  * No room in current buffer.
713                                  * Allocate a bigger one.
714                                  */
715                                 len *= 2;
716                                 *p = '\0';
717                                 p = (char *) ecalloc(len, sizeof(char));
718                                 strcpy(p, gfilename);
719                                 free(gfilename);
720                                 gfilename = p;
721                                 p = gfilename + strlen(gfilename);
722                         }
723                         strcpy(p, qpathname);
724                         free(qpathname);
725                         p += n;
726                         *p++ = ' ';
727                 }
728         } while (GLOB_NEXT_NAME(handle, &fnd) == 0);
729
730         /*
731          * Overwrite the final trailing space with a null terminator.
732          */
733         *--p = '\0';
734         GLOB_NAME_DONE(handle);
735 }
736 #else
737 #if HAVE_POPEN
738 {
739         /*
740          * We get the shell to glob the filename for us by passing
741          * an "echo" command to the shell and reading its output.
742          */
743         FILE *fd;
744         char *s;
745         char *lessecho;
746         char *cmd;
747         char *esc;
748         int len;
749
750         esc = get_meta_escape();
751         if (strlen(esc) == 0)
752                 esc = "-";
753         esc = shell_quote(esc);
754         if (esc == NULL)
755         {
756                 free(filename);
757                 return (ofilename);
758         }
759         lessecho = lgetenv("LESSECHO");
760         if (lessecho == NULL || *lessecho == '\0')
761                 lessecho = "lessecho";
762         /*
763          * Invoke lessecho, and read its output (a globbed list of filenames).
764          */
765         len = strlen(lessecho) + strlen(ofilename) + (7*strlen(metachars())) + 24;
766         cmd = (char *) ecalloc(len, sizeof(char));
767         SNPRINTF4(cmd, len, "%s -p0x%x -d0x%x -e%s ", lessecho, openquote, closequote, esc);
768         free(esc);
769         for (s = metachars();  *s != '\0';  s++)
770                 sprintf(cmd + strlen(cmd), "-n0x%x ", *s);
771         sprintf(cmd + strlen(cmd), "-- %s", ofilename);
772         fd = shellcmd(cmd);
773         free(cmd);
774         if (fd == NULL)
775         {
776                 /*
777                  * Cannot create the pipe.
778                  * Just return the original (fexpanded) filename.
779                  */
780                 free(filename);
781                 return (ofilename);
782         }
783         gfilename = readfd(fd);
784         pclose(fd);
785         if (*gfilename == '\0')
786         {
787                 free(gfilename);
788                 free(filename);
789                 return (ofilename);
790         }
791 }
792 #else
793         /*
794          * No globbing functions at all.  Just use the fexpanded filename.
795          */
796         gfilename = save(filename);
797 #endif
798 #endif
799 #endif
800         free(filename);
801         free(ofilename);
802         return (gfilename);
803 }
804
805 /*
806  * See if we should open a "replacement file" 
807  * instead of the file we're about to open.
808  */
809         public char *
810 open_altfile(filename, pf, pfd)
811         char *filename;
812         int *pf;
813         void **pfd;
814 {
815 #if !HAVE_POPEN
816         return (NULL);
817 #else
818         char *lessopen;
819         char *cmd;
820         int len;
821         FILE *fd;
822 #if HAVE_FILENO
823         int returnfd = 0;
824 #endif
825         
826         if (!use_lessopen || secure)
827                 return (NULL);
828         ch_ungetchar(-1);
829         if ((lessopen = lgetenv("LESSOPEN")) == NULL)
830                 return (NULL);
831         if (strcmp(filename, "-") == 0)
832                 return (NULL);
833         if (*lessopen == '|')
834         {
835                 /*
836                  * If LESSOPEN starts with a |, it indicates 
837                  * a "pipe preprocessor".
838                  */
839 #if HAVE_FILENO
840                 lessopen++;
841                 returnfd = 1;
842 #else
843                 error("LESSOPEN pipe is not supported", NULL_PARG);
844                 return (NULL);
845 #endif
846         }
847
848         len = strlen(lessopen) + strlen(filename) + 2;
849         cmd = (char *) ecalloc(len, sizeof(char));
850         SNPRINTF1(cmd, len, lessopen, filename);
851         fd = shellcmd(cmd);
852         free(cmd);
853         if (fd == NULL)
854         {
855                 /*
856                  * Cannot create the pipe.
857                  */
858                 return (NULL);
859         }
860 #if HAVE_FILENO
861         if (returnfd)
862         {
863                 int f;
864                 char c;
865
866                 /*
867                  * Read one char to see if the pipe will produce any data.
868                  * If it does, push the char back on the pipe.
869                  */
870                 f = fileno(fd);
871                 SET_BINARY(f);
872                 if (read(f, &c, 1) != 1)
873                 {
874                         /*
875                          * Pipe is empty.  This means there is no alt file.
876                          */
877                         pclose(fd);
878                         return (NULL);
879                 }
880                 ch_ungetchar(c);
881                 *pfd = (void *) fd;
882                 *pf = f;
883                 return (save("-"));
884         }
885 #endif
886         cmd = readfd(fd);
887         pclose(fd);
888         if (*cmd == '\0')
889                 /*
890                  * Pipe is empty.  This means there is no alt file.
891                  */
892                 return (NULL);
893         return (cmd);
894 #endif /* HAVE_POPEN */
895 }
896
897 /*
898  * Close a replacement file.
899  */
900         public void
901 close_altfile(altfilename, filename, pipefd)
902         char *altfilename;
903         char *filename;
904         void *pipefd;
905 {
906 #if HAVE_POPEN
907         char *lessclose;
908         FILE *fd;
909         char *cmd;
910         int len;
911         
912         if (secure)
913                 return;
914         if (pipefd != NULL)
915         {
916 #if OS2
917                 /*
918                  * The pclose function of OS/2 emx sometimes fails.
919                  * Send SIGINT to the piped process before closing it.
920                  */
921                 kill(((FILE*)pipefd)->_pid, SIGINT);
922 #endif
923                 pclose((FILE*) pipefd);
924         }
925         if ((lessclose = lgetenv("LESSCLOSE")) == NULL)
926                 return;
927         len = strlen(lessclose) + strlen(filename) + strlen(altfilename) + 2;
928         cmd = (char *) ecalloc(len, sizeof(char));
929         SNPRINTF2(cmd, len, lessclose, filename, altfilename);
930         fd = shellcmd(cmd);
931         free(cmd);
932         if (fd != NULL)
933                 pclose(fd);
934 #endif
935 }
936                 
937 /*
938  * Is the specified file a directory?
939  */
940         public int
941 is_dir(filename)
942         char *filename;
943 {
944         int isdir = 0;
945
946         filename = shell_unquote(filename);
947 #if HAVE_STAT
948 {
949         int r;
950         struct stat statbuf;
951
952         r = stat(filename, &statbuf);
953         isdir = (r >= 0 && S_ISDIR(statbuf.st_mode));
954 }
955 #else
956 #ifdef _OSK
957 {
958         register int f;
959
960         f = open(filename, S_IREAD | S_IFDIR);
961         if (f >= 0)
962                 close(f);
963         isdir = (f >= 0);
964 }
965 #endif
966 #endif
967         free(filename);
968         return (isdir);
969 }
970
971 /*
972  * Returns NULL if the file can be opened and
973  * is an ordinary file, otherwise an error message
974  * (if it cannot be opened or is a directory, etc.)
975  */
976         public char *
977 bad_file(filename)
978         char *filename;
979 {
980         register char *m = NULL;
981
982         filename = shell_unquote(filename);
983         if (!force_open && is_dir(filename))
984         {
985                 static char is_a_dir[] = " is a directory";
986
987                 m = (char *) ecalloc(strlen(filename) + sizeof(is_a_dir), 
988                         sizeof(char));
989                 strcpy(m, filename);
990                 strcat(m, is_a_dir);
991         } else
992         {
993 #if HAVE_STAT
994                 int r;
995                 struct stat statbuf;
996
997                 r = stat(filename, &statbuf);
998                 if (r < 0)
999                 {
1000                         m = errno_message(filename);
1001                 } else if (force_open)
1002                 {
1003                         m = NULL;
1004                 } else if (!S_ISREG(statbuf.st_mode))
1005                 {
1006                         static char not_reg[] = " is not a regular file (use -f to see it)";
1007                         m = (char *) ecalloc(strlen(filename) + sizeof(not_reg),
1008                                 sizeof(char));
1009                         strcpy(m, filename);
1010                         strcat(m, not_reg);
1011                 }
1012 #endif
1013         }
1014         free(filename);
1015         return (m);
1016 }
1017
1018 /*
1019  * Return the size of a file, as cheaply as possible.
1020  * In Unix, we can stat the file.
1021  */
1022         public POSITION
1023 filesize(f)
1024         int f;
1025 {
1026 #if HAVE_STAT
1027         struct stat statbuf;
1028
1029         if (fstat(f, &statbuf) >= 0)
1030                 return ((POSITION) statbuf.st_size);
1031 #else
1032 #ifdef _OSK
1033         long size;
1034
1035         if ((size = (long) _gs_size(f)) >= 0)
1036                 return ((POSITION) size);
1037 #endif
1038 #endif
1039         return (seek_filesize(f));
1040 }
1041
1042 /*
1043  * 
1044  */
1045         public char *
1046 shell_coption()
1047 {
1048         return ("-c");
1049 }