]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
(touch): Only do the fstat if we need to.
authorJim Meyering <jim@meyering.net>
Fri, 7 May 1999 18:44:20 +0000 (18:44 +0000)
committerJim Meyering <jim@meyering.net>
Fri, 7 May 1999 18:44:20 +0000 (18:44 +0000)
Resort to calling stat for directories, but only when necessary.
(usage): Mention --no-create.

src/touch.c

index 073e42cf2bfb610140574d8c870a386c32c29e24..26f3141fb51b15cf53feb6eb9a542324e4863c48 100644 (file)
@@ -83,7 +83,7 @@ static struct option const longopts[] =
   {"time", required_argument, 0, CHAR_MAX + 1},
   {"no-create", no_argument, 0, 'c'},
   {"date", required_argument, 0, 'd'},
-  {"file", required_argument, 0, 'r'},
+  {"file", required_argument, 0, 'r'}, /* FIXME: phase out --file */
   {"reference", required_argument, 0, 'r'},
   {GETOPT_HELP_OPTION_DECL},
   {GETOPT_VERSION_OPTION_DECL},
@@ -168,30 +168,44 @@ touch (const char *file)
       fd = open_maybe_create (file, &file_created);
     }
 
-  if (fd == -1)
+  /* Don't fail if the open failed because FILE is a directory.  */
+  if (fd == -1
+      /* As usual, using E* macros like this is risky...
+         So heads up.  */
+      && errno != EISDIR)
     {
       error (0, errno, "%s", file);
       return 1;
     }
 
-  if (file_created && amtime_now)
+  if (amtime_now)
     {
-      if (close (fd) < 0)
+      if (file_created)
        {
-         error (0, errno, "%s", file);
-         return 1;
+         if (close (fd) < 0)
+           {
+             error (0, errno, "%s", file);
+             return 1;
+           }
+
+         /* We've just created the file with the current times.  */
+         return 0;
        }
-      return 0;                /* We've done all we have to. */
     }
-
-  if (fstat (fd, &sbuf))
+  else
     {
-      error (0, errno, "%s", file);
-      close (fd);
-      return 1;
+      /* We're setting only one of the time values.  stat the target to get
+        the other one.  If we have the file descriptor already, use fstat,
+        otherwise, FILE is a directory, so we have to use stat.  */
+      if (fd == -1 ? stat (file, &sbuf) : fstat (fd, &sbuf))
+       {
+         error (0, errno, "%s", file);
+         close (fd);
+         return 1;
+       }
     }
 
-  if (close (fd) < 0)
+  if (fd != -1 && close (fd) < 0)
     {
       error (0, errno, "%s", file);
       return 1;
@@ -252,7 +266,7 @@ usage (int status)
 Update the access and modification times of each FILE to the current time.\n\
 \n\
   -a                     change only the access time\n\
-  -c                     do not create any files\n\
+  -c, --no-create        do not create any files\n\
   -d, --date=STRING      parse STRING and use it instead of current time\n\
   -f                     (ignored)\n\
   -m                     change only the modification time\n\