forked from radare/ired
-
Notifications
You must be signed in to change notification settings - Fork 0
/
io.c
75 lines (69 loc) · 1.92 KB
/
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* io.c - MIT - Copyleft 2009-2021 -- pancake */
#ifndef HAVE_FTRUNCATE
#define HAVE_FTRUNCATE 1
#endif
#define DEMO 0
#if DEMO
int bo = 0;
char b[4096];
static inline int io_open(const char *file) {
memset (b, 0, sizeof (b));
return 10;
}
static int io_read(void *x,int y) {
memcpy (x, b+bo, y);
return y;
}
#define io_write(x,y) memcpy (b+bo, x,y)
static int io_seek (int x,int y) {
bo=((y==2)?sizeof(b):(y==1)?bo+x:x);
return bo;
}
#define io_close() printf("close: TODO\n")
#define io_system(x) system(x)
#define io_truncate(x) printf("truncate: TODO\n");
#else
#if __WIN32__
#include <windows.h>
static HANDLE _fd = NULL;
static inline int io_open(const char *file) {
_fd = CreateFile(file, GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ|FILE_SHARE_WRITE,
NULL, OPEN_ALWAYS, 0, NULL);
if(_fd == INVALID_HANDLE_VALUE)
_fd = CreateFile(file, GENERIC_READ, FILE_SHARE_READ,
NULL, OPEN_ALWAYS, 0, NULL);
return (_fd==INVALID_HANDLE_VALUE)?-1:0;
}
static inline int io_read(void *x, int y) {
DWORD ret;
return ReadFile(_fd, x, y, &ret, NULL)?ret:-1;
}
#define setenv(x,y,z) SetEnvironmentVariable(x, y)
#define io_write(x,y) WriteFile(_fd, x, y, NULL, NULL)
#define io_seek(x,y) SetFilePointer(_fd,x,0,!y?FILE_BEGIN:y==1?FILE_CURRENT:FILE_END)
#define io_close() CloseHandle(_fd)
#define io_system(x) system(x)
#define io_truncate(x) 0; { io_seek(x,SEEK_SET); SetEndOfFile(_fd); }
#else
#define _FILE_OFFSET_BITS 64
#define _GNU_SOURCE
#include <fcntl.h>
static int _fd = -1;
static inline int io_open(const char *file) {
_fd = open(file, O_RDWR|O_CREAT, 0644);
if(_fd == -1) _fd = open(file, O_RDONLY);
return _fd;
}
#define io_read(x,y) read(_fd, x, y)
#define io_write(x,y) write(_fd, x, y)
#define io_seek(x,y) lseek(_fd, x, y)
#define io_close() close(_fd)
#define io_system(x) system(x)
#if HAVE_FTRUNCATE
#define io_truncate(x) ftruncate(_fd, (off_t)x)
#else
#define io_truncate(x) printf("truncate: TODO\n");
#endif
#endif
#endif