touch.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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. int touch_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
  20. int touch_main(int argc ATTRIBUTE_UNUSED, char **argv)
  21. {
  22. int fd;
  23. int status = EXIT_SUCCESS;
  24. int flags = getopt32(argv, "cf");
  25. flags &= 1; /* ignoring -f (BSD compat thingy) */
  26. argv += optind;
  27. if (!*argv) {
  28. bb_show_usage();
  29. }
  30. do {
  31. if (utime(*argv, NULL)) {
  32. if (errno == ENOENT) { /* no such file */
  33. if (flags) { /* Creation is disabled, so ignore. */
  34. continue;
  35. }
  36. /* Try to create the file. */
  37. fd = open(*argv, O_RDWR | O_CREAT,
  38. S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
  39. );
  40. if ((fd >= 0) && !close(fd)) {
  41. continue;
  42. }
  43. }
  44. status = EXIT_FAILURE;
  45. bb_simple_perror_msg(*argv);
  46. }
  47. } while (*++argv);
  48. return status;
  49. }