-
Notifications
You must be signed in to change notification settings - Fork 5
/
compat.h
99 lines (72 loc) · 1.65 KB
/
compat.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
#ifndef __COMPAT_H__
#define __COMPAT_H__
#include <stdlib.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/stat.h>
#ifdef __cplusplus
extern "C" {
#endif
typedef struct _file_stat_t
{
uint64_t file_size;
uint64_t mtime;
uint64_t ctime;
uint64_t atime;
uint32_t mode;
} file_stat_t;
#ifdef WIN32
#include <windows.h>
#include <winsock.h>
// Threads
typedef HANDLE thread_t;
// Files
#define INVALID_FD INVALID_HANDLE_VALUE
#define FD_OK(fd) (fd != INVALID_HANDLE_VALUE)
typedef HANDLE file_t;
// Sockets
typedef int socklen_t;
#ifndef SHUT_RD
#define SHUT_RD SD_RECEIVE
#endif
#ifndef SHUT_WR
#define SHUT_WR SD_SEND
#endif
#ifndef SHUT_RDWR
#define SHUT_RDWR SD_BOTH
#endif
#ifndef MSG_WAITALL
#define MSG_WAITALL 0x8
#endif
#define get_network_error() WSAGetLastError()
#else
#include <sys/errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#include <arpa/inet.h>
#include <pthread.h>
// Threads
typedef pthread_t thread_t;
// Files
#define INVALID_FD -1
#define FD_OK(fd) (fd >= 0)
typedef int file_t;
// Sockets
#define closesocket close
#define get_network_error() (errno)
#endif
int create_start_thread(thread_t *thread, void *(*start_routine)(void*), void *arg);
int join_thread(thread_t thread);
file_t open_file(const char *path, int oflag);
int close_file(file_t fd);
ssize_t read_file(file_t fd, void *buf, size_t nbyte);
ssize_t write_file(file_t fd, void *buf, size_t nbyte);
int64_t seek_file(file_t fd, int64_t offset, int whence);
int fstat_file(file_t fd, file_stat_t *fs);
int stat_file(const char *path, file_stat_t *fs);
#ifdef __cplusplus
}
#endif
#endif /* __COMPAT_H__ */