touch.c 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15. * General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  20. *
  21. */
  22. /* BB_AUDIT SUSv3 _NOT_ compliant -- options -a, -m, -r, -t not supported. */
  23. /* http://www.opengroup.org/onlinepubs/007904975/utilities/touch.html */
  24. /* Mar 16, 2003 Manuel Novoa III (mjn3@codepoet.org)
  25. *
  26. * Previous version called open() and then utime(). While this will be
  27. * be necessary to implement -r and -t, it currently only makes things bigger.
  28. * Also, exiting on a failure was a bug. All args should be processed.
  29. */
  30. #include <stdio.h>
  31. #include <sys/types.h>
  32. #include <fcntl.h>
  33. #include <utime.h>
  34. #include <errno.h>
  35. #include <unistd.h>
  36. #include <stdlib.h>
  37. #include "busybox.h"
  38. int touch_main(int argc, char **argv)
  39. {
  40. int fd;
  41. int flags;
  42. int status = EXIT_SUCCESS;
  43. flags = bb_getopt_ulflags(argc, argv, "c");
  44. argv += optind;
  45. if (!*argv) {
  46. bb_show_usage();
  47. }
  48. do {
  49. if (utime(*argv, NULL)) {
  50. if (errno == ENOENT) { /* no such file*/
  51. if (flags & 1) { /* Creation is disabled, so ignore. */
  52. continue;
  53. }
  54. /* Try to create the file. */
  55. fd = open(*argv, O_RDWR | O_CREAT,
  56. S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH
  57. );
  58. if ((fd >= 0) && !close(fd)) {
  59. continue;
  60. }
  61. }
  62. status = EXIT_FAILURE;
  63. bb_perror_msg("%s", *argv);
  64. }
  65. } while (*++argv);
  66. return status;
  67. }