-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbabyfile.c
78 lines (62 loc) · 1.15 KB
/
babyfile.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
static int menu(void);
static int getnline(char *buf, int size);
static int getint(void);
#define write_str(s) write(STDOUT_FILENO, s, sizeof(s)-1)
int main(void){
FILE *fp;
alarm(30);
write_str("Play with FILE structure\n");
if(!(fp = fopen("/dev/null", "r"))){
write_str("Open error");
return -1;
}
fp->_wide_data = NULL;
for(;;){
switch(menu()){
case 0:
goto END;
case 1:
fflush(fp);
break;
case 2:
{
unsigned char ofs;
write_str("offset: ");
if((ofs = getint()) & 0x80)
ofs |= 0x40;
write_str("value: ");
((char*)fp)[ofs] = getint();
}
break;
}
write_str("Done.\n");
}
END:
write_str("Bye!");
_exit(0);
}
static int menu(void){
write_str("\nMENU\n"
"1. Flush\n"
"2. Trick\n"
"0. Exit\n"
"> ");
return getint();
}
static int getnline(char *buf, int size){
int len;
if(size <= 0 || (len = read(STDIN_FILENO, buf, size-1)) <= 0)
return -1;
if(buf[len-1]=='\n')
len--;
buf[len] = '\0';
return len;
}
static int getint(void){
char buf[0x10] = {};
getnline(buf, sizeof(buf));
return atoi(buf);
}