blob: c17a504787db6a6a3f3921d53de0bd17ee6961ab [file] [log] [blame]
Alex Crichtondafaca92015-09-09 21:50:47 -07001// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10
Alex Crichton95b18b72015-09-17 10:06:58 -070011//! Crate docs
12
Devon Hollowood772f8912015-11-17 01:12:17 -080013#![allow(bad_style, overflowing_literals, improper_ctypes)]
Alex Crichtonf7efe342015-11-02 14:26:04 -080014#![crate_type = "rlib"]
15#![crate_name = "libc"]
Alex Crichton24abc4f2015-09-16 23:54:56 -070016#![cfg_attr(dox, feature(no_core, lang_items))]
17#![cfg_attr(dox, no_core)]
18#![doc(html_logo_url = "https://www.rust-lang.org/logos/rust-logo-128x128-blk-v2.png",
19 html_favicon_url = "https://doc.rust-lang.org/favicon.ico")]
20
Alex Crichton730a17f2015-09-17 10:05:36 -070021#![cfg_attr(all(target_os = "linux", target_arch = "x86_64"), doc(
Alex Crichton13418a52015-10-29 11:54:12 -070022 html_root_url = "https://doc.rust-lang.org/libc/x86_64-unknown-linux-gnu"
Alex Crichton730a17f2015-09-17 10:05:36 -070023))]
24#![cfg_attr(all(target_os = "linux", target_arch = "x86"), doc(
Alex Crichton13418a52015-10-29 11:54:12 -070025 html_root_url = "https://doc.rust-lang.org/libc/i686-unknown-linux-gnu"
Alex Crichton730a17f2015-09-17 10:05:36 -070026))]
27#![cfg_attr(all(target_os = "linux", target_arch = "arm"), doc(
Alex Crichton13418a52015-10-29 11:54:12 -070028 html_root_url = "https://doc.rust-lang.org/libc/arm-unknown-linux-gnueabihf"
Alex Crichton730a17f2015-09-17 10:05:36 -070029))]
30#![cfg_attr(all(target_os = "linux", target_arch = "mips"), doc(
Alex Crichton13418a52015-10-29 11:54:12 -070031 html_root_url = "https://doc.rust-lang.org/libc/mips-unknown-linux-gnu"
Alex Crichton730a17f2015-09-17 10:05:36 -070032))]
Alex Crichtond13e1e42015-09-17 17:29:18 -070033#![cfg_attr(all(target_os = "linux", target_arch = "aarch64"), doc(
Alex Crichton13418a52015-10-29 11:54:12 -070034 html_root_url = "https://doc.rust-lang.org/libc/aarch64-unknown-linux-gnu"
Alex Crichtond13e1e42015-09-17 17:29:18 -070035))]
Alex Crichton730a17f2015-09-17 10:05:36 -070036#![cfg_attr(all(target_os = "linux", target_env = "musl"), doc(
Alex Crichton13418a52015-10-29 11:54:12 -070037 html_root_url = "https://doc.rust-lang.org/libc/x86_64-unknown-linux-musl"
Alex Crichton730a17f2015-09-17 10:05:36 -070038))]
39#![cfg_attr(all(target_os = "macos", target_arch = "x86_64"), doc(
Alex Crichton13418a52015-10-29 11:54:12 -070040 html_root_url = "https://doc.rust-lang.org/libc/x86_64-apple-darwin"
Alex Crichton730a17f2015-09-17 10:05:36 -070041))]
42#![cfg_attr(all(target_os = "macos", target_arch = "x86"), doc(
Alex Crichton13418a52015-10-29 11:54:12 -070043 html_root_url = "https://doc.rust-lang.org/libc/i686-apple-darwin"
Alex Crichton730a17f2015-09-17 10:05:36 -070044))]
45#![cfg_attr(all(windows, target_arch = "x86_64", target_env = "gnu"), doc(
Alex Crichton13418a52015-10-29 11:54:12 -070046 html_root_url = "https://doc.rust-lang.org/libc/x86_64-pc-windows-gnu"
Alex Crichton730a17f2015-09-17 10:05:36 -070047))]
Alex Crichton611e9a32015-09-17 10:06:19 -070048#![cfg_attr(all(windows, target_arch = "x86", target_env = "gnu"), doc(
Alex Crichton13418a52015-10-29 11:54:12 -070049 html_root_url = "https://doc.rust-lang.org/libc/i686-pc-windows-gnu"
Alex Crichton730a17f2015-09-17 10:05:36 -070050))]
51#![cfg_attr(all(windows, target_arch = "x86_64", target_env = "msvc"), doc(
Alex Crichton13418a52015-10-29 11:54:12 -070052 html_root_url = "https://doc.rust-lang.org/libc/x86_64-pc-windows-msvc"
Alex Crichton730a17f2015-09-17 10:05:36 -070053))]
Alex Crichton611e9a32015-09-17 10:06:19 -070054#![cfg_attr(all(windows, target_arch = "x86", target_env = "msvc"), doc(
Alex Crichton13418a52015-10-29 11:54:12 -070055 html_root_url = "https://doc.rust-lang.org/libc/i686-pc-windows-msvc"
Alex Crichton730a17f2015-09-17 10:05:36 -070056))]
Alex Crichton37b71422015-12-17 10:50:53 -080057#![cfg_attr(target_os = "android", doc(
Alex Crichton13418a52015-10-29 11:54:12 -070058 html_root_url = "https://doc.rust-lang.org/libc/arm-linux-androideabi"
Alex Crichton730a17f2015-09-17 10:05:36 -070059))]
Alex Crichton37b71422015-12-17 10:50:53 -080060#![cfg_attr(target_os = "freebsd", doc(
61 html_root_url = "https://doc.rust-lang.org/libc/x86_64-unknown-freebsd"
62))]
63#![cfg_attr(target_os = "openbsd", doc(
64 html_root_url = "https://doc.rust-lang.org/libc/x86_64-unknown-openbsd"
65))]
66#![cfg_attr(target_os = "bitrig", doc(
67 html_root_url = "https://doc.rust-lang.org/libc/x86_64-unknown-bitrig"
68))]
69#![cfg_attr(target_os = "netbsd", doc(
70 html_root_url = "https://doc.rust-lang.org/libc/x86_64-unknown-netbsd"
71))]
72#![cfg_attr(target_os = "dragonfly", doc(
73 html_root_url = "https://doc.rust-lang.org/libc/x86_64-unknown-dragonfly"
74))]
Alex Crichtondafaca92015-09-09 21:50:47 -070075
Alex Crichtonf7efe342015-11-02 14:26:04 -080076// Attributes needed when building as part of the standard library
Sebastian Wicki37d4bb92016-02-27 14:27:10 +010077#![cfg_attr(stdbuild, feature(no_std, core, core_slice_ext, staged_api, custom_attribute, cfg_target_vendor))]
Alex Crichtonf9323d12016-10-31 16:44:29 -070078#![cfg_attr(stdbuild, feature(link_cfg))]
Alex Crichtonf7efe342015-11-02 14:26:04 -080079#![cfg_attr(stdbuild, no_std)]
80#![cfg_attr(stdbuild, staged_api)]
Alex Crichton56f61042015-11-02 16:57:39 -080081#![cfg_attr(stdbuild, allow(warnings))]
Alex Crichtonf7efe342015-11-02 14:26:04 -080082#![cfg_attr(stdbuild, unstable(feature = "libc",
83 reason = "use `libc` from crates.io",
84 issue = "27783"))]
85
Alex Crichton6d46b6f2016-01-20 16:39:37 -080086#![cfg_attr(not(feature = "use_std"), no_std)]
87
88#[cfg(all(not(stdbuild), not(dox), feature = "use_std"))]
Alex Crichtonf7efe342015-11-02 14:26:04 -080089extern crate std as core;
90
Alex Crichton5d6cf052015-09-11 14:52:34 -070091#[macro_use] mod macros;
Alex Crichton24abc4f2015-09-16 23:54:56 -070092mod dox;
Alex Crichton31504842015-09-10 23:43:41 -070093
Alex Crichtond5c4e552015-10-29 15:46:16 -070094// Use repr(u8) as LLVM expects `void*` to be the same as `i8*` to help enable
95// more optimization opportunities around it recognizing things like
96// malloc/free.
Alex Crichton5d6cf052015-09-11 14:52:34 -070097#[repr(u8)]
98pub enum c_void {
Alex Crichtond5c4e552015-10-29 15:46:16 -070099 // Two dummy variants so the #[repr] attribute can be used.
Alex Crichton9f2b1012015-09-16 23:32:21 -0700100 #[doc(hidden)]
Alex Crichton5d6cf052015-09-11 14:52:34 -0700101 __variant1,
Alex Crichton9f2b1012015-09-16 23:32:21 -0700102 #[doc(hidden)]
Alex Crichton5d6cf052015-09-11 14:52:34 -0700103 __variant2,
104}
Alex Crichtondafaca92015-09-09 21:50:47 -0700105
Alex Crichton5d6cf052015-09-11 14:52:34 -0700106pub type int8_t = i8;
107pub type int16_t = i16;
108pub type int32_t = i32;
109pub type int64_t = i64;
110pub type uint8_t = u8;
111pub type uint16_t = u16;
112pub type uint32_t = u32;
113pub type uint64_t = u64;
Alex Crichtondafaca92015-09-09 21:50:47 -0700114
Alex Crichton17910462015-09-22 19:11:04 -0700115pub type c_schar = i8;
116pub type c_uchar = u8;
117pub type c_short = i16;
118pub type c_ushort = u16;
119pub type c_int = i32;
120pub type c_uint = u32;
121pub type c_float = f32;
122pub type c_double = f64;
123pub type c_longlong = i64;
124pub type c_ulonglong = u64;
125pub type intmax_t = i64;
126pub type uintmax_t = u64;
127
128pub type size_t = usize;
129pub type ptrdiff_t = isize;
130pub type intptr_t = isize;
131pub type uintptr_t = usize;
132pub type ssize_t = isize;
133
Alex Crichton5d6cf052015-09-11 14:52:34 -0700134pub enum FILE {}
Alex Crichton74825222015-10-29 17:36:55 -0700135pub enum fpos_t {} // TODO: fill this out with a struct
Alex Crichtondafaca92015-09-09 21:50:47 -0700136
Alex Crichton5d6cf052015-09-11 14:52:34 -0700137extern {
138 pub fn isalnum(c: c_int) -> c_int;
139 pub fn isalpha(c: c_int) -> c_int;
140 pub fn iscntrl(c: c_int) -> c_int;
141 pub fn isdigit(c: c_int) -> c_int;
142 pub fn isgraph(c: c_int) -> c_int;
143 pub fn islower(c: c_int) -> c_int;
144 pub fn isprint(c: c_int) -> c_int;
145 pub fn ispunct(c: c_int) -> c_int;
146 pub fn isspace(c: c_int) -> c_int;
147 pub fn isupper(c: c_int) -> c_int;
148 pub fn isxdigit(c: c_int) -> c_int;
149 pub fn tolower(c: c_int) -> c_int;
150 pub fn toupper(c: c_int) -> c_int;
Alex Crichtondafaca92015-09-09 21:50:47 -0700151
Alex Crichton5d6cf052015-09-11 14:52:34 -0700152 #[cfg_attr(all(target_os = "macos", target_arch = "x86"),
153 link_name = "fopen$UNIX2003")]
154 pub fn fopen(filename: *const c_char,
155 mode: *const c_char) -> *mut FILE;
156 #[cfg_attr(all(target_os = "macos", target_arch = "x86"),
157 link_name = "freopen$UNIX2003")]
158 pub fn freopen(filename: *const c_char, mode: *const c_char,
Alex Crichtonbfc6ebc2015-09-11 15:29:40 -0700159 file: *mut FILE) -> *mut FILE;
Alex Crichton5d6cf052015-09-11 14:52:34 -0700160 pub fn fflush(file: *mut FILE) -> c_int;
161 pub fn fclose(file: *mut FILE) -> c_int;
162 pub fn remove(filename: *const c_char) -> c_int;
Alex Crichtonbfc6ebc2015-09-11 15:29:40 -0700163 pub fn rename(oldname: *const c_char, newname: *const c_char) -> c_int;
Alex Crichton5d6cf052015-09-11 14:52:34 -0700164 pub fn tmpfile() -> *mut FILE;
165 pub fn setvbuf(stream: *mut FILE,
166 buffer: *mut c_char,
167 mode: c_int,
Alex Crichtonbfc6ebc2015-09-11 15:29:40 -0700168 size: size_t) -> c_int;
Alex Crichton5d6cf052015-09-11 14:52:34 -0700169 pub fn setbuf(stream: *mut FILE, buf: *mut c_char);
Philipp Kellerdb27b722016-08-30 07:12:24 +0200170 pub fn getchar() -> c_int;
171 pub fn putchar(c: c_int) -> c_int;
Alex Crichton5d6cf052015-09-11 14:52:34 -0700172 pub fn fgetc(stream: *mut FILE) -> c_int;
Alex Crichtonbfc6ebc2015-09-11 15:29:40 -0700173 pub fn fgets(buf: *mut c_char, n: c_int, stream: *mut FILE) -> *mut c_char;
Alex Crichton5d6cf052015-09-11 14:52:34 -0700174 pub fn fputc(c: c_int, stream: *mut FILE) -> c_int;
175 #[cfg_attr(all(target_os = "macos", target_arch = "x86"),
176 link_name = "fputs$UNIX2003")]
177 pub fn fputs(s: *const c_char, stream: *mut FILE)-> c_int;
178 pub fn puts(s: *const c_char) -> c_int;
179 pub fn ungetc(c: c_int, stream: *mut FILE) -> c_int;
180 pub fn fread(ptr: *mut c_void,
181 size: size_t,
182 nobj: size_t,
183 stream: *mut FILE)
184 -> size_t;
185 #[cfg_attr(all(target_os = "macos", target_arch = "x86"),
186 link_name = "fwrite$UNIX2003")]
187 pub fn fwrite(ptr: *const c_void,
188 size: size_t,
189 nobj: size_t,
190 stream: *mut FILE)
191 -> size_t;
Alex Crichtonbfc6ebc2015-09-11 15:29:40 -0700192 pub fn fseek(stream: *mut FILE, offset: c_long, whence: c_int) -> c_int;
Alex Crichton5d6cf052015-09-11 14:52:34 -0700193 pub fn ftell(stream: *mut FILE) -> c_long;
194 pub fn rewind(stream: *mut FILE);
Alex Crichton49d7bca2015-11-27 09:40:37 -0800195 #[cfg_attr(target_os = "netbsd", link_name = "__fgetpos50")]
Alex Crichton5d6cf052015-09-11 14:52:34 -0700196 pub fn fgetpos(stream: *mut FILE, ptr: *mut fpos_t) -> c_int;
Alex Crichton49d7bca2015-11-27 09:40:37 -0800197 #[cfg_attr(target_os = "netbsd", link_name = "__fsetpos50")]
Alex Crichton5d6cf052015-09-11 14:52:34 -0700198 pub fn fsetpos(stream: *mut FILE, ptr: *const fpos_t) -> c_int;
199 pub fn feof(stream: *mut FILE) -> c_int;
200 pub fn ferror(stream: *mut FILE) -> c_int;
201 pub fn perror(s: *const c_char);
Alex Crichton5d6cf052015-09-11 14:52:34 -0700202 pub fn atoi(s: *const c_char) -> c_int;
203 #[cfg_attr(all(target_os = "macos", target_arch = "x86"),
204 link_name = "strtod$UNIX2003")]
Alex Crichtonbfc6ebc2015-09-11 15:29:40 -0700205 pub fn strtod(s: *const c_char, endp: *mut *mut c_char) -> c_double;
Alex Crichton5d6cf052015-09-11 14:52:34 -0700206 pub fn strtol(s: *const c_char,
207 endp: *mut *mut c_char, base: c_int) -> c_long;
208 pub fn strtoul(s: *const c_char, endp: *mut *mut c_char,
209 base: c_int) -> c_ulong;
210 pub fn calloc(nobj: size_t, size: size_t) -> *mut c_void;
211 pub fn malloc(size: size_t) -> *mut c_void;
212 pub fn realloc(p: *mut c_void, size: size_t) -> *mut c_void;
213 pub fn free(p: *mut c_void);
Brian Campbelld81e4a02016-01-31 22:04:05 -0500214 pub fn abort() -> !;
Alex Crichton5d6cf052015-09-11 14:52:34 -0700215 pub fn exit(status: c_int) -> !;
216 pub fn _exit(status: c_int) -> !;
217 pub fn atexit(cb: extern fn()) -> c_int;
218 #[cfg_attr(all(target_os = "macos", target_arch = "x86"),
219 link_name = "system$UNIX2003")]
220 pub fn system(s: *const c_char) -> c_int;
221 pub fn getenv(s: *const c_char) -> *mut c_char;
Alex Crichtondafaca92015-09-09 21:50:47 -0700222
Alex Crichton5d6cf052015-09-11 14:52:34 -0700223 pub fn strcpy(dst: *mut c_char, src: *const c_char) -> *mut c_char;
224 pub fn strncpy(dst: *mut c_char, src: *const c_char, n: size_t)
225 -> *mut c_char;
226 pub fn strcat(s: *mut c_char, ct: *const c_char) -> *mut c_char;
227 pub fn strncat(s: *mut c_char, ct: *const c_char, n: size_t) -> *mut c_char;
228 pub fn strcmp(cs: *const c_char, ct: *const c_char) -> c_int;
229 pub fn strncmp(cs: *const c_char, ct: *const c_char, n: size_t) -> c_int;
230 pub fn strcoll(cs: *const c_char, ct: *const c_char) -> c_int;
231 pub fn strchr(cs: *const c_char, c: c_int) -> *mut c_char;
232 pub fn strrchr(cs: *const c_char, c: c_int) -> *mut c_char;
233 pub fn strspn(cs: *const c_char, ct: *const c_char) -> size_t;
234 pub fn strcspn(cs: *const c_char, ct: *const c_char) -> size_t;
mehbcb7c482016-07-08 17:21:09 +0200235 pub fn strdup(cs: *const c_char) -> *mut c_char;
Alex Crichton5d6cf052015-09-11 14:52:34 -0700236 pub fn strpbrk(cs: *const c_char, ct: *const c_char) -> *mut c_char;
237 pub fn strstr(cs: *const c_char, ct: *const c_char) -> *mut c_char;
238 pub fn strlen(cs: *const c_char) -> size_t;
Raphael Cohn29b1cea2016-05-04 18:24:28 +0100239 pub fn strnlen(cs: *const c_char, maxlen: size_t) -> size_t;
Alex Crichton5d6cf052015-09-11 14:52:34 -0700240 #[cfg_attr(all(target_os = "macos", target_arch = "x86"),
241 link_name = "strerror$UNIX2003")]
242 pub fn strerror(n: c_int) -> *mut c_char;
243 pub fn strtok(s: *mut c_char, t: *const c_char) -> *mut c_char;
244 pub fn strxfrm(s: *mut c_char, ct: *const c_char, n: size_t) -> size_t;
245 pub fn wcslen(buf: *const wchar_t) -> size_t;
Andrii Dmytrenkoc01fd6f2017-02-03 12:02:05 +0000246 pub fn wcstombs(dest: *mut c_char, src: *const wchar_t, n: size_t) -> ::size_t;
Alex Crichtondafaca92015-09-09 21:50:47 -0700247
Alex Crichton5d6cf052015-09-11 14:52:34 -0700248 pub fn memchr(cx: *const c_void, c: c_int, n: size_t) -> *mut c_void;
meh988305b2016-08-04 21:12:13 +0200249 pub fn memcmp(cx: *const c_void, ct: *const c_void, n: size_t) -> c_int;
250 pub fn memcpy(dest: *mut c_void, src: *const c_void, n: size_t) -> *mut c_void;
251 pub fn memmove(dest: *mut c_void, src: *const c_void, n: size_t) -> *mut c_void;
meh5bf1fe42016-08-06 16:45:01 +0200252 pub fn memset(dest: *mut c_void, c: c_int, n: size_t) -> *mut c_void;
Alex Crichtondafaca92015-09-09 21:50:47 -0700253}
Alex Crichtond3d77922015-09-11 17:03:39 -0700254
Alex Crichtond5c4e552015-10-29 15:46:16 -0700255// These are all inline functions on android, so they end up just being entirely
256// missing on that platform.
Alex Crichtond3d77922015-09-11 17:03:39 -0700257#[cfg(not(target_os = "android"))]
258extern {
259 pub fn abs(i: c_int) -> c_int;
260 pub fn atof(s: *const c_char) -> c_double;
261 pub fn labs(i: c_long) -> c_long;
262 pub fn rand() -> c_int;
263 pub fn srand(seed: c_uint);
264}
Alex Crichton50a42e22015-09-15 14:27:15 -0700265
266cfg_if! {
267 if #[cfg(windows)] {
268 mod windows;
269 pub use windows::*;
Jeremy Sollerdcbe96b2016-11-08 14:05:56 -0700270 } else if #[cfg(target_os = "redox")] {
Jeremy Soller4ddaf1f2016-11-03 16:53:22 -0600271 mod redox;
272 pub use redox::*;
Kamal Marhubi66c33752016-03-10 15:07:32 -0500273 } else if #[cfg(unix)] {
Alex Crichton50a42e22015-09-15 14:27:15 -0700274 mod unix;
275 pub use unix::*;
Kamal Marhubi66c33752016-03-10 15:07:32 -0500276 } else {
277 // Unknown target_family
Alex Crichton50a42e22015-09-15 14:27:15 -0700278 }
279}