2000-02-08 19:58:47 +00:00
|
|
|
/* vi: set sw=4 ts=4: */
|
1999-10-07 08:30:23 +00:00
|
|
|
/*
|
|
|
|
* Mini touch implementation for busybox
|
|
|
|
*
|
2004-03-15 08:29:22 +00:00
|
|
|
* Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
|
1999-10-07 08:30:23 +00:00
|
|
|
*
|
2010-08-16 18:14:46 +00:00
|
|
|
* Licensed under GPLv2 or later, see file LICENSE in this source tree.
|
1999-10-07 08:30:23 +00:00
|
|
|
*/
|
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
/* BB_AUDIT SUSv3 _NOT_ compliant -- options -a, -m, -r, -t not supported. */
|
|
|
|
/* http://www.opengroup.org/onlinepubs/007904975/utilities/touch.html */
|
|
|
|
|
|
|
|
/* 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.
|
|
|
|
*/
|
|
|
|
|
2007-05-26 19:00:18 +00:00
|
|
|
#include "libbb.h"
|
1999-10-07 08:30:23 +00:00
|
|
|
|
2010-12-01 12:06:03 +00:00
|
|
|
//config:config TOUCH
|
|
|
|
//config: bool "touch"
|
|
|
|
//config: default y
|
|
|
|
//config: help
|
|
|
|
//config: touch is used to create or change the access and/or
|
|
|
|
//config: modification timestamp of specified files.
|
|
|
|
|
|
|
|
//applet:IF_TOUCH(APPLET_NOFORK(touch, touch, _BB_DIR_BIN, _BB_SUID_DROP, touch))
|
|
|
|
|
|
|
|
//kbuild:lib-$(CONFIG_TOUCH) += touch.o
|
|
|
|
|
|
|
|
//usage:#define touch_trivial_usage
|
|
|
|
//usage: "[-c]" IF_DESKTOP(" [-d DATE] [-r FILE]") " FILE [FILE]..."
|
|
|
|
//usage:#define touch_full_usage "\n\n"
|
|
|
|
//usage: "Update the last-modified date on the given FILE[s]\n"
|
|
|
|
//usage: "\nOptions:"
|
|
|
|
//usage: "\n -c Don't create files"
|
|
|
|
//usage: IF_DESKTOP(
|
|
|
|
//usage: "\n -d DT Date/time to use"
|
|
|
|
//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"
|
|
|
|
|
2007-04-09 21:35:07 +00:00
|
|
|
/* This is a NOFORK applet. Be very careful! */
|
|
|
|
|
2008-06-28 01:18:09 +00:00
|
|
|
/* 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
|
|
|
|
* -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
|
|
|
|
*/
|
|
|
|
|
2007-10-11 10:05:36 +00:00
|
|
|
int touch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
|
2008-07-05 09:18:54 +00:00
|
|
|
int touch_main(int argc UNUSED_PARAM, char **argv)
|
2000-02-08 19:58:47 +00:00
|
|
|
{
|
2009-11-15 01:28:56 +00:00
|
|
|
int fd;
|
|
|
|
int status = EXIT_SUCCESS;
|
|
|
|
int opts;
|
2008-06-28 01:18:09 +00:00
|
|
|
#if ENABLE_DESKTOP
|
2009-07-18 01:41:29 +00:00
|
|
|
# if ENABLE_LONG_OPTS
|
|
|
|
static const char touch_longopts[] ALIGN1 =
|
2009-03-15 01:28:30 +00:00
|
|
|
/* name, has_arg, val */
|
|
|
|
"no-create\0" No_argument "c"
|
|
|
|
"reference\0" Required_argument "r"
|
2009-07-18 01:41:29 +00:00
|
|
|
"date\0" Required_argument "d"
|
2009-03-15 01:28:30 +00:00
|
|
|
;
|
2009-07-18 01:41:29 +00:00
|
|
|
# endif
|
2008-06-28 01:18:09 +00:00
|
|
|
char *reference_file = NULL;
|
2009-07-18 01:41:29 +00:00
|
|
|
char *date_str = NULL;
|
2009-11-29 18:40:36 +00:00
|
|
|
struct timeval timebuf[2];
|
|
|
|
timebuf[1].tv_usec = timebuf[0].tv_usec = 0;
|
2008-06-28 01:18:09 +00:00
|
|
|
#else
|
2009-07-18 01:41:29 +00:00
|
|
|
# define reference_file NULL
|
|
|
|
# define date_str NULL
|
2009-11-30 10:36:14 +00:00
|
|
|
# define timebuf ((struct timeval*)NULL)
|
2008-06-28 01:18:09 +00:00
|
|
|
#endif
|
2009-03-15 01:28:30 +00:00
|
|
|
|
2009-07-18 01:41:29 +00:00
|
|
|
#if ENABLE_DESKTOP && ENABLE_LONG_OPTS
|
|
|
|
applet_long_options = touch_longopts;
|
2009-03-15 01:28:30 +00:00
|
|
|
#endif
|
2009-07-18 02:49:20 +00:00
|
|
|
/* -d and -t both set time. In coreutils,
|
|
|
|
* accepted data format differs a bit between -d and -t.
|
|
|
|
* We accept the same formats for both */
|
|
|
|
opts = getopt32(argv, "c" IF_DESKTOP("r:d:t:")
|
2008-06-28 01:18:09 +00:00
|
|
|
/*ignored:*/ "fma"
|
2009-07-18 01:41:29 +00:00
|
|
|
IF_DESKTOP(, &reference_file)
|
|
|
|
IF_DESKTOP(, &date_str)
|
2009-07-18 02:49:20 +00:00
|
|
|
IF_DESKTOP(, &date_str)
|
2009-07-18 01:41:29 +00:00
|
|
|
);
|
2003-03-19 09:13:01 +00:00
|
|
|
|
2009-03-15 01:28:30 +00:00
|
|
|
opts &= 1; /* only -c bit is left */
|
2003-03-19 09:13:01 +00:00
|
|
|
argv += optind;
|
|
|
|
if (!*argv) {
|
|
|
|
bb_show_usage();
|
2000-06-02 23:26:44 +00:00
|
|
|
}
|
|
|
|
|
2008-06-28 01:18:09 +00:00
|
|
|
if (reference_file) {
|
|
|
|
struct stat stbuf;
|
|
|
|
xstat(reference_file, &stbuf);
|
2009-11-29 18:40:36 +00:00
|
|
|
timebuf[1].tv_sec = timebuf[0].tv_sec = stbuf.st_mtime;
|
2008-06-28 01:18:09 +00:00
|
|
|
}
|
|
|
|
|
2009-07-18 01:41:29 +00:00
|
|
|
if (date_str) {
|
|
|
|
struct tm tm_time;
|
|
|
|
time_t t;
|
|
|
|
|
2011-01-02 19:02:09 +00:00
|
|
|
//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 01:41:29 +00:00
|
|
|
parse_datestr(date_str, &tm_time);
|
|
|
|
|
|
|
|
/* Correct any day of week and day of year etc. fields */
|
2010-10-28 16:57:19 +00:00
|
|
|
tm_time.tm_isdst = -1; /* Be sure to recheck dst */
|
2009-07-18 01:41:29 +00:00
|
|
|
t = validate_tm_time(date_str, &tm_time);
|
|
|
|
|
2009-11-29 18:40:36 +00:00
|
|
|
timebuf[1].tv_sec = timebuf[0].tv_sec = t;
|
2009-07-18 01:41:29 +00:00
|
|
|
}
|
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
do {
|
2010-03-21 08:22:47 +00:00
|
|
|
if (utimes(*argv, (reference_file || date_str) ? timebuf : NULL) != 0) {
|
2008-06-28 01:18:09 +00:00
|
|
|
if (errno == ENOENT) { /* no such file */
|
2009-03-15 01:28:30 +00:00
|
|
|
if (opts) { /* creation is disabled, so ignore */
|
2003-03-19 09:13:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
2009-11-30 10:36:14 +00:00
|
|
|
/* Try to create the file */
|
|
|
|
fd = open(*argv, O_RDWR | O_CREAT, 0666);
|
|
|
|
if (fd >= 0) {
|
|
|
|
xclose(fd);
|
2010-03-21 08:22:47 +00:00
|
|
|
if (reference_file || date_str)
|
2009-11-29 18:40:36 +00:00
|
|
|
utimes(*argv, timebuf);
|
2003-03-19 09:13:01 +00:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
status = EXIT_FAILURE;
|
2007-10-01 11:58:38 +00:00
|
|
|
bb_simple_perror_msg(*argv);
|
2000-02-08 19:58:47 +00:00
|
|
|
}
|
2003-03-19 09:13:01 +00:00
|
|
|
} while (*++argv);
|
2000-06-02 23:26:44 +00:00
|
|
|
|
2003-03-19 09:13:01 +00:00
|
|
|
return status;
|
2000-02-08 19:58:47 +00:00
|
|
|
}
|