xref: /rust-libc-0.2.174/src/windows/mod.rs (revision a01c32d6)
1 //! Windows CRT definitions
2 
3 pub type int8_t = i8;
4 pub type int16_t = i16;
5 pub type int32_t = i32;
6 pub type int64_t = i64;
7 pub type uint8_t = u8;
8 pub type uint16_t = u16;
9 pub type uint32_t = u32;
10 pub type uint64_t = u64;
11 
12 pub type c_schar = i8;
13 pub type c_uchar = u8;
14 pub type c_short = i16;
15 pub type c_ushort = u16;
16 pub type c_int = i32;
17 pub type c_uint = u32;
18 pub type c_float = f32;
19 pub type c_double = f64;
20 pub type c_longlong = i64;
21 pub type c_ulonglong = u64;
22 pub type intmax_t = i64;
23 pub type uintmax_t = u64;
24 
25 pub type size_t = usize;
26 pub type ptrdiff_t = isize;
27 pub type intptr_t = isize;
28 pub type uintptr_t = usize;
29 pub type ssize_t = isize;
30 
31 pub type c_char = i8;
32 pub type c_long = i32;
33 pub type c_ulong = u32;
34 pub type wchar_t = u16;
35 
36 pub type clock_t = i32;
37 
38 cfg_if! {
39     if #[cfg(all(target_arch = "x86", target_env = "gnu"))] {
40         pub type time_t = i32;
41     } else {
42         pub type time_t = i64;
43     }
44 }
45 
46 pub type off_t = i32;
47 pub type dev_t = u32;
48 pub type ino_t = u16;
49 pub enum timezone {}
50 pub type time64_t = i64;
51 
52 s! {
53     // note this is the struct called stat64 in Windows. Not stat, nor stati64.
54     pub struct stat {
55         pub st_dev: dev_t,
56         pub st_ino: ino_t,
57         pub st_mode: u16,
58         pub st_nlink: ::c_short,
59         pub st_uid: ::c_short,
60         pub st_gid: ::c_short,
61         pub st_rdev: dev_t,
62         pub st_size: i64,
63         pub st_atime: time64_t,
64         pub st_mtime: time64_t,
65         pub st_ctime: time64_t,
66     }
67 
68     // note that this is called utimbuf64 in Windows
69     pub struct utimbuf {
70         pub actime: time64_t,
71         pub modtime: time64_t,
72     }
73 
74     pub struct tm {
75         tm_sec: ::c_int,
76         tm_min: ::c_int,
77         tm_hour: ::c_int,
78         tm_mday: ::c_int,
79         tm_mon: ::c_int,
80         tm_year: ::c_int,
81         tm_wday: ::c_int,
82         tm_yday: ::c_int,
83         tm_isdst: ::c_int,
84     }
85 
86     pub struct timeval {
87         pub tv_sec: c_long,
88         pub tv_usec: c_long,
89     }
90 
91     pub struct timespec {
92         pub tv_sec: time_t,
93         pub tv_nsec: c_long,
94     }
95 }
96 
97 pub const INT_MIN: c_int = -2147483648;
98 pub const INT_MAX: c_int = 2147483647;
99 
100 pub const EXIT_FAILURE: ::c_int = 1;
101 pub const EXIT_SUCCESS: ::c_int = 0;
102 pub const RAND_MAX: ::c_int = 32767;
103 pub const EOF: ::c_int = -1;
104 pub const SEEK_SET: ::c_int = 0;
105 pub const SEEK_CUR: ::c_int = 1;
106 pub const SEEK_END: ::c_int = 2;
107 pub const _IOFBF: ::c_int = 0;
108 pub const _IONBF: ::c_int = 4;
109 pub const _IOLBF: ::c_int = 64;
110 pub const BUFSIZ: ::c_uint = 512;
111 pub const FOPEN_MAX: ::c_uint = 20;
112 pub const FILENAME_MAX: ::c_uint = 260;
113 
114 pub const O_RDONLY: ::c_int = 0;
115 pub const O_WRONLY: ::c_int = 1;
116 pub const O_RDWR: ::c_int = 2;
117 pub const O_APPEND: ::c_int = 8;
118 pub const O_CREAT: ::c_int = 256;
119 pub const O_EXCL: ::c_int = 1024;
120 pub const O_TEXT: ::c_int = 16384;
121 pub const O_BINARY: ::c_int = 32768;
122 pub const O_NOINHERIT: ::c_int = 128;
123 pub const O_TRUNC: ::c_int = 512;
124 pub const S_IFCHR: ::c_int = 8192;
125 pub const S_IFDIR: ::c_int = 16384;
126 pub const S_IFREG: ::c_int = 32768;
127 pub const S_IFMT: ::c_int = 61440;
128 pub const S_IEXEC: ::c_int = 64;
129 pub const S_IWRITE: ::c_int = 128;
130 pub const S_IREAD: ::c_int = 256;
131 
132 pub const LC_ALL: ::c_int = 0;
133 pub const LC_COLLATE: ::c_int = 1;
134 pub const LC_CTYPE: ::c_int = 2;
135 pub const LC_MONETARY: ::c_int = 3;
136 pub const LC_NUMERIC: ::c_int = 4;
137 pub const LC_TIME: ::c_int = 5;
138 
139 pub const EPERM: ::c_int = 1;
140 pub const ENOENT: ::c_int = 2;
141 pub const ESRCH: ::c_int = 3;
142 pub const EINTR: ::c_int = 4;
143 pub const EIO: ::c_int = 5;
144 pub const ENXIO: ::c_int = 6;
145 pub const E2BIG: ::c_int = 7;
146 pub const ENOEXEC: ::c_int = 8;
147 pub const EBADF: ::c_int = 9;
148 pub const ECHILD: ::c_int = 10;
149 pub const EAGAIN: ::c_int = 11;
150 pub const ENOMEM: ::c_int = 12;
151 pub const EACCES: ::c_int = 13;
152 pub const EFAULT: ::c_int = 14;
153 pub const EBUSY: ::c_int = 16;
154 pub const EEXIST: ::c_int = 17;
155 pub const EXDEV: ::c_int = 18;
156 pub const ENODEV: ::c_int = 19;
157 pub const ENOTDIR: ::c_int = 20;
158 pub const EISDIR: ::c_int = 21;
159 pub const EINVAL: ::c_int = 22;
160 pub const ENFILE: ::c_int = 23;
161 pub const EMFILE: ::c_int = 24;
162 pub const ENOTTY: ::c_int = 25;
163 pub const EFBIG: ::c_int = 27;
164 pub const ENOSPC: ::c_int = 28;
165 pub const ESPIPE: ::c_int = 29;
166 pub const EROFS: ::c_int = 30;
167 pub const EMLINK: ::c_int = 31;
168 pub const EPIPE: ::c_int = 32;
169 pub const EDOM: ::c_int = 33;
170 pub const ERANGE: ::c_int = 34;
171 pub const EDEADLK: ::c_int = 36;
172 pub const EDEADLOCK: ::c_int = 36;
173 pub const ENAMETOOLONG: ::c_int = 38;
174 pub const ENOLCK: ::c_int = 39;
175 pub const ENOSYS: ::c_int = 40;
176 pub const ENOTEMPTY: ::c_int = 41;
177 pub const EILSEQ: ::c_int = 42;
178 pub const STRUNCATE: ::c_int = 80;
179 
180 // inline comment below appeases style checker
181 #[cfg(all(target_env = "msvc", feature = "rustc-dep-of-std"))] // " if "
182 #[link(name = "msvcrt", cfg(not(target_feature = "crt-static")))]
183 #[link(name = "libcmt", cfg(target_feature = "crt-static"))]
184 extern {}
185 
186 pub enum FILE {}
187 pub enum fpos_t {} // TODO: fill this out with a struct
188 
189 extern {
190     pub fn isalnum(c: c_int) -> c_int;
191     pub fn isalpha(c: c_int) -> c_int;
192     pub fn iscntrl(c: c_int) -> c_int;
193     pub fn isdigit(c: c_int) -> c_int;
194     pub fn isgraph(c: c_int) -> c_int;
195     pub fn islower(c: c_int) -> c_int;
196     pub fn isprint(c: c_int) -> c_int;
197     pub fn ispunct(c: c_int) -> c_int;
198     pub fn isspace(c: c_int) -> c_int;
199     pub fn isupper(c: c_int) -> c_int;
200     pub fn isxdigit(c: c_int) -> c_int;
201     pub fn tolower(c: c_int) -> c_int;
202     pub fn toupper(c: c_int) -> c_int;
203     pub fn fopen(filename: *const c_char, mode: *const c_char) -> *mut FILE;
204     pub fn freopen(filename: *const c_char, mode: *const c_char,
205                    file: *mut FILE) -> *mut FILE;
206     pub fn fflush(file: *mut FILE) -> c_int;
207     pub fn fclose(file: *mut FILE) -> c_int;
208     pub fn remove(filename: *const c_char) -> c_int;
209     pub fn rename(oldname: *const c_char, newname: *const c_char) -> c_int;
210     pub fn tmpfile() -> *mut FILE;
211     pub fn setvbuf(stream: *mut FILE, buffer: *mut c_char, mode: c_int,
212                    size: size_t) -> c_int;
213     pub fn setbuf(stream: *mut FILE, buf: *mut c_char);
214     pub fn getchar() -> c_int;
215     pub fn putchar(c: c_int) -> c_int;
216     pub fn fgetc(stream: *mut FILE) -> c_int;
217     pub fn fgets(buf: *mut c_char, n: c_int, stream: *mut FILE) -> *mut c_char;
218     pub fn fputc(c: c_int, stream: *mut FILE) -> c_int;
219     pub fn fputs(s: *const c_char, stream: *mut FILE) -> c_int;
220     pub fn puts(s: *const c_char) -> c_int;
221     pub fn ungetc(c: c_int, stream: *mut FILE) -> c_int;
222     pub fn fread(ptr: *mut c_void, size: size_t, nobj: size_t,
223                  stream: *mut FILE) -> size_t;
224     pub fn fwrite(ptr: *const c_void, size: size_t, nobj: size_t,
225                   stream: *mut FILE) -> size_t;
226     pub fn fseek(stream: *mut FILE, offset: c_long, whence: c_int) -> c_int;
227     pub fn ftell(stream: *mut FILE) -> c_long;
228     pub fn rewind(stream: *mut FILE);
229     pub fn fgetpos(stream: *mut FILE, ptr: *mut fpos_t) -> c_int;
230     pub fn fsetpos(stream: *mut FILE, ptr: *const fpos_t) -> c_int;
231     pub fn feof(stream: *mut FILE) -> c_int;
232     pub fn ferror(stream: *mut FILE) -> c_int;
233     pub fn perror(s: *const c_char);
234     pub fn atoi(s: *const c_char) -> c_int;
235     pub fn strtod(s: *const c_char, endp: *mut *mut c_char) -> c_double;
236     pub fn strtol(s: *const c_char, endp: *mut *mut c_char,
237                   base: c_int) -> c_long;
238     pub fn strtoul(s: *const c_char, endp: *mut *mut c_char,
239                    base: c_int) -> c_ulong;
240     pub fn calloc(nobj: size_t, size: size_t) -> *mut c_void;
241     pub fn malloc(size: size_t) -> *mut c_void;
242     pub fn realloc(p: *mut c_void, size: size_t) -> *mut c_void;
243     pub fn free(p: *mut c_void);
244     pub fn abort() -> !;
245     pub fn exit(status: c_int) -> !;
246     pub fn _exit(status: c_int) -> !;
247     pub fn atexit(cb: extern fn()) -> c_int;
248     pub fn system(s: *const c_char) -> c_int;
249     pub fn getenv(s: *const c_char) -> *mut c_char;
250 
251     pub fn strcpy(dst: *mut c_char, src: *const c_char) -> *mut c_char;
252     pub fn strncpy(dst: *mut c_char, src: *const c_char,
253                    n: size_t) -> *mut c_char;
254     pub fn strcat(s: *mut c_char, ct: *const c_char) -> *mut c_char;
255     pub fn strncat(s: *mut c_char, ct: *const c_char,
256                    n: size_t) -> *mut c_char;
257     pub fn strcmp(cs: *const c_char, ct: *const c_char) -> c_int;
258     pub fn strncmp(cs: *const c_char, ct: *const c_char, n: size_t) -> c_int;
259     pub fn strcoll(cs: *const c_char, ct: *const c_char) -> c_int;
260     pub fn strchr(cs: *const c_char, c: c_int) -> *mut c_char;
261     pub fn strrchr(cs: *const c_char, c: c_int) -> *mut c_char;
262     pub fn strspn(cs: *const c_char, ct: *const c_char) -> size_t;
263     pub fn strcspn(cs: *const c_char, ct: *const c_char) -> size_t;
264     pub fn strdup(cs: *const c_char) -> *mut c_char;
265     pub fn strpbrk(cs: *const c_char, ct: *const c_char) -> *mut c_char;
266     pub fn strstr(cs: *const c_char, ct: *const c_char) -> *mut c_char;
267     pub fn strlen(cs: *const c_char) -> size_t;
268     pub fn strnlen(cs: *const c_char, maxlen: size_t) -> size_t;
269     pub fn strerror(n: c_int) -> *mut c_char;
270     pub fn strtok(s: *mut c_char, t: *const c_char) -> *mut c_char;
271     pub fn strxfrm(s: *mut c_char, ct: *const c_char, n: size_t) -> size_t;
272     pub fn wcslen(buf: *const wchar_t) -> size_t;
273     pub fn wcstombs(dest: *mut c_char, src: *const wchar_t,
274                     n: size_t) -> ::size_t;
275 
276     pub fn memchr(cx: *const c_void, c: c_int, n: size_t) -> *mut c_void;
277     pub fn memcmp(cx: *const c_void, ct: *const c_void, n: size_t) -> c_int;
278     pub fn memcpy(dest: *mut c_void, src: *const c_void,
279                   n: size_t) -> *mut c_void;
280     pub fn memmove(dest: *mut c_void, src: *const c_void,
281                    n: size_t) -> *mut c_void;
282     pub fn memset(dest: *mut c_void, c: c_int, n: size_t) -> *mut c_void;
283 
284     pub fn abs(i: c_int) -> c_int;
285     pub fn atof(s: *const c_char) -> c_double;
286     pub fn labs(i: c_long) -> c_long;
287     pub fn rand() -> c_int;
288     pub fn srand(seed: c_uint);
289 
290     #[link_name = "_chmod"]
291     pub fn chmod(path: *const c_char, mode: ::c_int) -> ::c_int;
292     #[link_name = "_wchmod"]
293     pub fn wchmod(path: *const wchar_t, mode: ::c_int) -> ::c_int;
294     #[link_name = "_mkdir"]
295     pub fn mkdir(path: *const c_char) -> ::c_int;
296     #[link_name = "_wrmdir"]
297     pub fn wrmdir(path: *const wchar_t) -> ::c_int;
298     #[link_name = "_fstat64"]
299     pub fn fstat(fildes: ::c_int, buf: *mut stat) -> ::c_int;
300     #[link_name = "_stat64"]
301     pub fn stat(path: *const c_char, buf: *mut stat) -> ::c_int;
302     #[link_name = "_wstat64"]
303     pub fn wstat(path: *const wchar_t, buf: *mut stat) -> ::c_int;
304     #[link_name = "_wutime64"]
305     pub fn wutime(file: *const wchar_t, buf: *mut utimbuf) -> ::c_int;
306     #[link_name = "_popen"]
307     pub fn popen(command: *const c_char, mode: *const c_char) -> *mut ::FILE;
308     #[link_name = "_pclose"]
309     pub fn pclose(stream: *mut ::FILE) -> ::c_int;
310     #[link_name = "_fdopen"]
311     pub fn fdopen(fd: ::c_int, mode: *const c_char) -> *mut ::FILE;
312     #[link_name = "_fileno"]
313     pub fn fileno(stream: *mut ::FILE) -> ::c_int;
314     #[link_name = "_open"]
315     pub fn open(path: *const c_char, oflag: ::c_int, ...) -> ::c_int;
316     #[link_name = "_wopen"]
317     pub fn wopen(path: *const wchar_t, oflag: ::c_int, ...) -> ::c_int;
318     #[link_name = "_creat"]
319     pub fn creat(path: *const c_char, mode: ::c_int) -> ::c_int;
320     #[link_name = "_access"]
321     pub fn access(path: *const c_char, amode: ::c_int) -> ::c_int;
322     #[link_name = "_chdir"]
323     pub fn chdir(dir: *const c_char) -> ::c_int;
324     #[link_name = "_close"]
325     pub fn close(fd: ::c_int) -> ::c_int;
326     #[link_name = "_dup"]
327     pub fn dup(fd: ::c_int) -> ::c_int;
328     #[link_name = "_dup2"]
329     pub fn dup2(src: ::c_int, dst: ::c_int) -> ::c_int;
330     #[link_name = "_execv"]
331     pub fn execv(prog: *const c_char, argv: *const *const c_char) -> ::intptr_t;
332     #[link_name = "_execve"]
333     pub fn execve(prog: *const c_char, argv: *const *const c_char,
334                   envp: *const *const c_char) -> ::c_int;
335     #[link_name = "_execvp"]
336     pub fn execvp(c: *const c_char, argv: *const *const c_char) -> ::c_int;
337     #[link_name = "_execvpe"]
338     pub fn execvpe(c: *const c_char, argv: *const *const c_char,
339                    envp: *const *const c_char) -> ::c_int;
340     #[link_name = "_getcwd"]
341     pub fn getcwd(buf: *mut c_char, size: ::c_int) -> *mut c_char;
342     #[link_name = "_getpid"]
343     pub fn getpid() -> ::c_int;
344     #[link_name = "_isatty"]
345     pub fn isatty(fd: ::c_int) -> ::c_int;
346     #[link_name = "_lseek"]
347     pub fn lseek(fd: ::c_int, offset: c_long, origin: ::c_int) -> c_long;
348     #[link_name = "_pipe"]
349     pub fn pipe(fds: *mut ::c_int,
350                 psize: ::c_uint,
351                 textmode: ::c_int) -> ::c_int;
352     #[link_name = "_read"]
353     pub fn read(fd: ::c_int, buf: *mut ::c_void, count: ::c_uint) -> ::c_int;
354     #[link_name = "_rmdir"]
355     pub fn rmdir(path: *const c_char) -> ::c_int;
356     #[link_name = "_unlink"]
357     pub fn unlink(c: *const c_char) -> ::c_int;
358     #[link_name = "_write"]
359     pub fn write(fd: ::c_int, buf: *const ::c_void, count: ::c_uint) -> ::c_int;
360     #[link_name = "_commit"]
361     pub fn commit(fd: ::c_int) -> ::c_int;
362     #[link_name = "_get_osfhandle"]
363     pub fn get_osfhandle(fd: ::c_int) -> ::intptr_t;
364     #[link_name = "_open_osfhandle"]
365     pub fn open_osfhandle(osfhandle: ::intptr_t, flags: ::c_int) -> ::c_int;
366     pub fn setlocale(category: ::c_int, locale: *const c_char) -> *mut c_char;
367     #[link_name = "_wsetlocale"]
368     pub fn wsetlocale(category: ::c_int,
369                       locale: *const wchar_t) -> *mut wchar_t;
370 }
371 
372 cfg_if! {
373     if #[cfg(core_cvoid)] {
374         pub use core::ffi::c_void;
375     } else {
376         // Use repr(u8) as LLVM expects `void*` to be the same as `i8*` to help
377         // enable more optimization opportunities around it recognizing things
378         // like malloc/free.
379         #[repr(u8)]
380         pub enum c_void {
381             // Two dummy variants so the #[repr] attribute can be used.
382             #[doc(hidden)]
383             __variant1,
384             #[doc(hidden)]
385             __variant2,
386         }
387     }
388 }
389 
390 cfg_if! {
391     if #[cfg(all(target_env = "gnu"))] {
392         mod gnu;
393         pub use self::gnu::*;
394     } else if #[cfg(all(target_env = "msvc"))] {
395         mod msvc;
396         pub use self::msvc::*;
397     } else {
398         // Unknown target_env
399     }
400 }