diff options
author | Jim Meyering <jim@meyering.net> | 1999-05-07 18:44:20 +0000 |
---|---|---|
committer | Jim Meyering <jim@meyering.net> | 1999-05-07 18:44:20 +0000 |
commit | a06a563ea467e28109b277ccc2b55aa3530ab6ba (patch) | |
tree | a7e72fbc8634e7a1ebd152fa6d8ca31daccee3e7 /src | |
parent | 9250d1a34f792da87f56edf5fb15c3e6c8008ff3 (diff) | |
download | coreutils-a06a563ea467e28109b277ccc2b55aa3530ab6ba.tar.xz |
(touch): Only do the fstat if we need to.
Resort to calling stat for directories, but only when necessary.
(usage): Mention --no-create.
Diffstat (limited to 'src')
-rw-r--r-- | src/touch.c | 42 |
1 files changed, 28 insertions, 14 deletions
diff --git a/src/touch.c b/src/touch.c index 073e42cf2..26f3141fb 100644 --- a/src/touch.c +++ b/src/touch.c @@ -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\ |