blob: db6e46b205cb05844a2d2f6cc91ae73148b4d343 [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
Alex Crichton07d3a0d2015-10-30 10:21:32 -070013#![allow(bad_style, raw_pointer_derive, 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))]
57#![cfg_attr(all(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 Crichtondafaca92015-09-09 21:50:47 -070060
Alex Crichtonf7efe342015-11-02 14:26:04 -080061// Attributes needed when building as part of the standard library
62#![cfg_attr(stdbuild, feature(no_std, core, core_slice_ext, staged_api))]
63#![cfg_attr(stdbuild, no_std)]
64#![cfg_attr(stdbuild, staged_api)]
65#![cfg_attr(stdbuild, unstable(feature = "libc",
66 reason = "use `libc` from crates.io",
67 issue = "27783"))]
68
69#[cfg(all(not(stdbuild), not(dox)))]
70extern crate std as core;
71
Alex Crichton5d6cf052015-09-11 14:52:34 -070072#[macro_use] mod macros;
Alex Crichton24abc4f2015-09-16 23:54:56 -070073mod dox;
Alex Crichton31504842015-09-10 23:43:41 -070074
Alex Crichtond5c4e552015-10-29 15:46:16 -070075// Use repr(u8) as LLVM expects `void*` to be the same as `i8*` to help enable
76// more optimization opportunities around it recognizing things like
77// malloc/free.
Alex Crichton5d6cf052015-09-11 14:52:34 -070078#[repr(u8)]
79pub enum c_void {
Alex Crichtond5c4e552015-10-29 15:46:16 -070080 // Two dummy variants so the #[repr] attribute can be used.
Alex Crichton9f2b1012015-09-16 23:32:21 -070081 #[doc(hidden)]
Alex Crichton5d6cf052015-09-11 14:52:34 -070082 __variant1,
Alex Crichton9f2b1012015-09-16 23:32:21 -070083 #[doc(hidden)]
Alex Crichton5d6cf052015-09-11 14:52:34 -070084 __variant2,
85}
Alex Crichtondafaca92015-09-09 21:50:47 -070086
Alex Crichton5d6cf052015-09-11 14:52:34 -070087pub type int8_t = i8;
88pub type int16_t = i16;
89pub type int32_t = i32;
90pub type int64_t = i64;
91pub type uint8_t = u8;
92pub type uint16_t = u16;
93pub type uint32_t = u32;
94pub type uint64_t = u64;
Alex Crichtondafaca92015-09-09 21:50:47 -070095
Alex Crichton17910462015-09-22 19:11:04 -070096pub type c_schar = i8;
97pub type c_uchar = u8;
98pub type c_short = i16;
99pub type c_ushort = u16;
100pub type c_int = i32;
101pub type c_uint = u32;
102pub type c_float = f32;
103pub type c_double = f64;
104pub type c_longlong = i64;
105pub type c_ulonglong = u64;
106pub type intmax_t = i64;
107pub type uintmax_t = u64;
108
109pub type size_t = usize;
110pub type ptrdiff_t = isize;
111pub type intptr_t = isize;
112pub type uintptr_t = usize;
113pub type ssize_t = isize;
114
Alex Crichton5d6cf052015-09-11 14:52:34 -0700115pub enum FILE {}
Alex Crichton74825222015-10-29 17:36:55 -0700116pub enum fpos_t {} // TODO: fill this out with a struct
Alex Crichtondafaca92015-09-09 21:50:47 -0700117
Alex Crichton5d6cf052015-09-11 14:52:34 -0700118extern {
119 pub fn isalnum(c: c_int) -> c_int;
120 pub fn isalpha(c: c_int) -> c_int;
121 pub fn iscntrl(c: c_int) -> c_int;
122 pub fn isdigit(c: c_int) -> c_int;
123 pub fn isgraph(c: c_int) -> c_int;
124 pub fn islower(c: c_int) -> c_int;
125 pub fn isprint(c: c_int) -> c_int;
126 pub fn ispunct(c: c_int) -> c_int;
127 pub fn isspace(c: c_int) -> c_int;
128 pub fn isupper(c: c_int) -> c_int;
129 pub fn isxdigit(c: c_int) -> c_int;
130 pub fn tolower(c: c_int) -> c_int;
131 pub fn toupper(c: c_int) -> c_int;
Alex Crichtondafaca92015-09-09 21:50:47 -0700132
Alex Crichton5d6cf052015-09-11 14:52:34 -0700133 #[cfg_attr(all(target_os = "macos", target_arch = "x86"),
134 link_name = "fopen$UNIX2003")]
135 pub fn fopen(filename: *const c_char,
136 mode: *const c_char) -> *mut FILE;
137 #[cfg_attr(all(target_os = "macos", target_arch = "x86"),
138 link_name = "freopen$UNIX2003")]
139 pub fn freopen(filename: *const c_char, mode: *const c_char,
Alex Crichtonbfc6ebc2015-09-11 15:29:40 -0700140 file: *mut FILE) -> *mut FILE;
Alex Crichton5d6cf052015-09-11 14:52:34 -0700141 pub fn fflush(file: *mut FILE) -> c_int;
142 pub fn fclose(file: *mut FILE) -> c_int;
143 pub fn remove(filename: *const c_char) -> c_int;
Alex Crichtonbfc6ebc2015-09-11 15:29:40 -0700144 pub fn rename(oldname: *const c_char, newname: *const c_char) -> c_int;
Alex Crichton5d6cf052015-09-11 14:52:34 -0700145 pub fn tmpfile() -> *mut FILE;
146 pub fn setvbuf(stream: *mut FILE,
147 buffer: *mut c_char,
148 mode: c_int,
Alex Crichtonbfc6ebc2015-09-11 15:29:40 -0700149 size: size_t) -> c_int;
Alex Crichton5d6cf052015-09-11 14:52:34 -0700150 pub fn setbuf(stream: *mut FILE, buf: *mut c_char);
Alex Crichton5d6cf052015-09-11 14:52:34 -0700151 pub fn fgetc(stream: *mut FILE) -> c_int;
Alex Crichtonbfc6ebc2015-09-11 15:29:40 -0700152 pub fn fgets(buf: *mut c_char, n: c_int, stream: *mut FILE) -> *mut c_char;
Alex Crichton5d6cf052015-09-11 14:52:34 -0700153 pub fn fputc(c: c_int, stream: *mut FILE) -> c_int;
154 #[cfg_attr(all(target_os = "macos", target_arch = "x86"),
155 link_name = "fputs$UNIX2003")]
156 pub fn fputs(s: *const c_char, stream: *mut FILE)-> c_int;
157 pub fn puts(s: *const c_char) -> c_int;
158 pub fn ungetc(c: c_int, stream: *mut FILE) -> c_int;
159 pub fn fread(ptr: *mut c_void,
160 size: size_t,
161 nobj: size_t,
162 stream: *mut FILE)
163 -> size_t;
164 #[cfg_attr(all(target_os = "macos", target_arch = "x86"),
165 link_name = "fwrite$UNIX2003")]
166 pub fn fwrite(ptr: *const c_void,
167 size: size_t,
168 nobj: size_t,
169 stream: *mut FILE)
170 -> size_t;
Alex Crichtonbfc6ebc2015-09-11 15:29:40 -0700171 pub fn fseek(stream: *mut FILE, offset: c_long, whence: c_int) -> c_int;
Alex Crichton5d6cf052015-09-11 14:52:34 -0700172 pub fn ftell(stream: *mut FILE) -> c_long;
173 pub fn rewind(stream: *mut FILE);
174 pub fn fgetpos(stream: *mut FILE, ptr: *mut fpos_t) -> c_int;
175 pub fn fsetpos(stream: *mut FILE, ptr: *const fpos_t) -> c_int;
176 pub fn feof(stream: *mut FILE) -> c_int;
177 pub fn ferror(stream: *mut FILE) -> c_int;
178 pub fn perror(s: *const c_char);
Alex Crichton5d6cf052015-09-11 14:52:34 -0700179 pub fn atoi(s: *const c_char) -> c_int;
180 #[cfg_attr(all(target_os = "macos", target_arch = "x86"),
181 link_name = "strtod$UNIX2003")]
Alex Crichtonbfc6ebc2015-09-11 15:29:40 -0700182 pub fn strtod(s: *const c_char, endp: *mut *mut c_char) -> c_double;
Alex Crichton5d6cf052015-09-11 14:52:34 -0700183 pub fn strtol(s: *const c_char,
184 endp: *mut *mut c_char, base: c_int) -> c_long;
185 pub fn strtoul(s: *const c_char, endp: *mut *mut c_char,
186 base: c_int) -> c_ulong;
187 pub fn calloc(nobj: size_t, size: size_t) -> *mut c_void;
188 pub fn malloc(size: size_t) -> *mut c_void;
189 pub fn realloc(p: *mut c_void, size: size_t) -> *mut c_void;
190 pub fn free(p: *mut c_void);
191 pub fn exit(status: c_int) -> !;
192 pub fn _exit(status: c_int) -> !;
193 pub fn atexit(cb: extern fn()) -> c_int;
194 #[cfg_attr(all(target_os = "macos", target_arch = "x86"),
195 link_name = "system$UNIX2003")]
196 pub fn system(s: *const c_char) -> c_int;
197 pub fn getenv(s: *const c_char) -> *mut c_char;
Alex Crichtondafaca92015-09-09 21:50:47 -0700198
Alex Crichton5d6cf052015-09-11 14:52:34 -0700199 pub fn strcpy(dst: *mut c_char, src: *const c_char) -> *mut c_char;
200 pub fn strncpy(dst: *mut c_char, src: *const c_char, n: size_t)
201 -> *mut c_char;
202 pub fn strcat(s: *mut c_char, ct: *const c_char) -> *mut c_char;
203 pub fn strncat(s: *mut c_char, ct: *const c_char, n: size_t) -> *mut c_char;
204 pub fn strcmp(cs: *const c_char, ct: *const c_char) -> c_int;
205 pub fn strncmp(cs: *const c_char, ct: *const c_char, n: size_t) -> c_int;
206 pub fn strcoll(cs: *const c_char, ct: *const c_char) -> c_int;
207 pub fn strchr(cs: *const c_char, c: c_int) -> *mut c_char;
208 pub fn strrchr(cs: *const c_char, c: c_int) -> *mut c_char;
209 pub fn strspn(cs: *const c_char, ct: *const c_char) -> size_t;
210 pub fn strcspn(cs: *const c_char, ct: *const c_char) -> size_t;
211 pub fn strpbrk(cs: *const c_char, ct: *const c_char) -> *mut c_char;
212 pub fn strstr(cs: *const c_char, ct: *const c_char) -> *mut c_char;
213 pub fn strlen(cs: *const c_char) -> size_t;
214 #[cfg_attr(all(target_os = "macos", target_arch = "x86"),
215 link_name = "strerror$UNIX2003")]
216 pub fn strerror(n: c_int) -> *mut c_char;
217 pub fn strtok(s: *mut c_char, t: *const c_char) -> *mut c_char;
218 pub fn strxfrm(s: *mut c_char, ct: *const c_char, n: size_t) -> size_t;
219 pub fn wcslen(buf: *const wchar_t) -> size_t;
Alex Crichtondafaca92015-09-09 21:50:47 -0700220
Alex Crichton5d6cf052015-09-11 14:52:34 -0700221 pub fn memcmp(cx: *const c_void, ct: *const c_void, n: size_t) -> c_int;
222 pub fn memchr(cx: *const c_void, c: c_int, n: size_t) -> *mut c_void;
Alex Crichtondafaca92015-09-09 21:50:47 -0700223}
Alex Crichtond3d77922015-09-11 17:03:39 -0700224
Alex Crichtond5c4e552015-10-29 15:46:16 -0700225// These are all inline functions on android, so they end up just being entirely
226// missing on that platform.
Alex Crichtond3d77922015-09-11 17:03:39 -0700227#[cfg(not(target_os = "android"))]
228extern {
229 pub fn abs(i: c_int) -> c_int;
230 pub fn atof(s: *const c_char) -> c_double;
231 pub fn labs(i: c_long) -> c_long;
232 pub fn rand() -> c_int;
233 pub fn srand(seed: c_uint);
234}
Alex Crichton50a42e22015-09-15 14:27:15 -0700235
236cfg_if! {
237 if #[cfg(windows)] {
238 mod windows;
239 pub use windows::*;
240 } else {
241 mod unix;
242 pub use unix::*;
243 }
244}