*** empty log message ***
[coreutils.git] / src / touch.c
blob9a366c274f50d369546eceb748caa5d4cbfdece7
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 "closeout.h"
28 #include "error.h"
29 #include "argmatch.h"
30 #include "getdate.h"
31 #include "safe-read.h"
32 #include "posixtm.h"
34 #ifndef STDC_HEADERS
35 time_t time ();
36 #endif
38 int full_write ();
40 /* Bitmasks for `change_times'. */
41 #define CH_ATIME 1
42 #define CH_MTIME 2
44 /* The name by which this program was run. */
45 char *program_name;
47 /* Which timestamps to change. */
48 static int change_times;
50 /* (-c) If nonzero, don't create if not already there. */
51 static int no_create;
53 /* (-d) If nonzero, date supplied on command line in get_date formats. */
54 static int flexible_date;
56 /* (-r) If nonzero, use times from a reference file. */
57 static int use_ref;
59 /* (-t) If nonzero, date supplied on command line in POSIX format. */
60 static int posix_date;
62 /* If nonzero, the only thing we have to do is change both the
63 modification and access time to the current time, so we don't
64 have to own the file, just be able to read and write it. */
65 static int amtime_now;
67 /* New time to use when setting time. */
68 static time_t newtime;
70 /* File to use for -r. */
71 static char *ref_file;
73 /* Info about the reference file. */
74 static struct stat ref_stats;
76 /* If nonzero, display usage information and exit. */
77 static int show_help;
79 /* If nonzero, print the version on standard output and exit. */
80 static int show_version;
82 static struct option const longopts[] =
84 {"time", required_argument, 0, CHAR_MAX + 1},
85 {"no-create", no_argument, 0, 'c'},
86 {"date", required_argument, 0, 'd'},
87 {"file", required_argument, 0, 'r'},
88 {"reference", required_argument, 0, 'r'},
89 {"help", no_argument, &show_help, 1},
90 {"version", no_argument, &show_version, 1},
91 {0, 0, 0, 0}
94 /* Valid arguments to the `--time' option. */
95 static char const* const time_args[] =
97 "atime", "access", "use", "mtime", "modify", 0
100 /* The bits in `change_times' that those arguments set. */
101 static int const time_masks[] =
103 CH_ATIME, CH_ATIME, CH_ATIME, CH_MTIME, CH_MTIME
106 /* Update the time of file FILE according to the options given.
107 Return 0 if successful, 1 if an error occurs. */
109 static int
110 touch (char *file)
112 int status;
113 struct stat sbuf;
114 int fd;
116 if (stat (file, &sbuf))
118 if (errno != ENOENT)
120 error (0, errno, "%s", file);
121 return 1;
123 if (no_create)
124 return 0;
125 fd = creat (file, 0666);
126 if (fd == -1)
128 error (0, errno, "%s", file);
129 return 1;
131 if (amtime_now)
133 if (close (fd) < 0)
135 error (0, errno, "%s", file);
136 return 1;
138 return 0; /* We've done all we have to. */
140 if (fstat (fd, &sbuf))
142 error (0, errno, "%s", file);
143 close (fd);
144 return 1;
146 if (close (fd) < 0)
148 error (0, errno, "%s", file);
149 return 1;
153 if (amtime_now)
155 /* Pass NULL to utime so it will not fail if we just have
156 write access to the file, but don't own it. */
157 status = utime (file, NULL);
159 else
161 struct utimbuf utb;
163 /* There's currently no interface to set file timestamps with
164 better than 1-second resolution, so discard any fractional
165 part of the source timestamp. */
167 if (use_ref)
169 utb.actime = ref_stats.st_atime;
170 utb.modtime = ref_stats.st_mtime;
172 else
173 utb.actime = utb.modtime = newtime;
175 if (!(change_times & CH_ATIME))
176 utb.actime = sbuf.st_atime;
178 if (!(change_times & CH_MTIME))
179 utb.modtime = sbuf.st_mtime;
181 status = utime (file, &utb);
184 if (status)
186 error (0, errno, "%s", file);
187 return 1;
190 return 0;
193 void
194 usage (int status)
196 if (status != 0)
197 fprintf (stderr, _("Try `%s --help' for more information.\n"),
198 program_name);
199 else
201 printf (_("Usage: %s [OPTION]... FILE...\n"), program_name);
202 printf (_(" or : %s [-acm] MMDDhhmm[YY] FILE... (obsolescent)\n"),
203 program_name);
204 printf (_("\
205 Update the access and modification times of each FILE to the current time.\n\
207 -a change only the access time\n\
208 -c do not create any files\n\
209 -d, --date=STRING parse STRING and use it instead of current time\n\
210 -f (ignored)\n\
211 -m change only the modification time\n\
212 -r, --reference=FILE use this file's times instead of current time\n\
213 -t STAMP use [[CC]YY]MMDDhhmm[.ss] instead of current time\n\
214 --time=WORD access -a, atime -a, mtime -m, modify -m, use -a\n\
215 --help display this help and exit\n\
216 --version output version information and exit\n\
218 STAMP may be used without -t if none of -drt, nor --, are used.\n\
219 Note that the three time-date formats recognized for the -d and -t options\n\
220 and for the obsolescent argument are all different.\n\
221 "));
222 puts (_("\nReport bugs to <bug-fileutils@gnu.org>."));
223 close_stdout ();
225 exit (status);
229 main (int argc, char **argv)
231 int c;
232 int date_set = 0;
233 int err = 0;
235 program_name = argv[0];
236 setlocale (LC_ALL, "");
237 bindtextdomain (PACKAGE, LOCALEDIR);
238 textdomain (PACKAGE);
240 change_times = no_create = use_ref = posix_date = flexible_date = 0;
241 newtime = (time_t) -1;
243 while ((c = getopt_long (argc, argv, "acd:fmr:t:", longopts, NULL)) != -1)
245 switch (c)
247 case 0:
248 break;
250 case 'a':
251 change_times |= CH_ATIME;
252 break;
254 case 'c':
255 no_create++;
256 break;
258 case 'd':
259 flexible_date++;
260 newtime = get_date (optarg, NULL);
261 if (newtime == (time_t) -1)
262 error (1, 0, _("invalid date format `%s'"), optarg);
263 date_set++;
264 break;
266 case 'f':
267 break;
269 case 'm':
270 change_times |= CH_MTIME;
271 break;
273 case 'r':
274 use_ref++;
275 ref_file = optarg;
276 break;
278 case 't':
279 posix_date++;
280 newtime = posixtime (optarg,
281 PDS_LEADING_YEAR | PDS_CENTURY | PDS_SECONDS);
282 if (newtime == (time_t) -1)
283 error (1, 0, _("invalid date format `%s'"), optarg);
284 date_set++;
285 break;
287 case CHAR_MAX + 1: /* --time */
288 change_times |= XARGMATCH ("--time", optarg,
289 time_args, time_masks);
290 break;
292 default:
293 usage (1);
297 if (show_version)
299 printf ("touch (%s) %s\n", GNU_PACKAGE, VERSION);
300 close_stdout ();
301 exit (0);
304 if (show_help)
305 usage (0);
307 if (change_times == 0)
308 change_times = CH_ATIME | CH_MTIME;
310 if ((use_ref && (posix_date || flexible_date))
311 || (posix_date && flexible_date))
313 error (0, 0, _("cannot specify times from more than one source"));
314 usage (1);
317 if (use_ref)
319 if (stat (ref_file, &ref_stats))
320 error (1, errno, "%s", ref_file);
321 date_set++;
324 if (!date_set && optind < argc && !STREQ (argv[optind - 1], "--"))
326 newtime = posixtime (argv[optind], PDS_TRAILING_YEAR);
327 if (newtime != (time_t) -1)
329 optind++;
330 date_set++;
333 if (!date_set)
335 if ((change_times & (CH_ATIME | CH_MTIME)) == (CH_ATIME | CH_MTIME))
336 amtime_now = 1;
337 else
338 time (&newtime);
341 if (optind == argc)
343 error (0, 0, _("file arguments missing"));
344 usage (1);
347 for (; optind < argc; ++optind)
348 err += touch (argv[optind]);
350 exit (err != 0);