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