[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.3 and 1.1.1.4

version 1.1.1.3, 2011/09/16 17:47:04 version 1.1.1.4, 2014/04/25 13:33:46
Line 1 
Line 1 
 /*  /*
  * Copyright (C) 1984-2011  Mark Nudelman   * Copyright (C) 1984-2012  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.
  *   *
  * For more information about less, or for information on how to   * For more information, see the README file.
  * contact the author, see the README file.  
  */   */
   
   
Line 207 
Line 206 
                                 /*                                  /*
                                  * Add the escape char.                                   * Add the escape char.
                                  */                                   */
                                 strcpy(p, esc);                                  strlcpy(p, esc, newstr + len - p);
                                 p += esclen;                                  p += esclen;
                         }                          }
                         *p++ = *s++;                          *p++ = *s++;
Line 228 
Line 227 
 {  {
         char *pathname;          char *pathname;
         char *qpathname;          char *qpathname;
         int len;          size_t len;
         int f;          int f;
   
         if (dirname == NULL || *dirname == '\0')          if (dirname == NULL || *dirname == '\0')
Line 258 
Line 257 
         return (pathname);          return (pathname);
 }  }
   
   #if USERFILE
 /*  /*
  * Return the full pathname of the given file in the "home directory".   * Return the full pathname of the given file in the "home directory".
  */   */
Line 292 
Line 292 
                 if (res == 0)                  if (res == 0)
                         *pathname = '\0';                          *pathname = '\0';
                 else                  else
                         strcpy(pathname, res);                          strlcpy(pathname, res, _MAX_PATH);
         }          }
 #else  #else
         _searchenv(filename, "PATH", pathname);          _searchenv(filename, "PATH", pathname);
Line 303 
Line 303 
 #endif  #endif
         return (NULL);          return (NULL);
 }  }
   #endif /* USERFILE */
   
 /*  /*
  * Expand a string, substituting any "%" with the current filename,   * Expand a string, substituting any "%" with the current filename,
Line 386 
Line 387 
                                         *to++ = *fr;                                          *to++ = *fr;
                                 else                                  else
                                 {                                  {
                                         strcpy(to, get_filename(ifile));                                          strlcpy(to, get_filename(ifile),
                                               e + n + 1 - to);
                                         to += strlen(to);                                          to += strlen(to);
                                 }                                  }
                         }                          }
Line 413 
Line 415 
 {  {
         char *fpat;          char *fpat;
         char *qs;          char *qs;
           size_t len;
   
         if (secure)          if (secure)
                 return (NULL);                  return (NULL);
Line 429 
Line 432 
          */           */
         {          {
                 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;
Line 442 
Line 444 
         }          }
 #else  #else
         {          {
         int len = strlen(s) + 2;          len = strlen(s) + 2;
         fpat = (char *) ecalloc(len, sizeof(char));          fpat = (char *) ecalloc(len, sizeof(char));
         SNPRINTF1(fpat, len, "%s*", s);          SNPRINTF1(fpat, len, "%s*", s);
         }          }
Line 548 
Line 550 
                         len *= 2;                          len *= 2;
                         *p = '\0';                          *p = '\0';
                         p = (char *) ecalloc(len, sizeof(char));                          p = (char *) ecalloc(len, sizeof(char));
                         strcpy(p, buf);                          strlcpy(p, buf, len);
                         free(buf);                          free(buf);
                         buf = p;                          buf = p;
                         p = buf + strlen(buf);                          p = buf + strlen(buf);
Line 594 
Line 596 
                         fd = popen(cmd, "r");                          fd = popen(cmd, "r");
                 } else                  } else
                 {                  {
                         int len = strlen(shell) + strlen(esccmd) + 5;                          size_t len = strlen(shell) + strlen(esccmd) + 5;
                         scmd = (char *) ecalloc(len, sizeof(char));                          scmd = (char *) ecalloc(len, sizeof(char));
                         SNPRINTF3(scmd, len, "%s %s %s", shell, shell_coption(), esccmd);                          SNPRINTF3(scmd, len, "%s %s %s", shell, shell_coption(), esccmd);
                         free(esccmd);                          free(esccmd);
Line 617 
Line 619 
 #endif /* HAVE_POPEN */  #endif /* HAVE_POPEN */
   
   
   #if !SMALL
 /*  /*
  * Expand a filename, doing any system-specific metacharacter substitutions.   * Expand a filename, doing any system-specific metacharacter substitutions.
  */   */
Line 666 
Line 669 
                 qfilename = shell_quote(p);                  qfilename = shell_quote(p);
                 if (qfilename != NULL)                  if (qfilename != NULL)
                 {                  {
                         sprintf(gfilename + strlen(gfilename), "%s ", qfilename);                          snprintf(gfilename + strlen(gfilename),
                               length - strlen(gfilename), "%s ", qfilename);
                         free(qfilename);                          free(qfilename);
                 }                  }
         }          }
         /*          /*
          * Overwrite the final trailing space with a null terminator.           * Overwrite the final trailing space with a null terminator.
          */           */
         *--p = '\0';          if (gfilename[0] != '\0' && gfilename[strlen(gfilename) - 1] == ' ')
                   gfilename[strlen(gfilename) - 1] = '\0';
         GLOB_LIST_DONE(list);          GLOB_LIST_DONE(list);
 }  }
 #else  #else
Line 719 
Line 724 
                                 len *= 2;                                  len *= 2;
                                 *p = '\0';                                  *p = '\0';
                                 p = (char *) ecalloc(len, sizeof(char));                                  p = (char *) ecalloc(len, sizeof(char));
                                 strcpy(p, gfilename);                                  strlcpy(p, gfilename, len);
                                 free(gfilename);                                  free(gfilename);
                                 gfilename = p;                                  gfilename = p;
                                 p = gfilename + strlen(gfilename);                                  p = gfilename + strlen(gfilename);
                         }                          }
                         strcpy(p, qpathname);                          strlcpy(p, qpathname, gfilename + len - p);
                         free(qpathname);                          free(qpathname);
                         p += n;                          p += n;
                         *p++ = ' ';                          *p++ = ' ';
Line 749 
Line 754 
         char *lessecho;          char *lessecho;
         char *cmd;          char *cmd;
         char *esc;          char *esc;
         int len;          size_t len;
   
         esc = get_meta_escape();          esc = get_meta_escape();
         if (strlen(esc) == 0)          if (strlen(esc) == 0)
Line 771 
Line 776 
         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, 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);                  snprintf(cmd + strlen(cmd), len - strlen(cmd), "-n0x%x ", *s);
         sprintf(cmd + strlen(cmd), "-- %s", ofilename);          snprintf(cmd + strlen(cmd), len - strlen(cmd), "-- %s", ofilename);
         fd = shellcmd(cmd);          fd = shellcmd(cmd);
         free(cmd);          free(cmd);
         if (fd == NULL)          if (fd == NULL)
Line 805 
Line 810 
         free(ofilename);          free(ofilename);
         return (gfilename);          return (gfilename);
 }  }
   #endif /* !SMALL */
   
 /*  /*
    * 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;
   {
           int num;
   
           for (num = 0;; num++)
           {
                   lessopen = strchr(lessopen, '%');
                   if (lessopen == NULL)
                           break;
                   if (*++lessopen != 's')
                           return (999);
           }
           return (num);
   }
   
   /*
  * See if we should open a "replacement file"   * See if we should open a "replacement file"
  * instead of the file we're about to open.   * instead of the file we're about to open.
  */   */
Line 821 
Line 848 
 #else  #else
         char *lessopen;          char *lessopen;
         char *cmd;          char *cmd;
         int len;          size_t len;
         FILE *fd;          FILE *fd;
 #if HAVE_FILENO  #if HAVE_FILENO
         int returnfd = 0;          int returnfd = 0;
Line 832 
Line 859 
         ch_ungetchar(-1);          ch_ungetchar(-1);
         if ((lessopen = lgetenv("LESSOPEN")) == NULL)          if ((lessopen = lgetenv("LESSOPEN")) == NULL)
                 return (NULL);                  return (NULL);
         if (*lessopen == '|')          while (*lessopen == '|')
         {          {
                 /*                  /*
                  * If LESSOPEN starts with a |, it indicates                   * If LESSOPEN starts with a |, it indicates
Line 843 
Line 870 
                 return (NULL);                  return (NULL);
 #else  #else
                 lessopen++;                  lessopen++;
                 returnfd = 1;                  returnfd++;
 #endif  #endif
         }          }
         if (*lessopen == '-') {          if (*lessopen == '-') {
Line 855 
Line 882 
                 if (strcmp(filename, "-") == 0)                  if (strcmp(filename, "-") == 0)
                         return (NULL);                          return (NULL);
         }          }
           if (num_pct_s(lessopen) > 1)
           {
                   error("Invalid LESSOPEN variable", NULL_PARG);
                   return (NULL);
           }
   
         len = strlen(lessopen) + strlen(filename) + 2;          len = strlen(lessopen) + strlen(filename) + 2;
         cmd = (char *) ecalloc(len, sizeof(char));          cmd = (char *) ecalloc(len, sizeof(char));
Line 883 
Line 915 
                 if (read(f, &c, 1) != 1)                  if (read(f, &c, 1) != 1)
                 {                  {
                         /*                          /*
                          * Pipe is empty.  This means there is no alt file.                           * Pipe is empty.
                            * If more than 1 pipe char was specified,
                            * the exit status tells whether the file itself
                            * is empty, or if there is no alt file.
                            * If only one pipe char, just assume no alt file.
                          */                           */
                         pclose(fd);                          int status = pclose(fd);
                           if (returnfd > 1 && status == 0) {
                                   *pfd = NULL;
                                   *pf = -1;
                                   return (save(FAKE_EMPTYFILE));
                           }
                         return (NULL);                          return (NULL);
                 }                  }
                 ch_ungetchar(c);                  ch_ungetchar(c);
Line 918 
Line 959 
         char *lessclose;          char *lessclose;
         FILE *fd;          FILE *fd;
         char *cmd;          char *cmd;
         int len;          size_t len;
   
         if (secure)          if (secure)
                 return;                  return;
Line 935 
Line 976 
         }          }
         if ((lessclose = lgetenv("LESSCLOSE")) == NULL)          if ((lessclose = lgetenv("LESSCLOSE")) == NULL)
                 return;                  return;
           if (num_pct_s(lessclose) > 2)
           {
                   error("Invalid LESSCLOSE variable");
                   return;
           }
         len = strlen(lessclose) + strlen(filename) + strlen(altfilename) + 2;          len = strlen(lessclose) + strlen(filename) + strlen(altfilename) + 2;
         cmd = (char *) ecalloc(len, sizeof(char));          cmd = (char *) ecalloc(len, sizeof(char));
         SNPRINTF2(cmd, len, lessclose, filename, altfilename);          SNPRINTF2(cmd, len, lessclose, filename, altfilename);
Line 989 
Line 1035 
         char *filename;          char *filename;
 {  {
         register char *m = NULL;          register char *m = NULL;
           size_t len;
   
         filename = shell_unquote(filename);          filename = shell_unquote(filename);
         if (!force_open && 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";
   
                 m = (char *) ecalloc(strlen(filename) + sizeof(is_a_dir),                  len = strlen(filename) + sizeof(is_a_dir);
                         sizeof(char));                  m = (char *) ecalloc(len, sizeof(char));
                 strcpy(m, filename);                  strlcpy(m, filename, len);
                 strcat(m, is_a_dir);                  strlcat(m, is_a_dir, len);
         } else          } else
         {          {
 #if HAVE_STAT  #if HAVE_STAT
Line 1015 
Line 1062 
                 } else if (!S_ISREG(statbuf.st_mode))                  } else if (!S_ISREG(statbuf.st_mode))
                 {                  {
                         static char not_reg[] = " is not a regular file (use -f to see it)";                          static char not_reg[] = " is not a regular file (use -f to see it)";
                         m = (char *) ecalloc(strlen(filename) + sizeof(not_reg),                          len = strlen(filename) + sizeof(not_reg);
                                 sizeof(char));                          m = (char *) ecalloc(len, sizeof(char));
                         strcpy(m, filename);                          strlcpy(m, filename, len);
                         strcat(m, not_reg);                          strlcat(m, not_reg, len);
                 }                  }
 #endif  #endif
         }          }

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