2 * Copyright (C) 1984-2005 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 SNPRINTF3(newstr
, len
, "%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
)
232 if (dirname
== NULL
|| *dirname
== '\0')
235 * Construct the full pathname.
237 len
= strlen(dirname
) + strlen(filename
) + 2;
238 pathname
= (char *) calloc(len
, sizeof(char));
239 if (pathname
== NULL
)
241 SNPRINTF3(pathname
, len
, "%s%s%s", dirname
, PATHNAME_SEP
, filename
);
243 * Make sure the file exists.
245 qpathname
= shell_unquote(pathname
);
246 f
= open(qpathname
, OPEN_READ
);
260 * Return the full pathname of the given file in the "home directory".
266 register char *pathname
;
269 * Try $HOME/filename.
271 pathname
= dirfile(lgetenv("HOME"), filename
);
272 if (pathname
!= NULL
)
276 * Try $INIT/filename.
278 pathname
= dirfile(lgetenv("INIT"), filename
);
279 if (pathname
!= NULL
)
282 #if MSDOS_COMPILER || OS2
284 * Look for the file anywhere on search path.
286 pathname
= (char *) calloc(_MAX_PATH
, sizeof(char));
287 #if MSDOS_COMPILER==DJGPPC
289 char *res
= searchpath(filename
);
293 strcpy(pathname
, res
);
296 _searchenv(filename
, "PATH", pathname
);
298 if (*pathname
!= '\0')
306 * Expand a string, substituting any "%" with the current filename,
307 * and any "#" with the previous filename.
308 * But a string of N "%"s is just replaced with N-1 "%"s.
309 * Likewise for a string of N "#"s.
310 * {{ This is a lot of work just to support % and #. }}
316 register char *fr
, *to
;
321 #define fchar_ifile(c) \
322 ((c) == '%' ? curr_ifile : \
323 (c) == '#' ? old_ifile : NULL_IFILE)
326 * Make one pass to see how big a buffer we
327 * need to allocate for the expanded string.
330 for (fr
= s
; *fr
!= '\0'; fr
++)
336 if (fr
> s
&& fr
[-1] == *fr
)
339 * Second (or later) char in a string
340 * of identical chars. Treat as normal.
343 } else if (fr
[1] != *fr
)
346 * Single char (not repeated). Treat specially.
348 ifile
= fchar_ifile(*fr
);
349 if (ifile
== NULL_IFILE
)
352 n
+= strlen(get_filename(ifile
));
355 * Else it is the first char in a string of
356 * identical chars. Just discard it.
365 e
= (char *) ecalloc(n
+1, sizeof(char));
368 * Now copy the string, expanding any "%" or "#".
371 for (fr
= s
; *fr
!= '\0'; fr
++)
377 if (fr
> s
&& fr
[-1] == *fr
)
380 } else if (fr
[1] != *fr
)
382 ifile
= fchar_ifile(*fr
);
383 if (ifile
== NULL_IFILE
)
387 strcpy(to
, get_filename(ifile
));
401 #if TAB_COMPLETE_FILENAME
404 * Return a blank-separated list of filenames which "complete"
417 * Complete the filename "s" by globbing "s*".
419 #if MSDOS_COMPILER && (MSDOS_COMPILER == MSOFTC || MSDOS_COMPILER == BORLANDC)
421 * But in DOS, we have to glob "s*.*".
422 * But if the final component of the filename already has
423 * a dot in it, just do "s*".
424 * (Thus, "FILE" is globbed as "FILE*.*",
425 * but "FILE.A" is globbed as "FILE.A*").
430 for (slash
= s
+strlen(s
)-1; slash
> s
; slash
--)
431 if (*slash
== *PATHNAME_SEP
|| *slash
== '/')
434 fpat
= (char *) ecalloc(len
, sizeof(char));
435 if (strchr(slash
, '.') == NULL
)
436 SNPRINTF1(fpat
, len
, "%s*.*", s
);
438 SNPRINTF1(fpat
, len
, "%s*", s
);
442 int len
= strlen(s
) + 2;
443 fpat
= (char *) ecalloc(len
, sizeof(char));
444 SNPRINTF1(fpat
, len
, "%s*", s
);
448 s
= shell_unquote(qs
);
449 if (strcmp(s
,fpat
) == 0)
452 * The filename didn't expand.
464 * Try to determine if a file is "binary".
465 * This is just a guess, and we need not try too hard to make it accurate.
473 unsigned char data
[64];
477 if (lseek(f
, (off_t
)0, 0) == BAD_LSEEK
)
479 n
= read(f
, data
, sizeof(data
));
480 for (i
= 0; i
< n
; i
++)
481 if (binary_char(data
[i
]))
487 * Try to determine the size of a file by seeking to the end.
495 spos
= lseek(f
, (off_t
)0, 2);
496 if (spos
== BAD_LSEEK
)
497 return (NULL_POSITION
);
498 return ((POSITION
) spos
);
502 * Read a string from a file.
503 * Return a pointer to the string in memory.
515 * Make a guess about how many chars in the string
516 * and allocate a buffer to hold it.
519 buf
= (char *) ecalloc(len
, sizeof(char));
522 if ((ch
= getc(fd
)) == '\n' || ch
== EOF
)
524 if (p
- buf
>= len
-1)
527 * The string is too big to fit in the buffer we have.
528 * Allocate a new buffer, twice as big.
532 p
= (char *) ecalloc(len
, sizeof(char));
536 p
= buf
+ strlen(buf
);
551 * Execute a shell command.
552 * Return a pointer to a pipe connected to the shell command's standard output.
563 shell
= lgetenv("SHELL");
564 if (shell
!= NULL
&& *shell
!= '\0')
570 * Read the output of <$SHELL -c cmd>.
571 * Escape any metacharacters in the command.
573 esccmd
= shell_quote(cmd
);
576 fd
= popen(cmd
, "r");
579 int len
= strlen(shell
) + strlen(esccmd
) + 5;
580 scmd
= (char *) ecalloc(len
, sizeof(char));
581 SNPRINTF3(scmd
, len
, "%s %s %s", shell
, shell_coption(), esccmd
);
583 fd
= popen(scmd
, "r");
589 fd
= popen(cmd
, "r");
592 * Redirection in `popen' might have messed with the
593 * standard devices. Restore binary input mode.
599 #endif /* HAVE_POPEN */
603 * Expand a filename, doing any system-specific metacharacter substitutions.
612 ofilename
= fexpand(filename
);
615 filename
= shell_unquote(ofilename
);
617 #ifdef DECL_GLOB_LIST
620 * The globbing function returns a list of names.
627 GLOB_LIST(filename
, list
);
628 if (GLOB_LIST_FAILED(list
))
633 length
= 1; /* Room for trailing null byte */
634 for (SCAN_GLOB_LIST(list
, p
))
636 INIT_GLOB_LIST(list
, p
);
637 qfilename
= shell_quote(p
);
638 if (qfilename
!= NULL
)
640 length
+= strlen(qfilename
) + 1;
644 gfilename
= (char *) ecalloc(length
, sizeof(char));
645 for (SCAN_GLOB_LIST(list
, p
))
647 INIT_GLOB_LIST(list
, p
);
648 qfilename
= shell_quote(p
);
649 if (qfilename
!= NULL
)
651 sprintf(gfilename
+ strlen(gfilename
), "%s ", qfilename
);
656 * Overwrite the final trailing space with a null terminator.
659 GLOB_LIST_DONE(list
);
662 #ifdef DECL_GLOB_NAME
665 * The globbing function returns a single name, and
666 * is called multiple times to walk thru all names.
673 DECL_GLOB_NAME(fnd
,drive
,dir
,fname
,ext
,handle
)
675 GLOB_FIRST_NAME(filename
, &fnd
, handle
);
676 if (GLOB_FIRST_FAILED(handle
))
682 _splitpath(filename
, drive
, dir
, fname
, ext
);
684 gfilename
= (char *) ecalloc(len
, sizeof(char));
687 n
= strlen(drive
) + strlen(dir
) + strlen(fnd
.GLOB_NAME
) + 1;
688 pathname
= (char *) ecalloc(n
, sizeof(char));
689 SNPRINTF3(pathname
, n
, "%s%s%s", drive
, dir
, fnd
.GLOB_NAME
);
690 qpathname
= shell_quote(pathname
);
692 if (qpathname
!= NULL
)
694 n
= strlen(qpathname
);
695 while (p
- gfilename
+ n
+ 2 >= len
)
698 * No room in current buffer.
699 * Allocate a bigger one.
703 p
= (char *) ecalloc(len
, sizeof(char));
704 strcpy(p
, gfilename
);
707 p
= gfilename
+ strlen(gfilename
);
709 strcpy(p
, qpathname
);
714 } while (GLOB_NEXT_NAME(handle
, &fnd
) == 0);
717 * Overwrite the final trailing space with a null terminator.
720 GLOB_NAME_DONE(handle
);
726 * We get the shell to glob the filename for us by passing
727 * an "echo" command to the shell and reading its output.
736 esc
= get_meta_escape();
737 if (strlen(esc
) == 0)
739 esc
= shell_quote(esc
);
745 lessecho
= lgetenv("LESSECHO");
746 if (lessecho
== NULL
|| *lessecho
== '\0')
747 lessecho
= "lessecho";
749 * Invoke lessecho, and read its output (a globbed list of filenames).
751 len
= strlen(lessecho
) + strlen(ofilename
) + (7*strlen(metachars())) + 24;
752 cmd
= (char *) ecalloc(len
, sizeof(char));
753 SNPRINTF4(cmd
, len
, "%s -p0x%x -d0x%x -e%s ", lessecho
, openquote
, closequote
, esc
);
755 for (s
= metachars(); *s
!= '\0'; s
++)
756 sprintf(cmd
+ strlen(cmd
), "-n0x%x ", *s
);
757 sprintf(cmd
+ strlen(cmd
), "-- %s", ofilename
);
763 * Cannot create the pipe.
764 * Just return the original (fexpanded) filename.
769 gfilename
= readfd(fd
);
771 if (*gfilename
== '\0')
780 * No globbing functions at all. Just use the fexpanded filename.
782 gfilename
= save(filename
);
792 * See if we should open a "replacement file"
793 * instead of the file we're about to open.
796 open_altfile(filename
, pf
, pfd
)
812 if (!use_lessopen
|| secure
)
815 if ((lessopen
= lgetenv("LESSOPEN")) == NULL
)
817 if (strcmp(filename
, "-") == 0)
819 if (*lessopen
== '|')
822 * If LESSOPEN starts with a |, it indicates
823 * a "pipe preprocessor".
829 error("LESSOPEN pipe is not supported", NULL_PARG
);
834 len
= strlen(lessopen
) + strlen(filename
) + 2;
835 cmd
= (char *) ecalloc(len
, sizeof(char));
836 SNPRINTF1(cmd
, len
, lessopen
, filename
);
842 * Cannot create the pipe.
853 * Read one char to see if the pipe will produce any data.
854 * If it does, push the char back on the pipe.
858 if (read(f
, &c
, 1) != 1)
861 * Pipe is empty. This means there is no alt file.
876 * Pipe is empty. This means there is no alt file.
880 #endif /* HAVE_POPEN */
884 * Close a replacement file.
887 close_altfile(altfilename
, filename
, pipefd
)
904 * The pclose function of OS/2 emx sometimes fails.
905 * Send SIGINT to the piped process before closing it.
907 kill(((FILE*)pipefd
)->_pid
, SIGINT
);
909 pclose((FILE*) pipefd
);
911 if ((lessclose
= lgetenv("LESSCLOSE")) == NULL
)
913 len
= strlen(lessclose
) + strlen(filename
) + strlen(altfilename
) + 2;
914 cmd
= (char *) ecalloc(len
, sizeof(char));
915 SNPRINTF2(cmd
, len
, lessclose
, filename
, altfilename
);
924 * Is the specified file a directory?
932 filename
= shell_unquote(filename
);
938 r
= stat(filename
, &statbuf
);
939 isdir
= (r
>= 0 && S_ISDIR(statbuf
.st_mode
));
946 f
= open(filename
, S_IREAD
| S_IFDIR
);
958 * Returns NULL if the file can be opened and
959 * is an ordinary file, otherwise an error message
960 * (if it cannot be opened or is a directory, etc.)
966 register char *m
= NULL
;
968 filename
= shell_unquote(filename
);
969 if (is_dir(filename
))
971 static char is_a_dir
[] = " is a directory";
973 m
= (char *) ecalloc(strlen(filename
) + sizeof(is_a_dir
),
983 r
= stat(filename
, &statbuf
);
986 m
= errno_message(filename
);
987 } else if (force_open
)
990 } else if (!S_ISREG(statbuf
.st_mode
))
992 static char not_reg
[] = " is not a regular file (use -f to see it)";
993 m
= (char *) ecalloc(strlen(filename
) + sizeof(not_reg
),
1005 * Return the size of a file, as cheaply as possible.
1006 * In Unix, we can stat the file.
1013 struct stat statbuf
;
1015 if (fstat(f
, &statbuf
) >= 0)
1016 return ((POSITION
) statbuf
.st_size
);
1021 if ((size
= (long) _gs_size(f
)) >= 0)
1022 return ((POSITION
) size
);
1025 return (seek_filesize(f
));