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