touch.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  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 tarball for details.
  8. */
  9. /* BB_AUDIT SUSv3 _NOT_ compliant -- options -a, -m, -r, -t not supported. */
  10. /* http://www.opengroup.org/onlinepubs/007904975/utilities/touch.html */
  11. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  12. *
  13. * Previous version called open() and then utime(). While this will be
  14. * be necessary to implement -r and -t, it currently only makes things bigger.
  15. * Also, exiting on a failure was a bug. All args should be processed.
  16. */
  17. #include "libbb.h"
  18. /* This is a NOFORK applet. Be very careful! */
  19. /* coreutils implements:
  20. * -a change only the access time
  21. * -c, --no-create
  22. * do not create any files
  23. * -d, --date=STRING
  24. * parse STRING and use it instead of current time
  25. * -f (ignored, BSD compat)
  26. * -m change only the modification time
  27. * -r, --reference=FILE
  28. * use this file's times instead of current time
  29. * -t STAMP
  30. * use [[CC]YY]MMDDhhmm[.ss] instead of current time
  31. * --time=WORD
  32. * change the specified time: WORD is access, atime, or use
  33. */
  34. int touch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  35. int touch_main(int argc UNUSED_PARAM, char **argv)
  36. {
  37. int fd;
  38. int status = EXIT_SUCCESS;
  39. int opts;
  40. #if ENABLE_DESKTOP
  41. # if ENABLE_LONG_OPTS
  42. static const char touch_longopts[] ALIGN1 =
  43. /* name, has_arg, val */
  44. "no-create\0" No_argument "c"
  45. "reference\0" Required_argument "r"
  46. "date\0" Required_argument "d"
  47. ;
  48. # endif
  49. char *reference_file = NULL;
  50. char *date_str = NULL;
  51. struct timeval timebuf[2];
  52. timebuf[1].tv_usec = timebuf[0].tv_usec = 0;
  53. #else
  54. # define reference_file NULL
  55. # define date_str NULL
  56. # define timebuf ((struct timeval*)NULL)
  57. #endif
  58. #if ENABLE_DESKTOP && ENABLE_LONG_OPTS
  59. applet_long_options = touch_longopts;
  60. #endif
  61. /* -d and -t both set time. In coreutils,
  62. * accepted data format differs a bit between -d and -t.
  63. * We accept the same formats for both */
  64. opts = getopt32(argv, "c" IF_DESKTOP("r:d:t:")
  65. /*ignored:*/ "fma"
  66. IF_DESKTOP(, &reference_file)
  67. IF_DESKTOP(, &date_str)
  68. IF_DESKTOP(, &date_str)
  69. );
  70. opts &= 1; /* only -c bit is left */
  71. argv += optind;
  72. if (!*argv) {
  73. bb_show_usage();
  74. }
  75. if (reference_file) {
  76. struct stat stbuf;
  77. xstat(reference_file, &stbuf);
  78. timebuf[1].tv_sec = timebuf[0].tv_sec = stbuf.st_mtime;
  79. }
  80. if (date_str) {
  81. struct tm tm_time;
  82. time_t t;
  83. //time(&t);
  84. //localtime_r(&t, &tm_time);
  85. memset(&tm_time, 0, sizeof(tm_time));
  86. parse_datestr(date_str, &tm_time);
  87. /* Correct any day of week and day of year etc. fields */
  88. tm_time.tm_isdst = -1; /* Be sure to recheck dst */
  89. t = validate_tm_time(date_str, &tm_time);
  90. timebuf[1].tv_sec = timebuf[0].tv_sec = t;
  91. }
  92. do {
  93. if (utimes(*argv, (reference_file || date_str) ? timebuf : NULL) != 0) {
  94. if (errno == ENOENT) { /* no such file */
  95. if (opts) { /* creation is disabled, so ignore */
  96. continue;
  97. }
  98. /* Try to create the file */
  99. fd = open(*argv, O_RDWR | O_CREAT, 0666);
  100. if (fd >= 0) {
  101. xclose(fd);
  102. if (reference_file || date_str)
  103. utimes(*argv, timebuf);
  104. continue;
  105. }
  106. }
  107. status = EXIT_FAILURE;
  108. bb_simple_perror_msg(*argv);
  109. }
  110. } while (*++argv);
  111. return status;
  112. }