Skip to content

Commit cf4e35f

Browse files
committed
Factor out platforms for which libc is empty
1 parent a9b3f97 commit cf4e35f

File tree

4 files changed

+160
-196
lines changed

4 files changed

+160
-196
lines changed

.travis.yml

+4
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,10 @@ matrix:
8181
# QEMU based targets that compile in an emulator
8282
- env: TARGET=x86_64-unknown-freebsd
8383

84+
- env: TARGET=wasm32-unknown-unknown
85+
install: rustup target add $TARGET
86+
script: cargo build --no-default-features --target $TARGET --release
87+
8488
- name: "Shellcheck"
8589
install: true
8690
script:

README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
libc
22
====
33

4-
A Rust library with native bindings to the types and functions commonly found on
5-
various systems, including libc.
4+
Rust wrapper over the system's `libc`.
65

76
[![Build Status](https://travis-ci.org/rust-lang/libc.svg?branch=master)](https://travis-ci.org/rust-lang/libc)
87
[![Build status](https://ci.appveyor.com/api/projects/status/github/rust-lang/libc?svg=true)](https://ci.appveyor.com/project/rust-lang-libs/libc)

src/lib.rs

+155-165
Original file line numberDiff line numberDiff line change
@@ -121,17 +121,17 @@
121121
)]
122122
#![cfg_attr(not(feature = "use_std"), no_std)]
123123

124+
// FIXME: this crate is empty for wasm32-unknown-unknown
125+
#![cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
126+
124127
#[cfg(all(not(cross_platform_docs), feature = "use_std"))]
125128
extern crate std as core;
126129

127130
#[macro_use]
128131
mod macros;
132+
129133
mod dox;
130134

131-
/*
132-
* `c_void` should be defined for all targets except wasm.
133-
*/
134-
#[cfg(not(all(target_arch = "wasm32", not(target_os = "emscripten"))))]
135135
cfg_if! {
136136
if #[cfg(core_cvoid)] {
137137
pub use core::ffi::c_void;
@@ -150,176 +150,166 @@ cfg_if! {
150150
}
151151
}
152152

153-
cfg_if! {
154-
if #[cfg(all(target_arch = "wasm32", not(target_os = "emscripten")))] {
155-
// empty ...
156-
} else if #[cfg(target_os = "switch")] {
157-
// On the Switch, we only define some useful universal types for
158-
// convenience. Those can be found in the switch.rs file.
159-
} else {
160-
pub type int8_t = i8;
161-
pub type int16_t = i16;
162-
pub type int32_t = i32;
163-
pub type int64_t = i64;
164-
pub type uint8_t = u8;
165-
pub type uint16_t = u16;
166-
pub type uint32_t = u32;
167-
pub type uint64_t = u64;
153+
pub type int8_t = i8;
154+
pub type int16_t = i16;
155+
pub type int32_t = i32;
156+
pub type int64_t = i64;
157+
pub type uint8_t = u8;
158+
pub type uint16_t = u16;
159+
pub type uint32_t = u32;
160+
pub type uint64_t = u64;
168161

169-
pub type c_schar = i8;
170-
pub type c_uchar = u8;
171-
pub type c_short = i16;
172-
pub type c_ushort = u16;
173-
pub type c_int = i32;
174-
pub type c_uint = u32;
175-
pub type c_float = f32;
176-
pub type c_double = f64;
177-
pub type c_longlong = i64;
178-
pub type c_ulonglong = u64;
179-
pub type intmax_t = i64;
180-
pub type uintmax_t = u64;
162+
pub type c_schar = i8;
163+
pub type c_uchar = u8;
164+
pub type c_short = i16;
165+
pub type c_ushort = u16;
166+
pub type c_int = i32;
167+
pub type c_uint = u32;
168+
pub type c_float = f32;
169+
pub type c_double = f64;
170+
pub type c_longlong = i64;
171+
pub type c_ulonglong = u64;
172+
pub type intmax_t = i64;
173+
pub type uintmax_t = u64;
181174

182-
pub type size_t = usize;
183-
pub type ptrdiff_t = isize;
184-
pub type intptr_t = isize;
185-
pub type uintptr_t = usize;
186-
pub type ssize_t = isize;
175+
pub type size_t = usize;
176+
pub type ptrdiff_t = isize;
177+
pub type intptr_t = isize;
178+
pub type uintptr_t = usize;
179+
pub type ssize_t = isize;
187180

188-
pub enum FILE {}
189-
pub enum fpos_t {} // TODO: fill this out with a struct
181+
pub enum FILE {}
182+
pub enum fpos_t {} // TODO: fill this out with a struct
190183

191-
pub const INT_MIN: c_int = -2147483648;
192-
pub const INT_MAX: c_int = 2147483647;
184+
pub const INT_MIN: c_int = -2147483648;
185+
pub const INT_MAX: c_int = 2147483647;
193186

194-
extern {
195-
pub fn isalnum(c: c_int) -> c_int;
196-
pub fn isalpha(c: c_int) -> c_int;
197-
pub fn iscntrl(c: c_int) -> c_int;
198-
pub fn isdigit(c: c_int) -> c_int;
199-
pub fn isgraph(c: c_int) -> c_int;
200-
pub fn islower(c: c_int) -> c_int;
201-
pub fn isprint(c: c_int) -> c_int;
202-
pub fn ispunct(c: c_int) -> c_int;
203-
pub fn isspace(c: c_int) -> c_int;
204-
pub fn isupper(c: c_int) -> c_int;
205-
pub fn isxdigit(c: c_int) -> c_int;
206-
pub fn tolower(c: c_int) -> c_int;
207-
pub fn toupper(c: c_int) -> c_int;
187+
extern "C" {
188+
pub fn isalnum(c: c_int) -> c_int;
189+
pub fn isalpha(c: c_int) -> c_int;
190+
pub fn iscntrl(c: c_int) -> c_int;
191+
pub fn isdigit(c: c_int) -> c_int;
192+
pub fn isgraph(c: c_int) -> c_int;
193+
pub fn islower(c: c_int) -> c_int;
194+
pub fn isprint(c: c_int) -> c_int;
195+
pub fn ispunct(c: c_int) -> c_int;
196+
pub fn isspace(c: c_int) -> c_int;
197+
pub fn isupper(c: c_int) -> c_int;
198+
pub fn isxdigit(c: c_int) -> c_int;
199+
pub fn tolower(c: c_int) -> c_int;
200+
pub fn toupper(c: c_int) -> c_int;
208201

209-
#[cfg_attr(all(target_os = "macos", target_arch = "x86"),
210-
link_name = "fopen$UNIX2003")]
211-
pub fn fopen(filename: *const c_char,
212-
mode: *const c_char) -> *mut FILE;
213-
#[cfg_attr(all(target_os = "macos", target_arch = "x86"),
214-
link_name = "freopen$UNIX2003")]
215-
pub fn freopen(filename: *const c_char, mode: *const c_char,
216-
file: *mut FILE) -> *mut FILE;
217-
pub fn fflush(file: *mut FILE) -> c_int;
218-
pub fn fclose(file: *mut FILE) -> c_int;
219-
pub fn remove(filename: *const c_char) -> c_int;
220-
pub fn rename(oldname: *const c_char, newname: *const c_char) -> c_int;
221-
pub fn tmpfile() -> *mut FILE;
222-
pub fn setvbuf(stream: *mut FILE,
223-
buffer: *mut c_char,
224-
mode: c_int,
225-
size: size_t) -> c_int;
226-
pub fn setbuf(stream: *mut FILE, buf: *mut c_char);
227-
pub fn getchar() -> c_int;
228-
pub fn putchar(c: c_int) -> c_int;
229-
pub fn fgetc(stream: *mut FILE) -> c_int;
230-
pub fn fgets(buf: *mut c_char, n: c_int, stream: *mut FILE) -> *mut c_char;
231-
pub fn fputc(c: c_int, stream: *mut FILE) -> c_int;
232-
#[cfg_attr(all(target_os = "macos", target_arch = "x86"),
233-
link_name = "fputs$UNIX2003")]
234-
pub fn fputs(s: *const c_char, stream: *mut FILE)-> c_int;
235-
pub fn puts(s: *const c_char) -> c_int;
236-
pub fn ungetc(c: c_int, stream: *mut FILE) -> c_int;
237-
pub fn fread(ptr: *mut c_void,
238-
size: size_t,
239-
nobj: size_t,
240-
stream: *mut FILE)
241-
-> size_t;
242-
#[cfg_attr(all(target_os = "macos", target_arch = "x86"),
243-
link_name = "fwrite$UNIX2003")]
244-
pub fn fwrite(ptr: *const c_void,
245-
size: size_t,
246-
nobj: size_t,
247-
stream: *mut FILE)
248-
-> size_t;
249-
pub fn fseek(stream: *mut FILE, offset: c_long, whence: c_int) -> c_int;
250-
pub fn ftell(stream: *mut FILE) -> c_long;
251-
pub fn rewind(stream: *mut FILE);
252-
#[cfg_attr(target_os = "netbsd", link_name = "__fgetpos50")]
253-
pub fn fgetpos(stream: *mut FILE, ptr: *mut fpos_t) -> c_int;
254-
#[cfg_attr(target_os = "netbsd", link_name = "__fsetpos50")]
255-
pub fn fsetpos(stream: *mut FILE, ptr: *const fpos_t) -> c_int;
256-
pub fn feof(stream: *mut FILE) -> c_int;
257-
pub fn ferror(stream: *mut FILE) -> c_int;
258-
pub fn perror(s: *const c_char);
259-
pub fn atoi(s: *const c_char) -> c_int;
260-
#[cfg_attr(all(target_os = "macos", target_arch = "x86"),
261-
link_name = "strtod$UNIX2003")]
262-
pub fn strtod(s: *const c_char, endp: *mut *mut c_char) -> c_double;
263-
pub fn strtol(s: *const c_char,
264-
endp: *mut *mut c_char, base: c_int) -> c_long;
265-
pub fn strtoul(s: *const c_char, endp: *mut *mut c_char,
266-
base: c_int) -> c_ulong;
267-
pub fn calloc(nobj: size_t, size: size_t) -> *mut c_void;
268-
pub fn malloc(size: size_t) -> *mut c_void;
269-
pub fn realloc(p: *mut c_void, size: size_t) -> *mut c_void;
270-
pub fn free(p: *mut c_void);
271-
pub fn abort() -> !;
272-
pub fn exit(status: c_int) -> !;
273-
pub fn _exit(status: c_int) -> !;
274-
pub fn atexit(cb: extern fn()) -> c_int;
275-
#[cfg_attr(all(target_os = "macos", target_arch = "x86"),
276-
link_name = "system$UNIX2003")]
277-
pub fn system(s: *const c_char) -> c_int;
278-
pub fn getenv(s: *const c_char) -> *mut c_char;
202+
#[cfg_attr(
203+
all(target_os = "macos", target_arch = "x86"),
204+
link_name = "fopen$UNIX2003"
205+
)]
206+
pub fn fopen(filename: *const c_char, mode: *const c_char) -> *mut FILE;
207+
#[cfg_attr(
208+
all(target_os = "macos", target_arch = "x86"),
209+
link_name = "freopen$UNIX2003"
210+
)]
211+
pub fn freopen(filename: *const c_char, mode: *const c_char, file: *mut FILE) -> *mut FILE;
212+
pub fn fflush(file: *mut FILE) -> c_int;
213+
pub fn fclose(file: *mut FILE) -> c_int;
214+
pub fn remove(filename: *const c_char) -> c_int;
215+
pub fn rename(oldname: *const c_char, newname: *const c_char) -> c_int;
216+
pub fn tmpfile() -> *mut FILE;
217+
pub fn setvbuf(stream: *mut FILE, buffer: *mut c_char, mode: c_int, size: size_t) -> c_int;
218+
pub fn setbuf(stream: *mut FILE, buf: *mut c_char);
219+
pub fn getchar() -> c_int;
220+
pub fn putchar(c: c_int) -> c_int;
221+
pub fn fgetc(stream: *mut FILE) -> c_int;
222+
pub fn fgets(buf: *mut c_char, n: c_int, stream: *mut FILE) -> *mut c_char;
223+
pub fn fputc(c: c_int, stream: *mut FILE) -> c_int;
224+
#[cfg_attr(
225+
all(target_os = "macos", target_arch = "x86"),
226+
link_name = "fputs$UNIX2003"
227+
)]
228+
pub fn fputs(s: *const c_char, stream: *mut FILE) -> c_int;
229+
pub fn puts(s: *const c_char) -> c_int;
230+
pub fn ungetc(c: c_int, stream: *mut FILE) -> c_int;
231+
pub fn fread(ptr: *mut c_void, size: size_t, nobj: size_t, stream: *mut FILE) -> size_t;
232+
#[cfg_attr(
233+
all(target_os = "macos", target_arch = "x86"),
234+
link_name = "fwrite$UNIX2003"
235+
)]
236+
pub fn fwrite(ptr: *const c_void, size: size_t, nobj: size_t, stream: *mut FILE) -> size_t;
237+
pub fn fseek(stream: *mut FILE, offset: c_long, whence: c_int) -> c_int;
238+
pub fn ftell(stream: *mut FILE) -> c_long;
239+
pub fn rewind(stream: *mut FILE);
240+
#[cfg_attr(target_os = "netbsd", link_name = "__fgetpos50")]
241+
pub fn fgetpos(stream: *mut FILE, ptr: *mut fpos_t) -> c_int;
242+
#[cfg_attr(target_os = "netbsd", link_name = "__fsetpos50")]
243+
pub fn fsetpos(stream: *mut FILE, ptr: *const fpos_t) -> c_int;
244+
pub fn feof(stream: *mut FILE) -> c_int;
245+
pub fn ferror(stream: *mut FILE) -> c_int;
246+
pub fn perror(s: *const c_char);
247+
pub fn atoi(s: *const c_char) -> c_int;
248+
#[cfg_attr(
249+
all(target_os = "macos", target_arch = "x86"),
250+
link_name = "strtod$UNIX2003"
251+
)]
252+
pub fn strtod(s: *const c_char, endp: *mut *mut c_char) -> c_double;
253+
pub fn strtol(s: *const c_char, endp: *mut *mut c_char, base: c_int) -> c_long;
254+
pub fn strtoul(s: *const c_char, endp: *mut *mut c_char, base: c_int) -> c_ulong;
255+
pub fn calloc(nobj: size_t, size: size_t) -> *mut c_void;
256+
pub fn malloc(size: size_t) -> *mut c_void;
257+
pub fn realloc(p: *mut c_void, size: size_t) -> *mut c_void;
258+
pub fn free(p: *mut c_void);
259+
pub fn abort() -> !;
260+
pub fn exit(status: c_int) -> !;
261+
pub fn _exit(status: c_int) -> !;
262+
pub fn atexit(cb: extern "C" fn()) -> c_int;
263+
#[cfg_attr(
264+
all(target_os = "macos", target_arch = "x86"),
265+
link_name = "system$UNIX2003"
266+
)]
267+
pub fn system(s: *const c_char) -> c_int;
268+
pub fn getenv(s: *const c_char) -> *mut c_char;
279269

280-
pub fn strcpy(dst: *mut c_char, src: *const c_char) -> *mut c_char;
281-
pub fn strncpy(dst: *mut c_char, src: *const c_char, n: size_t)
282-
-> *mut c_char;
283-
pub fn strcat(s: *mut c_char, ct: *const c_char) -> *mut c_char;
284-
pub fn strncat(s: *mut c_char, ct: *const c_char, n: size_t) -> *mut c_char;
285-
pub fn strcmp(cs: *const c_char, ct: *const c_char) -> c_int;
286-
pub fn strncmp(cs: *const c_char, ct: *const c_char, n: size_t) -> c_int;
287-
pub fn strcoll(cs: *const c_char, ct: *const c_char) -> c_int;
288-
pub fn strchr(cs: *const c_char, c: c_int) -> *mut c_char;
289-
pub fn strrchr(cs: *const c_char, c: c_int) -> *mut c_char;
290-
pub fn strspn(cs: *const c_char, ct: *const c_char) -> size_t;
291-
pub fn strcspn(cs: *const c_char, ct: *const c_char) -> size_t;
292-
pub fn strdup(cs: *const c_char) -> *mut c_char;
293-
pub fn strpbrk(cs: *const c_char, ct: *const c_char) -> *mut c_char;
294-
pub fn strstr(cs: *const c_char, ct: *const c_char) -> *mut c_char;
295-
pub fn strlen(cs: *const c_char) -> size_t;
296-
pub fn strnlen(cs: *const c_char, maxlen: size_t) -> size_t;
297-
#[cfg_attr(all(target_os = "macos", target_arch = "x86"),
298-
link_name = "strerror$UNIX2003")]
299-
pub fn strerror(n: c_int) -> *mut c_char;
300-
pub fn strtok(s: *mut c_char, t: *const c_char) -> *mut c_char;
301-
pub fn strxfrm(s: *mut c_char, ct: *const c_char, n: size_t) -> size_t;
302-
pub fn wcslen(buf: *const wchar_t) -> size_t;
303-
pub fn wcstombs(dest: *mut c_char, src: *const wchar_t, n: size_t) -> ::size_t;
270+
pub fn strcpy(dst: *mut c_char, src: *const c_char) -> *mut c_char;
271+
pub fn strncpy(dst: *mut c_char, src: *const c_char, n: size_t) -> *mut c_char;
272+
pub fn strcat(s: *mut c_char, ct: *const c_char) -> *mut c_char;
273+
pub fn strncat(s: *mut c_char, ct: *const c_char, n: size_t) -> *mut c_char;
274+
pub fn strcmp(cs: *const c_char, ct: *const c_char) -> c_int;
275+
pub fn strncmp(cs: *const c_char, ct: *const c_char, n: size_t) -> c_int;
276+
pub fn strcoll(cs: *const c_char, ct: *const c_char) -> c_int;
277+
pub fn strchr(cs: *const c_char, c: c_int) -> *mut c_char;
278+
pub fn strrchr(cs: *const c_char, c: c_int) -> *mut c_char;
279+
pub fn strspn(cs: *const c_char, ct: *const c_char) -> size_t;
280+
pub fn strcspn(cs: *const c_char, ct: *const c_char) -> size_t;
281+
pub fn strdup(cs: *const c_char) -> *mut c_char;
282+
pub fn strpbrk(cs: *const c_char, ct: *const c_char) -> *mut c_char;
283+
pub fn strstr(cs: *const c_char, ct: *const c_char) -> *mut c_char;
284+
pub fn strlen(cs: *const c_char) -> size_t;
285+
pub fn strnlen(cs: *const c_char, maxlen: size_t) -> size_t;
286+
#[cfg_attr(
287+
all(target_os = "macos", target_arch = "x86"),
288+
link_name = "strerror$UNIX2003"
289+
)]
290+
pub fn strerror(n: c_int) -> *mut c_char;
291+
pub fn strtok(s: *mut c_char, t: *const c_char) -> *mut c_char;
292+
pub fn strxfrm(s: *mut c_char, ct: *const c_char, n: size_t) -> size_t;
293+
pub fn wcslen(buf: *const wchar_t) -> size_t;
294+
pub fn wcstombs(dest: *mut c_char, src: *const wchar_t, n: size_t) -> ::size_t;
304295

305-
pub fn memchr(cx: *const c_void, c: c_int, n: size_t) -> *mut c_void;
306-
pub fn memcmp(cx: *const c_void, ct: *const c_void, n: size_t) -> c_int;
307-
pub fn memcpy(dest: *mut c_void, src: *const c_void, n: size_t) -> *mut c_void;
308-
pub fn memmove(dest: *mut c_void, src: *const c_void, n: size_t) -> *mut c_void;
309-
pub fn memset(dest: *mut c_void, c: c_int, n: size_t) -> *mut c_void;
310-
}
296+
pub fn memchr(cx: *const c_void, c: c_int, n: size_t) -> *mut c_void;
297+
pub fn memcmp(cx: *const c_void, ct: *const c_void, n: size_t) -> c_int;
298+
pub fn memcpy(dest: *mut c_void, src: *const c_void, n: size_t) -> *mut c_void;
299+
pub fn memmove(dest: *mut c_void, src: *const c_void, n: size_t) -> *mut c_void;
300+
pub fn memset(dest: *mut c_void, c: c_int, n: size_t) -> *mut c_void;
301+
}
311302

312-
// These are all inline functions on android, so they end up just being entirely
313-
// missing on that platform.
314-
#[cfg(not(target_os = "android"))]
315-
extern {
316-
pub fn abs(i: c_int) -> c_int;
317-
pub fn atof(s: *const c_char) -> c_double;
318-
pub fn labs(i: c_long) -> c_long;
319-
pub fn rand() -> c_int;
320-
pub fn srand(seed: c_uint);
321-
}
322-
}
303+
// These are all inline functions on android, so they end up just being entirely
304+
// missing on that platform.
305+
306+
#[cfg(not(target_os = "android"))]
307+
extern "C" {
308+
pub fn abs(i: c_int) -> c_int;
309+
pub fn atof(s: *const c_char) -> c_double;
310+
pub fn labs(i: c_long) -> c_long;
311+
pub fn rand() -> c_int;
312+
pub fn srand(seed: c_uint);
323313
}
324314

325315
cfg_if! {

0 commit comments

Comments
 (0)