(do_link): Produce the same sort of one-line output for
[coreutils.git] / src / touch.c
blob5e638005a0116dd8e9a1f9a8eb988b0b35f29060
1 /* touch -- change modification and access times of files
2 Copyright (C) 87, 1989-1991, 1995-1999 Free Software Foundation, Inc.
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2, or (at your option)
7 any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software Foundation,
16 Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 /* Written by Paul Rubin, Arnold Robbins, Jim Kingdon, David MacKenzie,
19 and Randy Smith. */
21 #include <config.h>
22 #include <stdio.h>
23 #include <getopt.h>
24 #include <sys/types.h>
26 #include "system.h"
27 #include "argmatch.h"
28 #include "error.h"
29 #include "getdate.h"
30 #include "posixtm.h"
31 #include "safe-read.h"
33 /* The official name of this program (e.g., no `g' prefix). */
34 #define PROGRAM_NAME "touch"
36 #define AUTHORS \
37 "Paul Rubin, Arnold Robbins, Jim Kingdon, David MacKenzie, and Randy Smith"
39 #ifndef STDC_HEADERS
40 time_t time ();
41 #endif
43 /* Bitmasks for `change_times'. */
44 #define CH_ATIME 1
45 #define CH_MTIME 2
47 /* The name by which this program was run. */
48 char *program_name;
50 /* Which timestamps to change. */
51 static int change_times;
53 /* (-c) If nonzero, don't create if not already there. */
54 static int no_create;
56 /* (-d) If nonzero, date supplied on command line in get_date formats. */
57 static int flexible_date;
59 /* (-r) If nonzero, use times from a reference file. */
60 static int use_ref;
62 /* (-t) If nonzero, date supplied on command line in POSIX format. */
63 static int posix_date;
65 /* If nonzero, the only thing we have to do is change both the
66 modification and access time to the current time, so we don't
67 have to own the file, just be able to read and write it.
68 On some systems, we can do this if we own the file, even though
69 we have neither read nor write access to it. */
70 static int amtime_now;
72 /* New time to use when setting time. */
73 static time_t newtime;
75 /* File to use for -r. */
76 static char *ref_file;
78 /* Info about the reference file. */
79 static struct stat ref_stats;
81 /* For long options that have no equivalent short option, use a
82 non-character as a pseudo short option, starting with CHAR_MAX + 1. */
83 enum
85 TIME_OPTION = CHAR_MAX + 1
88 static struct option const longopts[] =
90 {"time", required_argument, 0, TIME_OPTION},
91 {"no-create", no_argument, 0, 'c'},
92 {"date", required_argument, 0, 'd'},
93 {"file", required_argument, 0, 'r'}, /* FIXME: phase out --file */
94 {"reference", required_argument, 0, 'r'},
95 {GETOPT_HELP_OPTION_DECL},
96 {GETOPT_VERSION_OPTION_DECL},
97 {0, 0, 0, 0}
100 /* Valid arguments to the `--time' option. */
101 static char const* const time_args[] =
103 "atime", "access", "use", "mtime", "modify", 0
106 /* The bits in `change_times' that those arguments set. */
107 static int const time_masks[] =
109 CH_ATIME, CH_ATIME, CH_ATIME, CH_MTIME, CH_MTIME
112 /* Update the time of file FILE according to the options given.
113 Return 0 if successful, 1 if an error occurs. */
115 static int
116 touch (const char *file)
118 int status;
119 struct stat sbuf;
120 int fd = -1;
121 int open_errno = 0;
123 if (! no_create)
125 /* Try to open FILE, creating it if necessary. */
126 fd = open (file, O_WRONLY | O_CREAT,
127 S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
128 if (fd == -1)
129 open_errno = errno;
132 if (! amtime_now)
134 /* We're setting only one of the time values. stat the target to get
135 the other one. If we have the file descriptor already, use fstat.
136 Otherwise, either we're in no-create mode (and hence didn't call open)
137 or FILE is inaccessible or a directory, so we have to use stat. */
138 if (fd != -1 ? fstat (fd, &sbuf) : stat (file, &sbuf))
140 error (0, open_errno ? open_errno : errno, "%s", file);
141 close (fd);
142 return 1;
146 if (fd != -1 && close (fd) < 0)
148 error (0, errno, "%s", file);
149 return 1;
152 if (amtime_now)
154 /* Pass NULL to utime so it will not fail if we just have
155 write access to the file, but don't own it. */
156 status = utime (file, NULL);
158 else
160 struct utimbuf utb;
162 /* There's currently no interface to set file timestamps with
163 better than 1-second resolution, so discard any fractional
164 part of the source timestamp. */
166 if (use_ref)
168 utb.actime = ref_stats.st_atime;
169 utb.modtime = ref_stats.st_mtime;
171 else
172 utb.actime = utb.modtime = newtime;
174 if (!(change_times & CH_ATIME))
175 utb.actime = sbuf.st_atime;
177 if (!(change_times & CH_MTIME))
178 utb.modtime = sbuf.st_mtime;
180 status = utime (file, &utb);
183 if (status)
185 error (0, open_errno ? open_errno : errno, "%s", file);
186 return 1;
189 return 0;
192 void
193 usage (int status)
195 if (status != 0)
196 fprintf (stderr, _("Try `%s --help' for more information.\n"),
197 program_name);
198 else
200 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);
201 printf (_(" or : %s [-acm] MMDDhhmm[YY] FILE... (obsolescent)\n"),
202 program_name);
203 printf (_("\
204 Update the access and modification times of each FILE to the current time.\n\
206 -a change only the access time\n\
207 -c, --no-create do not create any files\n\
208 -d, --date=STRING parse STRING and use it instead of current time\n\
209 -f (ignored)\n\
210 -m change only the modification time\n\
211 -r, --reference=FILE use this file's times instead of current time\n\
212 -t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time\n\
213 --time=WORD set time given by WORD: access atime use (same as -a)\n\
214 modify mtime (same as -m)\n\
215 --help display this help and exit\n\
216 --version output version information and exit\n\
218 Note that the three time-date formats recognized for the -d and -t options\n\
219 and for the obsolescent argument are all different.\n\
220 "));
221 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
222 close_stdout ();
224 exit (status);
228 main (int argc, char **argv)
230 int c;
231 int date_set = 0;
232 int err = 0;
234 program_name = argv[0];
235 setlocale (LC_ALL, "");
236 bindtextdomain (PACKAGE, LOCALEDIR);
237 textdomain (PACKAGE);
239 change_times = no_create = use_ref = posix_date = flexible_date = 0;
240 newtime = (time_t) -1;
242 while ((c = getopt_long (argc, argv, "acd:fmr:t:", longopts, NULL)) != -1)
244 switch (c)
246 case 0:
247 break;
249 case 'a':
250 change_times |= CH_ATIME;
251 break;
253 case 'c':
254 no_create++;
255 break;
257 case 'd':
258 flexible_date++;
259 newtime = get_date (optarg, NULL);
260 if (newtime == (time_t) -1)
261 error (1, 0, _("invalid date format `%s'"), optarg);
262 date_set++;
263 break;
265 case 'f':
266 break;
268 case 'm':
269 change_times |= CH_MTIME;
270 break;
272 case 'r':
273 use_ref++;
274 ref_file = optarg;
275 break;
277 case 't':
278 posix_date++;
279 newtime = posixtime (optarg,
280 PDS_LEADING_YEAR | PDS_CENTURY | PDS_SECONDS);
281 if (newtime == (time_t) -1)
282 error (1, 0, _("invalid date format `%s'"), optarg);
283 date_set++;
284 break;
286 case TIME_OPTION: /* --time */
287 change_times |= XARGMATCH ("--time", optarg,
288 time_args, time_masks);
289 break;
291 case_GETOPT_HELP_CHAR;
293 case_GETOPT_VERSION_CHAR (PROGRAM_NAME, AUTHORS);
295 default:
296 usage (1);
300 if (change_times == 0)
301 change_times = CH_ATIME | CH_MTIME;
303 if ((use_ref && (posix_date || flexible_date))
304 || (posix_date && flexible_date))
306 error (0, 0, _("cannot specify times from more than one source"));
307 usage (1);
310 if (use_ref)
312 if (stat (ref_file, &ref_stats))
313 error (1, errno, "%s", ref_file);
314 date_set++;
317 if (!date_set && optind < argc && !STREQ (argv[optind - 1], "--"))
319 newtime = posixtime (argv[optind], PDS_TRAILING_YEAR);
320 if (newtime != (time_t) -1)
322 optind++;
323 date_set++;
326 if (!date_set)
328 if ((change_times & (CH_ATIME | CH_MTIME)) == (CH_ATIME | CH_MTIME))
329 amtime_now = 1;
330 else
331 time (&newtime);
334 if (optind == argc)
336 error (0, 0, _("file arguments missing"));
337 usage (1);
340 for (; optind < argc; ++optind)
341 err += touch (argv[optind]);
343 exit (err != 0);