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