blob: 293a9689078412731a7540ab7d9b7ee31bf1c0b0 [file] [log] [blame]
Erik Andersene49d5ec2000-02-08 19:58:47 +00001/* vi: set sw=4 ts=4: */
Eric Andersen596e5461999-10-07 08:30:23 +00002/*
3 * Mini touch implementation for busybox
4 *
Eric Andersenc7bda1c2004-03-15 08:29:22 +00005 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
Eric Andersen596e5461999-10-07 08:30:23 +00006 *
Denys Vlasenko0ef64bd2010-08-16 20:14:46 +02007 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
Eric Andersen596e5461999-10-07 08:30:23 +00008 */
Eric Andersencc8ed391999-10-05 16:24:54 +00009
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +020010/* BB_AUDIT SUSv3 _NOT_ compliant -- options -a, -m not supported. */
Manuel Novoa III cad53642003-03-19 09:13:01 +000011/* http://www.opengroup.org/onlinepubs/007904975/utilities/touch.html */
12
13/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
14 *
15 * Previous version called open() and then utime(). While this will be
16 * be necessary to implement -r and -t, it currently only makes things bigger.
17 * Also, exiting on a failure was a bug. All args should be processed.
18 */
19
Denis Vlasenkob6adbf12007-05-26 19:00:18 +000020#include "libbb.h"
Eric Andersen596e5461999-10-07 08:30:23 +000021
Denys Vlasenko3f2477e2010-12-01 13:06:03 +010022//config:config TOUCH
23//config: bool "touch"
24//config: default y
25//config: help
26//config: touch is used to create or change the access and/or
27//config: modification timestamp of specified files.
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +020028//config:
maxwen27116ba2015-08-14 21:41:28 +020029//config:config FEATURE_TOUCH_NODEREF
30//config: bool "Add support for -h"
31//config: default y
32//config: depends on TOUCH
33//config: help
34//config: Enable touch to have the -h option.
35//config: This requires libc support for lutimes() function.
36//config:
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +020037//config:config FEATURE_TOUCH_SUSV3
38//config: bool "Add support for SUSV3 features (-d -t -r)"
39//config: default y
40//config: depends on TOUCH
41//config: help
42//config: Enable touch to use a reference file or a given date/time argument.
Denys Vlasenko3f2477e2010-12-01 13:06:03 +010043
Denys Vlasenkob9f2d9f2011-01-18 13:58:01 +010044//applet:IF_TOUCH(APPLET_NOFORK(touch, touch, BB_DIR_BIN, BB_SUID_DROP, touch))
Denys Vlasenko3f2477e2010-12-01 13:06:03 +010045
46//kbuild:lib-$(CONFIG_TOUCH) += touch.o
47
48//usage:#define touch_trivial_usage
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +020049//usage: "[-c]" IF_FEATURE_TOUCH_SUSV3(" [-d DATE] [-t DATE] [-r FILE]") " FILE..."
Denys Vlasenko3f2477e2010-12-01 13:06:03 +010050//usage:#define touch_full_usage "\n\n"
51//usage: "Update the last-modified date on the given FILE[s]\n"
Denys Vlasenko3f2477e2010-12-01 13:06:03 +010052//usage: "\n -c Don't create files"
maxwen27116ba2015-08-14 21:41:28 +020053//usage: IF_FEATURE_TOUCH_NODEREF(
54//usage: "\n -h Don't follow links"
55//usage: )
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +020056//usage: IF_FEATURE_TOUCH_SUSV3(
Denys Vlasenko3f2477e2010-12-01 13:06:03 +010057//usage: "\n -d DT Date/time to use"
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +020058//usage: "\n -t DT Date/time to use"
Denys Vlasenko3f2477e2010-12-01 13:06:03 +010059//usage: "\n -r FILE Use FILE's date/time"
60//usage: )
61//usage:
62//usage:#define touch_example_usage
63//usage: "$ ls -l /tmp/foo\n"
64//usage: "/bin/ls: /tmp/foo: No such file or directory\n"
65//usage: "$ touch /tmp/foo\n"
66//usage: "$ ls -l /tmp/foo\n"
67//usage: "-rw-rw-r-- 1 andersen andersen 0 Apr 15 01:11 /tmp/foo\n"
68
Denis Vlasenko3f3aa2a2007-04-09 21:35:07 +000069/* This is a NOFORK applet. Be very careful! */
70
Denis Vlasenkoed90bda2008-06-28 01:18:09 +000071/* coreutils implements:
72 * -a change only the access time
73 * -c, --no-create
74 * do not create any files
75 * -d, --date=STRING
76 * parse STRING and use it instead of current time
77 * -f (ignored, BSD compat)
78 * -m change only the modification time
maxwen27116ba2015-08-14 21:41:28 +020079 * -h, --no-dereference
Denis Vlasenkoed90bda2008-06-28 01:18:09 +000080 * -r, --reference=FILE
81 * use this file's times instead of current time
82 * -t STAMP
83 * use [[CC]YY]MMDDhhmm[.ss] instead of current time
84 * --time=WORD
85 * change the specified time: WORD is access, atime, or use
86 */
87
Denis Vlasenko9b49a5e2007-10-11 10:05:36 +000088int touch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
Denis Vlasenkoa60f84e2008-07-05 09:18:54 +000089int touch_main(int argc UNUSED_PARAM, char **argv)
Eric Andersencc8ed391999-10-05 16:24:54 +000090{
Denys Vlasenko389cca42009-11-15 02:28:56 +010091 int fd;
92 int status = EXIT_SUCCESS;
93 int opts;
maxwen27116ba2015-08-14 21:41:28 +020094 enum {
95 OPT_c = (1 << 0),
96 OPT_r = (1 << 1) * ENABLE_FEATURE_TOUCH_SUSV3,
97 OPT_d = (1 << 2) * ENABLE_FEATURE_TOUCH_SUSV3,
98 OPT_t = (1 << 3) * ENABLE_FEATURE_TOUCH_SUSV3,
99 OPT_h = (1 << 4) * ENABLE_FEATURE_TOUCH_NODEREF,
100 };
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200101#if ENABLE_FEATURE_TOUCH_SUSV3
Denys Vlasenko7aca89a2009-07-18 03:41:29 +0200102# if ENABLE_LONG_OPTS
103 static const char touch_longopts[] ALIGN1 =
Denis Vlasenko7241e6d2009-03-15 01:28:30 +0000104 /* name, has_arg, val */
105 "no-create\0" No_argument "c"
106 "reference\0" Required_argument "r"
Denys Vlasenko7aca89a2009-07-18 03:41:29 +0200107 "date\0" Required_argument "d"
maxwen27116ba2015-08-14 21:41:28 +0200108 IF_FEATURE_TOUCH_NODEREF("no-dereference\0" No_argument "h")
Denis Vlasenko7241e6d2009-03-15 01:28:30 +0000109 ;
Denys Vlasenko7aca89a2009-07-18 03:41:29 +0200110# endif
Denis Vlasenkoed90bda2008-06-28 01:18:09 +0000111 char *reference_file = NULL;
Denys Vlasenko7aca89a2009-07-18 03:41:29 +0200112 char *date_str = NULL;
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100113 struct timeval timebuf[2];
114 timebuf[1].tv_usec = timebuf[0].tv_usec = 0;
Denis Vlasenkoed90bda2008-06-28 01:18:09 +0000115#else
Denys Vlasenko7aca89a2009-07-18 03:41:29 +0200116# define reference_file NULL
117# define date_str NULL
Denys Vlasenkod0f9d0e2009-11-30 11:36:14 +0100118# define timebuf ((struct timeval*)NULL)
Denis Vlasenkoed90bda2008-06-28 01:18:09 +0000119#endif
Denis Vlasenko7241e6d2009-03-15 01:28:30 +0000120
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200121#if ENABLE_FEATURE_TOUCH_SUSV3 && ENABLE_LONG_OPTS
Denys Vlasenko7aca89a2009-07-18 03:41:29 +0200122 applet_long_options = touch_longopts;
Denis Vlasenko7241e6d2009-03-15 01:28:30 +0000123#endif
Denys Vlasenko38dd8aa2009-07-18 04:49:20 +0200124 /* -d and -t both set time. In coreutils,
125 * accepted data format differs a bit between -d and -t.
126 * We accept the same formats for both */
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200127 opts = getopt32(argv, "c" IF_FEATURE_TOUCH_SUSV3("r:d:t:")
maxwen27116ba2015-08-14 21:41:28 +0200128 IF_FEATURE_TOUCH_NODEREF("h")
Denis Vlasenkoed90bda2008-06-28 01:18:09 +0000129 /*ignored:*/ "fma"
Tanguy Pruvot8a6c2c22012-04-28 00:24:09 +0200130 IF_FEATURE_TOUCH_SUSV3(, &reference_file)
131 IF_FEATURE_TOUCH_SUSV3(, &date_str)
132 IF_FEATURE_TOUCH_SUSV3(, &date_str)
Denys Vlasenko7aca89a2009-07-18 03:41:29 +0200133 );
Manuel Novoa III cad53642003-03-19 09:13:01 +0000134
135 argv += optind;
Manuel Novoa III cad53642003-03-19 09:13:01 +0000136 if (!*argv) {
137 bb_show_usage();
Eric Andersen5a0a2aa2000-06-02 23:26:44 +0000138 }
139
Denis Vlasenkoed90bda2008-06-28 01:18:09 +0000140 if (reference_file) {
141 struct stat stbuf;
142 xstat(reference_file, &stbuf);
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100143 timebuf[1].tv_sec = timebuf[0].tv_sec = stbuf.st_mtime;
maxwen27116ba2015-08-14 21:41:28 +0200144 /* Can use .st_mtim.tv_nsec
145 * (or is it .st_mtimensec?? see date.c)
146 * to set microseconds too.
147 */
Denis Vlasenkoed90bda2008-06-28 01:18:09 +0000148 }
149
Denys Vlasenko7aca89a2009-07-18 03:41:29 +0200150 if (date_str) {
151 struct tm tm_time;
152 time_t t;
153
Denys Vlasenko92ffe052011-01-02 20:02:09 +0100154 //memset(&tm_time, 0, sizeof(tm_time));
155 /* Better than memset: makes "HH:MM" dates meaningful */
156 time(&t);
157 localtime_r(&t, &tm_time);
Denys Vlasenko7aca89a2009-07-18 03:41:29 +0200158 parse_datestr(date_str, &tm_time);
159
160 /* Correct any day of week and day of year etc. fields */
Denys Vlasenkoe4dcba12010-10-28 18:57:19 +0200161 tm_time.tm_isdst = -1; /* Be sure to recheck dst */
Denys Vlasenko7aca89a2009-07-18 03:41:29 +0200162 t = validate_tm_time(date_str, &tm_time);
163
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100164 timebuf[1].tv_sec = timebuf[0].tv_sec = t;
Denys Vlasenko7aca89a2009-07-18 03:41:29 +0200165 }
166
Manuel Novoa III cad53642003-03-19 09:13:01 +0000167 do {
maxwen27116ba2015-08-14 21:41:28 +0200168 int result;
169 result = (
170#if ENABLE_FEATURE_TOUCH_NODEREF
171 (opts & OPT_h) ? lutimes :
172#endif
173 utimes)(*argv, (reference_file || date_str) ? timebuf : NULL);
174 if (result != 0) {
175 if (errno == ENOENT) { /* no such file? */
176 if (opts & OPT_c) {
177 /* Creation is disabled, so ignore */
Manuel Novoa III cad53642003-03-19 09:13:01 +0000178 continue;
179 }
Denys Vlasenkod0f9d0e2009-11-30 11:36:14 +0100180 /* Try to create the file */
181 fd = open(*argv, O_RDWR | O_CREAT, 0666);
182 if (fd >= 0) {
183 xclose(fd);
Mikhail Gusarov927e4bb2010-03-21 14:22:47 +0600184 if (reference_file || date_str)
Denys Vlasenkodcbfaba2009-11-29 19:40:36 +0100185 utimes(*argv, timebuf);
Manuel Novoa III cad53642003-03-19 09:13:01 +0000186 continue;
187 }
188 }
189 status = EXIT_FAILURE;
Denis Vlasenko0c97c9d2007-10-01 11:58:38 +0000190 bb_simple_perror_msg(*argv);
Eric Andersen5a0a2aa2000-06-02 23:26:44 +0000191 }
Manuel Novoa III cad53642003-03-19 09:13:01 +0000192 } while (*++argv);
Erik Andersene49d5ec2000-02-08 19:58:47 +0000193
Manuel Novoa III cad53642003-03-19 09:13:01 +0000194 return status;
Eric Andersencc8ed391999-10-05 16:24:54 +0000195}