touch.c 1.5 KB

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