touch.c 1.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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. touch(int nocreate, char *name)
  48. {
  49. Dir stbuff;
  50. int fd;
  51. nulldir(&stbuff);
  52. stbuff.mtime = now;
  53. if(dirwstat(name, &stbuff) >= 0)
  54. return 0;
  55. if(nocreate){
  56. fprint(2, "touch: %s: cannot wstat: %r\n", name);
  57. return 1;
  58. }
  59. if((fd = create(name, OREAD|OEXCL, 0666)) < 0){
  60. fprint(2, "touch: %s: cannot create: %r\n", name);
  61. return 1;
  62. }
  63. dirfwstat(fd, &stbuff);
  64. close(fd);
  65. return 0;
  66. }