kernel - Fix races created by a comedy of circumstansces (3)
[dragonfly.git] / contrib / less / filename.c
blob9631f1c2b3d819552dc0ebeb68b16ee1e548e349
1 /*
2 * Copyright (C) 1984-2015 Mark Nudelman
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.
7 * For more information, see the README file.
8 */
12 * Routines to mess around with filenames (and files).
13 * Much of this is very OS dependent.
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
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
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
63 * Remove quotes around a filename.
65 public char *
66 shell_unquote(str)
67 char *str;
69 char *name;
70 char *p;
72 name = p = (char *) ecalloc(strlen(str)+1, sizeof(char));
73 if (*str == openquote)
75 str++;
76 while (*str != '\0')
78 if (*str == closequote)
80 if (str[1] != closequote)
81 break;
82 str++;
84 *p++ = *str++;
86 } else
88 char *esc = get_meta_escape();
89 int esclen = (int) strlen(esc);
90 while (*str != '\0')
92 if (esclen > 0 && strncmp(str, esc, esclen) == 0)
93 str += esclen;
94 *p++ = *str++;
97 *p = '\0';
98 return (name);
102 * Get the shell's escape character.
104 public char *
105 get_meta_escape()
107 char *s;
109 s = lgetenv("LESSMETAESCAPE");
110 if (s == NULL)
111 s = DEF_METAESCAPE;
112 return (s);
116 * Get the characters which the shell considers to be "metacharacters".
118 static char *
119 metachars()
121 static char *mchars = NULL;
123 if (mchars == NULL)
125 mchars = lgetenv("LESSMETACHARS");
126 if (mchars == NULL)
127 mchars = DEF_METACHARS;
129 return (mchars);
133 * Is this a shell metacharacter?
135 static int
136 metachar(c)
137 char c;
139 return (strchr(metachars(), c) != NULL);
143 * Insert a backslash before each metacharacter in a string.
145 public char *
146 shell_quote(s)
147 char *s;
149 char *p;
150 char *newstr;
151 int len;
152 char *esc = get_meta_escape();
153 int esclen = (int) strlen(esc);
154 int use_quotes = 0;
155 int have_quotes = 0;
158 * Determine how big a string we need to allocate.
160 len = 1; /* Trailing null byte */
161 for (p = s; *p != '\0'; p++)
163 len++;
164 if (*p == openquote || *p == closequote)
165 have_quotes = 1;
166 if (metachar(*p))
168 if (esclen == 0)
171 * We've got a metachar, but this shell
172 * doesn't support escape chars. Use quotes.
174 use_quotes = 1;
175 } else
178 * Allow space for the escape char.
180 len += esclen;
184 if (use_quotes)
186 if (have_quotes)
188 * We can't quote a string that contains quotes.
190 return (NULL);
191 len = (int) strlen(s) + 3;
194 * Allocate and construct the new string.
196 newstr = p = (char *) ecalloc(len, sizeof(char));
197 if (use_quotes)
199 SNPRINTF3(newstr, len, "%c%s%c", openquote, s, closequote);
200 } else
202 while (*s != '\0')
204 if (metachar(*s))
207 * Add the escape char.
209 strcpy(p, esc);
210 p += esclen;
212 *p++ = *s++;
214 *p = '\0';
216 return (newstr);
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.
223 static char *
224 dirfile(dirname, filename)
225 char *dirname;
226 char *filename;
228 char *pathname;
229 char *qpathname;
230 int len;
231 int f;
233 if (dirname == NULL || *dirname == '\0')
234 return (NULL);
236 * Construct the full pathname.
238 len = (int) (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);
244 * Make sure the file exists.
246 qpathname = shell_unquote(pathname);
247 f = open(qpathname, OPEN_READ);
248 if (f < 0)
250 free(pathname);
251 pathname = NULL;
252 } else
254 close(f);
256 free(qpathname);
257 return (pathname);
261 * Return the full pathname of the given file in the "home directory".
263 public char *
264 homefile(filename)
265 char *filename;
267 register char *pathname;
270 * Try $HOME/filename.
272 pathname = dirfile(lgetenv("HOME"), filename);
273 if (pathname != NULL)
274 return (pathname);
275 #if OS2
277 * Try $INIT/filename.
279 pathname = dirfile(lgetenv("INIT"), filename);
280 if (pathname != NULL)
281 return (pathname);
282 #endif
283 #if MSDOS_COMPILER || OS2
285 * Look for the file anywhere on search path.
287 pathname = (char *) calloc(_MAX_PATH, sizeof(char));
288 #if MSDOS_COMPILER==DJGPPC
290 char *res = searchpath(filename);
291 if (res == 0)
292 *pathname = '\0';
293 else
294 strcpy(pathname, res);
296 #else
297 _searchenv(filename, "PATH", pathname);
298 #endif
299 if (*pathname != '\0')
300 return (pathname);
301 free(pathname);
302 #endif
303 return (NULL);
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 #. }}
313 public char *
314 fexpand(s)
315 char *s;
317 register char *fr, *to;
318 register int n;
319 register char *e;
320 IFILE ifile;
322 #define fchar_ifile(c) \
323 ((c) == '%' ? curr_ifile : \
324 (c) == '#' ? old_ifile : NULL_IFILE)
327 * Make one pass to see how big a buffer we
328 * need to allocate for the expanded string.
330 n = 0;
331 for (fr = s; *fr != '\0'; fr++)
333 switch (*fr)
335 case '%':
336 case '#':
337 if (fr > s && fr[-1] == *fr)
340 * Second (or later) char in a string
341 * of identical chars. Treat as normal.
343 n++;
344 } else if (fr[1] != *fr)
347 * Single char (not repeated). Treat specially.
349 ifile = fchar_ifile(*fr);
350 if (ifile == NULL_IFILE)
351 n++;
352 else
353 n += (int) strlen(get_filename(ifile));
356 * Else it is the first char in a string of
357 * identical chars. Just discard it.
359 break;
360 default:
361 n++;
362 break;
366 e = (char *) ecalloc(n+1, sizeof(char));
369 * Now copy the string, expanding any "%" or "#".
371 to = e;
372 for (fr = s; *fr != '\0'; fr++)
374 switch (*fr)
376 case '%':
377 case '#':
378 if (fr > s && fr[-1] == *fr)
380 *to++ = *fr;
381 } else if (fr[1] != *fr)
383 ifile = fchar_ifile(*fr);
384 if (ifile == NULL_IFILE)
385 *to++ = *fr;
386 else
388 strcpy(to, get_filename(ifile));
389 to += strlen(to);
392 break;
393 default:
394 *to++ = *fr;
395 break;
398 *to = '\0';
399 return (e);
403 #if TAB_COMPLETE_FILENAME
406 * Return a blank-separated list of filenames which "complete"
407 * the given string.
409 public char *
410 fcomplete(s)
411 char *s;
413 char *fpat;
414 char *qs;
416 if (secure)
417 return (NULL);
419 * Complete the filename "s" by globbing "s*".
421 #if MSDOS_COMPILER && (MSDOS_COMPILER == MSOFTC || MSDOS_COMPILER == BORLANDC)
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*").
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 = (int) 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);
442 #else
444 int len = (int) strlen(s) + 2;
445 fpat = (char *) ecalloc(len, sizeof(char));
446 SNPRINTF1(fpat, len, "%s*", s);
448 #endif
449 qs = lglob(fpat);
450 s = shell_unquote(qs);
451 if (strcmp(s,fpat) == 0)
454 * The filename didn't expand.
456 free(qs);
457 qs = NULL;
459 free(s);
460 free(fpat);
461 return (qs);
463 #endif
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.
469 public int
470 bin_file(f)
471 int f;
473 int n;
474 int bin_count = 0;
475 char data[256];
476 char* p;
477 char* pend;
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 if (n <= 0)
485 return (0);
486 if (utf_mode)
488 bin_count = utf_bin_count(data, n);
489 } else
491 pend = &data[n];
492 for (p = data; p < pend; )
494 LWCHAR c = step_char(&p, +1, pend);
495 if (ctldisp == OPT_ONPLUS && IS_CSI_START(c))
497 do {
498 c = step_char(&p, +1, pend);
499 } while (p < pend && is_ansi_middle(c));
500 } else if (binary_char(c))
501 bin_count++;
505 * Call it a binary file if there are more than 5 binary characters
506 * in the first 256 bytes of the file.
508 return (bin_count > 5);
512 * Try to determine the size of a file by seeking to the end.
514 static POSITION
515 seek_filesize(f)
516 int f;
518 off_t spos;
520 spos = lseek(f, (off_t)0, SEEK_END);
521 if (spos == BAD_LSEEK)
522 return (NULL_POSITION);
523 return ((POSITION) spos);
527 * Read a string from a file.
528 * Return a pointer to the string in memory.
530 static char *
531 readfd(fd)
532 FILE *fd;
534 int len;
535 int ch;
536 char *buf;
537 char *p;
540 * Make a guess about how many chars in the string
541 * and allocate a buffer to hold it.
543 len = 100;
544 buf = (char *) ecalloc(len, sizeof(char));
545 for (p = buf; ; p++)
547 if ((ch = getc(fd)) == '\n' || ch == EOF)
548 break;
549 if (p - buf >= len-1)
552 * The string is too big to fit in the buffer we have.
553 * Allocate a new buffer, twice as big.
555 len *= 2;
556 *p = '\0';
557 p = (char *) ecalloc(len, sizeof(char));
558 strcpy(p, buf);
559 free(buf);
560 buf = p;
561 p = buf + strlen(buf);
563 *p = ch;
565 *p = '\0';
566 return (buf);
571 #if HAVE_POPEN
573 FILE *popen();
576 * Execute a shell command.
577 * Return a pointer to a pipe connected to the shell command's standard output.
579 static FILE *
580 shellcmd(cmd)
581 char *cmd;
583 FILE *fd;
585 #if HAVE_SHELL
586 char *shell;
588 shell = lgetenv("SHELL");
589 if (shell != NULL && *shell != '\0')
591 char *scmd;
592 char *esccmd;
595 * Read the output of <$SHELL -c cmd>.
596 * Escape any metacharacters in the command.
598 esccmd = shell_quote(cmd);
599 if (esccmd == NULL)
601 fd = popen(cmd, "r");
602 } else
604 int len = (int) (strlen(shell) + strlen(esccmd) + 5);
605 scmd = (char *) ecalloc(len, sizeof(char));
606 SNPRINTF3(scmd, len, "%s %s %s", shell, shell_coption(), esccmd);
607 free(esccmd);
608 fd = popen(scmd, "r");
609 free(scmd);
611 } else
612 #endif
614 fd = popen(cmd, "r");
617 * Redirection in `popen' might have messed with the
618 * standard devices. Restore binary input mode.
620 SET_BINARY(0);
621 return (fd);
624 #endif /* HAVE_POPEN */
628 * Expand a filename, doing any system-specific metacharacter substitutions.
630 public char *
631 lglob(filename)
632 char *filename;
634 char *gfilename;
635 char *ofilename;
637 ofilename = fexpand(filename);
638 if (secure)
639 return (ofilename);
640 filename = shell_unquote(ofilename);
642 #ifdef DECL_GLOB_LIST
645 * The globbing function returns a list of names.
647 int length;
648 char *p;
649 char *qfilename;
650 DECL_GLOB_LIST(list)
652 GLOB_LIST(filename, list);
653 if (GLOB_LIST_FAILED(list))
655 free(filename);
656 return (ofilename);
658 length = 1; /* Room for trailing null byte */
659 for (SCAN_GLOB_LIST(list, p))
661 INIT_GLOB_LIST(list, p);
662 qfilename = shell_quote(p);
663 if (qfilename != NULL)
665 length += strlen(qfilename) + 1;
666 free(qfilename);
669 gfilename = (char *) ecalloc(length, sizeof(char));
670 for (SCAN_GLOB_LIST(list, p))
672 INIT_GLOB_LIST(list, p);
673 qfilename = shell_quote(p);
674 if (qfilename != NULL)
676 sprintf(gfilename + strlen(gfilename), "%s ", qfilename);
677 free(qfilename);
681 * Overwrite the final trailing space with a null terminator.
683 *--p = '\0';
684 GLOB_LIST_DONE(list);
686 #else
687 #ifdef DECL_GLOB_NAME
690 * The globbing function returns a single name, and
691 * is called multiple times to walk thru all names.
693 register char *p;
694 register int len;
695 register int n;
696 char *pathname;
697 char *qpathname;
698 DECL_GLOB_NAME(fnd,drive,dir,fname,ext,handle)
700 GLOB_FIRST_NAME(filename, &fnd, handle);
701 if (GLOB_FIRST_FAILED(handle))
703 free(filename);
704 return (ofilename);
707 _splitpath(filename, drive, dir, fname, ext);
708 len = 100;
709 gfilename = (char *) ecalloc(len, sizeof(char));
710 p = gfilename;
711 do {
712 n = (int) (strlen(drive) + strlen(dir) + strlen(fnd.GLOB_NAME) + 1);
713 pathname = (char *) ecalloc(n, sizeof(char));
714 SNPRINTF3(pathname, n, "%s%s%s", drive, dir, fnd.GLOB_NAME);
715 qpathname = shell_quote(pathname);
716 free(pathname);
717 if (qpathname != NULL)
719 n = (int) strlen(qpathname);
720 while (p - gfilename + n + 2 >= len)
723 * No room in current buffer.
724 * Allocate a bigger one.
726 len *= 2;
727 *p = '\0';
728 p = (char *) ecalloc(len, sizeof(char));
729 strcpy(p, gfilename);
730 free(gfilename);
731 gfilename = p;
732 p = gfilename + strlen(gfilename);
734 strcpy(p, qpathname);
735 free(qpathname);
736 p += n;
737 *p++ = ' ';
739 } while (GLOB_NEXT_NAME(handle, &fnd) == 0);
742 * Overwrite the final trailing space with a null terminator.
744 *--p = '\0';
745 GLOB_NAME_DONE(handle);
747 #else
748 #if HAVE_POPEN
751 * We get the shell to glob the filename for us by passing
752 * an "echo" command to the shell and reading its output.
754 FILE *fd;
755 char *s;
756 char *lessecho;
757 char *cmd;
758 char *esc;
759 int len;
761 esc = get_meta_escape();
762 if (strlen(esc) == 0)
763 esc = "-";
764 esc = shell_quote(esc);
765 if (esc == NULL)
767 free(filename);
768 return (ofilename);
770 lessecho = lgetenv("LESSECHO");
771 if (lessecho == NULL || *lessecho == '\0')
772 lessecho = "lessecho";
774 * Invoke lessecho, and read its output (a globbed list of filenames).
776 len = (int) (strlen(lessecho) + strlen(ofilename) + (7*strlen(metachars())) + 24);
777 cmd = (char *) ecalloc(len, sizeof(char));
778 SNPRINTF4(cmd, len, "%s -p0x%x -d0x%x -e%s ", lessecho, openquote, closequote, esc);
779 free(esc);
780 for (s = metachars(); *s != '\0'; s++)
781 sprintf(cmd + strlen(cmd), "-n0x%x ", *s);
782 sprintf(cmd + strlen(cmd), "-- %s", ofilename);
783 fd = shellcmd(cmd);
784 free(cmd);
785 if (fd == NULL)
788 * Cannot create the pipe.
789 * Just return the original (fexpanded) filename.
791 free(filename);
792 return (ofilename);
794 gfilename = readfd(fd);
795 pclose(fd);
796 if (*gfilename == '\0')
798 free(gfilename);
799 free(filename);
800 return (ofilename);
803 #else
805 * No globbing functions at all. Just use the fexpanded filename.
807 gfilename = save(filename);
808 #endif
809 #endif
810 #endif
811 free(filename);
812 free(ofilename);
813 return (gfilename);
817 * Return number of %s escapes in a string.
818 * Return a large number if there are any other % escapes besides %s.
820 static int
821 num_pct_s(lessopen)
822 char *lessopen;
824 int num = 0;
826 while (*lessopen != '\0')
828 if (*lessopen == '%')
830 if (lessopen[1] == '%')
831 ++lessopen;
832 else if (lessopen[1] == 's')
833 ++num;
834 else
835 return (999);
837 ++lessopen;
839 return (num);
843 * See if we should open a "replacement file"
844 * instead of the file we're about to open.
846 public char *
847 open_altfile(filename, pf, pfd)
848 char *filename;
849 int *pf;
850 void **pfd;
852 #if !HAVE_POPEN
853 return (NULL);
854 #else
855 char *lessopen;
856 char *cmd;
857 int len;
858 FILE *fd;
859 #if HAVE_FILENO
860 int returnfd = 0;
861 #endif
863 if (!use_lessopen || secure)
864 return (NULL);
865 ch_ungetchar(-1);
866 if ((lessopen = lgetenv("LESSOPEN")) == NULL)
867 return (NULL);
868 while (*lessopen == '|')
871 * If LESSOPEN starts with a |, it indicates
872 * a "pipe preprocessor".
874 #if !HAVE_FILENO
875 error("LESSOPEN pipe is not supported", NULL_PARG);
876 return (NULL);
877 #else
878 lessopen++;
879 returnfd++;
880 #endif
882 if (*lessopen == '-') {
884 * Lessopen preprocessor will accept "-" as a filename.
886 lessopen++;
887 } else {
888 if (strcmp(filename, "-") == 0)
889 return (NULL);
891 if (num_pct_s(lessopen) > 1)
893 error("Invalid LESSOPEN variable", NULL_PARG);
894 return (NULL);
897 len = (int) (strlen(lessopen) + strlen(filename) + 2);
898 cmd = (char *) ecalloc(len, sizeof(char));
899 SNPRINTF1(cmd, len, lessopen, filename);
900 fd = shellcmd(cmd);
901 free(cmd);
902 if (fd == NULL)
905 * Cannot create the pipe.
907 return (NULL);
909 #if HAVE_FILENO
910 if (returnfd)
912 int f;
913 char c;
916 * Read one char to see if the pipe will produce any data.
917 * If it does, push the char back on the pipe.
919 f = fileno(fd);
920 SET_BINARY(f);
921 if (read(f, &c, 1) != 1)
924 * Pipe is empty.
925 * If more than 1 pipe char was specified,
926 * the exit status tells whether the file itself
927 * is empty, or if there is no alt file.
928 * If only one pipe char, just assume no alt file.
930 int status = pclose(fd);
931 if (returnfd > 1 && status == 0) {
932 *pfd = NULL;
933 *pf = -1;
934 return (save(FAKE_EMPTYFILE));
936 return (NULL);
938 ch_ungetchar(c);
939 *pfd = (void *) fd;
940 *pf = f;
941 return (save("-"));
943 #endif
944 cmd = readfd(fd);
945 pclose(fd);
946 if (*cmd == '\0')
948 * Pipe is empty. This means there is no alt file.
950 return (NULL);
951 return (cmd);
952 #endif /* HAVE_POPEN */
956 * Close a replacement file.
958 public void
959 close_altfile(altfilename, filename, pipefd)
960 char *altfilename;
961 char *filename;
962 void *pipefd;
964 #if HAVE_POPEN
965 char *lessclose;
966 FILE *fd;
967 char *cmd;
968 int len;
970 if (secure)
971 return;
972 if (pipefd != NULL)
974 #if OS2
976 * The pclose function of OS/2 emx sometimes fails.
977 * Send SIGINT to the piped process before closing it.
979 kill(((FILE*)pipefd)->_pid, SIGINT);
980 #endif
981 pclose((FILE*) pipefd);
983 if ((lessclose = lgetenv("LESSCLOSE")) == NULL)
984 return;
985 if (num_pct_s(lessclose) > 2)
987 error("Invalid LESSCLOSE variable", NULL_PARG);
988 return;
990 len = (int) (strlen(lessclose) + strlen(filename) + strlen(altfilename) + 2);
991 cmd = (char *) ecalloc(len, sizeof(char));
992 SNPRINTF2(cmd, len, lessclose, filename, altfilename);
993 fd = shellcmd(cmd);
994 free(cmd);
995 if (fd != NULL)
996 pclose(fd);
997 #endif
1001 * Is the specified file a directory?
1003 public int
1004 is_dir(filename)
1005 char *filename;
1007 int isdir = 0;
1009 filename = shell_unquote(filename);
1010 #if HAVE_STAT
1012 int r;
1013 struct stat statbuf;
1015 r = stat(filename, &statbuf);
1016 isdir = (r >= 0 && S_ISDIR(statbuf.st_mode));
1018 #else
1019 #ifdef _OSK
1021 register int f;
1023 f = open(filename, S_IREAD | S_IFDIR);
1024 if (f >= 0)
1025 close(f);
1026 isdir = (f >= 0);
1028 #endif
1029 #endif
1030 free(filename);
1031 return (isdir);
1035 * Returns NULL if the file can be opened and
1036 * is an ordinary file, otherwise an error message
1037 * (if it cannot be opened or is a directory, etc.)
1039 public char *
1040 bad_file(filename)
1041 char *filename;
1043 register char *m = NULL;
1045 filename = shell_unquote(filename);
1046 if (!force_open && is_dir(filename))
1048 static char is_a_dir[] = " is a directory";
1050 m = (char *) ecalloc(strlen(filename) + sizeof(is_a_dir),
1051 sizeof(char));
1052 strcpy(m, filename);
1053 strcat(m, is_a_dir);
1054 } else
1056 #if HAVE_STAT
1057 int r;
1058 struct stat statbuf;
1060 r = stat(filename, &statbuf);
1061 if (r < 0)
1063 m = errno_message(filename);
1064 } else if (force_open)
1066 m = NULL;
1067 } else if (!S_ISREG(statbuf.st_mode))
1069 static char not_reg[] = " is not a regular file (use -f to see it)";
1070 m = (char *) ecalloc(strlen(filename) + sizeof(not_reg),
1071 sizeof(char));
1072 strcpy(m, filename);
1073 strcat(m, not_reg);
1075 #endif
1077 free(filename);
1078 return (m);
1082 * Return the size of a file, as cheaply as possible.
1083 * In Unix, we can stat the file.
1085 public POSITION
1086 filesize(f)
1087 int f;
1089 #if HAVE_STAT
1090 struct stat statbuf;
1092 if (fstat(f, &statbuf) >= 0)
1093 return ((POSITION) statbuf.st_size);
1094 #else
1095 #ifdef _OSK
1096 long size;
1098 if ((size = (long) _gs_size(f)) >= 0)
1099 return ((POSITION) size);
1100 #endif
1101 #endif
1102 return (seek_filesize(f));
1108 public char *
1109 shell_coption()
1111 return ("-c");
1115 * Return last component of a pathname.
1117 public char *
1118 last_component(name)
1119 char *name;
1121 char *slash;
1123 for (slash = name + strlen(name); slash > name; )
1125 --slash;
1126 if (*slash == *PATHNAME_SEP || *slash == '/')
1127 return (slash + 1);
1129 return (name);