touch.c 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /*
  2. * This file is part of the UCB release of Plan 9. It is subject to the license
  3. * terms in the LICENSE file found in the top-level directory of this
  4. * distribution and at http://akaros.cs.berkeley.edu/files/Plan9License. No
  5. * part of the UCB release of Plan 9, including this file, may be copied,
  6. * modified, propagated, or distributed except according to the terms contained
  7. * in the LICENSE file.
  8. */
  9. #include <u.h>
  10. #include <libc.h>
  11. int touch(int, char *);
  12. uint32_t now;
  13. void
  14. usage(void)
  15. {
  16. fprint(2, "usage: touch [-c] [-t time] files\n");
  17. exits("usage");
  18. }
  19. void
  20. main(int argc, char **argv)
  21. {
  22. char *t, *s;
  23. int nocreate = 0;
  24. int status = 0;
  25. now = time(0);
  26. ARGBEGIN{
  27. case 't':
  28. t = EARGF(usage());
  29. now = strtoul(t, &s, 0);
  30. if(s == t || *s != '\0')
  31. usage();
  32. break;
  33. case 'c':
  34. nocreate = 1;
  35. break;
  36. default:
  37. usage();
  38. }ARGEND
  39. if(!*argv)
  40. usage();
  41. while(*argv)
  42. status += touch(nocreate, *argv++);
  43. if(status)
  44. exits("touch");
  45. exits(0);
  46. }
  47. int
  48. touch(int nocreate, char *name)
  49. {
  50. Dir stbuff;
  51. int fd;
  52. nulldir(&stbuff);
  53. stbuff.mtime = now;
  54. if(dirwstat(name, &stbuff) >= 0)
  55. return 0;
  56. if(nocreate){
  57. fprint(2, "touch: %s: cannot wstat: %r\n", name);
  58. return 1;
  59. }
  60. if((fd = create(name, OREAD|OEXCL, 0666)) < 0){
  61. fprint(2, "touch: %s: cannot create: %r\n", name);
  62. return 1;
  63. }
  64. dirfwstat(fd, &stbuff);
  65. close(fd);
  66. return 0;
  67. }