forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 0
/
libc.c
256 lines (201 loc) · 4.88 KB
/
libc.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
// Copyright 2014 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
/*
* This calculates the platform-variable portion of the libc module.
* Move code in here only as you discover it is platform-variable.
*
*/
/* c95 */
#include <stddef.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <wchar.h>
/* c99 */
#include <inttypes.h>
/* posix */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define S(T) ((((T)-1)<0) ? 'i' : 'u')
#define B(T) (((int)sizeof(T)) * CHAR_BIT)
#define put_type(N,T) \
printf(" type %s = %c%d;\n", N, S(T), B(T))
#define put_ftype(N,T) \
printf(" type %s = f%d;\n", N, B(T))
#define CT(T) ((((T)-1)<0) ? "int" : "uint")
#define CS(T) ((((T)-1)<0) ? "" : "_u")
#define put_const(N,T) \
printf(" const %s : %s = %d%s;\n", \
#N, CT(T), N, CS(T))
void c95_types() {
printf(" mod c95 {\n");
put_type("c_char", char);
put_type("c_schar", signed char);
put_type("c_uchar", unsigned char);
put_type("c_short", short);
put_type("c_ushort", unsigned short);
put_type("c_int", int);
put_type("c_uint", unsigned int);
put_type("c_long", long);
put_type("c_ulong", unsigned long);
put_ftype("c_float", float);
put_ftype("c_double", double);
put_type("size_t", size_t);
put_type("ptrdiff_t", ptrdiff_t);
put_type("clock_t", clock_t);
put_type("time_t", time_t);
put_type("wchar_t", wchar_t);
printf(" }\n");
}
void c99_types() {
printf(" mod c99 {\n");
put_type("c_longlong", long long);
put_type("c_ulonglong", unsigned long long);
put_type("intptr_t", intptr_t);
put_type("uintptr_t", uintptr_t);
printf(" }\n");
}
void posix88_types() {
printf(" mod posix88 {\n");
put_type("off_t", off_t);
put_type("dev_t", dev_t);
put_type("ino_t", ino_t);
put_type("pid_t", pid_t);
#ifndef __WIN32__
put_type("uid_t", uid_t);
put_type("gid_t", gid_t);
#endif
put_type("useconds_t", useconds_t);
put_type("mode_t", mode_t);
put_type("ssize_t", ssize_t);
printf(" }\n");
}
void extra_types() {
printf(" mod extra {\n");
printf(" }\n");
}
void c95_consts() {
printf(" mod c95 {\n");
put_const(EXIT_FAILURE, int);
put_const(EXIT_SUCCESS, int);
put_const(RAND_MAX, int);
put_const(EOF, int);
put_const(SEEK_SET, int);
put_const(SEEK_CUR, int);
put_const(SEEK_END, int);
put_const(_IOFBF, int);
put_const(_IONBF, int);
put_const(_IOLBF, int);
put_const(BUFSIZ, size_t);
put_const(FOPEN_MAX, size_t);
put_const(FILENAME_MAX, size_t);
put_const(L_tmpnam, size_t);
put_const(TMP_MAX, size_t);
printf(" }\n");
}
void posix88_consts() {
printf(" mod posix88 {\n");
put_const(O_RDONLY, int);
put_const(O_WRONLY, int);
put_const(O_RDWR, int);
put_const(O_APPEND, int);
put_const(O_CREAT, int);
put_const(O_EXCL, int);
put_const(O_TRUNC, int);
put_const(S_IFIFO, int);
put_const(S_IFCHR, int);
put_const(S_IFBLK, int);
put_const(S_IFDIR, int);
put_const(S_IFREG, int);
put_const(S_IFLNK, int);
put_const(S_IFMT, int);
put_const(S_IEXEC, int);
put_const(S_IWRITE, int);
put_const(S_IREAD, int);
put_const(S_IRWXU, int);
put_const(S_IXUSR, int);
put_const(S_IWUSR, int);
put_const(S_IRUSR, int);
#ifdef F_OK
put_const(F_OK, int);
#endif
#ifdef R_OK
put_const(R_OK, int);
#endif
#ifdef W_OK
put_const(W_OK, int);
#endif
#ifdef X_OK
put_const(X_OK, int);
#endif
#ifdef STDIN_FILENO
put_const(STDIN_FILENO, int);
#endif
#ifdef STDOUT_FILENO
put_const(STDOUT_FILENO, int);
#endif
#ifdef STDERR_FILENO
put_const(STDERR_FILENO, int);
#endif
#ifdef F_LOCK
put_const(F_LOCK, int);
#endif
#ifdef F_TEST
put_const(F_TEST, int);
#endif
#ifdef F_TLOCK
put_const(F_TLOCK, int);
#endif
#ifdef F_ULOCK
put_const(F_ULOCK, int);
#endif
printf(" }\n");
}
void extra_consts() {
printf(" mod extra {\n");
#ifdef O_RSYNC
put_const(O_RSYNC, int);
#endif
#ifdef O_DSYNC
put_const(O_DSYNC, int);
#endif
#ifdef O_SYNC
put_const(O_SYNC, int);
#endif
#ifdef O_TEXT
put_const(O_TEXT, int);
#endif
#ifdef O_BINARY
put_const(O_BINARY, int);
#endif
#ifdef O_IRUSR
put_const(O_IRUSR, int);
#endif
#ifdef O_IWUSR
put_const(O_IWUSR, int);
#endif
printf(" }\n");
}
int main() {
printf("mod types {");
c95_types();
c99_types();
posix88_types();
extra_types();
printf("}\n");
printf("mod consts {\n");
c95_consts();
posix88_consts();
extra_consts();
printf("}\n");
}