("" < 3.4) always evaluates to true, which unconditionally
[dragonfly.git] / contrib / less-381 / filename.c
blob0b56bdc13d5416443d7f1e39d6d31140e2f25a8e
1 /*
2 * Copyright (C) 1984-2002 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 about less, or for information on how to
8 * contact the author, see the README file.
9 */
13 * Routines to mess around with filenames (and files).
14 * Much of this is very OS dependent.
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
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
51 extern int force_open;
52 extern int secure;
53 extern int use_lessopen;
54 extern IFILE curr_ifile;
55 extern IFILE old_ifile;
56 #if SPACES_IN_FILENAMES
57 extern char openquote;
58 extern char closequote;
59 #endif
62 * Remove quotes around a filename.
64 public char *
65 shell_unquote(str)
66 char *str;
68 char *name;
69 char *p;
71 name = p = (char *) ecalloc(strlen(str)+1, sizeof(char));
72 if (*str == openquote)
74 str++;
75 while (*str != '\0')
77 if (*str == closequote)
79 if (str[1] != closequote)
80 break;
81 str++;
83 *p++ = *str++;
85 } else
87 char *esc = get_meta_escape();
88 int esclen = strlen(esc);
89 while (*str != '\0')
91 if (esclen > 0 && strncmp(str, esc, esclen) == 0)
92 str += esclen;
93 *p++ = *str++;
96 *p = '\0';
97 return (name);
101 * Get the shell's escape character.
103 public char *
104 get_meta_escape()
106 char *s;
108 s = lgetenv("LESSMETAESCAPE");
109 if (s == NULL)
110 s = DEF_METAESCAPE;
111 return (s);
115 * Get the characters which the shell considers to be "metacharacters".
117 static char *
118 metachars()
120 static char *mchars = NULL;
122 if (mchars == NULL)
124 mchars = lgetenv("LESSMETACHARS");
125 if (mchars == NULL)
126 mchars = DEF_METACHARS;
128 return (mchars);
132 * Is this a shell metacharacter?
134 static int
135 metachar(c)
136 char c;
138 return (strchr(metachars(), c) != NULL);
142 * Insert a backslash before each metacharacter in a string.
144 public char *
145 shell_quote(s)
146 char *s;
148 char *p;
149 char *newstr;
150 int len;
151 char *esc = get_meta_escape();
152 int esclen = strlen(esc);
153 int use_quotes = 0;
154 int have_quotes = 0;
157 * Determine how big a string we need to allocate.
159 len = 1; /* Trailing null byte */
160 for (p = s; *p != '\0'; p++)
162 len++;
163 if (*p == openquote || *p == closequote)
164 have_quotes = 1;
165 if (metachar(*p))
167 if (esclen == 0)
170 * We've got a metachar, but this shell
171 * doesn't support escape chars. Use quotes.
173 use_quotes = 1;
174 } else
177 * Allow space for the escape char.
179 len += esclen;
183 if (use_quotes)
185 if (have_quotes)
187 * We can't quote a string that contains quotes.
189 return (NULL);
190 len = strlen(s) + 3;
193 * Allocate and construct the new string.
195 newstr = p = (char *) ecalloc(len, sizeof(char));
196 if (use_quotes)
198 sprintf(newstr, "%c%s%c", openquote, s, closequote);
199 } else
201 while (*s != '\0')
203 if (metachar(*s))
206 * Add the escape char.
208 strcpy(p, esc);
209 p += esclen;
211 *p++ = *s++;
213 *p = '\0';
215 return (newstr);
219 * Return a pathname that points to a specified file in a specified directory.
220 * Return NULL if the file does not exist in the directory.
222 static char *
223 dirfile(dirname, filename)
224 char *dirname;
225 char *filename;
227 char *pathname;
228 char *qpathname;
229 int f;
231 if (dirname == NULL || *dirname == '\0')
232 return (NULL);
234 * Construct the full pathname.
236 pathname = (char *) calloc(strlen(dirname) + strlen(filename) + 2,
237 sizeof(char));
238 if (pathname == NULL)
239 return (NULL);
240 sprintf(pathname, "%s%s%s", dirname, PATHNAME_SEP, filename);
242 * Make sure the file exists.
244 qpathname = shell_unquote(pathname);
245 f = open(qpathname, OPEN_READ);
246 if (f < 0)
248 free(pathname);
249 pathname = NULL;
250 } else
252 close(f);
254 free(qpathname);
255 return (pathname);
259 * Return the full pathname of the given file in the "home directory".
261 public char *
262 homefile(filename)
263 char *filename;
265 register char *pathname;
268 * Try $HOME/filename.
270 pathname = dirfile(lgetenv("HOME"), filename);
271 if (pathname != NULL)
272 return (pathname);
273 #if OS2
275 * Try $INIT/filename.
277 pathname = dirfile(lgetenv("INIT"), filename);
278 if (pathname != NULL)
279 return (pathname);
280 #endif
281 #if MSDOS_COMPILER || OS2
283 * Look for the file anywhere on search path.
285 pathname = (char *) calloc(_MAX_PATH, sizeof(char));
286 #if MSDOS_COMPILER==DJGPPC
288 char *res = searchpath(filename);
289 if (res == 0)
290 *pathname = '\0';
291 else
292 strcpy(pathname, res);
294 #else
295 _searchenv(filename, "PATH", pathname);
296 #endif
297 if (*pathname != '\0')
298 return (pathname);
299 free(pathname);
300 #endif
301 return (NULL);
305 * Expand a string, substituting any "%" with the current filename,
306 * and any "#" with the previous filename.
307 * But a string of N "%"s is just replaced with N-1 "%"s.
308 * Likewise for a string of N "#"s.
309 * {{ This is a lot of work just to support % and #. }}
311 public char *
312 fexpand(s)
313 char *s;
315 register char *fr, *to;
316 register int n;
317 register char *e;
318 IFILE ifile;
320 #define fchar_ifile(c) \
321 ((c) == '%' ? curr_ifile : \
322 (c) == '#' ? old_ifile : NULL_IFILE)
325 * Make one pass to see how big a buffer we
326 * need to allocate for the expanded string.
328 n = 0;
329 for (fr = s; *fr != '\0'; fr++)
331 switch (*fr)
333 case '%':
334 case '#':
335 if (fr > s && fr[-1] == *fr)
338 * Second (or later) char in a string
339 * of identical chars. Treat as normal.
341 n++;
342 } else if (fr[1] != *fr)
345 * Single char (not repeated). Treat specially.
347 ifile = fchar_ifile(*fr);
348 if (ifile == NULL_IFILE)
349 n++;
350 else
351 n += strlen(get_filename(ifile));
354 * Else it is the first char in a string of
355 * identical chars. Just discard it.
357 break;
358 default:
359 n++;
360 break;
364 e = (char *) ecalloc(n+1, sizeof(char));
367 * Now copy the string, expanding any "%" or "#".
369 to = e;
370 for (fr = s; *fr != '\0'; fr++)
372 switch (*fr)
374 case '%':
375 case '#':
376 if (fr > s && fr[-1] == *fr)
378 *to++ = *fr;
379 } else if (fr[1] != *fr)
381 ifile = fchar_ifile(*fr);
382 if (ifile == NULL_IFILE)
383 *to++ = *fr;
384 else
386 strcpy(to, get_filename(ifile));
387 to += strlen(to);
390 break;
391 default:
392 *to++ = *fr;
393 break;
396 *to = '\0';
397 return (e);
400 #if TAB_COMPLETE_FILENAME
403 * Return a blank-separated list of filenames which "complete"
404 * the given string.
406 public char *
407 fcomplete(s)
408 char *s;
410 char *fpat;
411 char *qs;
413 if (secure)
414 return (NULL);
416 * Complete the filename "s" by globbing "s*".
418 #if MSDOS_COMPILER && (MSDOS_COMPILER == MSOFTC || MSDOS_COMPILER == BORLANDC)
420 * But in DOS, we have to glob "s*.*".
421 * But if the final component of the filename already has
422 * a dot in it, just do "s*".
423 * (Thus, "FILE" is globbed as "FILE*.*",
424 * but "FILE.A" is globbed as "FILE.A*").
427 char *slash;
428 for (slash = s+strlen(s)-1; slash > s; slash--)
429 if (*slash == *PATHNAME_SEP || *slash == '/')
430 break;
431 fpat = (char *) ecalloc(strlen(s)+4, sizeof(char));
432 if (strchr(slash, '.') == NULL)
433 sprintf(fpat, "%s*.*", s);
434 else
435 sprintf(fpat, "%s*", s);
437 #else
438 fpat = (char *) ecalloc(strlen(s)+2, sizeof(char));
439 sprintf(fpat, "%s*", s);
440 #endif
441 qs = lglob(fpat);
442 s = shell_unquote(qs);
443 if (strcmp(s,fpat) == 0)
446 * The filename didn't expand.
448 free(qs);
449 qs = NULL;
451 free(s);
452 free(fpat);
453 return (qs);
455 #endif
458 * Try to determine if a file is "binary".
459 * This is just a guess, and we need not try too hard to make it accurate.
461 public int
462 bin_file(f)
463 int f;
465 int i;
466 int n;
467 unsigned char data[64];
469 if (!seekable(f))
470 return (0);
471 if (lseek(f, (off_t)0, 0) == BAD_LSEEK)
472 return (0);
473 n = read(f, data, sizeof(data));
474 for (i = 0; i < n; i++)
475 if (binary_char(data[i]))
476 return (1);
477 return (0);
481 * Try to determine the size of a file by seeking to the end.
483 static POSITION
484 seek_filesize(f)
485 int f;
487 off_t spos;
489 spos = lseek(f, (off_t)0, 2);
490 if (spos == BAD_LSEEK)
491 return (NULL_POSITION);
492 return ((POSITION) spos);
496 * Read a string from a file.
497 * Return a pointer to the string in memory.
499 static char *
500 readfd(fd)
501 FILE *fd;
503 int len;
504 int ch;
505 char *buf;
506 char *p;
509 * Make a guess about how many chars in the string
510 * and allocate a buffer to hold it.
512 len = 100;
513 buf = (char *) ecalloc(len, sizeof(char));
514 for (p = buf; ; p++)
516 if ((ch = getc(fd)) == '\n' || ch == EOF)
517 break;
518 if (p - buf >= len-1)
521 * The string is too big to fit in the buffer we have.
522 * Allocate a new buffer, twice as big.
524 len *= 2;
525 *p = '\0';
526 p = (char *) ecalloc(len, sizeof(char));
527 strcpy(p, buf);
528 free(buf);
529 buf = p;
530 p = buf + strlen(buf);
532 *p = ch;
534 *p = '\0';
535 return (buf);
540 #if HAVE_POPEN
542 FILE *popen();
545 * Execute a shell command.
546 * Return a pointer to a pipe connected to the shell command's standard output.
548 static FILE *
549 shellcmd(cmd)
550 char *cmd;
552 FILE *fd;
554 #if HAVE_SHELL
555 char *shell;
557 shell = lgetenv("SHELL");
558 if (shell != NULL && *shell != '\0')
560 char *scmd;
561 char *esccmd;
564 * Read the output of <$SHELL -c cmd>.
565 * Escape any metacharacters in the command.
567 esccmd = shell_quote(cmd);
568 if (esccmd == NULL)
570 fd = popen(cmd, "r");
571 } else
573 scmd = (char *) ecalloc(strlen(shell) + strlen(esccmd) + 5,
574 sizeof(char));
575 sprintf(scmd, "%s %s %s", shell, shell_coption(), esccmd);
576 free(esccmd);
577 fd = popen(scmd, "r");
578 free(scmd);
580 } else
581 #endif
583 fd = popen(cmd, "r");
586 * Redirection in `popen' might have messed with the
587 * standard devices. Restore binary input mode.
589 SET_BINARY(0);
590 return (fd);
593 #endif /* HAVE_POPEN */
597 * Expand a filename, doing any system-specific metacharacter substitutions.
599 public char *
600 lglob(filename)
601 char *filename;
603 char *gfilename;
604 char *ofilename;
606 ofilename = fexpand(filename);
607 if (secure)
608 return (ofilename);
609 filename = shell_unquote(ofilename);
611 #ifdef DECL_GLOB_LIST
614 * The globbing function returns a list of names.
616 int length;
617 char *p;
618 char *qfilename;
619 DECL_GLOB_LIST(list)
621 GLOB_LIST(filename, list);
622 if (GLOB_LIST_FAILED(list))
624 free(filename);
625 return (ofilename);
627 length = 1; /* Room for trailing null byte */
628 for (SCAN_GLOB_LIST(list, p))
630 INIT_GLOB_LIST(list, p);
631 qfilename = shell_quote(p);
632 if (qfilename != NULL)
634 length += strlen(qfilename) + 1;
635 free(qfilename);
638 gfilename = (char *) ecalloc(length, sizeof(char));
639 for (SCAN_GLOB_LIST(list, p))
641 INIT_GLOB_LIST(list, p);
642 qfilename = shell_quote(p);
643 if (qfilename != NULL)
645 sprintf(gfilename + strlen(gfilename), "%s ", qfilename);
646 free(qfilename);
650 * Overwrite the final trailing space with a null terminator.
652 *--p = '\0';
653 GLOB_LIST_DONE(list);
655 #else
656 #ifdef DECL_GLOB_NAME
659 * The globbing function returns a single name, and
660 * is called multiple times to walk thru all names.
662 register char *p;
663 register int len;
664 register int n;
665 char *pathname;
666 char *qpathname;
667 DECL_GLOB_NAME(fnd,drive,dir,fname,ext,handle)
669 GLOB_FIRST_NAME(filename, &fnd, handle);
670 if (GLOB_FIRST_FAILED(handle))
672 free(filename);
673 return (ofilename);
676 _splitpath(filename, drive, dir, fname, ext);
677 len = 100;
678 gfilename = (char *) ecalloc(len, sizeof(char));
679 p = gfilename;
680 do {
681 n = strlen(drive) + strlen(dir) + strlen(fnd.GLOB_NAME) + 1;
682 pathname = (char *) ecalloc(n, sizeof(char));
683 sprintf(pathname, "%s%s%s", drive, dir, fnd.GLOB_NAME);
684 qpathname = shell_quote(pathname);
685 free(pathname);
686 if (qpathname != NULL)
688 n = strlen(qpathname);
689 while (p - gfilename + n + 2 >= len)
692 * No room in current buffer.
693 * Allocate a bigger one.
695 len *= 2;
696 *p = '\0';
697 p = (char *) ecalloc(len, sizeof(char));
698 strcpy(p, gfilename);
699 free(gfilename);
700 gfilename = p;
701 p = gfilename + strlen(gfilename);
703 strcpy(p, qpathname);
704 free(qpathname);
705 p += n;
706 *p++ = ' ';
708 } while (GLOB_NEXT_NAME(handle, &fnd) == 0);
711 * Overwrite the final trailing space with a null terminator.
713 *--p = '\0';
714 GLOB_NAME_DONE(handle);
716 #else
717 #if HAVE_POPEN
720 * We get the shell to glob the filename for us by passing
721 * an "echo" command to the shell and reading its output.
723 FILE *fd;
724 char *s;
725 char *lessecho;
726 char *cmd;
727 char *esc;
729 esc = get_meta_escape();
730 if (strlen(esc) == 0)
731 esc = "-";
732 esc = shell_quote(esc);
733 if (esc == NULL)
735 free(filename);
736 return (ofilename);
738 lessecho = lgetenv("LESSECHO");
739 if (lessecho == NULL || *lessecho == '\0')
740 lessecho = "lessecho";
742 * Invoke lessecho, and read its output (a globbed list of filenames).
744 cmd = (char *) ecalloc(strlen(lessecho) + strlen(ofilename) + (7*strlen(metachars())) + 24, sizeof(char));
745 sprintf(cmd, "%s -p0x%x -d0x%x -e%s ", lessecho, openquote, closequote, esc);
746 free(esc);
747 for (s = metachars(); *s != '\0'; s++)
748 sprintf(cmd + strlen(cmd), "-n0x%x ", *s);
749 sprintf(cmd + strlen(cmd), "-- %s", ofilename);
750 fd = shellcmd(cmd);
751 free(cmd);
752 if (fd == NULL)
755 * Cannot create the pipe.
756 * Just return the original (fexpanded) filename.
758 free(filename);
759 return (ofilename);
761 gfilename = readfd(fd);
762 pclose(fd);
763 if (*gfilename == '\0')
765 free(gfilename);
766 free(filename);
767 return (ofilename);
770 #else
772 * No globbing functions at all. Just use the fexpanded filename.
774 gfilename = save(filename);
775 #endif
776 #endif
777 #endif
778 free(filename);
779 free(ofilename);
780 return (gfilename);
784 * See if we should open a "replacement file"
785 * instead of the file we're about to open.
787 public char *
788 open_altfile(filename, pf, pfd)
789 char *filename;
790 int *pf;
791 void **pfd;
793 #if !HAVE_POPEN
794 return (NULL);
795 #else
796 char *lessopen;
797 char *cmd;
798 FILE *fd;
799 #if HAVE_FILENO
800 int returnfd = 0;
801 #endif
803 if (!use_lessopen || secure)
804 return (NULL);
805 ch_ungetchar(-1);
806 if ((lessopen = lgetenv("LESSOPEN")) == NULL)
807 return (NULL);
808 if (strcmp(filename, "-") == 0)
809 return (NULL);
810 if (*lessopen == '|')
813 * If LESSOPEN starts with a |, it indicates
814 * a "pipe preprocessor".
816 #if HAVE_FILENO
817 lessopen++;
818 returnfd = 1;
819 #else
820 error("LESSOPEN pipe is not supported", NULL_PARG);
821 return (NULL);
822 #endif
825 cmd = (char *) ecalloc(strlen(lessopen) + strlen(filename) + 2,
826 sizeof(char));
827 sprintf(cmd, lessopen, filename);
828 fd = shellcmd(cmd);
829 free(cmd);
830 if (fd == NULL)
833 * Cannot create the pipe.
835 return (NULL);
837 #if HAVE_FILENO
838 if (returnfd)
840 int f;
841 char c;
844 * Read one char to see if the pipe will produce any data.
845 * If it does, push the char back on the pipe.
847 f = fileno(fd);
848 SET_BINARY(f);
849 if (read(f, &c, 1) != 1)
852 * Pipe is empty. This means there is no alt file.
854 pclose(fd);
855 return (NULL);
857 ch_ungetchar(c);
858 *pfd = (void *) fd;
859 *pf = f;
860 return (save("-"));
862 #endif
863 cmd = readfd(fd);
864 pclose(fd);
865 if (*cmd == '\0')
867 * Pipe is empty. This means there is no alt file.
869 return (NULL);
870 return (cmd);
871 #endif /* HAVE_POPEN */
875 * Close a replacement file.
877 public void
878 close_altfile(altfilename, filename, pipefd)
879 char *altfilename;
880 char *filename;
881 void *pipefd;
883 #if HAVE_POPEN
884 char *lessclose;
885 FILE *fd;
886 char *cmd;
888 if (secure)
889 return;
890 if (pipefd != NULL)
892 #if OS2
894 * The pclose function of OS/2 emx sometimes fails.
895 * Send SIGINT to the piped process before closing it.
897 kill(((FILE*)pipefd)->_pid, SIGINT);
898 #endif
899 pclose((FILE*) pipefd);
901 if ((lessclose = lgetenv("LESSCLOSE")) == NULL)
902 return;
903 cmd = (char *) ecalloc(strlen(lessclose) + strlen(filename) +
904 strlen(altfilename) + 2, sizeof(char));
905 sprintf(cmd, lessclose, filename, altfilename);
906 fd = shellcmd(cmd);
907 free(cmd);
908 if (fd != NULL)
909 pclose(fd);
910 #endif
914 * Is the specified file a directory?
916 public int
917 is_dir(filename)
918 char *filename;
920 int isdir = 0;
922 filename = shell_unquote(filename);
923 #if HAVE_STAT
925 int r;
926 struct stat statbuf;
928 r = stat(filename, &statbuf);
929 isdir = (r >= 0 && S_ISDIR(statbuf.st_mode));
931 #else
932 #ifdef _OSK
934 register int f;
936 f = open(filename, S_IREAD | S_IFDIR);
937 if (f >= 0)
938 close(f);
939 isdir = (f >= 0);
941 #endif
942 #endif
943 free(filename);
944 return (isdir);
948 * Returns NULL if the file can be opened and
949 * is an ordinary file, otherwise an error message
950 * (if it cannot be opened or is a directory, etc.)
952 public char *
953 bad_file(filename)
954 char *filename;
956 register char *m = NULL;
958 filename = shell_unquote(filename);
959 if (is_dir(filename))
961 static char is_a_dir[] = " is a directory";
963 m = (char *) ecalloc(strlen(filename) + sizeof(is_a_dir),
964 sizeof(char));
965 strcpy(m, filename);
966 strcat(m, is_a_dir);
967 } else
969 #if HAVE_STAT
970 int r;
971 struct stat statbuf;
973 r = stat(filename, &statbuf);
974 if (r < 0)
976 m = errno_message(filename);
977 } else if (force_open)
979 m = NULL;
980 } else if (!S_ISREG(statbuf.st_mode))
982 static char not_reg[] = " is not a regular file (use -f to see it)";
983 m = (char *) ecalloc(strlen(filename) + sizeof(not_reg),
984 sizeof(char));
985 strcpy(m, filename);
986 strcat(m, not_reg);
988 #endif
990 free(filename);
991 return (m);
995 * Return the size of a file, as cheaply as possible.
996 * In Unix, we can stat the file.
998 public POSITION
999 filesize(f)
1000 int f;
1002 #if HAVE_STAT
1003 struct stat statbuf;
1005 if (fstat(f, &statbuf) >= 0)
1006 return ((POSITION) statbuf.st_size);
1007 #else
1008 #ifdef _OSK
1009 long size;
1011 if ((size = (long) _gs_size(f)) >= 0)
1012 return ((POSITION) size);
1013 #endif
1014 #endif
1015 return (seek_filesize(f));
1021 public char *
1022 shell_coption()
1024 return ("-c");