123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193 |
- #include "libbb.h"
- int touch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
- int touch_main(int argc UNUSED_PARAM, char **argv)
- {
- int fd;
- int opts;
- exitcode_t status = EXIT_SUCCESS;
- #if ENABLE_FEATURE_TOUCH_SUSV3
- char *reference_file;
- char *date_str;
-
- struct timespec timebuf[2];
- #else
- # define reference_file NULL
- # define date_str NULL
- # define timebuf ((struct timespec*)NULL)
- #endif
- enum {
- OPT_c = (1 << 0),
- 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,
- };
- #if ENABLE_LONG_OPTS
- static const char touch_longopts[] ALIGN1 =
-
- "no-create\0" No_argument "c"
- "no-dereference\0" No_argument "h"
- IF_FEATURE_TOUCH_SUSV3("reference\0" Required_argument "r")
- IF_FEATURE_TOUCH_SUSV3("date\0" Required_argument "d")
- ;
- #endif
-
- opts = getopt32long(argv, "^"
- "ch"
- IF_FEATURE_TOUCH_SUSV3("r:d:t:am")
- "f" IF_NOT_FEATURE_TOUCH_SUSV3("am")
- "\0"
- "-1"
- IF_FEATURE_TOUCH_SUSV3(":r--t:t--r")
-
- , touch_longopts
- #if ENABLE_FEATURE_TOUCH_SUSV3
- , &reference_file
- , &date_str
- , &date_str
- #endif
- );
- #if ENABLE_FEATURE_TOUCH_SUSV3
- timebuf[0].tv_nsec = timebuf[1].tv_nsec = UTIME_NOW;
- if (opts & OPT_r) {
- struct stat stbuf;
- xstat(reference_file, &stbuf);
- timebuf[0].tv_sec = stbuf.st_atime;
- timebuf[1].tv_sec = stbuf.st_mtime;
- timebuf[0].tv_nsec = stbuf.st_atim.tv_nsec;
- timebuf[1].tv_nsec = stbuf.st_mtim.tv_nsec;
- }
- if (opts & (OPT_d|OPT_t)) {
- struct tm tm_time;
- time_t t;
- int check_dst;
-
-
- time(&t);
- localtime_r(&t, &tm_time);
- check_dst = parse_datestr(date_str, &tm_time);
-
- if (check_dst)
- tm_time.tm_isdst = -1;
- t = validate_tm_time(date_str, &tm_time);
- timebuf[1].tv_sec = timebuf[0].tv_sec = t;
- timebuf[1].tv_nsec = timebuf[0].tv_nsec = 0;
- }
-
- 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;
- #endif
- argv += optind;
- do {
- int result = utimensat(AT_FDCWD, *argv, timebuf,
- (opts & OPT_h) ? AT_SYMLINK_NOFOLLOW : 0);
- if (result != 0) {
- if (errno == ENOENT) {
- if (opts & OPT_c) {
-
- continue;
- }
-
- fd = open(*argv, O_RDWR | O_CREAT, 0666);
- if (fd >= 0) {
- if (opts & (OPT_r|OPT_d|OPT_t))
- futimens(fd, timebuf);
- xclose(fd);
- continue;
- }
- }
- status = EXIT_FAILURE;
- bb_simple_perror_msg(*argv);
- }
- } while (*++argv);
- return status;
- }
|