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 char *t, *s; 18 int nocreate = 0; 19 int status = 0; 20 21 now = time(0); 22 ARGBEGIN{ 23 case 't': 24 t = EARGF(usage()); 25 now = strtoul(t, &s, 0); 26 if(s == t || *s != '\0') 27 usage(); 28 break; 29 case 'c': 30 nocreate = 1; 31 break; 32 default: 33 usage(); 34 }ARGEND 35 36 if(!*argv) 37 usage(); 38 while(*argv) 39 status += touch(nocreate, *argv++); 40 if(status) 41 exits("touch"); 42 exits(0); 43 } 44 45 touch(int nocreate, char *name) 46 { 47 Dir stbuff; 48 int fd; 49 50 nulldir(&stbuff); 51 stbuff.mtime = now; 52 if(dirwstat(name, &stbuff) >= 0) 53 return 0; 54 if(nocreate){ 55 fprint(2, "touch: %s: cannot wstat: %r\n", name); 56 return 1; 57 } 58 if((fd = create(name, OREAD|OEXCL, 0666)) < 0){ 59 fprint(2, "touch: %s: cannot create: %r\n", name); 60 return 1; 61 } 62 dirfwstat(fd, &stbuff); 63 close(fd); 64 return 0; 65 } 66