2 * Copyright (C) 1984-2007 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 int use_lessopen;
55 extern IFILE curr_ifile;
56 extern IFILE old_ifile;
57 #if SPACES_IN_FILENAMES
58 extern char openquote;
59 extern char closequote;
63 * Remove quotes around a filename.
72 name = p = (char *) ecalloc(strlen(str)+1, sizeof(char));
73 if (*str == openquote)
78 if (*str == closequote)
80 if (str[1] != closequote)
88 char *esc = get_meta_escape();
89 int esclen = strlen(esc);
92 if (esclen > 0 && strncmp(str, esc, esclen) == 0)
102 * Get the shell's escape character.
109 s = lgetenv("LESSMETAESCAPE");
116 * Get the characters which the shell considers to be "metacharacters".
121 static char *mchars = NULL;
125 mchars = lgetenv("LESSMETACHARS");
127 mchars = DEF_METACHARS;
133 * Is this a shell metacharacter?
139 return (strchr(metachars(), c) != NULL);
143 * Insert a backslash before each metacharacter in a string.
152 char *esc = get_meta_escape();
153 int esclen = strlen(esc);
158 * Determine how big a string we need to allocate.
160 len = 1; /* Trailing null byte */
161 for (p = s; *p != '\0'; p++)
164 if (*p == openquote || *p == closequote)
171 * We've got a metachar, but this shell
172 * doesn't support escape chars. Use quotes.
178 * Allow space for the escape char.
188 * We can't quote a string that contains quotes.
194 * Allocate and construct the new string.
196 newstr = p = (char *) ecalloc(len, sizeof(char));
199 SNPRINTF3(newstr, len, "%c%s%c", openquote, s, closequote);
207 * Add the escape char.
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.
224 dirfile(dirname, filename)
233 if (dirname == NULL || *dirname == '\0')
236 * Construct the full pathname.
238 len= strlen(dirname) + strlen(filename) + 2;
239 pathname = (char *) calloc(len, sizeof(char));
240 if (pathname == 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);
261 * Return the full pathname of the given file in the "home directory".
267 register char *pathname;
270 * Try $HOME/filename.
272 pathname = dirfile(lgetenv("HOME"), filename);
273 if (pathname != NULL)
277 * Try $INIT/filename.
279 pathname = dirfile(lgetenv("INIT"), filename);
280 if (pathname != NULL)
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);
294 strcpy(pathname, res);
297 _searchenv(filename, "PATH", pathname);
299 if (*pathname != '\0')
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 #. }}
317 register char *fr, *to;
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.
331 for (fr = s; *fr != '\0'; fr++)
337 if (fr > s && fr[-1] == *fr)
340 * Second (or later) char in a string
341 * of identical chars. Treat as normal.
344 } else if (fr[1] != *fr)
347 * Single char (not repeated). Treat specially.
349 ifile = fchar_ifile(*fr);
350 if (ifile == NULL_IFILE)
353 n += strlen(get_filename(ifile));
356 * Else it is the first char in a string of
357 * identical chars. Just discard it.
366 e = (char *) ecalloc(n+1, sizeof(char));
369 * Now copy the string, expanding any "%" or "#".
372 for (fr = s; *fr != '\0'; fr++)
378 if (fr > s && fr[-1] == *fr)
381 } else if (fr[1] != *fr)
383 ifile = fchar_ifile(*fr);
384 if (ifile == NULL_IFILE)
388 strcpy(to, get_filename(ifile));
402 #if TAB_COMPLETE_FILENAME
405 * Return a blank-separated list of filenames which "complete"
418 * Complete the filename "s" by globbing "s*".
420 #if MSDOS_COMPILER && (MSDOS_COMPILER == MSOFTC || MSDOS_COMPILER == BORLANDC)
422 * But in DOS, we have to glob "s*.*".
423 * But if the final component of the filename already has
424 * a dot in it, just do "s*".
425 * (Thus, "FILE" is globbed as "FILE*.*",
426 * but "FILE.A" is globbed as "FILE.A*").
431 for (slash = s+strlen(s)-1; slash > s; slash--)
432 if (*slash == *PATHNAME_SEP || *slash == '/')
435 fpat = (char *) ecalloc(len, sizeof(char));
436 if (strchr(slash, '.') == NULL)
437 SNPRINTF1(fpat, len, "%s*.*", s);
439 SNPRINTF1(fpat, len, "%s*", s);
443 int len = strlen(s) + 2;
444 fpat = (char *) ecalloc(len, sizeof(char));
445 SNPRINTF1(fpat, len, "%s*", s);
449 s = shell_unquote(qs);
450 if (strcmp(s,fpat) == 0)
453 * The filename didn't expand.
465 * Try to determine if a file is "binary".
466 * This is just a guess, and we need not try too hard to make it accurate.
475 unsigned char data[256];
479 if (lseek(f, (off_t)0, SEEK_SET) == BAD_LSEEK)
481 n = read(f, data, sizeof(data));
482 for (i = 0; i < n; i++)
485 if (ctldisp == OPT_ONPLUS && IS_CSI_START(c))
487 while (++i < n && is_ansi_middle(data[i]))
489 } else if (binary_char(c))
493 * Call it a binary file if there are more than 5 binary characters
494 * in the first 256 bytes of the file.
496 return (bin_count > 5);
500 * Try to determine the size of a file by seeking to the end.
508 spos = lseek(f, (off_t)0, SEEK_END);
509 if (spos == BAD_LSEEK)
510 return (NULL_POSITION);
511 return ((POSITION) spos);
515 * Read a string from a file.
516 * Return a pointer to the string in memory.
528 * Make a guess about how many chars in the string
529 * and allocate a buffer to hold it.
532 buf = (char *) ecalloc(len, sizeof(char));
535 if ((ch = getc(fd)) == '\n' || ch == EOF)
537 if (p - buf >= len-1)
540 * The string is too big to fit in the buffer we have.
541 * Allocate a new buffer, twice as big.
545 p = (char *) ecalloc(len, sizeof(char));
549 p = buf + strlen(buf);
564 * Execute a shell command.
565 * Return a pointer to a pipe connected to the shell command's standard output.
576 shell = lgetenv("SHELL");
577 if (shell != NULL && *shell != '\0')
583 * Read the output of <$SHELL -c cmd>.
584 * Escape any metacharacters in the command.
586 esccmd = shell_quote(cmd);
589 fd = popen(cmd, "r");
592 int len = strlen(shell) + strlen(esccmd) + 5;
593 scmd = (char *) ecalloc(len, sizeof(char));
594 SNPRINTF3(scmd, len, "%s %s %s", shell, shell_coption(), esccmd);
596 fd = popen(scmd, "r");
602 fd = popen(cmd, "r");
605 * Redirection in `popen' might have messed with the
606 * standard devices. Restore binary input mode.
612 #endif /* HAVE_POPEN */
616 * Expand a filename, doing any system-specific metacharacter substitutions.
625 ofilename = fexpand(filename);
628 filename = shell_unquote(ofilename);
630 #ifdef DECL_GLOB_LIST
633 * The globbing function returns a list of names.
640 GLOB_LIST(filename, list);
641 if (GLOB_LIST_FAILED(list))
646 length = 1; /* Room for trailing null byte */
647 for (SCAN_GLOB_LIST(list, p))
649 INIT_GLOB_LIST(list, p);
650 qfilename = shell_quote(p);
651 if (qfilename != NULL)
653 length += strlen(qfilename) + 1;
657 gfilename = (char *) ecalloc(length, sizeof(char));
658 for (SCAN_GLOB_LIST(list, p))
660 INIT_GLOB_LIST(list, p);
661 qfilename = shell_quote(p);
662 if (qfilename != NULL)
664 sprintf(gfilename + strlen(gfilename), "%s ", qfilename);
669 * Overwrite the final trailing space with a null terminator.
672 GLOB_LIST_DONE(list);
675 #ifdef DECL_GLOB_NAME
678 * The globbing function returns a single name, and
679 * is called multiple times to walk thru all names.
686 DECL_GLOB_NAME(fnd,drive,dir,fname,ext,handle)
688 GLOB_FIRST_NAME(filename, &fnd, handle);
689 if (GLOB_FIRST_FAILED(handle))
695 _splitpath(filename, drive, dir, fname, ext);
697 gfilename = (char *) ecalloc(len, sizeof(char));
700 n = strlen(drive) + strlen(dir) + strlen(fnd.GLOB_NAME) + 1;
701 pathname = (char *) ecalloc(n, sizeof(char));
702 SNPRINTF3(pathname, n, "%s%s%s", drive, dir, fnd.GLOB_NAME);
703 qpathname = shell_quote(pathname);
705 if (qpathname != NULL)
707 n = strlen(qpathname);
708 while (p - gfilename + n + 2 >= len)
711 * No room in current buffer.
712 * Allocate a bigger one.
716 p = (char *) ecalloc(len, sizeof(char));
717 strcpy(p, gfilename);
720 p = gfilename + strlen(gfilename);
722 strcpy(p, qpathname);
727 } while (GLOB_NEXT_NAME(handle, &fnd) == 0);
730 * Overwrite the final trailing space with a null terminator.
733 GLOB_NAME_DONE(handle);
739 * We get the shell to glob the filename for us by passing
740 * an "echo" command to the shell and reading its output.
749 esc = get_meta_escape();
750 if (strlen(esc) == 0)
752 esc = shell_quote(esc);
758 lessecho = lgetenv("LESSECHO");
759 if (lessecho == NULL || *lessecho == '\0')
760 lessecho = "lessecho";
762 * Invoke lessecho, and read its output (a globbed list of filenames).
764 len = strlen(lessecho) + strlen(ofilename) + (7*strlen(metachars())) + 24;
765 cmd = (char *) ecalloc(len, sizeof(char));
766 SNPRINTF4(cmd, len, "%s -p0x%x -d0x%x -e%s ", lessecho, openquote, closequote, esc);
768 for (s = metachars(); *s != '\0'; s++)
769 sprintf(cmd + strlen(cmd), "-n0x%x ", *s);
770 sprintf(cmd + strlen(cmd), "-- %s", ofilename);
776 * Cannot create the pipe.
777 * Just return the original (fexpanded) filename.
782 gfilename = readfd(fd);
784 if (*gfilename == '\0')
793 * No globbing functions at all. Just use the fexpanded filename.
795 gfilename = save(filename);
805 * See if we should open a "replacement file"
806 * instead of the file we're about to open.
809 open_altfile(filename, pf, pfd)
825 if (!use_lessopen || secure)
828 if ((lessopen = lgetenv("LESSOPEN")) == NULL)
830 if (strcmp(filename, "-") == 0)
832 if (*lessopen == '|')
835 * If LESSOPEN starts with a |, it indicates
836 * a "pipe preprocessor".
842 error("LESSOPEN pipe is not supported", NULL_PARG);
847 len = strlen(lessopen) + strlen(filename) + 2;
848 cmd = (char *) ecalloc(len, sizeof(char));
849 SNPRINTF1(cmd, len, lessopen, filename);
855 * Cannot create the pipe.
866 * Read one char to see if the pipe will produce any data.
867 * If it does, push the char back on the pipe.
871 if (read(f, &c, 1) != 1)
874 * Pipe is empty. This means there is no alt file.
889 * Pipe is empty. This means there is no alt file.
893 #endif /* HAVE_POPEN */
897 * Close a replacement file.
900 close_altfile(altfilename, filename, pipefd)
917 * The pclose function of OS/2 emx sometimes fails.
918 * Send SIGINT to the piped process before closing it.
920 kill(((FILE*)pipefd)->_pid, SIGINT);
922 pclose((FILE*) pipefd);
924 if ((lessclose = lgetenv("LESSCLOSE")) == NULL)
926 len = strlen(lessclose) + strlen(filename) + strlen(altfilename) + 2;
927 cmd = (char *) ecalloc(len, sizeof(char));
928 SNPRINTF2(cmd, len, lessclose, filename, altfilename);
937 * Is the specified file a directory?
945 filename = shell_unquote(filename);
951 r = stat(filename, &statbuf);
952 isdir = (r >= 0 && S_ISDIR(statbuf.st_mode));
959 f = open(filename, S_IREAD | S_IFDIR);
971 * Returns NULL if the file can be opened and
972 * is an ordinary file, otherwise an error message
973 * (if it cannot be opened or is a directory, etc.)
979 register char *m = NULL;
981 filename = shell_unquote(filename);
982 if (!force_open && is_dir(filename))
984 static char is_a_dir[] = " is a directory";
986 m = (char *) ecalloc(strlen(filename) + sizeof(is_a_dir),
996 r = stat(filename, &statbuf);
999 m = errno_message(filename);
1000 } else if (force_open)
1003 } else if (!S_ISREG(statbuf.st_mode))
1005 static char not_reg[] = " is not a regular file (use -f to see it)";
1006 m = (char *) ecalloc(strlen(filename) + sizeof(not_reg),
1008 strcpy(m, filename);
1018 * Return the size of a file, as cheaply as possible.
1019 * In Unix, we can stat the file.
1026 struct stat statbuf;
1028 if (fstat(f, &statbuf) >= 0)
1029 return ((POSITION) statbuf.st_size);
1034 if ((size = (long) _gs_size(f)) >= 0)
1035 return ((POSITION) size);
1038 return (seek_filesize(f));