touch.c 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini touch implementation for busybox
  4. *
  5. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  6. *
  7. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  8. */
  9. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  10. *
  11. * Previous version called open() and then utime(). While this will be
  12. * be necessary to implement -r and -t, it currently only makes things bigger.
  13. * Also, exiting on a failure was a bug. All args should be processed.
  14. */
  15. //config:config TOUCH
  16. //config: bool "touch (5.9 kb)"
  17. //config: default y
  18. //config: help
  19. //config: touch is used to create or change the access and/or
  20. //config: modification timestamp of specified files.
  21. //config:
  22. //config:config FEATURE_TOUCH_SUSV3
  23. //config: bool "Add support for SUSV3 features (-a -d -m -t -r)"
  24. //config: default y
  25. //config: depends on TOUCH
  26. //config: help
  27. //config: Enable touch to use a reference file or a given date/time argument.
  28. //applet:IF_TOUCH(APPLET_NOFORK(touch, touch, BB_DIR_BIN, BB_SUID_DROP, touch))
  29. //kbuild:lib-$(CONFIG_TOUCH) += touch.o
  30. //usage:#define touch_trivial_usage
  31. //usage: "[-ch" IF_FEATURE_TOUCH_SUSV3("am") "]"
  32. //usage: IF_FEATURE_TOUCH_SUSV3(" [-d DATE] [-t DATE] [-r FILE]")
  33. //usage: " FILE..."
  34. //usage:#define touch_full_usage "\n\n"
  35. //usage: "Update mtime of FILEs\n"
  36. //usage: "\n -c Don't create files"
  37. //usage: "\n -h Don't follow links"
  38. //usage: IF_FEATURE_TOUCH_SUSV3(
  39. //usage: "\n -a Change only atime"
  40. //usage: "\n -m Change only mtime"
  41. //usage: "\n -d DT Date/time to use"
  42. //usage: "\n -t DT Date/time to use"
  43. //usage: "\n -r FILE Use FILE's date/time"
  44. //usage: )
  45. //usage:
  46. //usage:#define touch_example_usage
  47. //usage: "$ ls -l /tmp/foo\n"
  48. //usage: "/bin/ls: /tmp/foo: No such file or directory\n"
  49. //usage: "$ touch /tmp/foo\n"
  50. //usage: "$ ls -l /tmp/foo\n"
  51. //usage: "-rw-rw-r-- 1 andersen andersen 0 Apr 15 01:11 /tmp/foo\n"
  52. /* coreutils implements:
  53. * -a change only the access time
  54. * -c, --no-create
  55. * do not create any files
  56. * -d, --date=STRING
  57. * parse STRING and use it instead of current time
  58. * -f (ignored, BSD compat)
  59. * -m change only the modification time
  60. * -h, --no-dereference
  61. * -r, --reference=FILE
  62. * use this file's times instead of current time
  63. * -t STAMP
  64. * use [[CC]YY]MMDDhhmm[.ss] instead of current time
  65. * --time=WORD
  66. * change the specified time: WORD is access, atime, or use
  67. */
  68. #include "libbb.h"
  69. int touch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  70. int touch_main(int argc UNUSED_PARAM, char **argv)
  71. {
  72. int fd;
  73. int opts;
  74. smalluint status = EXIT_SUCCESS;
  75. #if ENABLE_FEATURE_TOUCH_SUSV3
  76. char *reference_file;
  77. char *date_str;
  78. /* timebuf[0] is atime, timebuf[1] is mtime */
  79. struct timespec timebuf[2];
  80. #else
  81. # define reference_file NULL
  82. # define date_str NULL
  83. # define timebuf ((struct timespec*)NULL)
  84. #endif
  85. enum {
  86. OPT_c = (1 << 0),
  87. OPT_h = (1 << 1),
  88. OPT_r = (1 << 2) * ENABLE_FEATURE_TOUCH_SUSV3,
  89. OPT_d = (1 << 3) * ENABLE_FEATURE_TOUCH_SUSV3,
  90. OPT_t = (1 << 4) * ENABLE_FEATURE_TOUCH_SUSV3,
  91. OPT_a = (1 << 5) * ENABLE_FEATURE_TOUCH_SUSV3,
  92. OPT_m = (1 << 6) * ENABLE_FEATURE_TOUCH_SUSV3,
  93. };
  94. #if ENABLE_LONG_OPTS
  95. static const char touch_longopts[] ALIGN1 =
  96. /* name, has_arg, val */
  97. "no-create\0" No_argument "c"
  98. "no-dereference\0" No_argument "h"
  99. IF_FEATURE_TOUCH_SUSV3("reference\0" Required_argument "r")
  100. IF_FEATURE_TOUCH_SUSV3("date\0" Required_argument "d")
  101. ;
  102. #endif
  103. /* -d and -t both set time. In coreutils,
  104. * accepted data format differs a bit between -d and -t.
  105. * We accept the same formats for both
  106. */
  107. opts = getopt32long(argv, "^"
  108. "ch"
  109. IF_FEATURE_TOUCH_SUSV3("r:d:t:am")
  110. /*ignored:*/ "f" IF_NOT_FEATURE_TOUCH_SUSV3("am")
  111. "\0" /* opt_complementary: */
  112. /* at least one arg: */ "-1"
  113. /* coreutils forbids -r and -t at once: */ IF_FEATURE_TOUCH_SUSV3(":r--t:t--r")
  114. /* but allows these combinations: "r--d:d--r:t--d:d--t" */
  115. , touch_longopts
  116. #if ENABLE_FEATURE_TOUCH_SUSV3
  117. , &reference_file
  118. , &date_str
  119. , &date_str
  120. #endif
  121. );
  122. #if ENABLE_FEATURE_TOUCH_SUSV3
  123. timebuf[0].tv_nsec = timebuf[1].tv_nsec = UTIME_NOW;
  124. if (opts & OPT_r) {
  125. struct stat stbuf;
  126. xstat(reference_file, &stbuf);
  127. timebuf[0].tv_sec = stbuf.st_atime;
  128. timebuf[1].tv_sec = stbuf.st_mtime;
  129. timebuf[0].tv_nsec = stbuf.st_atim.tv_nsec;
  130. timebuf[1].tv_nsec = stbuf.st_mtim.tv_nsec;
  131. }
  132. if (opts & (OPT_d|OPT_t)) {
  133. struct tm tm_time;
  134. time_t t;
  135. int check_dst;
  136. //memset(&tm_time, 0, sizeof(tm_time));
  137. /* Better than memset: makes "HH:MM" dates meaningful */
  138. time(&t);
  139. localtime_r(&t, &tm_time);
  140. check_dst = parse_datestr(date_str, &tm_time);
  141. /* Correct any day of week and day of year etc. fields */
  142. if (check_dst)
  143. tm_time.tm_isdst = -1; /* recheck dst unless date is UTC */
  144. t = validate_tm_time(date_str, &tm_time);
  145. timebuf[1].tv_sec = timebuf[0].tv_sec = t;
  146. timebuf[1].tv_nsec = timebuf[0].tv_nsec = 0;
  147. }
  148. /* If both -a and -m specified, both times should be set.
  149. * IOW: set OMIT only if one, not both, of them is given!
  150. */
  151. if ((opts & (OPT_a|OPT_m)) == OPT_a)
  152. timebuf[1].tv_nsec = UTIME_OMIT;
  153. if ((opts & (OPT_a|OPT_m)) == OPT_m)
  154. timebuf[0].tv_nsec = UTIME_OMIT;
  155. #endif
  156. argv += optind;
  157. do {
  158. int result = utimensat(AT_FDCWD, *argv, timebuf,
  159. (opts & OPT_h) ? AT_SYMLINK_NOFOLLOW : 0);
  160. if (result != 0) {
  161. if (errno == ENOENT) { /* no such file? */
  162. if (opts & OPT_c) {
  163. /* Creation is disabled, so ignore */
  164. continue;
  165. }
  166. /* Try to create the file */
  167. fd = open(*argv, O_RDWR | O_CREAT, 0666);
  168. if (fd >= 0) {
  169. if (opts & (OPT_r|OPT_d|OPT_t))
  170. futimens(fd, timebuf);
  171. xclose(fd);
  172. continue;
  173. }
  174. }
  175. status = EXIT_FAILURE;
  176. bb_simple_perror_msg(*argv);
  177. }
  178. } while (*++argv);
  179. return status;
  180. }