-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstdio.h
191 lines (154 loc) · 3.51 KB
/
stdio.h
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//stdio.h
//
#include "minicrt.h"
int mini_crt_io_init()
{
return 1;
}
#ifdef WIN32
#include <Windows.h>
FILE* fopen(const char* filename, const char* mode)
{
HANDLE hFile = 0;
int access = 0;
int creation = 0;
if(strcmp(mode, "w") == 0){
access |= GENERIC_WRITE;
creation |=CREATE_ALWAYS;
}
if(strcmp(mode, "w+") == 0){
access |= GENERIC_WRITE | GENERIC_READ;
creation |= CREATE_ALWAYS;
}
if(strcmp(mode, "r") == 0){
access |= GENERIC_READ;
creation += OPEN_EXISTING;
}
if(strcmp(mode, "r+") == 0){
access |= GENERIC_WRITE | GENERIC_READ;
creation |= TRUNCATE_EXISTING;
}
hFile = CreateFileA(filename, access, 0, 0, creation, 0, 0);
if(hFile == INVALID_HANDLE_VALUE)
return 0;i
return (FILE*)hFile;
}
int fread(const void* buffer, int size, int conut, FILE* stream)
{
int read = 0;
if(!ReadFile((HANDLE)stream, buffer, size * count, &read, 0))
return 0;
return read;
}
int fwrite(const void* buffer, int size, int conut, FILE* stream)
{
int written = 0;
if(!WriteFile((HANDLE)stream, buffer, size * count, &written, 0))
return 0;
return written;
}
inf fclose(FILE* fp)
{
return CloseHandle((HANDLE)fp);
}
int fseek(FILE* fp, int offset, int set)
{
return SetFilePointer((HANDLE)fp, offset, 0, set);
}
#else //in Linux
static int open(const char *pathname, int flags, int mode)
{
int fd = 0;
asm("movl $5,%%eax \n\t"
"movl %1,%%ebx \n\t"
"movl %2,%%ecx \n\t"
"movl %3,%%edx \n\t"
"int $0x80 \n\t"
"movl %%eax,%0 \n\t":
"=m"(fd):"m"(pathname),"m"(falgs),"m"(mode));
}
static int read(int fd, void* buffer, unsigned size) //std_call
{
int ret = 0;
asm("movl $3,%%eax \n\t" //__NR_read__ = 3
"movl %1,%%ebx \n\t"
"movl %2,%%ecx \n\t"
"movl %3,%%edx \n\t"
"int $0x80 \n\t"
"movl %%eax,%0 \n\t":
"=m"(ret):"m"(fd),"m"(buffer),"m"(size));
return ret;
}
static int write(int fd, void* buffer, unsigned size)
{
int ret = 0;
asm("movl $4,%%eax \n\t"
"movl %1,%%ebx \n\t"
"movl %2,%%ecx \n\t"
"movl %3,%%edx \n\t"
"int $0x80 \n\t"
"movl %%eax,%0 \n\t":
"=m"(ret):"m"(fd),"m"(buffer),"m"(size));
return ret;
}
static int close(int fd, void* buffer, unsigned size)
{
int ret = 0;
asm("movl $6,%%eax \n\t"
"movl %1,%%ebx \n\t"
"int $0x80 \n\t"
"movl %%eax,%0 \n\t":
"=m"(ret):"m"(fd));
return ret;
}
static int seek(int fd, int offset, int mode)
{
int ret = 0;
asm("movl $19,%%eax \n\t"
"movl %1,%%ebx \n\t"
"movl %2,%%ecx \n\t"
"movl %3,%%ecx \n\t"
"int $0x80 \n\t"
"movl %%eax,%0 \n\t":
"=m"(ret):"m"(fd),"m"(offset),"m"(mode) );
return ret;
}
FILE* fopen(const char* filename, const char* mode)
{
int fd = -1;
int flags = 0;
int access = 00700;
#define O_RDONLY 00
#define O_WRONLY 01
#define O_RDWR 02
#define O_CREATE 0100
#define O_TRUNC 01000
#define O_APPEND 02000
if(strcmp(mode, "w") == 0)
flags |= O_WRONLTY | O_CREAT | O_TRUNC;
if(strcmp(mode, "w+") == 0)
flags |= O_RDWR | O_CREAT | O_TRUNC;
if(strcmp(mode, "r") == 0)
flags |= RDONLY;
if(strcmp(mode, "r+") == 0)
flags |= O_RDWR | O_CREAT;
fd = open(filename, flags, access);
return (FILE*)fd;
}
int fread(void* buffer, int size, int count, FILE* stream)
{
return read((int)stream, buffer, size * count);
}
int fwrite(const void* buffer, int size, int count, FILE* stream)
{
return write((int)stream, buffer, size * count);
}
int fclose(FILE* fp)
{
return close((int)fp);
}
int fseek(FILE* fp, int offset, int set)
{
return seek((int)fp, offset, set);
}
#endif