touch.c 1.4 KB

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