-
Notifications
You must be signed in to change notification settings - Fork 5
/
compat.h
197 lines (157 loc) · 6.84 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
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
192
193
194
195
196
197
/*
This file contains some kit-wide typedefs and structs that basically
emulate most of a normal posix-y type system. The purpose of hiding
everything behind these typedefs is to avoid inconsistencies between
various systems (such as the difference in size between off_t on BeOS
and some versions of Unix). To further avoid complications I've also
hidden the stat and dirent structs since those vary even more widely.
THIS CODE COPYRIGHT DOMINIC GIAMPAOLO. NO WARRANTY IS EXPRESSED
OR IMPLIED. YOU MAY USE THIS CODE AND FREELY DISTRIBUTE IT FOR
NON-COMMERCIAL USE AS LONG AS THIS NOTICE REMAINS ATTACHED.
FOR COMMERCIAL USE, CONTACT DOMINIC GIAMPAOLO (dbg@be.com).
Dominic Giampaolo
dbg@be.com
*/
#ifndef _COMPAT_H
#define _COMPAT_H
#include <stdlib.h>
#include <stdarg.h>
#include <stdint.h>
#include <errno.h>
#include <memory.h>
#include <string.h>
#include <fcntl.h>
#include <time.h>
#include <sys/uio.h>
/*
By default (for portability reasons) the size of off_t's and ino_t's
is 32-bit. You can change the file system to be 64-bit if you want
by defining OFF_T_SIZE to be 8.
NOTE: if you change the size of OFF_T_SIZE to be 8 you will have to
go through the code and change any calls to printf() to use the
appropriate format for 64-bit integers on your OS. I have seen
4 different formats now: %Ld (BeOS and Linux), %qd (FreeBSD),
%lld (Irix) and %I64d (NT).
*/
#define OFF_T_SIZE (__SIZEOF_POINTER__)
#if OFF_T_SIZE == 4
typedef int32_t fs_off_t;
typedef int32_t my_ino_t;
#elif OFF_T_SIZE == 8
typedef int64_t fs_off_t;
typedef int64_t my_ino_t;
#else
#error OFF_T_SIZE must be either 4 or 8.
#endif
typedef int my_dev_t;
typedef int my_mode_t;
typedef int my_uid_t;
typedef int my_gid_t;
/* This is the maximum length of a file name. Adjust it as you see fit */
#define FILE_NAME_LENGTH 256
/* This is maximum name size for naming a volume or semaphore/lock */
#define IDENT_NAME_LENGTH 32
typedef struct my_dirent {
my_dev_t d_dev;
my_ino_t d_ino;
unsigned short d_reclen;
char d_name[1];
} my_dirent_t;
typedef struct {
int fd;
struct my_dirent ent;
} MY_DIR;
/*
This is a pretty regular stat structure but it's our "internal"
version since if we depended on the host version we'd be exposed
to all sorts of nasty things (different sized ino_t's, etc).
We also can't use the normal naming style of "st_" for each field
name because on some systems fields like st_atime are really just
define's that expand to all sorts of weird stuff.
*/
struct my_stat {
my_dev_t dev; /* "device" that this file resides on */
my_ino_t ino; /* this file's inode #, unique per device */
my_mode_t mode; /* mode bits (rwx for user, group, etc) */
int nlink; /* number of hard links to this file */
my_uid_t uid; /* user id of the owner of this file */
my_gid_t gid; /* group id of the owner of this file */
fs_off_t size; /* size in bytes of this file */
size_t blksize; /* preferred block size for i/o */
time_t atime; /* last access time */
time_t mtime; /* last modification time */
time_t ctime; /* last change time, not creation time */
time_t crtime; /* creation time; not posix but useful */
};
#define MY_S_IFMT 00000170000 /* type of file */
#define MY_S_IFLNK 00000120000 /* symbolic link */
#define MY_S_IFREG 00000100000 /* regular */
#define MY_S_IFBLK 00000060000 /* block special */
#define MY_S_IFDIR 00000040000 /* directory */
#define MY_S_IFCHR 00000020000 /* character special */
#define MY_S_IFIFO 00000010000 /* fifo */
#define MY_S_ISREG(m) (((m) & MY_S_IFMT) == MY_S_IFREG)
#define MY_S_ISLNK(m) (((m) & MY_S_IFMT) == MY_S_IFLNK)
#define MY_S_ISBLK(m) (((m) & MY_S_IFMT) == MY_S_IFBLK)
#define MY_S_ISDIR(m) (((m) & MY_S_IFMT) == MY_S_IFDIR)
#define MY_S_ISCHR(m) (((m) & MY_S_IFMT) == MY_S_IFCHR)
#define MY_S_ISFIFO(m) (((m) & MY_S_IFMT) == MY_S_IFIFO)
#define MY_S_IUMSK 07777 /* user settable bits */
#define MY_S_ISUID 04000 /* set user id on execution */
#define MY_S_ISGID 02000 /* set group id on execution */
#define MY_S_ISVTX 01000 /* save swapped text even after use */
#define MY_S_IRWXU 00700 /* read, write, execute: owner */
#define MY_S_IRUSR 00400 /* read permission: owner */
#define MY_S_IWUSR 00200 /* write permission: owner */
#define MY_S_IXUSR 00100 /* execute permission: owner */
#define MY_S_IRWXG 00070 /* read, write, execute: group */
#define MY_S_IRGRP 00040 /* read permission: group */
#define MY_S_IWGRP 00020 /* write permission: group */
#define MY_S_IXGRP 00010 /* execute permission: group */
#define MY_S_IRWXO 00007 /* read, write, execute: other */
#define MY_S_IROTH 00004 /* read permission: other */
#define MY_S_IWOTH 00002 /* write permission: other */
#define MY_S_IXOTH 00001 /* execute permission: other */
#ifndef TRUE
#define TRUE 1
#endif
#ifndef FALSE
#define FALSE 0
#endif
typedef int* sem_id;
typedef uint8_t uchar;
typedef int16_t int16;
typedef uint16_t uint16;
typedef int32_t int32;
typedef uint32_t uint32;
#define ulong unsigned long /* make it a #define to avoid conflicts */
typedef int64_t int64;
typedef uint64_t uint64;
typedef unsigned int port_id;
typedef int bool;
typedef int image_id;
typedef int64_t bigtime_t;
typedef long thread_id;
typedef long status_t;
sem_id create_sem(long count, const char *name);
long delete_sem(sem_id sem);
long acquire_sem(sem_id sem);
long acquire_sem_etc(sem_id sem, int count, int flags,
bigtime_t microsecond_timeout);
long release_sem(sem_id sem);
long release_sem_etc(sem_id sem, long count, long flags);
long atomic_add(long *value, long addvalue);
int snooze(bigtime_t f);
bigtime_t system_time(void);
ssize_t read_pos(int fd, fs_off_t _pos, void *data, size_t nbytes);
ssize_t write_pos(int fd, fs_off_t _pos, const void *data, size_t nbytes);
ssize_t readv_pos(int fd, fs_off_t _pos, struct iovec *iov, int count);
ssize_t writev_pos(int fd, fs_off_t _pos, struct iovec *iov, int count);
void panic(const char *msg, ...);
int device_is_read_only(const char *device);
int get_device_block_size(int fd);
fs_off_t get_num_device_blocks(int fd);
int device_is_removeable(int fd);
int lock_removeable_device(int fd, bool on_or_off);
void hexdump(void *address, int size);
#endif /* _COMPAT_H */