1 #include <u.h> 2 #include <libc.h> 3 4 int touch(int, char *); 5 6 void 7 main(int argc, char **argv) 8 { 9 int force = 1; 10 int status = 0; 11 12 ARGBEGIN{ 13 case 'c': force = 0; break; 14 default: goto usage; break; 15 }ARGEND 16 if(!*argv){ 17 usage: 18 fprint(2, "usage: touch [-c] files\n"); 19 exits("usage"); 20 } 21 while(*argv) 22 status += touch(force, *argv++); 23 if(status) 24 exits("touch"); 25 exits(0); 26 } 27 28 touch(int force, char *name) 29 { 30 Dir stbuff; 31 char junk[1]; 32 int fd; 33 34 stbuff.length = 0; 35 stbuff.mode = 0666; 36 if (dirstat(name, &stbuff) < 0 && force == 0) { 37 fprint(2, "touch: %s: cannot stat: %r\n", name); 38 return 1; 39 } 40 if (stbuff.length == 0) { 41 if ((fd = create(name, 0, stbuff.mode)) < 0) { 42 fprint(2, "touch: %s: cannot create: %r\n", name); 43 return 1; 44 } 45 close(fd); 46 return 0; 47 } 48 if ((fd = open(name, ORDWR)) < 0) { 49 fprint(2, "touch: %s: cannot open: %r\n", name); 50 return 1; 51 } 52 if(read(fd, junk, 1) < 1) { 53 fprint(2, "touch: %s: read error: %r\n", name); 54 close(fd); 55 return 1; 56 } 57 seek(fd, 0L, 0); 58 if(write(fd, junk, 1) < 1 ) { 59 fprint(2, "touch: %s: write error: %r\n", name); 60 close(fd); 61 return 1; 62 } 63 close(fd); 64 return 0; 65 } 66