diff options
Diffstat (limited to 'contrib/less/filename.c')
-rw-r--r-- | contrib/less/filename.c | 154 |
1 files changed, 74 insertions, 80 deletions
diff --git a/contrib/less/filename.c b/contrib/less/filename.c index aba8d3a362e3..a8726dc26e34 100644 --- a/contrib/less/filename.c +++ b/contrib/less/filename.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 1984-2021 Mark Nudelman + * Copyright (C) 1984-2023 Mark Nudelman * * You may distribute under the terms of either the GNU General Public * License or the Less License, as specified in the README file. @@ -54,13 +54,15 @@ extern IFILE old_ifile; extern char openquote; extern char closequote; #endif +#if HAVE_STAT_INO +extern ino_t curr_ino; +extern dev_t curr_dev; +#endif /* * Remove quotes around a filename. */ - public char * -shell_unquote(str) - char *str; +public char * shell_unquote(char *str) { char *name; char *p; @@ -97,8 +99,7 @@ shell_unquote(str) /* * Get the shell's escape character. */ - public char * -get_meta_escape(VOID_PARAM) +public char * get_meta_escape(void) { char *s; @@ -111,8 +112,7 @@ get_meta_escape(VOID_PARAM) /* * Get the characters which the shell considers to be "metacharacters". */ - static char * -metachars(VOID_PARAM) +static char * metachars(void) { static char *mchars = NULL; @@ -128,9 +128,7 @@ metachars(VOID_PARAM) /* * Is this a shell metacharacter? */ - static int -metachar(c) - char c; +static int metachar(char c) { return (strchr(metachars(), c) != NULL); } @@ -138,9 +136,7 @@ metachar(c) /* * Insert a backslash before each metacharacter in a string. */ - public char * -shell_quote(s) - char *s; +public char * shell_quote(char *s) { char *p; char *newstr; @@ -216,11 +212,7 @@ shell_quote(s) * Return a pathname that points to a specified file in a specified directory. * Return NULL if the file does not exist in the directory. */ - public char * -dirfile(dirname, filename, must_exist) - char *dirname; - char *filename; - int must_exist; +public char * dirfile(char *dirname, char *filename, int must_exist) { char *pathname; int len; @@ -257,9 +249,7 @@ dirfile(dirname, filename, must_exist) /* * Return the full pathname of the given file in the "home directory". */ - public char * -homefile(filename) - char *filename; +public char * homefile(char *filename) { char *pathname; @@ -301,9 +291,7 @@ homefile(filename) * Likewise for a string of N "#"s. * {{ This is a lot of work just to support % and #. }} */ - public char * -fexpand(s) - char *s; +public char * fexpand(char *s) { char *fr, *to; int n; @@ -397,9 +385,7 @@ fexpand(s) * Return a blank-separated list of filenames which "complete" * the given string. */ - public char * -fcomplete(s) - char *s; +public char * fcomplete(char *s) { char *fpat; char *qs; @@ -457,9 +443,7 @@ fcomplete(s) * Try to determine if a file is "binary". * This is just a guess, and we need not try too hard to make it accurate. */ - public int -bin_file(f) - int f; +public int bin_file(int f) { int n; int bin_count = 0; @@ -477,7 +461,7 @@ bin_file(f) edata = &data[n]; for (p = data; p < edata; ) { - if (utf_mode && !is_utf8_well_formed(p, edata-data)) + if (utf_mode && !is_utf8_well_formed(p, edata-p)) { bin_count++; utf_skip_to_lead(&p, edata); @@ -503,9 +487,7 @@ bin_file(f) /* * Try to determine the size of a file by seeking to the end. */ - static POSITION -seek_filesize(f) - int f; +static POSITION seek_filesize(int f) { off_t spos; @@ -520,9 +502,7 @@ seek_filesize(f) * Read a string from a file. * Return a pointer to the string in memory. */ - static char * -readfd(fd) - FILE *fd; +static char * readfd(FILE *fd) { int len; int ch; @@ -563,9 +543,7 @@ readfd(fd) * Execute a shell command. * Return a pointer to a pipe connected to the shell command's standard output. */ - static FILE * -shellcmd(cmd) - char *cmd; +static FILE * shellcmd(char *cmd) { FILE *fd; @@ -614,9 +592,7 @@ shellcmd(cmd) /* * Expand a filename, doing any system-specific metacharacter substitutions. */ - public char * -lglob(filename) - char *filename; +public char * lglob(char *filename) { char *gfilename; @@ -757,10 +733,11 @@ lglob(filename) */ len = (int) (strlen(lessecho) + strlen(filename) + (7*strlen(metachars())) + 24); cmd = (char *) ecalloc(len, sizeof(char)); - SNPRINTF4(cmd, len, "%s -p0x%x -d0x%x -e%s ", lessecho, openquote, closequote, esc); + SNPRINTF4(cmd, len, "%s -p0x%x -d0x%x -e%s ", lessecho, + (unsigned char) openquote, (unsigned char) closequote, esc); free(esc); for (s = metachars(); *s != '\0'; s++) - sprintf(cmd + strlen(cmd), "-n0x%x ", *s); + sprintf(cmd + strlen(cmd), "-n0x%x ", (unsigned char) *s); sprintf(cmd + strlen(cmd), "-- %s", filename); fd = shellcmd(cmd); free(cmd); @@ -793,17 +770,27 @@ lglob(filename) } /* + * Does path not represent something in the file system? + */ +public int is_fake_pathname(char *path) +{ + return (strcmp(path, "-") == 0 || + strcmp(path, FAKE_HELPFILE) == 0 || strcmp(path, FAKE_EMPTYFILE) == 0); +} + +/* * Return canonical pathname. */ - public char * -lrealpath(path) - char *path; +public char * lrealpath(char *path) { + if (!is_fake_pathname(path)) + { #if HAVE_REALPATH - char rpath[PATH_MAX]; - if (realpath(path, rpath) != NULL) - return (save(rpath)); + char rpath[PATH_MAX]; + if (realpath(path, rpath) != NULL) + return (save(rpath)); #endif + } return (save(path)); } @@ -812,9 +799,7 @@ lrealpath(path) * Return number of %s escapes in a string. * Return a large number if there are any other % escapes besides %s. */ - static int -num_pct_s(lessopen) - char *lessopen; +static int num_pct_s(char *lessopen) { int num = 0; @@ -839,11 +824,7 @@ num_pct_s(lessopen) * See if we should open a "replacement file" * instead of the file we're about to open. */ - public char * -open_altfile(filename, pf, pfd) - char *filename; - int *pf; - void **pfd; +public char * open_altfile(char *filename, int *pf, void **pfd) { #if !HAVE_POPEN return (NULL); @@ -964,13 +945,12 @@ open_altfile(filename, pf, pfd) /* * Close a replacement file. */ - public void -close_altfile(altfilename, filename) - char *altfilename; - char *filename; +public void close_altfile(char *altfilename, char *filename) { #if HAVE_POPEN char *lessclose; + char *qfilename; + char *qaltfilename; FILE *fd; char *cmd; int len; @@ -985,9 +965,13 @@ close_altfile(altfilename, filename) error("LESSCLOSE ignored; must contain no more than 2 %%s", NULL_PARG); return; } - len = (int) (strlen(lessclose) + strlen(filename) + strlen(altfilename) + 2); + qfilename = shell_quote(filename); + qaltfilename = shell_quote(altfilename); + len = (int) (strlen(lessclose) + strlen(qfilename) + strlen(qaltfilename) + 2); cmd = (char *) ecalloc(len, sizeof(char)); - SNPRINTF2(cmd, len, lessclose, filename, altfilename); + SNPRINTF2(cmd, len, lessclose, qfilename, qaltfilename); + free(qaltfilename); + free(qfilename); fd = shellcmd(cmd); free(cmd); if (fd != NULL) @@ -998,9 +982,7 @@ close_altfile(altfilename, filename) /* * Is the specified file a directory? */ - public int -is_dir(filename) - char *filename; +public int is_dir(char *filename) { int isdir = 0; @@ -1032,9 +1014,7 @@ is_dir(filename) * is an ordinary file, otherwise an error message * (if it cannot be opened or is a directory, etc.) */ - public char * -bad_file(filename) - char *filename; +public char * bad_file(char *filename) { char *m = NULL; @@ -1076,9 +1056,7 @@ bad_file(filename) * Return the size of a file, as cheaply as possible. * In Unix, we can stat the file. */ - public POSITION -filesize(f) - int f; +public POSITION filesize(int f) { #if HAVE_STAT struct stat statbuf; @@ -1096,11 +1074,29 @@ filesize(f) return (seek_filesize(f)); } +public int curr_ifile_changed(void) +{ +#if HAVE_STAT_INO + /* + * If the file's i-number or device has changed, + * or if the file is smaller than it previously was, + * the file must be different. + */ + struct stat st; + POSITION curr_pos = ch_tell(); + int r = stat(get_filename(curr_ifile), &st); + if (r == 0 && (st.st_ino != curr_ino || + st.st_dev != curr_dev || + (curr_pos != NULL_POSITION && st.st_size < curr_pos))) + return (TRUE); +#endif + return (FALSE); +} + /* * */ - public char * -shell_coption(VOID_PARAM) +public char * shell_coption(void) { return ("-c"); } @@ -1108,9 +1104,7 @@ shell_coption(VOID_PARAM) /* * Return last component of a pathname. */ - public char * -last_component(name) - char *name; +public char * last_component(char *name) { char *slash; |