touch.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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. #if ENABLE_DESKTOP
  38. # if ENABLE_LONG_OPTS
  39. static const char touch_longopts[] ALIGN1 =
  40. /* name, has_arg, val */
  41. "no-create\0" No_argument "c"
  42. "reference\0" Required_argument "r"
  43. "date\0" Required_argument "d"
  44. ;
  45. # endif
  46. struct utimbuf timebuf;
  47. char *reference_file = NULL;
  48. char *date_str = NULL;
  49. #else
  50. # define reference_file NULL
  51. # define date_str NULL
  52. # define timebuf (*(struct utimbuf*)NULL)
  53. #endif
  54. int fd;
  55. int status = EXIT_SUCCESS;
  56. int opts;
  57. #if ENABLE_DESKTOP && ENABLE_LONG_OPTS
  58. applet_long_options = touch_longopts;
  59. #endif
  60. /* -d and -t both set time. In coreutils,
  61. * accepted data format differs a bit between -d and -t.
  62. * We accept the same formats for both */
  63. opts = getopt32(argv, "c" IF_DESKTOP("r:d:t:")
  64. /*ignored:*/ "fma"
  65. IF_DESKTOP(, &reference_file)
  66. IF_DESKTOP(, &date_str)
  67. IF_DESKTOP(, &date_str)
  68. );
  69. opts &= 1; /* only -c bit is left */
  70. argv += optind;
  71. if (!*argv) {
  72. bb_show_usage();
  73. }
  74. if (reference_file) {
  75. struct stat stbuf;
  76. xstat(reference_file, &stbuf);
  77. timebuf.actime = stbuf.st_atime;
  78. timebuf.modtime = 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.actime = t;
  91. timebuf.modtime = t;
  92. }
  93. do {
  94. if (utime(*argv, reference_file ? &timebuf : NULL)) {
  95. if (errno == ENOENT) { /* no such file */
  96. if (opts) { /* creation is disabled, so ignore */
  97. continue;
  98. }
  99. /* Try to create the file. */
  100. fd = open(*argv, O_RDWR | O_CREAT,
  101. S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
  102. );
  103. if ((fd >= 0) && !close(fd)) {
  104. if (reference_file)
  105. utime(*argv, &timebuf);
  106. continue;
  107. }
  108. }
  109. status = EXIT_FAILURE;
  110. bb_simple_perror_msg(*argv);
  111. }
  112. } while (*++argv);
  113. return status;
  114. }