[BACK]Return to filename.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / less

Diff for /src/usr.bin/less/filename.c between version 1.1.1.2 and 1.1.1.3

version 1.1.1.2, 2003/04/13 18:21:21 version 1.1.1.3, 2011/09/16 17:47:04
Line 1 
Line 1 
 /*  /*
  * Copyright (C) 1984-2002  Mark Nudelman   * Copyright (C) 1984-2011  Mark Nudelman
  *   *
  * You may distribute under the terms of either the GNU General Public   * You may distribute under the terms of either the GNU General Public
  * License or the Less License, as specified in the README file.   * License or the Less License, as specified in the README file.
Line 51 
Line 51 
 extern int force_open;  extern int force_open;
 extern int secure;  extern int secure;
 extern int use_lessopen;  extern int use_lessopen;
   extern int ctldisp;
   extern int utf_mode;
 extern IFILE curr_ifile;  extern IFILE curr_ifile;
 extern IFILE old_ifile;  extern IFILE old_ifile;
 #if SPACES_IN_FILENAMES  #if SPACES_IN_FILENAMES
Line 195 
Line 197 
         newstr = p = (char *) ecalloc(len, sizeof(char));          newstr = p = (char *) ecalloc(len, sizeof(char));
         if (use_quotes)          if (use_quotes)
         {          {
                 sprintf(newstr, "%c%s%c", openquote, s, closequote);                  SNPRINTF3(newstr, len, "%c%s%c", openquote, s, closequote);
         } else          } else
         {          {
                 while (*s != '\0')                  while (*s != '\0')
Line 226 
Line 228 
 {  {
         char *pathname;          char *pathname;
         char *qpathname;          char *qpathname;
           int len;
         int f;          int f;
   
         if (dirname == NULL || *dirname == '\0')          if (dirname == NULL || *dirname == '\0')
Line 233 
Line 236 
         /*          /*
          * Construct the full pathname.           * Construct the full pathname.
          */           */
         pathname = (char *) calloc(strlen(dirname) + strlen(filename) + 2,          len= strlen(dirname) + strlen(filename) + 2;
                                         sizeof(char));          pathname = (char *) calloc(len, sizeof(char));
         if (pathname == NULL)          if (pathname == NULL)
                 return (NULL);                  return (NULL);
         sprintf(pathname, "%s%s%s", dirname, PATHNAME_SEP, filename);          SNPRINTF3(pathname, len, "%s%s%s", dirname, PATHNAME_SEP, filename);
         /*          /*
          * Make sure the file exists.           * Make sure the file exists.
          */           */
Line 397 
Line 400 
         return (e);          return (e);
 }  }
   
   
 #if TAB_COMPLETE_FILENAME  #if TAB_COMPLETE_FILENAME
   
 /*  /*
Line 425 
Line 429 
          */           */
         {          {
                 char *slash;                  char *slash;
                   int len;
                 for (slash = s+strlen(s)-1;  slash > s;  slash--)                  for (slash = s+strlen(s)-1;  slash > s;  slash--)
                         if (*slash == *PATHNAME_SEP || *slash == '/')                          if (*slash == *PATHNAME_SEP || *slash == '/')
                                 break;                                  break;
                 fpat = (char *) ecalloc(strlen(s)+4, sizeof(char));                  len = strlen(s) + 4;
                   fpat = (char *) ecalloc(len, sizeof(char));
                 if (strchr(slash, '.') == NULL)                  if (strchr(slash, '.') == NULL)
                         sprintf(fpat, "%s*.*", s);                          SNPRINTF1(fpat, len, "%s*.*", s);
                 else                  else
                         sprintf(fpat, "%s*", s);                          SNPRINTF1(fpat, len, "%s*", s);
         }          }
 #else  #else
         fpat = (char *) ecalloc(strlen(s)+2, sizeof(char));          {
         sprintf(fpat, "%s*", s);          int len = strlen(s) + 2;
           fpat = (char *) ecalloc(len, sizeof(char));
           SNPRINTF1(fpat, len, "%s*", s);
           }
 #endif  #endif
         qs = lglob(fpat);          qs = lglob(fpat);
         s = shell_unquote(qs);          s = shell_unquote(qs);
Line 462 
Line 471 
 bin_file(f)  bin_file(f)
         int f;          int f;
 {  {
         int i;  
         int n;          int n;
         unsigned char data[64];          int bin_count = 0;
           char data[256];
           char* p;
           char* pend;
   
         if (!seekable(f))          if (!seekable(f))
                 return (0);                  return (0);
         if (lseek(f, (off_t)0, 0) == BAD_LSEEK)          if (lseek(f, (off_t)0, SEEK_SET) == BAD_LSEEK)
                 return (0);                  return (0);
         n = read(f, data, sizeof(data));          n = read(f, data, sizeof(data));
         for (i = 0;  i < n;  i++)          pend = &data[n];
                 if (binary_char(data[i]))          for (p = data;  p < pend;  )
                         return (1);          {
         return (0);                  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);
 }  }
   
 /*  /*
Line 486 
Line 510 
 {  {
         off_t spos;          off_t spos;
   
         spos = lseek(f, (off_t)0, 2);          spos = lseek(f, (off_t)0, SEEK_END);
         if (spos == BAD_LSEEK)          if (spos == BAD_LSEEK)
                 return (NULL_POSITION);                  return (NULL_POSITION);
         return ((POSITION) spos);          return ((POSITION) spos);
Line 570 
Line 594 
                         fd = popen(cmd, "r");                          fd = popen(cmd, "r");
                 } else                  } else
                 {                  {
                         scmd = (char *) ecalloc(strlen(shell) + strlen(esccmd) + 5,                          int len = strlen(shell) + strlen(esccmd) + 5;
                                                 sizeof(char));                          scmd = (char *) ecalloc(len, sizeof(char));
                         sprintf(scmd, "%s %s %s", shell, shell_coption(), esccmd);                          SNPRINTF3(scmd, len, "%s %s %s", shell, shell_coption(), esccmd);
                         free(esccmd);                          free(esccmd);
                         fd = popen(scmd, "r");                          fd = popen(scmd, "r");
                         free(scmd);                          free(scmd);
Line 680 
Line 704 
         do {          do {
                 n = strlen(drive) + strlen(dir) + strlen(fnd.GLOB_NAME) + 1;                  n = strlen(drive) + strlen(dir) + strlen(fnd.GLOB_NAME) + 1;
                 pathname = (char *) ecalloc(n, sizeof(char));                  pathname = (char *) ecalloc(n, sizeof(char));
                 sprintf(pathname, "%s%s%s", drive, dir, fnd.GLOB_NAME);                  SNPRINTF3(pathname, n, "%s%s%s", drive, dir, fnd.GLOB_NAME);
                 qpathname = shell_quote(pathname);                  qpathname = shell_quote(pathname);
                 free(pathname);                  free(pathname);
                 if (qpathname != NULL)                  if (qpathname != NULL)
Line 725 
Line 749 
         char *lessecho;          char *lessecho;
         char *cmd;          char *cmd;
         char *esc;          char *esc;
           int len;
   
         esc = get_meta_escape();          esc = get_meta_escape();
         if (strlen(esc) == 0)          if (strlen(esc) == 0)
Line 741 
Line 766 
         /*          /*
          * Invoke lessecho, and read its output (a globbed list of filenames).           * Invoke lessecho, and read its output (a globbed list of filenames).
          */           */
         cmd = (char *) ecalloc(strlen(lessecho) + strlen(ofilename) + (7*strlen(metachars())) + 24, sizeof(char));          len = strlen(lessecho) + strlen(ofilename) + (7*strlen(metachars())) + 24;
         sprintf(cmd, "%s -p0x%x -d0x%x -e%s ", lessecho, openquote, closequote, esc);          cmd = (char *) ecalloc(len, sizeof(char));
           SNPRINTF4(cmd, len, "%s -p0x%x -d0x%x -e%s ", lessecho, openquote, closequote, esc);
         free(esc);          free(esc);
         for (s = metachars();  *s != '\0';  s++)          for (s = metachars();  *s != '\0';  s++)
                 sprintf(cmd + strlen(cmd), "-n0x%x ", *s);                  sprintf(cmd + strlen(cmd), "-n0x%x ", *s);
Line 795 
Line 821 
 #else  #else
         char *lessopen;          char *lessopen;
         char *cmd;          char *cmd;
           int len;
         FILE *fd;          FILE *fd;
 #if HAVE_FILENO  #if HAVE_FILENO
         int returnfd = 0;          int returnfd = 0;
Line 805 
Line 832 
         ch_ungetchar(-1);          ch_ungetchar(-1);
         if ((lessopen = lgetenv("LESSOPEN")) == NULL)          if ((lessopen = lgetenv("LESSOPEN")) == NULL)
                 return (NULL);                  return (NULL);
         if (strcmp(filename, "-") == 0)  
                 return (NULL);  
         if (*lessopen == '|')          if (*lessopen == '|')
         {          {
                 /*                  /*
                  * If LESSOPEN starts with a |, it indicates                   * If LESSOPEN starts with a |, it indicates
                  * a "pipe preprocessor".                   * a "pipe preprocessor".
                  */                   */
 #if HAVE_FILENO  #if !HAVE_FILENO
                 lessopen++;  
                 returnfd = 1;  
 #else  
                 error("LESSOPEN pipe is not supported", NULL_PARG);                  error("LESSOPEN pipe is not supported", NULL_PARG);
                 return (NULL);                  return (NULL);
   #else
                   lessopen++;
                   returnfd = 1;
 #endif  #endif
         }          }
           if (*lessopen == '-') {
                   /*
                    * Lessopen preprocessor will accept "-" as a filename.
                    */
                   lessopen++;
           } else {
                   if (strcmp(filename, "-") == 0)
                           return (NULL);
           }
   
         cmd = (char *) ecalloc(strlen(lessopen) + strlen(filename) + 2,          len = strlen(lessopen) + strlen(filename) + 2;
                         sizeof(char));          cmd = (char *) ecalloc(len, sizeof(char));
         sprintf(cmd, lessopen, filename);          SNPRINTF1(cmd, len, lessopen, filename);
         fd = shellcmd(cmd);          fd = shellcmd(cmd);
         free(cmd);          free(cmd);
         if (fd == NULL)          if (fd == NULL)
Line 884 
Line 918 
         char *lessclose;          char *lessclose;
         FILE *fd;          FILE *fd;
         char *cmd;          char *cmd;
           int len;
   
         if (secure)          if (secure)
                 return;                  return;
Line 900 
Line 935 
         }          }
         if ((lessclose = lgetenv("LESSCLOSE")) == NULL)          if ((lessclose = lgetenv("LESSCLOSE")) == NULL)
                 return;                  return;
         cmd = (char *) ecalloc(strlen(lessclose) + strlen(filename) +          len = strlen(lessclose) + strlen(filename) + strlen(altfilename) + 2;
                         strlen(altfilename) + 2, sizeof(char));          cmd = (char *) ecalloc(len, sizeof(char));
         sprintf(cmd, lessclose, filename, altfilename);          SNPRINTF2(cmd, len, lessclose, filename, altfilename);
         fd = shellcmd(cmd);          fd = shellcmd(cmd);
         free(cmd);          free(cmd);
         if (fd != NULL)          if (fd != NULL)
Line 956 
Line 991 
         register char *m = NULL;          register char *m = NULL;
   
         filename = shell_unquote(filename);          filename = shell_unquote(filename);
         if (is_dir(filename))          if (!force_open && is_dir(filename))
         {          {
                 static char is_a_dir[] = " is a directory";                  static char is_a_dir[] = " is a directory";
   
Line 1023 
Line 1058 
 {  {
         return ("-c");          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);
   }
   

Legend:
Removed from v.1.1.1.2  
changed lines
  Added in v.1.1.1.3