BSD 3 development
[unix-history] / usr / src / cmd / touch.c
CommitLineData
9c79dca0
BJ
1#include <stdio.h>
2
3
4main(argc,argv)
5int argc;
6char *argv[];
7{
8int i;
9static int force = 1;
10
11for(i = 1 ; i < argc ; ++i)
12 if( strcmp(argv[i], "-c") )
13 touch(force, argv[i]);
14 else
15 force = 0;
16}
17
18
19
20
21#include <sys/types.h>
22#include <sys/stat.h>
23
24
25touch(force, name)
26int force;
27char *name;
28{
29struct stat stbuff;
30char junk[1];
31int fd;
32
33if( stat(name,&stbuff) < 0)
34 if(force)
35 goto create;
36 else
37 {
38 fprintf(stderr, "touch: file %s does not exist.\n", name);
39 return;
40 }
41
42if(stbuff.st_size == 0)
43 goto create;
44
45if( (fd = open(name, 2)) < 0)
46 goto bad;
47
48if( read(fd, junk, 1) < 1)
49 {
50 close(fd);
51 goto bad;
52 }
53lseek(fd, 0L, 0);
54if( write(fd, junk, 1) < 1 )
55 {
56 close(fd);
57 goto bad;
58 }
59close(fd);
60return;
61
62bad:
63 fprintf(stderr, "Cannot touch %s\n", name);
64 return;
65
66create:
67 if( (fd = creat(name, 0666)) < 0)
68 goto bad;
69 close(fd);
70}