]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
maint: ls: use stpncpy/stpcpy, not strncpy/strcpy
authorJim Meyering <meyering@redhat.com>
Wed, 18 Apr 2012 12:49:22 +0000 (14:49 +0200)
committerJim Meyering <meyering@redhat.com>
Thu, 19 Apr 2012 11:10:36 +0000 (13:10 +0200)
* src/ls.c (gobble_file): Move a decl "down".
(make_link_name): Do not hard-code '/'.  Use IS_ABSOLUTE_FILE_NAME
and dir_len instead.
Use stpcpy/stpncpy in place of strncpy/strcpy.

src/ls.c

index db5819280f9c769a3ef8e0a0c54d7f70dbb865eb..800f8138cf0cc00c4811561f36e2c2d5f1a213de 100644 (file)
--- a/src/ls.c
+++ b/src/ls.c
@@ -3037,11 +3037,10 @@ gobble_file (char const *name, enum filetype type, ino_t inode,
       if (S_ISLNK (f->stat.st_mode)
           && (format == long_format || check_symlink_color))
         {
-          char *linkname;
           struct stat linkstats;
 
           get_link_name (absolute_name, f, command_line_arg);
-          linkname = make_link_name (absolute_name, f->linkname);
+          char *linkname = make_link_name (absolute_name, f->linkname);
 
           /* Avoid following symbolic links when possible, ie, when
              they won't be traced and when no indicator is needed.  */
@@ -3204,19 +3203,17 @@ make_link_name (char const *name, char const *linkname)
   if (!linkname)
     return NULL;
 
-  if (*linkname == '/')
+  if (IS_ABSOLUTE_FILE_NAME (linkname))
     return xstrdup (linkname);
 
   /* The link is to a relative name.  Prepend any leading directory
      in 'name' to the link name.  */
-  char const *linkbuf = strrchr (name, '/');
-  if (linkbuf == NULL)
+  size_t prefix_len = dir_len (name);
+  if (prefix_len == 0)
     return xstrdup (linkname);
 
-  size_t bufsiz = linkbuf - name + 1;
-  char *p = xmalloc (bufsiz + strlen (linkname) + 1);
-  strncpy (p, name, bufsiz);
-  strcpy (p + bufsiz, linkname);
+  char *p = xmalloc (prefix_len + 1 + strlen (linkname) + 1);
+  stpcpy (stpncpy (p, name, prefix_len + 1), linkname);
   return p;
 }