1 #include <u.h> 2 #include <libc.h> 3 4 int touch(int, char *); 5 ulong now; 6 7 void 8 usage(void) 9 { 10 fprint(2, "usage: touch [-c] [-t time] files\n"); 11 exits("usage"); 12 } 13 14 void 15 main(int argc, char **argv) 16 { 17 int nocreate = 0; 18 int status = 0; 19 20 now = time(0); 21 ARGBEGIN{ 22 case 't': 23 now = strtoul(EARGF(usage()), 0, 0); 24 break; 25 case 'c': 26 nocreate = 1; 27 break; 28 default: 29 usage(); 30 }ARGEND 31 32 if(!*argv) 33 usage(); 34 while(*argv) 35 status += touch(nocreate, *argv++); 36 if(status) 37 exits("touch"); 38 exits(0); 39 } 40 41 touch(int nocreate, char *name) 42 { 43 Dir stbuff; 44 int fd; 45 46 nulldir(&stbuff); 47 stbuff.mtime = now; 48 if(dirwstat(name, &stbuff) >= 0) 49 return 0; 50 if(nocreate){ 51 fprint(2, "touch: %s: cannot wstat: %r\n", name); 52 return 1; 53 } 54 if ((fd = create(name, OREAD|OEXCL, 0666)) < 0) { 55 fprint(2, "touch: %s: cannot create: %r\n", name); 56 return 1; 57 } 58 dirfwstat(fd, &stbuff); 59 close(fd); 60 return 0; 61 } 62