-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathseek_io.c
53 lines (52 loc) · 2.14 KB
/
seek_io.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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
int fd;
void main (int argc, char* argv[]) {
int fd, num;
int mode, len, offset;
char* buff = NULL;
char buf[20];
printf("file provided : %s\n", argv[1]);
fd = open(argv[1], O_RDWR, 0777);
for(int i = 2; i < argc; i++) {
switch(argv[i][0]){
case 'w':
//mode = O_WRONLY;
//fd = open(argv[1], mode, 0777);
num = write(fd, argv[i]+1, strlen(argv[i]));
if(num != -1) {
printf("write : %s\n", argv[i]+1);
} else {
printf("write error %d\n", num);
}
//close(fd);
break;
case 'r':
//mode = O_RDONLY;
len = atoi(argv[i]+1);
buff = malloc(len);
//fd = open(argv[1], mode, 0777);
num = read(fd, buff, len);
if(num != -1) {
printf("read %d: %s\n", num, buff);
} else {
printf("read error %d\n", num);
}
free(buff);
//close(fd);
break;
case 's':
//mode = O_RDWR;
//fd = open(argv[1], mode, 0777);
offset = atoi(argv[i]+1);
lseek(fd,offset, SEEK_SET);
printf("seek set: %d\n", offset);
default :
mode = -1;
break;
}
}
}