Blob


1 #include <u.h>
2 #include <libc.h>
4 int touch(int, char *);
5 ulong now;
7 void
8 usage(void)
9 {
10 fprint(2, "usage: touch [-c] [-t time] files\n");
11 exits("usage");
12 }
14 void
15 main(int argc, char **argv)
16 {
17 int nocreate = 0;
18 int status = 0;
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
32 if(!*argv)
33 usage();
34 while(*argv)
35 status += touch(nocreate, *argv++);
36 if(status)
37 exits("touch");
38 exits(0);
39 }
41 int
42 touch(int nocreate, char *name)
43 {
44 Dir stbuff;
45 int fd;
47 nulldir(&stbuff);
48 stbuff.mtime = now;
49 if(dirwstat(name, &stbuff) >= 0)
50 return 0;
51 if(nocreate){
52 fprint(2, "touch: %s: cannot wstat: %r\n", name);
53 return 1;
54 }
55 if ((fd = create(name, OREAD, 0666)) < 0) {
56 fprint(2, "touch: %s: cannot create: %r\n", name);
57 return 1;
58 }
59 dirfwstat(fd, &stbuff);
60 close(fd);
61 return 0;
62 }