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.
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
;
54 extern IFILE curr_ifile
;
55 extern IFILE old_ifile
;
56 #if SPACES_IN_FILENAMES
57 extern char openquote
;
58 extern char closequote
;
62 * Remove quotes around a filename.
71 name
= p
= (char *) ecalloc(strlen(str
)+1, sizeof(char));
72 if (*str
== openquote
)
77 if (*str
== closequote
)
79 if (str
[1] != closequote
)
87 char *esc
= get_meta_escape();
88 int esclen
= strlen(esc
);
91 if (esclen
> 0 && strncmp(str
, esc
, esclen
) == 0)
101 * Get the shell's escape character.
108 s
= lgetenv("LESSMETAESCAPE");
115 * Get the characters which the shell considers to be "metacharacters".
120 static char *mchars
= NULL
;
124 mchars
= lgetenv("LESSMETACHARS");
126 mchars
= DEF_METACHARS
;
132 * Is this a shell metacharacter?
138 return (strchr(metachars(), c
) != NULL
);
142 * Insert a backslash before each metacharacter in a string.
151 char *esc
= get_meta_escape();
152 int esclen
= strlen(esc
);
157 * Determine how big a string we need to allocate.
159 len
= 1; /* Trailing null byte */
160 for (p
= s
; *p
!= '\0'; p
++)
163 if (*p
== openquote
|| *p
== closequote
)
170 * We've got a metachar, but this shell
171 * doesn't support escape chars. Use quotes.
177 * Allow space for the escape char.
187 * We can't quote a string that contains quotes.
193 * Allocate and construct the new string.
195 newstr
= p
= (char *) ecalloc(len
, sizeof(char));
198 sprintf(newstr
, "%c%s%c", openquote
, s
, closequote
);
206 * Add the escape char.
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.
223 dirfile(dirname
, filename
)
231 if (dirname
== NULL
|| *dirname
== '\0')
234 * Construct the full pathname.
236 pathname
= (char *) calloc(strlen(dirname
) + strlen(filename
) + 2,
238 if (pathname
== 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
);
259 * Return the full pathname of the given file in the "home directory".
265 register char *pathname
;
268 * Try $HOME/filename.
270 pathname
= dirfile(lgetenv("HOME"), filename
);
271 if (pathname
!= NULL
)
275 * Try $INIT/filename.
277 pathname
= dirfile(lgetenv("INIT"), filename
);
278 if (pathname
!= NULL
)
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
);
292 strcpy(pathname
, res
);
295 _searchenv(filename
, "PATH", pathname
);
297 if (*pathname
!= '\0')
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 #. }}
315 register char *fr
, *to
;
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.
329 for (fr
= s
; *fr
!= '\0'; fr
++)
335 if (fr
> s
&& fr
[-1] == *fr
)
338 * Second (or later) char in a string
339 * of identical chars. Treat as normal.
342 } else if (fr
[1] != *fr
)
345 * Single char (not repeated). Treat specially.
347 ifile
= fchar_ifile(*fr
);
348 if (ifile
== NULL_IFILE
)
351 n
+= strlen(get_filename(ifile
));
354 * Else it is the first char in a string of
355 * identical chars. Just discard it.
364 e
= (char *) ecalloc(n
+1, sizeof(char));
367 * Now copy the string, expanding any "%" or "#".
370 for (fr
= s
; *fr
!= '\0'; fr
++)
376 if (fr
> s
&& fr
[-1] == *fr
)
379 } else if (fr
[1] != *fr
)
381 ifile
= fchar_ifile(*fr
);
382 if (ifile
== NULL_IFILE
)
386 strcpy(to
, get_filename(ifile
));
400 #if TAB_COMPLETE_FILENAME
403 * Return a blank-separated list of filenames which "complete"
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*").
428 for (slash
= s
+strlen(s
)-1; slash
> s
; slash
--)
429 if (*slash
== *PATHNAME_SEP
|| *slash
== '/')
431 fpat
= (char *) ecalloc(strlen(s
)+4, sizeof(char));
432 if (strchr(slash
, '.') == NULL
)
433 sprintf(fpat
, "%s*.*", s
);
435 sprintf(fpat
, "%s*", s
);
438 fpat
= (char *) ecalloc(strlen(s
)+2, sizeof(char));
439 sprintf(fpat
, "%s*", s
);
442 s
= shell_unquote(qs
);
443 if (strcmp(s
,fpat
) == 0)
446 * The filename didn't expand.
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.
467 unsigned char data
[64];
471 if (lseek(f
, (off_t
)0, 0) == BAD_LSEEK
)
473 n
= read(f
, data
, sizeof(data
));
474 for (i
= 0; i
< n
; i
++)
475 if (binary_char(data
[i
]))
481 * Try to determine the size of a file by seeking to the end.
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.
509 * Make a guess about how many chars in the string
510 * and allocate a buffer to hold it.
513 buf
= (char *) ecalloc(len
, sizeof(char));
516 if ((ch
= getc(fd
)) == '\n' || ch
== EOF
)
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.
526 p
= (char *) ecalloc(len
, sizeof(char));
530 p
= buf
+ strlen(buf
);
545 * Execute a shell command.
546 * Return a pointer to a pipe connected to the shell command's standard output.
557 shell
= lgetenv("SHELL");
558 if (shell
!= NULL
&& *shell
!= '\0')
564 * Read the output of <$SHELL -c cmd>.
565 * Escape any metacharacters in the command.
567 esccmd
= shell_quote(cmd
);
570 fd
= popen(cmd
, "r");
573 scmd
= (char *) ecalloc(strlen(shell
) + strlen(esccmd
) + 5,
575 sprintf(scmd
, "%s %s %s", shell
, shell_coption(), esccmd
);
577 fd
= popen(scmd
, "r");
583 fd
= popen(cmd
, "r");
586 * Redirection in `popen' might have messed with the
587 * standard devices. Restore binary input mode.
593 #endif /* HAVE_POPEN */
597 * Expand a filename, doing any system-specific metacharacter substitutions.
606 ofilename
= fexpand(filename
);
609 filename
= shell_unquote(ofilename
);
611 #ifdef DECL_GLOB_LIST
614 * The globbing function returns a list of names.
621 GLOB_LIST(filename
, list
);
622 if (GLOB_LIST_FAILED(list
))
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;
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
);
650 * Overwrite the final trailing space with a null terminator.
653 GLOB_LIST_DONE(list
);
656 #ifdef DECL_GLOB_NAME
659 * The globbing function returns a single name, and
660 * is called multiple times to walk thru all names.
667 DECL_GLOB_NAME(fnd
,drive
,dir
,fname
,ext
,handle
)
669 GLOB_FIRST_NAME(filename
, &fnd
, handle
);
670 if (GLOB_FIRST_FAILED(handle
))
676 _splitpath(filename
, drive
, dir
, fname
, ext
);
678 gfilename
= (char *) ecalloc(len
, sizeof(char));
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
);
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.
697 p
= (char *) ecalloc(len
, sizeof(char));
698 strcpy(p
, gfilename
);
701 p
= gfilename
+ strlen(gfilename
);
703 strcpy(p
, qpathname
);
708 } while (GLOB_NEXT_NAME(handle
, &fnd
) == 0);
711 * Overwrite the final trailing space with a null terminator.
714 GLOB_NAME_DONE(handle
);
720 * We get the shell to glob the filename for us by passing
721 * an "echo" command to the shell and reading its output.
729 esc
= get_meta_escape();
730 if (strlen(esc
) == 0)
732 esc
= shell_quote(esc
);
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
);
747 for (s
= metachars(); *s
!= '\0'; s
++)
748 sprintf(cmd
+ strlen(cmd
), "-n0x%x ", *s
);
749 sprintf(cmd
+ strlen(cmd
), "-- %s", ofilename
);
755 * Cannot create the pipe.
756 * Just return the original (fexpanded) filename.
761 gfilename
= readfd(fd
);
763 if (*gfilename
== '\0')
772 * No globbing functions at all. Just use the fexpanded filename.
774 gfilename
= save(filename
);
784 * See if we should open a "replacement file"
785 * instead of the file we're about to open.
788 open_altfile(filename
, pf
, pfd
)
803 if (!use_lessopen
|| secure
)
806 if ((lessopen
= lgetenv("LESSOPEN")) == NULL
)
808 if (strcmp(filename
, "-") == 0)
810 if (*lessopen
== '|')
813 * If LESSOPEN starts with a |, it indicates
814 * a "pipe preprocessor".
820 error("LESSOPEN pipe is not supported", NULL_PARG
);
825 cmd
= (char *) ecalloc(strlen(lessopen
) + strlen(filename
) + 2,
827 sprintf(cmd
, lessopen
, filename
);
833 * Cannot create the pipe.
844 * Read one char to see if the pipe will produce any data.
845 * If it does, push the char back on the pipe.
849 if (read(f
, &c
, 1) != 1)
852 * Pipe is empty. This means there is no alt file.
867 * Pipe is empty. This means there is no alt file.
871 #endif /* HAVE_POPEN */
875 * Close a replacement file.
878 close_altfile(altfilename
, filename
, pipefd
)
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
);
899 pclose((FILE*) pipefd
);
901 if ((lessclose
= lgetenv("LESSCLOSE")) == NULL
)
903 cmd
= (char *) ecalloc(strlen(lessclose
) + strlen(filename
) +
904 strlen(altfilename
) + 2, sizeof(char));
905 sprintf(cmd
, lessclose
, filename
, altfilename
);
914 * Is the specified file a directory?
922 filename
= shell_unquote(filename
);
928 r
= stat(filename
, &statbuf
);
929 isdir
= (r
>= 0 && S_ISDIR(statbuf
.st_mode
));
936 f
= open(filename
, S_IREAD
| S_IFDIR
);
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.)
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
),
973 r
= stat(filename
, &statbuf
);
976 m
= errno_message(filename
);
977 } else if (force_open
)
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
),
995 * Return the size of a file, as cheaply as possible.
996 * In Unix, we can stat the file.
1003 struct stat statbuf
;
1005 if (fstat(f
, &statbuf
) >= 0)
1006 return ((POSITION
) statbuf
.st_size
);
1011 if ((size
= (long) _gs_size(f
)) >= 0)
1012 return ((POSITION
) size
);
1015 return (seek_filesize(f
));