2000-02-09 01:28:47 +05:30
|
|
|
/* vi: set sw=4 ts=4: */
|
1999-10-07 14:00:23 +05:30
|
|
|
/*
|
|
|
|
* Mini touch implementation for busybox
|
|
|
|
*
|
2004-03-15 13:59:22 +05:30
|
|
|
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
1999-10-07 14:00:23 +05:30
|
|
|
*
|
2010-08-16 23:44:46 +05:30
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
1999-10-07 14:00:23 +05:30
|
|
|
*/
|
2003-03-19 14:43:01 +05:30
|
|
|
/* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
|
|
|
|
*
|
|
|
|
* Previous version called open() and then utime(). While this will be
|
|
|
|
* be necessary to implement -r and -t, it currently only makes things bigger.
|
|
|
|
* Also, exiting on a failure was a bug. All args should be processed.
|
|
|
|
*/
|
2010-12-01 17:36:03 +05:30
|
|
|
//config:config TOUCH
|
2018-12-28 07:50:17 +05:30
|
|
|
//config: bool "touch (5.9 kb)"
|
2010-12-01 17:36:03 +05:30
|
|
|
//config: default y
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: touch is used to create or change the access and/or
|
|
|
|
//config: modification timestamp of specified files.
|
2011-12-14 13:18:59 +05:30
|
|
|
//config:
|
|
|
|
//config:config FEATURE_TOUCH_SUSV3
|
2021-04-10 02:45:29 +05:30
|
|
|
//config: bool "Add support for SUSV3 features (-a -d -m -t -r)"
|
2011-12-14 13:18:59 +05:30
|
|
|
//config: default y
|
|
|
|
//config: depends on TOUCH
|
|
|
|
//config: help
|
2017-07-21 13:20:55 +05:30
|
|
|
//config: Enable touch to use a reference file or a given date/time argument.
|
2010-12-01 17:36:03 +05:30
|
|
|
|
2011-01-18 18:28:01 +05:30
|
|
|
//applet:IF_TOUCH(APPLET_NOFORK(touch, touch, BB_DIR_BIN, BB_SUID_DROP, touch))
|
2010-12-01 17:36:03 +05:30
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_TOUCH) += touch.o
|
|
|
|
|
|
|
|
//usage:#define touch_trivial_usage
|
2021-06-13 06:42:09 +05:30
|
|
|
//usage: "[-ch" IF_FEATURE_TOUCH_SUSV3("am") "]"
|
2021-04-10 02:45:29 +05:30
|
|
|
//usage: IF_FEATURE_TOUCH_SUSV3(" [-d DATE] [-t DATE] [-r FILE]")
|
|
|
|
//usage: " FILE..."
|
2010-12-01 17:36:03 +05:30
|
|
|
//usage:#define touch_full_usage "\n\n"
|
2021-04-14 18:45:45 +05:30
|
|
|
//usage: "Update mtime of FILEs\n"
|
2010-12-01 17:36:03 +05:30
|
|
|
//usage: "\n -c Don't create files"
|
2013-09-11 15:28:33 +05:30
|
|
|
//usage: "\n -h Don't follow links"
|
2011-12-14 13:18:59 +05:30
|
|
|
//usage: IF_FEATURE_TOUCH_SUSV3(
|
2021-04-10 02:45:29 +05:30
|
|
|
//usage: "\n -a Change only atime"
|
|
|
|
//usage: "\n -m Change only mtime"
|
2010-12-01 17:36:03 +05:30
|
|
|
//usage: "\n -d DT Date/time to use"
|
2011-12-13 21:26:47 +05:30
|
|
|
//usage: "\n -t DT Date/time to use"
|
2010-12-01 17:36:03 +05:30
|
|
|
//usage: "\n -r FILE Use FILE's date/time"
|
|
|
|
//usage: )
|
|
|
|
//usage:
|
|
|
|
//usage:#define touch_example_usage
|
|
|
|
//usage: "$ ls -l /tmp/foo\n"
|
|
|
|
//usage: "/bin/ls: /tmp/foo: No such file or directory\n"
|
|
|
|
//usage: "$ touch /tmp/foo\n"
|
|
|
|
//usage: "$ ls -l /tmp/foo\n"
|
|
|
|
//usage: "-rw-rw-r-- 1 andersen andersen 0 Apr 15 01:11 /tmp/foo\n"
|
|
|
|
|
2008-06-28 06:48:09 +05:30
|
|
|
/* coreutils implements:
|
|
|
|
* -a change only the access time
|
|
|
|
* -c, --no-create
|
|
|
|
* do not create any files
|
|
|
|
* -d, --date=STRING
|
|
|
|
* parse STRING and use it instead of current time
|
|
|
|
* -f (ignored, BSD compat)
|
|
|
|
* -m change only the modification time
|
2013-09-11 15:28:33 +05:30
|
|
|
* -h, --no-dereference
|
2008-06-28 06:48:09 +05:30
|
|
|
* -r, --reference=FILE
|
|
|
|
* use this file's times instead of current time
|
|
|
|
* -t STAMP
|
|
|
|
* use [[CC]YY]MMDDhhmm[.ss] instead of current time
|
|
|
|
* --time=WORD
|
|
|
|
* change the specified time: WORD is access, atime, or use
|
|
|
|
*/
|
|
|
|
|
2016-11-23 19:16:56 +05:30
|
|
|
#include "libbb.h"
|
|
|
|
|
2007-10-11 15:35:36 +05:30
|
|
|
int touch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 14:48:54 +05:30
|
|
|
int touch_main(int argc UNUSED_PARAM, char **argv)
|
2000-02-09 01:28:47 +05:30
|
|
|
{
|
2009-11-15 06:58:56 +05:30
|
|
|
int fd;
|
|
|
|
int opts;
|
2021-04-13 18:41:47 +05:30
|
|
|
smalluint status = EXIT_SUCCESS;
|
|
|
|
#if ENABLE_FEATURE_TOUCH_SUSV3
|
2021-04-13 19:11:57 +05:30
|
|
|
char *reference_file;
|
|
|
|
char *date_str;
|
2021-04-13 18:41:47 +05:30
|
|
|
/* timebuf[0] is atime, timebuf[1] is mtime */
|
|
|
|
struct timespec timebuf[2];
|
|
|
|
#else
|
|
|
|
# define reference_file NULL
|
|
|
|
# define date_str NULL
|
|
|
|
# define timebuf ((struct timespec*)NULL)
|
|
|
|
#endif
|
|
|
|
|
2013-09-11 15:28:33 +05:30
|
|
|
enum {
|
|
|
|
OPT_c = (1 << 0),
|
2021-04-13 19:14:13 +05:30
|
|
|
OPT_h = (1 << 1),
|
|
|
|
OPT_r = (1 << 2) * ENABLE_FEATURE_TOUCH_SUSV3,
|
|
|
|
OPT_d = (1 << 3) * ENABLE_FEATURE_TOUCH_SUSV3,
|
|
|
|
OPT_t = (1 << 4) * ENABLE_FEATURE_TOUCH_SUSV3,
|
|
|
|
OPT_a = (1 << 5) * ENABLE_FEATURE_TOUCH_SUSV3,
|
|
|
|
OPT_m = (1 << 6) * ENABLE_FEATURE_TOUCH_SUSV3,
|
2013-09-11 15:28:33 +05:30
|
|
|
};
|
2021-04-10 02:57:41 +05:30
|
|
|
#if ENABLE_LONG_OPTS
|
2009-07-18 07:11:29 +05:30
|
|
|
static const char touch_longopts[] ALIGN1 =
|
2009-03-15 06:58:30 +05:30
|
|
|
/* name, has_arg, val */
|
2021-04-13 19:14:13 +05:30
|
|
|
"no-create\0" No_argument "c"
|
|
|
|
"no-dereference\0" No_argument "h"
|
2021-04-10 02:57:41 +05:30
|
|
|
IF_FEATURE_TOUCH_SUSV3("reference\0" Required_argument "r")
|
|
|
|
IF_FEATURE_TOUCH_SUSV3("date\0" Required_argument "d")
|
2009-03-15 06:58:30 +05:30
|
|
|
;
|
2021-04-10 02:57:41 +05:30
|
|
|
#endif
|
2009-07-18 08:19:20 +05:30
|
|
|
/* -d and -t both set time. In coreutils,
|
|
|
|
* accepted data format differs a bit between -d and -t.
|
2021-04-12 23:42:56 +05:30
|
|
|
* We accept the same formats for both
|
|
|
|
*/
|
2021-04-10 03:41:46 +05:30
|
|
|
opts = getopt32long(argv, "^"
|
2021-04-13 19:14:13 +05:30
|
|
|
"ch"
|
2021-04-10 03:41:46 +05:30
|
|
|
IF_FEATURE_TOUCH_SUSV3("r:d:t:am")
|
|
|
|
/*ignored:*/ "f" IF_NOT_FEATURE_TOUCH_SUSV3("am")
|
|
|
|
"\0" /* opt_complementary: */
|
2021-04-13 18:41:47 +05:30
|
|
|
/* at least one arg: */ "-1"
|
|
|
|
/* coreutils forbids -r and -t at once: */ IF_FEATURE_TOUCH_SUSV3(":r--t:t--r")
|
|
|
|
/* but allows these combinations: "r--d:d--r:t--d:d--t" */
|
|
|
|
, touch_longopts
|
2021-04-10 02:57:41 +05:30
|
|
|
#if ENABLE_FEATURE_TOUCH_SUSV3
|
2021-04-10 03:41:46 +05:30
|
|
|
, &reference_file
|
|
|
|
, &date_str
|
|
|
|
, &date_str
|
2021-04-10 02:57:41 +05:30
|
|
|
#endif
|
2009-07-18 07:11:29 +05:30
|
|
|
);
|
2003-03-19 14:43:01 +05:30
|
|
|
|
2021-08-15 23:53:40 +05:30
|
|
|
#if ENABLE_FEATURE_TOUCH_SUSV3
|
2021-04-11 11:11:31 +05:30
|
|
|
timebuf[0].tv_nsec = timebuf[1].tv_nsec = UTIME_NOW;
|
2021-04-13 19:11:57 +05:30
|
|
|
if (opts & OPT_r) {
|
2008-06-28 06:48:09 +05:30
|
|
|
struct stat stbuf;
|
|
|
|
xstat(reference_file, &stbuf);
|
2021-04-08 05:35:37 +05:30
|
|
|
timebuf[0].tv_sec = stbuf.st_atime;
|
|
|
|
timebuf[1].tv_sec = stbuf.st_mtime;
|
2021-04-11 11:11:31 +05:30
|
|
|
timebuf[0].tv_nsec = stbuf.st_atim.tv_nsec;
|
|
|
|
timebuf[1].tv_nsec = stbuf.st_mtim.tv_nsec;
|
2008-06-28 06:48:09 +05:30
|
|
|
}
|
2021-04-13 19:11:57 +05:30
|
|
|
if (opts & (OPT_d|OPT_t)) {
|
2009-07-18 07:11:29 +05:30
|
|
|
struct tm tm_time;
|
|
|
|
time_t t;
|
|
|
|
|
2011-01-03 00:32:09 +05:30
|
|
|
//memset(&tm_time, 0, sizeof(tm_time));
|
|
|
|
/* Better than memset: makes "HH:MM" dates meaningful */
|
|
|
|
time(&t);
|
|
|
|
localtime_r(&t, &tm_time);
|
2009-07-18 07:11:29 +05:30
|
|
|
parse_datestr(date_str, &tm_time);
|
|
|
|
|
|
|
|
/* Correct any day of week and day of year etc. fields */
|
2010-10-28 22:27:19 +05:30
|
|
|
tm_time.tm_isdst = -1; /* Be sure to recheck dst */
|
2009-07-18 07:11:29 +05:30
|
|
|
t = validate_tm_time(date_str, &tm_time);
|
|
|
|
|
2009-11-30 00:10:36 +05:30
|
|
|
timebuf[1].tv_sec = timebuf[0].tv_sec = t;
|
2021-04-11 11:11:31 +05:30
|
|
|
timebuf[1].tv_nsec = timebuf[0].tv_nsec = 0;
|
2021-04-10 02:45:29 +05:30
|
|
|
}
|
2021-04-14 04:31:20 +05:30
|
|
|
/* If both -a and -m specified, both times should be set.
|
|
|
|
* IOW: set OMIT only if one, not both, of them is given!
|
|
|
|
*/
|
2021-04-14 18:16:35 +05:30
|
|
|
if ((opts & (OPT_a|OPT_m)) == OPT_a)
|
|
|
|
timebuf[1].tv_nsec = UTIME_OMIT;
|
|
|
|
if ((opts & (OPT_a|OPT_m)) == OPT_m)
|
|
|
|
timebuf[0].tv_nsec = UTIME_OMIT;
|
2021-08-15 23:53:40 +05:30
|
|
|
#endif
|
2009-07-18 07:11:29 +05:30
|
|
|
|
2021-04-13 18:41:47 +05:30
|
|
|
argv += optind;
|
2003-03-19 14:43:01 +05:30
|
|
|
do {
|
2021-04-11 11:11:31 +05:30
|
|
|
int result = utimensat(AT_FDCWD, *argv, timebuf,
|
|
|
|
(opts & OPT_h) ? AT_SYMLINK_NOFOLLOW : 0);
|
2013-09-11 15:28:33 +05:30
|
|
|
if (result != 0) {
|
|
|
|
if (errno == ENOENT) { /* no such file? */
|
|
|
|
if (opts & OPT_c) {
|
|
|
|
/* Creation is disabled, so ignore */
|
2003-03-19 14:43:01 +05:30
|
|
|
continue;
|
|
|
|
}
|
2009-11-30 16:06:14 +05:30
|
|
|
/* Try to create the file */
|
|
|
|
fd = open(*argv, O_RDWR | O_CREAT, 0666);
|
|
|
|
if (fd >= 0) {
|
2021-04-13 19:19:06 +05:30
|
|
|
if (opts & (OPT_r|OPT_d|OPT_t))
|
2021-04-11 11:11:31 +05:30
|
|
|
futimens(fd, timebuf);
|
|
|
|
xclose(fd);
|
2003-03-19 14:43:01 +05:30
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
status = EXIT_FAILURE;
|
2007-10-01 17:28:38 +05:30
|
|
|
bb_simple_perror_msg(*argv);
|
2000-02-09 01:28:47 +05:30
|
|
|
}
|
2003-03-19 14:43:01 +05:30
|
|
|
} while (*++argv);
|
2000-06-03 04:56:44 +05:30
|
|
|
|
2003-03-19 14:43:01 +05:30
|
|
|
return status;
|
2000-02-09 01:28:47 +05:30
|
|
|
}
|