-
Notifications
You must be signed in to change notification settings - Fork 0
/
lseek_test.c
83 lines (70 loc) · 2 KB
/
lseek_test.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
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <fcntl.h>
int writeFile(int fd, char *str){
size_t nbytes;
nbytes = strlen(str);
return write(fd, str, nbytes);
}
int lseekRead(int fd, off_t offset, size_t nbytes, char *buf){
if(lseek(fd, offset, SEEK_SET) == -1){
return -2;
}
return read(fd, buf, nbytes);
}
int lseekUpdate(int fd, off_t offset, size_t nbytes, char *nbuf){
if(lseek(fd, offset, SEEK_SET) == -1){
return -2;
}
return write(fd, nbuf, nbytes);
}
int main(void){
off_t offset;
int fd, n;
mode_t mode;
char str[32] = "SouthParkIsSoBeautiful!";
char nbuf[10] = "Funny!!!";
char buf[10];
mode = S_IRUSR|S_IWUSR|S_IXUSR|S_IRGRP|S_IWGRP|S_IXGRP|S_IROTH|S_IWOTH|S_IXOTH;
/*open and write a new file*/
if((fd = open("exercise3_6.txt", O_RDWR|O_CREAT|O_TRUNC, mode)) == -1){
return -1;
}
if(writeFile(fd, str) == -1){
return -1;
}
close(fd);
offset = 13; //set place after "so"
/*check lseek before Read*/
/*open the existed file with O_APPEND to test lseekRead*/
if((fd = open("exercise3_6.txt", O_RDWR|O_CREAT|O_APPEND, mode)) == -1){
return -1;
}
if((n = lseekRead(fd, offset, 6, buf)) == -2){
printf("lseek cannot do anything after open with O_APPEND!\n");
}
else if(n == -1){
printf("inner read error!\n");
}
else{
printf("check the read string: %s\n", buf);
}
close(fd);
/*open the existed file with O_APPEND to test lseekUpdate*/
if((fd = open("exercise3_6.txt", O_RDWR|O_CREAT|O_APPEND, mode)) == -1){
return -1;
}
/*check can lseek can update or not*/
if((n = lseekUpdate(fd, offset, strlen(nbuf), nbuf)) == -2){
printf("lseek cannot be used to update file at anywhere!\n");
}
else if(n == -1){
printf("inner write error!\n");
}
else{
printf("check the updated string: %s\n", nbuf);
}
close(fd);
return 0;
}