2 * Copyright (C) 1984-2000 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.
13 * Routines to mess around with filenames (and files).
14 * Much of this is very OS dependent.
21 #if MSDOS_COMPILER==WIN32C && !defined(_MSC_VER)
24 #if MSDOS_COMPILER==DJGPPC
27 #define _MAX_PATH PATH_MAX
43 #define S_ISDIR(m) (((m) & S_IFMT) == S_IFDIR)
46 #define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
51 extern int force_open;
53 extern IFILE curr_ifile;
54 extern IFILE old_ifile;
55 #if SPACES_IN_FILENAMES
56 extern char openquote;
57 extern char closequote;
61 * Remove quotes around a filename.
67 #if SPACES_IN_FILENAMES
71 if (*str != openquote)
73 name = (char *) ecalloc(strlen(str), sizeof(char));
75 p = name + strlen(name) - 1;
85 * Return a pathname that points to a specified file in a specified directory.
86 * Return NULL if the file does not exist in the directory.
89 dirfile(dirname, filename)
97 if (dirname == NULL || *dirname == '\0')
100 * Construct the full pathname.
102 pathname = (char *) calloc(strlen(dirname) + strlen(filename) + 2,
104 if (pathname == NULL)
106 sprintf(pathname, "%s%s%s", dirname, PATHNAME_SEP, filename);
108 * Make sure the file exists.
110 qpathname = unquote_file(pathname);
111 f = open(qpathname, OPEN_READ);
125 * Return the full pathname of the given file in the "home directory".
131 register char *pathname;
134 * Try $HOME/filename.
136 pathname = dirfile(lgetenv("HOME"), filename);
137 if (pathname != NULL)
141 * Try $INIT/filename.
143 pathname = dirfile(lgetenv("INIT"), filename);
144 if (pathname != NULL)
147 #if MSDOS_COMPILER || OS2
149 * Look for the file anywhere on search path.
151 pathname = (char *) calloc(_MAX_PATH, sizeof(char));
152 #if MSDOS_COMPILER==DJGPPC
154 char *res = searchpath(filename);
158 strcpy(pathname, res);
161 _searchenv(filename, "PATH", pathname);
163 if (*pathname != '\0')
171 * Expand a string, substituting any "%" with the current filename,
172 * and any "#" with the previous filename.
173 * But a string of N "%"s is just replaced with N-1 "%"s.
174 * Likewise for a string of N "#"s.
175 * {{ This is a lot of work just to support % and #. }}
181 register char *fr, *to;
186 #define fchar_ifile(c) \
187 ((c) == '%' ? curr_ifile : \
188 (c) == '#' ? old_ifile : NULL_IFILE)
191 * Make one pass to see how big a buffer we
192 * need to allocate for the expanded string.
195 for (fr = s; *fr != '\0'; fr++)
201 if (fr > s && fr[-1] == *fr)
204 * Second (or later) char in a string
205 * of identical chars. Treat as normal.
208 } else if (fr[1] != *fr)
211 * Single char (not repeated). Treat specially.
213 ifile = fchar_ifile(*fr);
214 if (ifile == NULL_IFILE)
217 n += strlen(get_filename(ifile));
220 * Else it is the first char in a string of
221 * identical chars. Just discard it.
230 e = (char *) ecalloc(n+1, sizeof(char));
233 * Now copy the string, expanding any "%" or "#".
236 for (fr = s; *fr != '\0'; fr++)
242 if (fr > s && fr[-1] == *fr)
245 } else if (fr[1] != *fr)
247 ifile = fchar_ifile(*fr);
248 if (ifile == NULL_IFILE)
252 strcpy(to, get_filename(ifile));
266 #if TAB_COMPLETE_FILENAME
269 * Return a blank-separated list of filenames which "complete"
281 * Complete the filename "s" by globbing "s*".
283 #if MSDOS_COMPILER && (MSDOS_COMPILER == MSOFTC || MSDOS_COMPILER == BORLANDC)
285 * But in DOS, we have to glob "s*.*".
286 * But if the final component of the filename already has
287 * a dot in it, just do "s*".
288 * (Thus, "FILE" is globbed as "FILE*.*",
289 * but "FILE.A" is globbed as "FILE.A*").
293 for (slash = s+strlen(s)-1; slash > s; slash--)
294 if (*slash == *PATHNAME_SEP || *slash == '/')
296 fpat = (char *) ecalloc(strlen(s)+4, sizeof(char));
297 if (strchr(slash, '.') == NULL)
298 sprintf(fpat, "%s*.*", s);
300 sprintf(fpat, "%s*", s);
303 fpat = (char *) ecalloc(strlen(s)+2, sizeof(char));
304 sprintf(fpat, "%s*", s);
307 if (strcmp(s,fpat) == 0)
310 * The filename didn't expand.
321 * Try to determine if a file is "binary".
322 * This is just a guess, and we need not try too hard to make it accurate.
330 unsigned char data[64];
334 if (lseek(f, (off_t)0, 0) == BAD_LSEEK)
336 n = read(f, data, sizeof(data));
337 for (i = 0; i < n; i++)
338 if (binary_char(data[i]))
344 * Try to determine the size of a file by seeking to the end.
352 spos = lseek(f, (off_t)0, 2);
353 if (spos == BAD_LSEEK)
354 return (NULL_POSITION);
355 return ((POSITION) spos);
359 * Read a string from a file.
360 * Return a pointer to the string in memory.
372 * Make a guess about how many chars in the string
373 * and allocate a buffer to hold it.
376 buf = (char *) ecalloc(len, sizeof(char));
379 if ((ch = getc(fd)) == '\n' || ch == EOF)
381 if (p - buf >= len-1)
384 * The string is too big to fit in the buffer we have.
385 * Allocate a new buffer, twice as big.
389 p = (char *) ecalloc(len, sizeof(char));
393 p = buf + strlen(buf);
404 * Get the shell's escape character.
411 s = lgetenv("LESSMETAESCAPE");
418 * Is this a shell metacharacter?
424 static char *metachars = NULL;
426 if (metachars == NULL)
428 metachars = lgetenv("LESSMETACHARS");
429 if (metachars == NULL)
430 metachars = DEF_METACHARS;
432 return (strchr(metachars, c) != NULL);
436 * Insert a backslash before each metacharacter in a string.
449 * Determine how big a string we need to allocate.
451 esc = get_meta_escape();
452 esclen = strlen(esc);
453 len = 1; /* Trailing null byte */
454 for (p = s; *p != '\0'; p++)
462 * We've got a metachar, but this shell
463 * doesn't support escape chars. Give up.
468 * Allow space for the escape char.
474 * Allocate and construct the new string.
476 newstr = p = (char *) ecalloc(len, sizeof(char));
482 * Add the escape char.
493 #else /* HAVE_SHELL */
502 #endif /* HAVE_SHELL */
510 * Execute a shell command.
511 * Return a pointer to a pipe connected to the shell command's standard output.
522 shell = lgetenv("SHELL");
523 if (shell != NULL && *shell != '\0')
529 * Try to escape any metacharacters in the command.
530 * If we can't do that, just put the command in quotes.
531 * (But that doesn't work well if the command itself
534 if ((esccmd = esc_metachars(cmd)) == NULL)
537 * Cannot escape the metacharacters, so use quotes.
538 * Read the output of <$SHELL -c "cmd">.
540 scmd = (char *) ecalloc(strlen(shell) + strlen(cmd) + 7,
542 sprintf(scmd, "%s -c \"%s\"", shell, cmd);
546 * Read the output of <$SHELL -c cmd>.
547 * No quotes; use the escaped cmd.
549 scmd = (char *) ecalloc(strlen(shell) + strlen(esccmd) + 5,
551 sprintf(scmd, "%s -c %s", shell, esccmd);
554 fd = popen(scmd, "r");
559 fd = popen(cmd, "r");
561 * Redirection in `popen' might have messed with the
562 * standard devices. Restore binary input mode.
569 #endif /* HAVE_POPEN */
573 * Expand a filename, doing any system-specific metacharacter substitutions.
582 ofilename = fexpand(filename);
585 filename = unquote_file(ofilename);
587 #ifdef DECL_GLOB_LIST
590 * The globbing function returns a list of names.
596 GLOB_LIST(filename, list);
597 if (GLOB_LIST_FAILED(list))
602 length = 1; /* Room for trailing null byte */
603 for (SCAN_GLOB_LIST(list, p))
605 INIT_GLOB_LIST(list, p);
606 length += strlen(p) + 1;
607 #if SPACES_IN_FILENAMES
608 if (strchr(p, ' ') != NULL)
609 length += 2; /* Allow for quotes */
612 gfilename = (char *) ecalloc(length, sizeof(char));
613 for (SCAN_GLOB_LIST(list, p))
615 INIT_GLOB_LIST(list, p);
616 #if SPACES_IN_FILENAMES
617 if (strchr(p, ' ') != NULL)
618 sprintf(gfilename + strlen(gfilename), "%c%s%c ",
619 openquote, p, closequote);
622 sprintf(gfilename + strlen(gfilename), "%s ", p);
625 * Overwrite the final trailing space with a null terminator.
628 GLOB_LIST_DONE(list);
631 #ifdef DECL_GLOB_NAME
634 * The globbing function returns a single name, and
635 * is called multiple times to walk thru all names.
640 #if SPACES_IN_FILENAMES
641 register int spaces_in_file;
643 DECL_GLOB_NAME(fnd,drive,dir,fname,ext,handle)
645 GLOB_FIRST_NAME(filename, &fnd, handle);
646 if (GLOB_FIRST_FAILED(handle))
652 _splitpath(filename, drive, dir, fname, ext);
654 gfilename = (char *) ecalloc(len, sizeof(char));
657 n = strlen(drive) + strlen(dir) + strlen(fnd.GLOB_NAME) + 1;
658 #if SPACES_IN_FILENAMES
660 if (strchr(fnd.GLOB_NAME, ' ') != NULL ||
661 strchr(filename, ' ') != NULL)
667 while (p - gfilename + n+2 >= len)
670 * No room in current buffer. Allocate a bigger one.
674 p = (char *) ecalloc(len, sizeof(char));
675 strcpy(p, gfilename);
678 p = gfilename + strlen(gfilename);
680 #if SPACES_IN_FILENAMES
682 sprintf(p, "%c%s%s%s%c ", openquote,
683 drive, dir, fnd.GLOB_NAME, closequote);
686 sprintf(p, "%s%s%s ", drive, dir, fnd.GLOB_NAME);
688 } while (GLOB_NEXT_NAME(handle, &fnd) == 0);
691 * Overwrite the final trailing space with a null terminator.
694 GLOB_NAME_DONE(handle);
700 * We get the shell to glob the filename for us by passing
701 * an "echo" command to the shell and reading its output.
708 lessecho = lgetenv("LESSECHO");
709 if (lessecho == NULL || *lessecho == '\0')
710 lessecho = "lessecho";
711 s = esc_metachars(filename);
715 * There may be dangerous metachars in this name.
716 * We can't risk passing it to the shell.
717 * {{ For example, do "!;TAB" when the first file
718 * in the dir is named "rm". }}
724 * Invoke lessecho, and read its output (a globbed list of filenames).
726 cmd = (char *) ecalloc(strlen(lessecho) + strlen(s) + 24, sizeof(char));
727 sprintf(cmd, "%s -p0x%x -d0x%x -- %s",
728 lessecho, openquote, closequote, s);
735 * Cannot create the pipe.
736 * Just return the original (fexpanded) filename.
741 gfilename = readfd(fd);
743 if (*gfilename == '\0')
752 * No globbing functions at all. Just use the fexpanded filename.
754 gfilename = save(filename);
764 * See if we should open a "replacement file"
765 * instead of the file we're about to open.
768 open_altfile(filename, pf, pfd)
787 if ((lessopen = lgetenv("LESSOPEN")) == NULL)
789 if (strcmp(filename, "-") == 0)
791 if (*lessopen == '|')
794 * If LESSOPEN starts with a |, it indicates
795 * a "pipe preprocessor".
801 error("LESSOPEN pipe is not supported", NULL_PARG);
806 gfilename = esc_metachars(filename);
807 if (gfilename == NULL)
810 * Cannot escape metacharacters.
814 cmd = (char *) ecalloc(strlen(lessopen) + strlen(gfilename) + 2,
816 sprintf(cmd, lessopen, gfilename);
823 * Cannot create the pipe.
834 * Read one char to see if the pipe will produce any data.
835 * If it does, push the char back on the pipe.
839 if (read(f, &c, 1) != 1)
842 * Pipe is empty. This means there is no alt file.
853 gfilename = readfd(fd);
855 if (*gfilename == '\0')
857 * Pipe is empty. This means there is no alt file.
861 #endif /* HAVE_POPEN */
865 * Close a replacement file.
868 close_altfile(altfilename, filename, pipefd)
886 * The pclose function of OS/2 emx sometimes fails.
887 * Send SIGINT to the piped process before closing it.
889 kill(((FILE*)pipefd)->_pid, SIGINT);
891 pclose((FILE*) pipefd);
893 if ((lessclose = lgetenv("LESSCLOSE")) == NULL)
895 gfilename = esc_metachars(filename);
896 if (gfilename == NULL)
900 galtfilename = esc_metachars(altfilename);
901 if (galtfilename == NULL)
906 cmd = (char *) ecalloc(strlen(lessclose) + strlen(gfilename) +
907 strlen(galtfilename) + 2, sizeof(char));
908 sprintf(cmd, lessclose, gfilename, galtfilename);
919 * Is the specified file a directory?
927 filename = unquote_file(filename);
933 r = stat(filename, &statbuf);
934 isdir = (r >= 0 && S_ISDIR(statbuf.st_mode));
941 f = open(filename, S_IREAD | S_IFDIR);
953 * Returns NULL if the file can be opened and
954 * is an ordinary file, otherwise an error message
955 * (if it cannot be opened or is a directory, etc.)
961 register char *m = NULL;
963 filename = unquote_file(filename);
964 if (is_dir(filename))
966 static char is_dir[] = " is a directory";
968 m = (char *) ecalloc(strlen(filename) + sizeof(is_dir),
978 r = stat(filename, &statbuf);
981 m = errno_message(filename);
982 } else if (force_open)
985 } else if (!S_ISREG(statbuf.st_mode))
987 static char not_reg[] = " is not a regular file (use -f to see it)";
988 m = (char *) ecalloc(strlen(filename) + sizeof(not_reg),
1000 * Return the size of a file, as cheaply as possible.
1001 * In Unix, we can stat the file.
1008 struct stat statbuf;
1010 if (fstat(f, &statbuf) >= 0)
1011 return ((POSITION) statbuf.st_size);
1016 if ((size = (long) _gs_size(f)) >= 0)
1017 return ((POSITION) size);
1020 return (seek_filesize(f));