~ubuntu-branches/ubuntu/lucid/9base/lucid

« back to all changes in this revision

Viewing changes to touch/touch.c

  • Committer: Bazaar Package Importer
  • Author(s): Daniel Baumann
  • Date: 2006-01-25 15:33:00 UTC
  • Revision ID: james.westby@ubuntu.com-20060125153300-6hh4p9wx8iqqply5
Tags: upstream-2
ImportĀ upstreamĀ versionĀ 2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
int
 
42
touch(int nocreate, char *name)
 
43
{
 
44
        Dir stbuff;
 
45
        int fd;
 
46
 
 
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
}