bug-coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

"touch -" touching standard output


From: Paul Eggert
Subject: "touch -" touching standard output
Date: Sat, 24 Sep 2005 23:21:23 -0700
User-agent: Gnus/5.1007 (Gnus v5.10.7) Emacs/21.4 (gnu/linux)

The recent changes involving adding better support for futimes,
fchmod, and fchown got me to thinking that these system calls aren't
easily accessible to shell scripts, which goes counter to a primary
original motivation for the shell -- namely, that it was a thin shell
around the operating system calls.

To help counteract this for futimes, we can have "touch -" touch the
standard output, i.e., it can invoke futimes on the standard output
rather than utimes on the file named "-".  This is pretty
straightforward so I installed the following patch.

Harder to implement (and more controversial, perhaps) would be that
"chmod a-w -" and "chown root:sys -" should affect standard output
rather than a file named "-".  I'll omit this change for now.  (The
mind reels at "chmod -r a-w - 1</usr" anyway.  :-).

2005-09-24  Paul Eggert  <address@hidden>

        * NEWS: "touch -" now touches standard output.
        * doc/coreutils.texi (touch invocation): Document this.
        * lib/utimens.c (ENOSYS): Define if not already defined.
        (futimens): Support having a null PATH if the file descriptor
        is nonnegative.
        * src/touch.c (touch): Implement this.
        (usage): Document this.

Index: NEWS
===================================================================
RCS file: /fetish/cu/NEWS,v
retrieving revision 1.312
diff -p -u -r1.312 NEWS
--- NEWS        16 Sep 2005 08:19:04 -0000      1.312
+++ NEWS        25 Sep 2005 06:03:24 -0000
@@ -216,6 +216,8 @@ GNU coreutils NEWS                      
   stat -f's default output format has been changed to output this size as well.
   stat -f recognizes file systems of type XFS and JFS
 
+  "touch -" now touches standard output, not a file named "-".
+
   uname -a no longer generates the -p and -i outputs if they are unknown.
 
 * Major changes in release 5.3.0 (2005-01-08) [unstable]
Index: doc/coreutils.texi
===================================================================
RCS file: /fetish/cu/doc/coreutils.texi,v
retrieving revision 1.284
diff -p -u -r1.284 coreutils.texi
--- doc/coreutils.texi  17 Sep 2005 07:44:42 -0000      1.284
+++ doc/coreutils.texi  25 Sep 2005 06:01:48 -0000
@@ -8541,6 +8541,9 @@ touch address@hidden@dots{} @address@hidden
 @cindex empty files, creating
 Any @var{file} that does not exist is created empty.
 
+A @var{file} of @samp{-} causes @command{touch} to change the
+times of the file associated with standard output.
+
 @cindex permissions, for changing file timestamps
 If changing both the access and modification times to the current
 time, @command{touch} can change the timestamps for files that the user
Index: lib/utimens.c
===================================================================
RCS file: /fetish/cu/lib/utimens.c,v
retrieving revision 1.8
diff -p -u -r1.8 utimens.c
--- lib/utimens.c       23 Sep 2005 19:18:27 -0000      1.8
+++ lib/utimens.c       25 Sep 2005 06:01:48 -0000
@@ -41,6 +41,16 @@ struct utimbuf
 };
 #endif
 
+/* Some systems don't have ENOSYS.  */
+#ifndef ENOSYS
+# ifdef ENOTSUP
+#  define ENOSYS ENOTSUP
+# else
+/* Some systems don't have ENOTSUP either.  */
+#  define ENOSYS EINVAL
+# endif
+#endif
+
 #if __GNUC__ < 2 || (__GNUC__ == 2 && __GNUC_MINOR__ < 8) || __STRICT_ANSI__
 # define __attribute__(x)
 #endif
@@ -53,7 +63,11 @@ struct utimbuf
    TIMESPEC[0] and TIMESPEC[1], respectively.
    FD must be either negative -- in which case it is ignored --
    or a file descriptor that is open on FILE.
-   If TIMESPEC is null, set the time stamps to the current time.  */
+   If FD is nonnegative, then FILE can be NULL, which means
+   use just futimes (or equivalent) instead of utimes (or equivalent),
+   and fail if on an old system without futimes (or equivalent).
+   If TIMESPEC is null, set the time stamps to the current time.
+   Return 0 on success, -1 (setting errno) on failure.  */
 
 int
 futimens (int fd ATTRIBUTE_UNUSED,
@@ -78,8 +92,7 @@ futimens (int fd ATTRIBUTE_UNUSED,
 
 # if HAVE_FUTIMESAT
   return fd < 0 ? futimesat (AT_FDCWD, file, t) : futimesat (fd, NULL, t);
-# else
-#  if HAVE_FUTIMES
+# elif HAVE_FUTIMES
   if (0 <= fd)
     {
       if (futimes (fd, t) == 0)
@@ -97,23 +110,35 @@ futimens (int fd ATTRIBUTE_UNUSED,
          return -1;
        }
     }
-#  endif
-  return utimes (file, t);
 # endif
+#endif
 
-#else
+#if ! HAVE_FUTIMES_AT
 
-  struct utimbuf utimbuf;
-  struct utimbuf const *t;
-  if (timespec)
+  if (!file)
     {
-      utimbuf.actime = timespec[0].tv_sec;
-      utimbuf.modtime = timespec[1].tv_sec;
-      t = &utimbuf;
+      errno = ENOSYS;
+      return -1;
     }
-  else
-    t = NULL;
-  return utime (file, t);
+
+# if HAVE_WORKING_UTIMES
+  return utimes (file, t);
+# else
+  {
+    struct utimbuf utimbuf;
+    struct utimbuf const *ut;
+    if (timespec)
+      {
+       utimbuf.actime = timespec[0].tv_sec;
+       utimbuf.modtime = timespec[1].tv_sec;
+       ut = &utimbuf;
+      }
+    else
+      ut = NULL;
+
+    return utime (file, ut);
+  }
+# endif
 
 #endif
 }
Index: src/touch.c
===================================================================
RCS file: /fetish/cu/src/touch.c,v
retrieving revision 1.134
diff -p -u -r1.134 touch.c
--- src/touch.c 16 Sep 2005 07:50:33 -0000      1.134
+++ src/touch.c 25 Sep 2005 06:01:48 -0000
@@ -125,7 +125,9 @@ touch (const char *file)
   struct timespec timespec[2];
   struct timespec const *t;
 
-  if (! no_create)
+  if (STREQ (file, "-"))
+    fd = STDOUT_FILENO;
+  else if (! no_create)
     {
       /* Try to open FILE, creating it if necessary.  */
       fd = fd_reopen (STDIN_FILENO, file,
@@ -157,7 +159,7 @@ touch (const char *file)
              error (0, errno, _("failed to get attributes of %s"),
                     quote (file));
            }
-         if (fd != -1)
+         if (fd == STDIN_FILENO)
            close (fd);
          return false;
        }
@@ -181,7 +183,7 @@ touch (const char *file)
     }
 
   ok = (futimens (fd, file, t) == 0);
-  if (fd != -1)
+  if (fd == STDIN_FILENO)
     ok &= (close (fd) == 0);
 
   if (!ok)
@@ -241,6 +243,8 @@ Mandatory arguments to long options are 
       fputs (_("\
 \n\
 Note that the -d and -t options accept different time-date formats.\n\
+\n\
+If a FILE is -, touch standard output.\n\
 "), stdout);
       printf (_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
     }




reply via email to

[Prev in Thread] Current Thread [Next in Thread]