summaryrefslogtreecommitdiff
path: root/libc/src/unix/mod.rs
diff options
context:
space:
mode:
Diffstat (limited to 'libc/src/unix/mod.rs')
-rw-r--r--libc/src/unix/mod.rs246
1 files changed, 235 insertions, 11 deletions
diff --git a/libc/src/unix/mod.rs b/libc/src/unix/mod.rs
index 57067e0..370d7f4 100644
--- a/libc/src/unix/mod.rs
+++ b/libc/src/unix/mod.rs
@@ -5,6 +5,34 @@
use dox::Option;
+pub type int8_t = i8;
+pub type int16_t = i16;
+pub type int32_t = i32;
+pub type int64_t = i64;
+pub type uint8_t = u8;
+pub type uint16_t = u16;
+pub type uint32_t = u32;
+pub type uint64_t = u64;
+
+pub type c_schar = i8;
+pub type c_uchar = u8;
+pub type c_short = i16;
+pub type c_ushort = u16;
+pub type c_int = i32;
+pub type c_uint = u32;
+pub type c_float = f32;
+pub type c_double = f64;
+pub type c_longlong = i64;
+pub type c_ulonglong = u64;
+pub type intmax_t = i64;
+pub type uintmax_t = u64;
+
+pub type size_t = usize;
+pub type ptrdiff_t = isize;
+pub type intptr_t = isize;
+pub type uintptr_t = usize;
+pub type ssize_t = isize;
+
pub type pid_t = i32;
pub type uid_t = u32;
pub type gid_t = u32;
@@ -104,8 +132,10 @@ s! {
pub s_addr: in_addr_t,
}
+ #[cfg_attr(feature = "align", repr(align(4)))]
pub struct in6_addr {
pub s6_addr: [u8; 16],
+ #[cfg(not(feature = "align"))]
__align: [u32; 0],
}
@@ -186,10 +216,14 @@ s! {
}
}
+pub const INT_MIN: c_int = -2147483648;
+pub const INT_MAX: c_int = 2147483647;
+
pub const SIG_DFL: sighandler_t = 0 as sighandler_t;
pub const SIG_IGN: sighandler_t = 1 as sighandler_t;
pub const SIG_ERR: sighandler_t = !0 as sighandler_t;
+pub const DT_UNKNOWN: u8 = 0;
pub const DT_FIFO: u8 = 1;
pub const DT_CHR: u8 = 2;
pub const DT_DIR: u8 = 4;
@@ -267,22 +301,35 @@ pub const INADDR_ANY: in_addr_t = 0;
pub const INADDR_BROADCAST: in_addr_t = 4294967295;
pub const INADDR_NONE: in_addr_t = 4294967295;
+pub const ARPOP_REQUEST: u16 = 1;
+pub const ARPOP_REPLY: u16 = 2;
+
+pub const ATF_COM: ::c_int = 0x02;
+pub const ATF_PERM: ::c_int = 0x04;
+pub const ATF_PUBL: ::c_int = 0x08;
+pub const ATF_USETRAILERS: ::c_int = 0x10;
+
cfg_if! {
- if #[cfg(dox)] {
+ if #[cfg(cross_platform_docs)] {
// on dox builds don't pull in anything
} else if #[cfg(target_os = "l4re")] {
// required libraries for L4Re are linked externally, ATM
} else if #[cfg(feature = "use_std")] {
// cargo build, don't pull in anything extra as the libstd dep
// already pulls in all libs.
- } else if #[cfg(any(all(target_env = "musl", not(target_arch = "mips"))))] {
- #[link(name = "c", kind = "static", cfg(target_feature = "crt-static"))]
- #[link(name = "c", cfg(not(target_feature = "crt-static")))]
+ } else if #[cfg(target_env = "musl")] {
+ #[cfg_attr(feature = "rustc-dep-of-std",
+ link(name = "c", kind = "static",
+ cfg(target_feature = "crt-static")))]
+ #[cfg_attr(feature = "rustc-dep-of-std",
+ link(name = "c", cfg(not(target_feature = "crt-static"))))]
extern {}
} else if #[cfg(target_os = "emscripten")] {
#[link(name = "c")]
extern {}
- } else if #[cfg(all(target_os = "netbsd", target_vendor = "rumprun"))] {
+ } else if #[cfg(all(target_os = "netbsd",
+ feature = "rustc-dep-of-std",
+ target_vendor = "rumprun"))] {
// Since we don't use -nodefaultlibs on Rumprun, libc is always pulled
// in automatically by the linker. We avoid passing it explicitly, as it
// causes some versions of binutils to crash with an assertion failure.
@@ -300,14 +347,15 @@ cfg_if! {
#[link(name = "root")]
#[link(name = "network")]
extern {}
- } else if #[cfg(target_os = "fuchsia")] {
- #[link(name = "c")]
- #[link(name = "fdio")]
- extern {}
} else if #[cfg(target_env = "newlib")] {
#[link(name = "c")]
#[link(name = "m")]
extern {}
+ } else if #[cfg(target_os = "hermit")] {
+ // no_default_libraries is set to false for HermitCore, so only a link
+ // to "pthread" needs to be added.
+ #[link(name = "pthread")]
+ extern {}
} else {
#[link(name = "c")]
#[link(name = "m")]
@@ -317,6 +365,138 @@ cfg_if! {
}
}
+pub enum FILE {}
+pub enum fpos_t {} // TODO: fill this out with a struct
+
+extern {
+ pub fn isalnum(c: c_int) -> c_int;
+ pub fn isalpha(c: c_int) -> c_int;
+ pub fn iscntrl(c: c_int) -> c_int;
+ pub fn isdigit(c: c_int) -> c_int;
+ pub fn isgraph(c: c_int) -> c_int;
+ pub fn islower(c: c_int) -> c_int;
+ pub fn isprint(c: c_int) -> c_int;
+ pub fn ispunct(c: c_int) -> c_int;
+ pub fn isspace(c: c_int) -> c_int;
+ pub fn isupper(c: c_int) -> c_int;
+ pub fn isxdigit(c: c_int) -> c_int;
+ pub fn tolower(c: c_int) -> c_int;
+ pub fn toupper(c: c_int) -> c_int;
+ #[cfg_attr(
+ all(target_os = "macos", target_arch = "x86"),
+ link_name = "fopen$UNIX2003"
+ )]
+ pub fn fopen(filename: *const c_char, mode: *const c_char) -> *mut FILE;
+ #[cfg_attr(
+ all(target_os = "macos", target_arch = "x86"),
+ link_name = "freopen$UNIX2003"
+ )]
+ pub fn freopen(filename: *const c_char, mode: *const c_char,
+ file: *mut FILE) -> *mut FILE;
+ pub fn fflush(file: *mut FILE) -> c_int;
+ pub fn fclose(file: *mut FILE) -> c_int;
+ pub fn remove(filename: *const c_char) -> c_int;
+ pub fn rename(oldname: *const c_char, newname: *const c_char) -> c_int;
+ pub fn tmpfile() -> *mut FILE;
+ pub fn setvbuf(stream: *mut FILE, buffer: *mut c_char, mode: c_int,
+ size: size_t) -> c_int;
+ pub fn setbuf(stream: *mut FILE, buf: *mut c_char);
+ pub fn getchar() -> c_int;
+ pub fn putchar(c: c_int) -> c_int;
+ pub fn fgetc(stream: *mut FILE) -> c_int;
+ pub fn fgets(buf: *mut c_char, n: c_int, stream: *mut FILE) -> *mut c_char;
+ pub fn fputc(c: c_int, stream: *mut FILE) -> c_int;
+ #[cfg_attr(
+ all(target_os = "macos", target_arch = "x86"),
+ link_name = "fputs$UNIX2003"
+ )]
+ pub fn fputs(s: *const c_char, stream: *mut FILE) -> c_int;
+ pub fn puts(s: *const c_char) -> c_int;
+ pub fn ungetc(c: c_int, stream: *mut FILE) -> c_int;
+ pub fn fread(ptr: *mut c_void, size: size_t, nobj: size_t,
+ stream: *mut FILE) -> size_t;
+ #[cfg_attr(
+ all(target_os = "macos", target_arch = "x86"),
+ link_name = "fwrite$UNIX2003"
+ )]
+ pub fn fwrite(ptr: *const c_void, size: size_t, nobj: size_t,
+ stream: *mut FILE) -> size_t;
+ pub fn fseek(stream: *mut FILE, offset: c_long, whence: c_int) -> c_int;
+ pub fn ftell(stream: *mut FILE) -> c_long;
+ pub fn rewind(stream: *mut FILE);
+ #[cfg_attr(target_os = "netbsd", link_name = "__fgetpos50")]
+ pub fn fgetpos(stream: *mut FILE, ptr: *mut fpos_t) -> c_int;
+ #[cfg_attr(target_os = "netbsd", link_name = "__fsetpos50")]
+ pub fn fsetpos(stream: *mut FILE, ptr: *const fpos_t) -> c_int;
+ pub fn feof(stream: *mut FILE) -> c_int;
+ pub fn ferror(stream: *mut FILE) -> c_int;
+ pub fn perror(s: *const c_char);
+ pub fn atoi(s: *const c_char) -> c_int;
+ #[cfg_attr(
+ all(target_os = "macos", target_arch = "x86"),
+ link_name = "strtod$UNIX2003"
+ )]
+ pub fn strtod(s: *const c_char, endp: *mut *mut c_char) -> c_double;
+ pub fn strtol(s: *const c_char, endp: *mut *mut c_char,
+ base: c_int) -> c_long;
+ pub fn strtoul(s: *const c_char, endp: *mut *mut c_char,
+ base: c_int) -> c_ulong;
+ pub fn calloc(nobj: size_t, size: size_t) -> *mut c_void;
+ pub fn malloc(size: size_t) -> *mut c_void;
+ pub fn realloc(p: *mut c_void, size: size_t) -> *mut c_void;
+ pub fn free(p: *mut c_void);
+ pub fn abort() -> !;
+ pub fn exit(status: c_int) -> !;
+ pub fn _exit(status: c_int) -> !;
+ pub fn atexit(cb: extern fn()) -> c_int;
+ #[cfg_attr(
+ all(target_os = "macos", target_arch = "x86"),
+ link_name = "system$UNIX2003"
+ )]
+ pub fn system(s: *const c_char) -> c_int;
+ pub fn getenv(s: *const c_char) -> *mut c_char;
+
+ pub fn strcpy(dst: *mut c_char, src: *const c_char) -> *mut c_char;
+ pub fn strncpy(dst: *mut c_char, src: *const c_char,
+ n: size_t) -> *mut c_char;
+ pub fn strcat(s: *mut c_char, ct: *const c_char) -> *mut c_char;
+ pub fn strncat(s: *mut c_char, ct: *const c_char,
+ n: size_t) -> *mut c_char;
+ pub fn strcmp(cs: *const c_char, ct: *const c_char) -> c_int;
+ pub fn strncmp(cs: *const c_char, ct: *const c_char, n: size_t) -> c_int;
+ pub fn strcoll(cs: *const c_char, ct: *const c_char) -> c_int;
+ pub fn strchr(cs: *const c_char, c: c_int) -> *mut c_char;
+ pub fn strrchr(cs: *const c_char, c: c_int) -> *mut c_char;
+ pub fn strspn(cs: *const c_char, ct: *const c_char) -> size_t;
+ pub fn strcspn(cs: *const c_char, ct: *const c_char) -> size_t;
+ pub fn strdup(cs: *const c_char) -> *mut c_char;
+ pub fn strpbrk(cs: *const c_char, ct: *const c_char) -> *mut c_char;
+ pub fn strstr(cs: *const c_char, ct: *const c_char) -> *mut c_char;
+ pub fn strcasecmp(s1: *const c_char, s2: *const c_char) -> c_int;
+ pub fn strncasecmp(s1: *const c_char, s2: *const c_char,
+ n: size_t) -> c_int;
+ pub fn strlen(cs: *const c_char) -> size_t;
+ pub fn strnlen(cs: *const c_char, maxlen: size_t) -> size_t;
+ #[cfg_attr(
+ all(target_os = "macos", target_arch = "x86"),
+ link_name = "strerror$UNIX2003"
+ )]
+ pub fn strerror(n: c_int) -> *mut c_char;
+ pub fn strtok(s: *mut c_char, t: *const c_char) -> *mut c_char;
+ pub fn strxfrm(s: *mut c_char, ct: *const c_char, n: size_t) -> size_t;
+ pub fn wcslen(buf: *const wchar_t) -> size_t;
+ pub fn wcstombs(dest: *mut c_char, src: *const wchar_t,
+ n: size_t) -> ::size_t;
+
+ pub fn memchr(cx: *const c_void, c: c_int, n: size_t) -> *mut c_void;
+ pub fn memcmp(cx: *const c_void, ct: *const c_void, n: size_t) -> c_int;
+ pub fn memcpy(dest: *mut c_void, src: *const c_void,
+ n: size_t) -> *mut c_void;
+ pub fn memmove(dest: *mut c_void, src: *const c_void,
+ n: size_t) -> *mut c_void;
+ pub fn memset(dest: *mut c_void, c: c_int, n: size_t) -> *mut c_void;
+}
+
extern {
#[cfg_attr(target_os = "netbsd", link_name = "__getpwnam50")]
pub fn getpwnam(name: *const ::c_char) -> *mut passwd;
@@ -379,12 +559,14 @@ extern {
#[cfg_attr(target_os = "macos", link_name = "fstat$INODE64")]
#[cfg_attr(target_os = "netbsd", link_name = "__fstat50")]
+ #[cfg_attr(target_os = "freebsd", link_name = "fstat@FBSD_1.0")]
pub fn fstat(fildes: ::c_int, buf: *mut stat) -> ::c_int;
pub fn mkdir(path: *const c_char, mode: mode_t) -> ::c_int;
#[cfg_attr(target_os = "macos", link_name = "stat$INODE64")]
#[cfg_attr(target_os = "netbsd", link_name = "__stat50")]
+ #[cfg_attr(target_os = "freebsd", link_name = "stat@FBSD_1.0")]
pub fn stat(path: *const c_char, buf: *mut stat) -> ::c_int;
pub fn pclose(stream: *mut ::FILE) -> ::c_int;
@@ -409,12 +591,21 @@ extern {
link_name = "opendir$INODE64$UNIX2003")]
#[cfg_attr(target_os = "netbsd", link_name = "__opendir30")]
pub fn opendir(dirname: *const c_char) -> *mut ::DIR;
+
+ #[cfg_attr(all(target_os = "macos", target_arch = "x86_64"),
+ link_name = "fdopendir$INODE64")]
+ #[cfg_attr(all(target_os = "macos", target_arch = "x86"),
+ link_name = "fdopendir$INODE64$UNIX2003")]
+ pub fn fdopendir(fd: ::c_int) -> *mut ::DIR;
+
#[cfg_attr(target_os = "macos", link_name = "readdir$INODE64")]
#[cfg_attr(target_os = "netbsd", link_name = "__readdir30")]
+ #[cfg_attr(target_os = "freebsd", link_name = "readdir@FBSD_1.0")]
pub fn readdir(dirp: *mut ::DIR) -> *mut ::dirent;
#[cfg_attr(target_os = "macos", link_name = "readdir_r$INODE64")]
#[cfg_attr(target_os = "netbsd", link_name = "__readdir_r30")]
#[cfg_attr(target_os = "solaris", link_name = "__posix_readdir_r")]
+ #[cfg_attr(target_os = "freebsd", link_name = "readdir_r@FBSD_1.0")]
pub fn readdir_r(dirp: *mut ::DIR, entry: *mut ::dirent,
result: *mut *mut ::dirent) -> ::c_int;
#[cfg_attr(all(target_os = "macos", target_arch = "x86"),
@@ -437,6 +628,7 @@ extern {
owner: ::uid_t, group: ::gid_t,
flags: ::c_int) -> ::c_int;
#[cfg_attr(target_os = "macos", link_name = "fstatat$INODE64")]
+ #[cfg_attr(target_os = "freebsd", link_name = "fstatat@FBSD_1.1")]
pub fn fstatat(dirfd: ::c_int, pathname: *const ::c_char,
buf: *mut stat, flags: ::c_int) -> ::c_int;
pub fn linkat(olddirfd: ::c_int, oldpath: *const ::c_char,
@@ -519,6 +711,7 @@ extern {
-> ::ssize_t;
pub fn rmdir(path: *const c_char) -> ::c_int;
pub fn seteuid(uid: uid_t) -> ::c_int;
+ pub fn setegid(gid: gid_t) -> ::c_int;
pub fn setgid(gid: gid_t) -> ::c_int;
pub fn setpgid(pid: pid_t, pgid: pid_t) -> ::c_int;
pub fn setsid() -> pid_t;
@@ -562,6 +755,9 @@ extern {
#[cfg_attr(all(target_os = "macos", target_arch = "x86"),
link_name = "kill$UNIX2003")]
pub fn kill(pid: pid_t, sig: ::c_int) -> ::c_int;
+ #[cfg_attr(all(target_os = "macos", target_arch = "x86"),
+ link_name = "killpg$UNIX2003")]
+ pub fn killpg(pgrp: pid_t, sig: ::c_int) -> ::c_int;
pub fn mlock(addr: *const ::c_void, len: ::size_t) -> ::c_int;
pub fn munlock(addr: *const ::c_void, len: ::size_t) -> ::c_int;
@@ -587,6 +783,7 @@ extern {
#[cfg_attr(target_os = "macos", link_name = "lstat$INODE64")]
#[cfg_attr(target_os = "netbsd", link_name = "__lstat50")]
+ #[cfg_attr(target_os = "freebsd", link_name = "lstat@FBSD_1.0")]
pub fn lstat(path: *const c_char, buf: *mut stat) -> ::c_int;
#[cfg_attr(all(target_os = "macos", target_arch = "x86"),
@@ -605,6 +802,7 @@ extern {
pub fn symlink(path1: *const c_char,
path2: *const c_char) -> ::c_int;
+ pub fn truncate(path: *const c_char, length: off_t) -> ::c_int;
pub fn ftruncate(fd: ::c_int, length: off_t) -> ::c_int;
pub fn signal(signum: ::c_int, handler: sighandler_t) -> sighandler_t;
@@ -771,6 +969,7 @@ extern {
pub fn difftime(time1: time_t, time0: time_t) -> ::c_double;
#[cfg_attr(target_os = "netbsd", link_name = "__mknod50")]
+ #[cfg_attr(target_os = "freebsd", link_name = "mknod@FBSD_1.0")]
pub fn mknod(pathname: *const ::c_char, mode: ::mode_t,
dev: ::dev_t) -> ::c_int;
pub fn uname(buf: *mut ::utsname) -> ::c_int;
@@ -899,6 +1098,7 @@ extern {
pub fn openlog(ident: *const ::c_char, logopt: ::c_int, facility: ::c_int);
pub fn closelog();
pub fn setlogmask(maskpri: ::c_int) -> ::c_int;
+ #[cfg_attr(target_os = "macos", link_name = "syslog$DARWIN_EXTSN")]
pub fn syslog(priority: ::c_int, message: *const ::c_char, ...);
#[cfg_attr(all(target_os = "macos", target_arch = "x86"),
link_name = "nice$UNIX2003")]
@@ -908,6 +1108,10 @@ extern {
pub fn posix_openpt(flags: ::c_int) -> ::c_int;
pub fn ptsname(fd: ::c_int) -> *mut ::c_char;
pub fn unlockpt(fd: ::c_int) -> ::c_int;
+
+ pub fn strcasestr(cs: *const c_char, ct: *const c_char) -> *mut c_char;
+ pub fn getline (lineptr: *mut *mut c_char, n: *mut size_t,
+ stream: *mut FILE) -> ssize_t;
}
cfg_if! {
@@ -919,8 +1123,7 @@ cfg_if! {
pub use self::newlib::*;
} else if #[cfg(any(target_os = "linux",
target_os = "android",
- target_os = "emscripten",
- target_os = "fuchsia"))] {
+ target_os = "emscripten"))] {
mod notbsd;
pub use self::notbsd::*;
} else if #[cfg(any(target_os = "macos",
@@ -938,7 +1141,28 @@ cfg_if! {
} else if #[cfg(target_os = "haiku")] {
mod haiku;
pub use self::haiku::*;
+ } else if #[cfg(target_os = "hermit")] {
+ mod hermit;
+ pub use self::hermit::*;
} else {
// Unknown target_os
}
}
+
+cfg_if! {
+ if #[cfg(core_cvoid)] {
+ pub use core::ffi::c_void;
+ } else {
+ // Use repr(u8) as LLVM expects `void*` to be the same as `i8*` to help
+ // enable more optimization opportunities around it recognizing things
+ // like malloc/free.
+ #[repr(u8)]
+ pub enum c_void {
+ // Two dummy variants so the #[repr] attribute can be used.
+ #[doc(hidden)]
+ __variant1,
+ #[doc(hidden)]
+ __variant2,
+ }
+ }
+}