2 * Copyright (C) 1984-2023 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.
12 * Routines to mess around with filenames (and files).
13 * Much of this is very OS dependent.
20 #if MSDOS_COMPILER==WIN32C && !defined(_MSC_VER)
23 #if MSDOS_COMPILER==DJGPPC
26 #define _MAX_PATH PATH_MAX
39 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
42 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
46 extern int force_open;
48 extern int use_lessopen;
51 extern IFILE curr_ifile;
52 extern IFILE old_ifile;
53 #if SPACES_IN_FILENAMES
54 extern char openquote;
55 extern char closequote;
58 extern ino_t curr_ino;
59 extern dev_t curr_dev;
63 * Remove quotes around a filename.
65 public char * shell_unquote(char *str)
70 name = p = (char *) ecalloc(strlen(str)+1, sizeof(char));
71 if (*str == openquote)
76 if (*str == closequote)
78 if (str[1] != closequote)
86 char *esc = get_meta_escape();
87 int esclen = (int) strlen(esc);
90 if (esclen > 0 && strncmp(str, esc, esclen) == 0)
100 * Get the shell's escape character.
102 public char * get_meta_escape(void)
106 s = lgetenv("LESSMETAESCAPE");
113 * Get the characters which the shell considers to be "metacharacters".
115 static char * metachars(void)
117 static char *mchars = NULL;
121 mchars = lgetenv("LESSMETACHARS");
123 mchars = DEF_METACHARS;
129 * Is this a shell metacharacter?
131 static int metachar(char c)
133 return (strchr(metachars(), c) != NULL);
137 * Insert a backslash before each metacharacter in a string.
139 public char * shell_quote(char *s)
144 char *esc = get_meta_escape();
145 int esclen = (int) strlen(esc);
150 * Determine how big a string we need to allocate.
152 len = 1; /* Trailing null byte */
153 for (p = s; *p != '\0'; p++)
156 if (*p == openquote || *p == closequote)
163 * We've got a metachar, but this shell
164 * doesn't support escape chars. Use quotes.
170 * Allow space for the escape char.
180 * We can't quote a string that contains quotes.
183 len = (int) strlen(s) + 3;
186 * Allocate and construct the new string.
188 newstr = p = (char *) ecalloc(len, sizeof(char));
191 SNPRINTF3(newstr, len, "%c%s%c", openquote, s, closequote);
199 * Add the escape char.
212 * Return a pathname that points to a specified file in a specified directory.
213 * Return NULL if the file does not exist in the directory.
215 public char * dirfile(char *dirname, char *filename, int must_exist)
221 if (dirname == NULL || *dirname == '\0')
224 * Construct the full pathname.
226 len = (int) (strlen(dirname) + strlen(filename) + 2);
227 pathname = (char *) calloc(len, sizeof(char));
228 if (pathname == NULL)
230 SNPRINTF3(pathname, len, "%s%s%s", dirname, PATHNAME_SEP, filename);
234 * Make sure the file exists.
236 f = open(pathname, OPEN_READ);
250 * Return the full pathname of the given file in the "home directory".
252 public char * homefile(char *filename)
256 /* Try $HOME/filename. */
257 pathname = dirfile(lgetenv("HOME"), filename, 1);
258 if (pathname != NULL)
261 /* Try $INIT/filename. */
262 pathname = dirfile(lgetenv("INIT"), filename, 1);
263 if (pathname != NULL)
266 #if MSDOS_COMPILER || OS2
267 /* Look for the file anywhere on search path. */
268 pathname = (char *) ecalloc(_MAX_PATH, sizeof(char));
269 #if MSDOS_COMPILER==DJGPPC
271 char *res = searchpath(filename);
275 strcpy(pathname, res);
278 _searchenv(filename, "PATH", pathname);
280 if (*pathname != '\0')
288 * Expand a string, substituting any "%" with the current filename,
289 * and any "#" with the previous filename.
290 * But a string of N "%"s is just replaced with N-1 "%"s.
291 * Likewise for a string of N "#"s.
292 * {{ This is a lot of work just to support % and #. }}
294 public char * fexpand(char *s)
301 #define fchar_ifile(c) \
302 ((c) == '%' ? curr_ifile : \
303 (c) == '#' ? old_ifile : NULL_IFILE)
306 * Make one pass to see how big a buffer we
307 * need to allocate for the expanded string.
310 for (fr = s; *fr != '\0'; fr++)
316 if (fr > s && fr[-1] == *fr)
319 * Second (or later) char in a string
320 * of identical chars. Treat as normal.
323 } else if (fr[1] != *fr)
326 * Single char (not repeated). Treat specially.
328 ifile = fchar_ifile(*fr);
329 if (ifile == NULL_IFILE)
332 n += (int) strlen(get_filename(ifile));
335 * Else it is the first char in a string of
336 * identical chars. Just discard it.
345 e = (char *) ecalloc(n+1, sizeof(char));
348 * Now copy the string, expanding any "%" or "#".
351 for (fr = s; *fr != '\0'; fr++)
357 if (fr > s && fr[-1] == *fr)
360 } else if (fr[1] != *fr)
362 ifile = fchar_ifile(*fr);
363 if (ifile == NULL_IFILE)
367 strcpy(to, get_filename(ifile));
382 #if TAB_COMPLETE_FILENAME
385 * Return a blank-separated list of filenames which "complete"
388 public char * fcomplete(char *s)
396 * Complete the filename "s" by globbing "s*".
398 #if MSDOS_COMPILER && (MSDOS_COMPILER == MSOFTC || MSDOS_COMPILER == BORLANDC)
400 * But in DOS, we have to glob "s*.*".
401 * But if the final component of the filename already has
402 * a dot in it, just do "s*".
403 * (Thus, "FILE" is globbed as "FILE*.*",
404 * but "FILE.A" is globbed as "FILE.A*").
409 for (slash = s+strlen(s)-1; slash > s; slash--)
410 if (*slash == *PATHNAME_SEP || *slash == '/')
412 len = (int) strlen(s) + 4;
413 fpat = (char *) ecalloc(len, sizeof(char));
414 if (strchr(slash, '.') == NULL)
415 SNPRINTF1(fpat, len, "%s*.*", s);
417 SNPRINTF1(fpat, len, "%s*", s);
421 int len = (int) strlen(s) + 2;
422 fpat = (char *) ecalloc(len, sizeof(char));
423 SNPRINTF1(fpat, len, "%s*", s);
427 s = shell_unquote(qs);
428 if (strcmp(s,fpat) == 0)
431 * The filename didn't expand.
443 * Try to determine if a file is "binary".
444 * This is just a guess, and we need not try too hard to make it accurate.
446 public int bin_file(int f)
456 if (lseek(f, (off_t)0, SEEK_SET) == BAD_LSEEK)
458 n = read(f, data, sizeof(data));
462 for (p = data; p < edata; )
464 if (utf_mode && !is_utf8_well_formed(p, edata-p))
467 utf_skip_to_lead(&p, edata);
470 LWCHAR c = step_char(&p, +1, edata);
471 struct ansi_state *pansi;
472 if (ctldisp == OPT_ONPLUS && (pansi = ansi_start(c)) != NULL)
474 skip_ansi(pansi, &p, edata);
476 } else if (binary_char(c))
481 * Call it a binary file if there are more than 5 binary characters
482 * in the first 256 bytes of the file.
484 return (bin_count > 5);
488 * Try to determine the size of a file by seeking to the end.
490 static POSITION seek_filesize(int f)
494 spos = lseek(f, (off_t)0, SEEK_END);
495 if (spos == BAD_LSEEK)
496 return (NULL_POSITION);
497 return ((POSITION) spos);
502 * Read a string from a file.
503 * Return a pointer to the string in memory.
505 static char * readfd(FILE *fd)
513 * Make a guess about how many chars in the string
514 * and allocate a buffer to hold it.
517 buf = (char *) ecalloc(len, sizeof(char));
520 if ((ch = getc(fd)) == '\n' || ch == EOF)
522 if (p - buf >= len-1)
525 * The string is too big to fit in the buffer we have.
526 * Allocate a new buffer, twice as big.
530 p = (char *) ecalloc(len, sizeof(char));
534 p = buf + strlen(buf);
543 * Execute a shell command.
544 * Return a pointer to a pipe connected to the shell command's standard output.
546 static FILE * shellcmd(char *cmd)
553 shell = lgetenv("SHELL");
554 if (!isnullenv(shell))
560 * Read the output of <$SHELL -c cmd>.
561 * Escape any metacharacters in the command.
563 esccmd = shell_quote(cmd);
566 fd = popen(cmd, "r");
569 int len = (int) (strlen(shell) + strlen(esccmd) + 5);
570 scmd = (char *) ecalloc(len, sizeof(char));
571 SNPRINTF3(scmd, len, "%s %s %s", shell, shell_coption(), esccmd);
573 fd = popen(scmd, "r");
579 fd = popen(cmd, "r");
582 * Redirection in `popen' might have messed with the
583 * standard devices. Restore binary input mode.
589 #endif /* HAVE_POPEN */
593 * Expand a filename, doing any system-specific metacharacter substitutions.
595 public char * lglob(char *filename)
599 filename = fexpand(filename);
603 #ifdef DECL_GLOB_LIST
606 * The globbing function returns a list of names.
613 GLOB_LIST(filename, list);
614 if (GLOB_LIST_FAILED(list))
618 length = 1; /* Room for trailing null byte */
619 for (SCAN_GLOB_LIST(list, p))
621 INIT_GLOB_LIST(list, p);
622 qfilename = shell_quote(p);
623 if (qfilename != NULL)
625 length += strlen(qfilename) + 1;
629 gfilename = (char *) ecalloc(length, sizeof(char));
630 for (SCAN_GLOB_LIST(list, p))
632 INIT_GLOB_LIST(list, p);
633 qfilename = shell_quote(p);
634 if (qfilename != NULL)
636 sprintf(gfilename + strlen(gfilename), "%s ", qfilename);
641 * Overwrite the final trailing space with a null terminator.
644 GLOB_LIST_DONE(list);
647 #ifdef DECL_GLOB_NAME
650 * The globbing function returns a single name, and
651 * is called multiple times to walk thru all names.
658 DECL_GLOB_NAME(fnd,drive,dir,fname,ext,handle)
660 GLOB_FIRST_NAME(filename, &fnd, handle);
661 if (GLOB_FIRST_FAILED(handle))
666 _splitpath(filename, drive, dir, fname, ext);
668 gfilename = (char *) ecalloc(len, sizeof(char));
671 n = (int) (strlen(drive) + strlen(dir) + strlen(fnd.GLOB_NAME) + 1);
672 pfilename = (char *) ecalloc(n, sizeof(char));
673 SNPRINTF3(pfilename, n, "%s%s%s", drive, dir, fnd.GLOB_NAME);
674 qfilename = shell_quote(pfilename);
676 if (qfilename != NULL)
678 n = (int) strlen(qfilename);
679 while (p - gfilename + n + 2 >= len)
682 * No room in current buffer.
683 * Allocate a bigger one.
687 p = (char *) ecalloc(len, sizeof(char));
688 strcpy(p, gfilename);
691 p = gfilename + strlen(gfilename);
693 strcpy(p, qfilename);
698 } while (GLOB_NEXT_NAME(handle, &fnd) == 0);
701 * Overwrite the final trailing space with a null terminator.
704 GLOB_NAME_DONE(handle);
710 * We get the shell to glob the filename for us by passing
711 * an "echo" command to the shell and reading its output.
720 esc = get_meta_escape();
721 if (strlen(esc) == 0)
723 esc = shell_quote(esc);
728 lessecho = lgetenv("LESSECHO");
729 if (isnullenv(lessecho))
730 lessecho = "lessecho";
732 * Invoke lessecho, and read its output (a globbed list of filenames).
734 len = (int) (strlen(lessecho) + strlen(filename) + (7*strlen(metachars())) + 24);
735 cmd = (char *) ecalloc(len, sizeof(char));
736 SNPRINTF4(cmd, len, "%s -p0x%x -d0x%x -e%s ", lessecho,
737 (unsigned char) openquote, (unsigned char) closequote, esc);
739 for (s = metachars(); *s != '\0'; s++)
740 sprintf(cmd + strlen(cmd), "-n0x%x ", (unsigned char) *s);
741 sprintf(cmd + strlen(cmd), "-- %s", filename);
747 * Cannot create the pipe.
748 * Just return the original (fexpanded) filename.
752 gfilename = readfd(fd);
754 if (*gfilename == '\0')
762 * No globbing functions at all. Just use the fexpanded filename.
764 gfilename = save(filename);
773 * Does path not represent something in the file system?
775 public int is_fake_pathname(char *path)
777 return (strcmp(path, "-") == 0 ||
778 strcmp(path, FAKE_HELPFILE) == 0 || strcmp(path, FAKE_EMPTYFILE) == 0);
782 * Return canonical pathname.
784 public char * lrealpath(char *path)
786 if (!is_fake_pathname(path))
789 char rpath[PATH_MAX];
790 if (realpath(path, rpath) != NULL)
791 return (save(rpath));
799 * Return number of %s escapes in a string.
800 * Return a large number if there are any other % escapes besides %s.
802 static int num_pct_s(char *lessopen)
806 while (*lessopen != '\0')
808 if (*lessopen == '%')
810 if (lessopen[1] == '%')
812 else if (lessopen[1] == 's')
824 * See if we should open a "replacement file"
825 * instead of the file we're about to open.
827 public char * open_altfile(char *filename, int *pf, void **pfd)
841 if (!use_lessopen || secure)
844 if ((lessopen = lgetenv("LESSOPEN")) == NULL)
846 while (*lessopen == '|')
849 * If LESSOPEN starts with a |, it indicates
850 * a "pipe preprocessor".
853 error("LESSOPEN pipe is not supported", NULL_PARG);
860 if (*lessopen == '-')
863 * Lessopen preprocessor will accept "-" as a filename.
868 if (strcmp(filename, "-") == 0)
871 if (num_pct_s(lessopen) != 1)
873 error("LESSOPEN ignored: must contain exactly one %%s", NULL_PARG);
877 qfilename = shell_quote(filename);
878 len = (int) (strlen(lessopen) + strlen(qfilename) + 2);
879 cmd = (char *) ecalloc(len, sizeof(char));
880 SNPRINTF1(cmd, len, lessopen, qfilename);
887 * Cannot create the pipe.
898 * The alt file is a pipe. Read one char
899 * to see if the pipe will produce any data.
900 * If it does, push the char back on the pipe.
904 if (read(f, &c, 1) != 1)
908 * If more than 1 pipe char was specified,
909 * the exit status tells whether the file itself
910 * is empty, or if there is no alt file.
911 * If only one pipe char, just assume no alt file.
913 int status = pclose(fd);
914 if (returnfd > 1 && status == 0) {
918 return (save(FAKE_EMPTYFILE));
923 /* Alt pipe contains data, so use it. */
930 /* The alt file is a regular file. Read its name from LESSOPEN. */
936 * Pipe is empty. This means there is no alt file.
942 #endif /* HAVE_POPEN */
946 * Close a replacement file.
948 public void close_altfile(char *altfilename, char *filename)
961 if ((lessclose = lgetenv("LESSCLOSE")) == NULL)
963 if (num_pct_s(lessclose) > 2)
965 error("LESSCLOSE ignored; must contain no more than 2 %%s", NULL_PARG);
968 qfilename = shell_quote(filename);
969 qaltfilename = shell_quote(altfilename);
970 len = (int) (strlen(lessclose) + strlen(qfilename) + strlen(qaltfilename) + 2);
971 cmd = (char *) ecalloc(len, sizeof(char));
972 SNPRINTF2(cmd, len, lessclose, qfilename, qaltfilename);
983 * Is the specified file a directory?
985 public int is_dir(char *filename)
994 r = stat(filename, &statbuf);
995 isdir = (r >= 0 && S_ISDIR(statbuf.st_mode));
1002 f = open(filename, S_IREAD | S_IFDIR);
1013 * Returns NULL if the file can be opened and
1014 * is an ordinary file, otherwise an error message
1015 * (if it cannot be opened or is a directory, etc.)
1017 public char * bad_file(char *filename)
1021 if (!force_open && is_dir(filename))
1023 static char is_a_dir[] = " is a directory";
1025 m = (char *) ecalloc(strlen(filename) + sizeof(is_a_dir),
1027 strcpy(m, filename);
1028 strcat(m, is_a_dir);
1033 struct stat statbuf;
1035 r = stat(filename, &statbuf);
1038 m = errno_message(filename);
1039 } else if (force_open)
1042 } else if (!S_ISREG(statbuf.st_mode))
1044 static char not_reg[] = " is not a regular file (use -f to see it)";
1045 m = (char *) ecalloc(strlen(filename) + sizeof(not_reg),
1047 strcpy(m, filename);
1056 * Return the size of a file, as cheaply as possible.
1057 * In Unix, we can stat the file.
1059 public POSITION filesize(int f)
1062 struct stat statbuf;
1064 if (fstat(f, &statbuf) >= 0)
1065 return ((POSITION) statbuf.st_size);
1070 if ((size = (long) _gs_size(f)) >= 0)
1071 return ((POSITION) size);
1074 return (seek_filesize(f));
1077 public int curr_ifile_changed(void)
1081 * If the file's i-number or device has changed,
1082 * or if the file is smaller than it previously was,
1083 * the file must be different.
1086 POSITION curr_pos = ch_tell();
1087 int r = stat(get_filename(curr_ifile), &st);
1088 if (r == 0 && (st.st_ino != curr_ino ||
1089 st.st_dev != curr_dev ||
1090 (curr_pos != NULL_POSITION && st.st_size < curr_pos)))
1099 public char * shell_coption(void)
1105 * Return last component of a pathname.
1107 public char * last_component(char *name)
1111 for (slash = name + strlen(name); slash > name; )
1114 if (*slash == *PATHNAME_SEP || *slash == '/')