=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/less/filename.c,v retrieving revision 1.1.1.2 retrieving revision 1.1.1.3 diff -c -r1.1.1.2 -r1.1.1.3 *** src/usr.bin/less/filename.c 2003/04/13 18:21:21 1.1.1.2 --- src/usr.bin/less/filename.c 2011/09/16 17:47:04 1.1.1.3 *************** *** 1,5 **** /* ! * Copyright (C) 1984-2002 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. --- 1,5 ---- /* ! * Copyright (C) 1984-2011 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. *************** *** 51,56 **** --- 51,58 ---- extern int force_open; extern int secure; extern int use_lessopen; + extern int ctldisp; + extern int utf_mode; extern IFILE curr_ifile; extern IFILE old_ifile; #if SPACES_IN_FILENAMES *************** *** 195,201 **** newstr = p = (char *) ecalloc(len, sizeof(char)); if (use_quotes) { ! sprintf(newstr, "%c%s%c", openquote, s, closequote); } else { while (*s != '\0') --- 197,203 ---- newstr = p = (char *) ecalloc(len, sizeof(char)); if (use_quotes) { ! SNPRINTF3(newstr, len, "%c%s%c", openquote, s, closequote); } else { while (*s != '\0') *************** *** 226,231 **** --- 228,234 ---- { char *pathname; char *qpathname; + int len; int f; if (dirname == NULL || *dirname == '\0') *************** *** 233,243 **** /* * Construct the full pathname. */ ! pathname = (char *) calloc(strlen(dirname) + strlen(filename) + 2, ! sizeof(char)); if (pathname == NULL) return (NULL); ! sprintf(pathname, "%s%s%s", dirname, PATHNAME_SEP, filename); /* * Make sure the file exists. */ --- 236,246 ---- /* * Construct the full pathname. */ ! len= strlen(dirname) + strlen(filename) + 2; ! pathname = (char *) calloc(len, sizeof(char)); if (pathname == NULL) return (NULL); ! SNPRINTF3(pathname, len, "%s%s%s", dirname, PATHNAME_SEP, filename); /* * Make sure the file exists. */ *************** *** 397,402 **** --- 400,406 ---- return (e); } + #if TAB_COMPLETE_FILENAME /* *************** *** 425,442 **** */ { char *slash; for (slash = s+strlen(s)-1; slash > s; slash--) if (*slash == *PATHNAME_SEP || *slash == '/') break; ! fpat = (char *) ecalloc(strlen(s)+4, sizeof(char)); if (strchr(slash, '.') == NULL) ! sprintf(fpat, "%s*.*", s); else ! sprintf(fpat, "%s*", s); } #else ! fpat = (char *) ecalloc(strlen(s)+2, sizeof(char)); ! sprintf(fpat, "%s*", s); #endif qs = lglob(fpat); s = shell_unquote(qs); --- 429,451 ---- */ { char *slash; + int len; for (slash = s+strlen(s)-1; slash > s; slash--) if (*slash == *PATHNAME_SEP || *slash == '/') break; ! len = strlen(s) + 4; ! fpat = (char *) ecalloc(len, sizeof(char)); if (strchr(slash, '.') == NULL) ! SNPRINTF1(fpat, len, "%s*.*", s); else ! SNPRINTF1(fpat, len, "%s*", s); } #else ! { ! int len = strlen(s) + 2; ! fpat = (char *) ecalloc(len, sizeof(char)); ! SNPRINTF1(fpat, len, "%s*", s); ! } #endif qs = lglob(fpat); s = shell_unquote(qs); *************** *** 462,480 **** bin_file(f) int f; { - int i; int n; ! unsigned char data[64]; if (!seekable(f)) return (0); ! if (lseek(f, (off_t)0, 0) == BAD_LSEEK) return (0); n = read(f, data, sizeof(data)); ! for (i = 0; i < n; i++) ! if (binary_char(data[i])) ! return (1); ! return (0); } /* --- 471,504 ---- bin_file(f) int f; { int n; ! int bin_count = 0; ! char data[256]; ! char* p; ! char* pend; if (!seekable(f)) return (0); ! if (lseek(f, (off_t)0, SEEK_SET) == BAD_LSEEK) return (0); n = read(f, data, sizeof(data)); ! pend = &data[n]; ! for (p = data; p < pend; ) ! { ! LWCHAR c = step_char(&p, +1, pend); ! if (ctldisp == OPT_ONPLUS && IS_CSI_START(c)) ! { ! do { ! c = step_char(&p, +1, pend); ! } while (p < pend && is_ansi_middle(c)); ! } else if (binary_char(c)) ! bin_count++; ! } ! /* ! * Call it a binary file if there are more than 5 binary characters ! * in the first 256 bytes of the file. ! */ ! return (bin_count > 5); } /* *************** *** 486,492 **** { off_t spos; ! spos = lseek(f, (off_t)0, 2); if (spos == BAD_LSEEK) return (NULL_POSITION); return ((POSITION) spos); --- 510,516 ---- { off_t spos; ! spos = lseek(f, (off_t)0, SEEK_END); if (spos == BAD_LSEEK) return (NULL_POSITION); return ((POSITION) spos); *************** *** 570,578 **** fd = popen(cmd, "r"); } else { ! scmd = (char *) ecalloc(strlen(shell) + strlen(esccmd) + 5, ! sizeof(char)); ! sprintf(scmd, "%s %s %s", shell, shell_coption(), esccmd); free(esccmd); fd = popen(scmd, "r"); free(scmd); --- 594,602 ---- fd = popen(cmd, "r"); } else { ! int len = strlen(shell) + strlen(esccmd) + 5; ! scmd = (char *) ecalloc(len, sizeof(char)); ! SNPRINTF3(scmd, len, "%s %s %s", shell, shell_coption(), esccmd); free(esccmd); fd = popen(scmd, "r"); free(scmd); *************** *** 680,686 **** do { n = strlen(drive) + strlen(dir) + strlen(fnd.GLOB_NAME) + 1; pathname = (char *) ecalloc(n, sizeof(char)); ! sprintf(pathname, "%s%s%s", drive, dir, fnd.GLOB_NAME); qpathname = shell_quote(pathname); free(pathname); if (qpathname != NULL) --- 704,710 ---- do { n = strlen(drive) + strlen(dir) + strlen(fnd.GLOB_NAME) + 1; pathname = (char *) ecalloc(n, sizeof(char)); ! SNPRINTF3(pathname, n, "%s%s%s", drive, dir, fnd.GLOB_NAME); qpathname = shell_quote(pathname); free(pathname); if (qpathname != NULL) *************** *** 725,730 **** --- 749,755 ---- char *lessecho; char *cmd; char *esc; + int len; esc = get_meta_escape(); if (strlen(esc) == 0) *************** *** 741,748 **** /* * Invoke lessecho, and read its output (a globbed list of filenames). */ ! cmd = (char *) ecalloc(strlen(lessecho) + strlen(ofilename) + (7*strlen(metachars())) + 24, sizeof(char)); ! sprintf(cmd, "%s -p0x%x -d0x%x -e%s ", lessecho, openquote, closequote, esc); free(esc); for (s = metachars(); *s != '\0'; s++) sprintf(cmd + strlen(cmd), "-n0x%x ", *s); --- 766,774 ---- /* * Invoke lessecho, and read its output (a globbed list of filenames). */ ! len = strlen(lessecho) + strlen(ofilename) + (7*strlen(metachars())) + 24; ! cmd = (char *) ecalloc(len, sizeof(char)); ! SNPRINTF4(cmd, len, "%s -p0x%x -d0x%x -e%s ", lessecho, openquote, closequote, esc); free(esc); for (s = metachars(); *s != '\0'; s++) sprintf(cmd + strlen(cmd), "-n0x%x ", *s); *************** *** 795,800 **** --- 821,827 ---- #else char *lessopen; char *cmd; + int len; FILE *fd; #if HAVE_FILENO int returnfd = 0; *************** *** 805,830 **** ch_ungetchar(-1); if ((lessopen = lgetenv("LESSOPEN")) == NULL) return (NULL); - if (strcmp(filename, "-") == 0) - return (NULL); if (*lessopen == '|') { /* * If LESSOPEN starts with a |, it indicates * a "pipe preprocessor". */ ! #if HAVE_FILENO ! lessopen++; ! returnfd = 1; ! #else error("LESSOPEN pipe is not supported", NULL_PARG); return (NULL); #endif } ! cmd = (char *) ecalloc(strlen(lessopen) + strlen(filename) + 2, ! sizeof(char)); ! sprintf(cmd, lessopen, filename); fd = shellcmd(cmd); free(cmd); if (fd == NULL) --- 832,864 ---- ch_ungetchar(-1); if ((lessopen = lgetenv("LESSOPEN")) == NULL) return (NULL); if (*lessopen == '|') { /* * If LESSOPEN starts with a |, it indicates * a "pipe preprocessor". */ ! #if !HAVE_FILENO error("LESSOPEN pipe is not supported", NULL_PARG); return (NULL); + #else + lessopen++; + returnfd = 1; #endif } + if (*lessopen == '-') { + /* + * Lessopen preprocessor will accept "-" as a filename. + */ + lessopen++; + } else { + if (strcmp(filename, "-") == 0) + return (NULL); + } ! len = strlen(lessopen) + strlen(filename) + 2; ! cmd = (char *) ecalloc(len, sizeof(char)); ! SNPRINTF1(cmd, len, lessopen, filename); fd = shellcmd(cmd); free(cmd); if (fd == NULL) *************** *** 884,889 **** --- 918,924 ---- char *lessclose; FILE *fd; char *cmd; + int len; if (secure) return; *************** *** 900,908 **** } if ((lessclose = lgetenv("LESSCLOSE")) == NULL) return; ! cmd = (char *) ecalloc(strlen(lessclose) + strlen(filename) + ! strlen(altfilename) + 2, sizeof(char)); ! sprintf(cmd, lessclose, filename, altfilename); fd = shellcmd(cmd); free(cmd); if (fd != NULL) --- 935,943 ---- } if ((lessclose = lgetenv("LESSCLOSE")) == NULL) return; ! len = strlen(lessclose) + strlen(filename) + strlen(altfilename) + 2; ! cmd = (char *) ecalloc(len, sizeof(char)); ! SNPRINTF2(cmd, len, lessclose, filename, altfilename); fd = shellcmd(cmd); free(cmd); if (fd != NULL) *************** *** 956,962 **** register char *m = NULL; filename = shell_unquote(filename); ! if (is_dir(filename)) { static char is_a_dir[] = " is a directory"; --- 991,997 ---- register char *m = NULL; filename = shell_unquote(filename); ! if (!force_open && is_dir(filename)) { static char is_a_dir[] = " is a directory"; *************** *** 1023,1025 **** --- 1058,1079 ---- { return ("-c"); } + + /* + * Return last component of a pathname. + */ + public char * + last_component(name) + char *name; + { + char *slash; + + for (slash = name + strlen(name); slash > name; ) + { + --slash; + if (*slash == *PATHNAME_SEP || *slash == '/') + return (slash + 1); + } + return (name); + } +