Add libc module to libcore and utility file to help generate it.

This commit is contained in:
Graydon Hoare 2012-02-27 17:22:42 -08:00
parent 61691c2428
commit 324ecb58a7
4 changed files with 1144 additions and 7 deletions

237
src/etc/libc.c Normal file
View File

@ -0,0 +1,237 @@
/*
* This calculates the platform-variable portion of the libc module.
* Move code in here only as you discover it is platform-variable.
*
*/
/* c95 */
#include <stddef.h>
#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
/* c99 */
#include <inttypes.h>
/* posix */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#define S(T) ((((T)-1)<0) ? 'i' : 'u')
#define B(T) (((int)sizeof(T)) * CHAR_BIT)
#define put_type(N,T) \
printf(" type %s = %c%d;\n", N, S(T), B(T))
#define CT(T) ((((T)-1)<0) ? "int" : "uint")
#define CS(T) ((((T)-1)<0) ? "" : "_u")
#define put_const(N,T) \
printf(" const %s : %s = %d%s;\n", \
#N, CT(T), N, CS(T))
void c95_types() {
printf(" mod c95 {\n");
put_type("c_char", char);
put_type("c_schar", signed char);
put_type("c_uchar", unsigned char);
put_type("c_short", short);
put_type("c_ushort", unsigned short);
put_type("c_int", int);
put_type("c_uint", unsigned int);
put_type("c_long", long);
put_type("c_ulong", unsigned long);
put_type("size_t", size_t);
put_type("ptrdiff_t", ptrdiff_t);
put_type("clock_t", clock_t);
put_type("time_t", time_t);
printf(" }\n");
}
void c99_types() {
printf(" mod c99 {\n");
put_type("c_longlong", long long);
put_type("c_ulonglong", unsigned long long);
put_type("intptr_t", intptr_t);
put_type("uintptr_t", uintptr_t);
printf(" }\n");
}
void posix88_types() {
printf(" mod posix88 {\n");
put_type("off_t", off_t);
put_type("dev_t", dev_t);
put_type("ino_t", ino_t);
put_type("pid_t", pid_t);
#ifndef __WIN32__
put_type("uid_t", uid_t);
put_type("gid_t", gid_t);
#endif
put_type("useconds_t", useconds_t);
put_type("mode_t", mode_t);
put_type("ssize_t", ssize_t);
printf(" }\n");
}
void extra_types() {
printf(" mod extra {\n");
printf(" }\n");
}
void c95_consts() {
printf(" mod c95 {\n");
put_const(EXIT_FAILURE, int);
put_const(EXIT_SUCCESS, int);
put_const(RAND_MAX, int);
put_const(EOF, int);
put_const(SEEK_SET, int);
put_const(SEEK_CUR, int);
put_const(SEEK_END, int);
put_const(_IOFBF, int);
put_const(_IONBF, int);
put_const(_IOLBF, int);
put_const(BUFSIZ, size_t);
put_const(FOPEN_MAX, size_t);
put_const(FILENAME_MAX, size_t);
put_const(L_tmpnam, size_t);
put_const(TMP_MAX, size_t);
printf(" }\n");
}
void posix88_consts() {
printf(" mod posix88 {\n");
put_const(O_RDONLY, int);
put_const(O_WRONLY, int);
put_const(O_RDWR, int);
put_const(O_APPEND, int);
put_const(O_CREAT, int);
put_const(O_EXCL, int);
put_const(O_TRUNC, int);
put_const(S_IFIFO, int);
put_const(S_IFCHR, int);
put_const(S_IFBLK, int);
put_const(S_IFDIR, int);
put_const(S_IFREG, int);
put_const(S_IFMT, int);
put_const(S_IEXEC, int);
put_const(S_IWRITE, int);
put_const(S_IREAD, int);
put_const(S_IRWXU, int);
put_const(S_IXUSR, int);
put_const(S_IWUSR, int);
put_const(S_IRUSR, int);
#ifdef F_OK
put_const(F_OK, int);
#endif
#ifdef R_OK
put_const(R_OK, int);
#endif
#ifdef W_OK
put_const(W_OK, int);
#endif
#ifdef X_OK
put_const(X_OK, int);
#endif
#ifdef STDERR_FILENO
put_const(STDERR_FILENO, int);
#endif
#ifdef STDIN_FILENO
put_const(STDIN_FILENO, int);
#endif
#ifdef STDOUT_FILENO
put_const(STDOUT_FILENO, int);
#endif
#ifdef F_LOCK
put_const(F_LOCK, int);
#endif
#ifdef F_TEST
put_const(F_TEST, int);
#endif
#ifdef F_TLOCK
put_const(F_TLOCK, int);
#endif
#ifdef F_ULOCK
put_const(F_ULOCK, int);
#endif
printf(" }\n");
}
void extra_consts() {
printf(" mod extra {\n");
#ifdef O_RSYNC
put_const(O_RSYNC, int);
#endif
#ifdef O_DSYNC
put_const(O_DSYNC, int);
#endif
#ifdef O_SYNC
put_const(O_SYNC, int);
#endif
#ifdef O_TEXT
put_const(O_TEXT, int);
#endif
#ifdef O_BINARY
put_const(O_BINARY, int);
#endif
#ifdef O_IRUSR
put_const(O_IRUSR, int);
#endif
#ifdef O_IWUSR
put_const(O_IWUSR, int);
#endif
printf(" }\n");
}
int main() {
printf("mod types {");
c95_types();
c99_types();
posix88_types();
extra_types();
printf("}\n");
printf("mod consts {\n");
c95_consts();
posix88_consts();
extra_consts();
printf("}\n");
}

View File

@ -81,6 +81,7 @@ mod to_str;
// Runtime and language-primitive support
mod libc;
mod ctypes;
mod math;
mod cmath;

904
src/libcore/libc.rs Normal file
View File

@ -0,0 +1,904 @@
//
// We consider the following specs reasonably normative with respect
// to interoperating with the C standard library (libc/msvcrt):
//
// - ISO 9899:1990 ('C95', 'ANSI C', 'Standard C'), NA1, 1995.
// - ISO 9899:1999 ('C99' or 'C9x').
// - ISO 9945:1988 / IEEE 1003.1-1988 ('POSIX.1').
// - ISO 9945:2008 / IEEE 1003.1-2008 ('POSIX:2008').
//
// Despite having several names each, these are *reasonably* coherent
// point-in-time, list-of-definition sorts of specs. You can get each under a
// variety of names but will wind up with the same definition in each case.
//
// Our interface to these libraries is complicated by the non-universality of
// conformance to any of them. About the only thing universally supported is
// the first (C95), beyond that definitions quickly become absent on various
// platforms.
//
// We therefore wind up dividing our module-space up (mostly for the sake of
// sanity while editing) into definitions common-to-all (held in modules named
// c95, c99, posix88, and posix08) and definitions that appear only on *some*
// platforms (named 'extra').
//
// Initial glob-exports mean that all the contents of all the modules
// wind up exported, if you're interested in writing platform-specific code.
// FIXME: change these to glob-exports when sufficiently supported.
import types::common::c95::*;
import types::common::posix88::*;
import types::os::arch::c95::*;
import types::os::arch::c99::*;
import types::os::arch::posix88::*;
import types::os::arch::extra::*;
import consts::os::c95::*;
import consts::os::posix88::*;
import consts::os::extra::*;
import funcs::c95::ctype::*;
import funcs::c95::stdio::*;
import funcs::c95::stdlib::*;
import funcs::c95::string::*;
import funcs::posix88::stat::*;
import funcs::posix88::stdio::*;
import funcs::posix88::fcntl::*;
import funcs::posix88::dirent::*;
import funcs::posix88::unistd::*;
// Explicit export lists for the intersection (provided here) mean that
// you can write more-platform-agnostic code if you stick to just these
// symbols.
export c_double, c_void, FILE, fpos_t;
export DIR, dirent;
export c_char, c_schar, c_uchar;
export c_short, c_ushort, c_int, c_uint, c_long, c_ulong;
export size_t, ptrdiff_t, clock_t, time_t;
export c_longlong, c_ulonglong, intptr_t, uintptr_t;
export off_t, dev_t, ino_t, pid_t, mode_t, ssize_t;
export isalnum, isalpha, iscntrl, isdigit, islower, isprint, ispunct,
isspace, isupper, isxdigit, tolower, toupper;
export fopen, freopen, fflush, fclose, remove, tmpfile, setvbuf, setbuf,
fgetc, fgets, fputc, fputs, puts, ungetc, fread, fwrite, fseek, ftell,
rewind, fgetpos, fsetpos, feof, ferror, perror;
export abs, labs, atof, atoi, strtod, strtol, strtoul, calloc, malloc,
realloc, free, abort, exit, system, getenv, rand, srand;
export strcpy, strncpy, strcat, strncat, strcmp, strncmp, strcoll, strchr,
strrchr, strspn, strcspn, strpbrk, strstr, strlen, strerror, strtok,
strxfrm, memcpy, memmove, memcmp, memchr, memset;
export chmod, mkdir;
export popen, pclose;
export open, creat;
export access, chdir, close, dup, dup2, execv, execve, execvp, getcwd,
getpid, isatty, lseek, pipe, read, rmdir, unlink, write;
mod types {
// Types tend to vary *per architecture* so we pull their definitions out
// into this module.
// Standard types that are opaque or common, so are not per-target.
mod common {
mod c95 {
type c_double = float;
enum c_void {}
enum FILE {}
enum fpos_t {}
}
mod posix88 {
enum DIR {}
enum dirent {}
}
}
// Standard types that are scalar but vary by OS and arch.
#[cfg(target_os = "linux")]
mod os {
#[cfg(target_arch = "x86")]
mod arch {
mod c95 {
type c_char = i8;
type c_schar = i8;
type c_uchar = u8;
type c_short = i16;
type c_ushort = u16;
type c_int = i32;
type c_uint = u32;
type c_long = i32;
type c_ulong = u32;
type size_t = u32;
type ptrdiff_t = i32;
type clock_t = i32;
type time_t = i32;
}
mod c99 {
type c_longlong = i64;
type c_ulonglong = u64;
type intptr_t = i32;
type uintptr_t = u32;
}
mod posix88 {
type off_t = i32;
type dev_t = u64;
type ino_t = u32;
type pid_t = i32;
type uid_t = u32;
type gid_t = u32;
type useconds_t = u32;
type mode_t = u32;
type ssize_t = i32;
}
mod extra {
}
}
#[cfg(target_arch = "x86_64")]
mod arch {
mod c95 {
type c_char = i8;
type c_schar = i8;
type c_uchar = u8;
type c_short = i16;
type c_ushort = u16;
type c_int = i32;
type c_uint = u32;
type c_long = i64;
type c_ulong = u64;
type size_t = u64;
type ptrdiff_t = i64;
type clock_t = i64;
type time_t = i64;
}
mod c99 {
type c_longlong = i64;
type c_ulonglong = u64;
type intptr_t = i64;
type uintptr_t = u64;
}
mod posix88 {
type off_t = i64;
type dev_t = u64;
type ino_t = u64;
type pid_t = i32;
type uid_t = u32;
type gid_t = u32;
type useconds_t = u32;
type mode_t = u32;
type ssize_t = i64;
}
mod extra {
}
}
}
#[cfg(target_os = "freebsd")]
mod os {
#[cfg(target_arch = "x86_64")]
mod arch {
mod c95 {
type c_char = i8;
type c_schar = i8;
type c_uchar = u8;
type c_short = i16;
type c_ushort = u16;
type c_int = i32;
type c_uint = u32;
type c_long = i64;
type c_ulong = u64;
type size_t = u64;
type ptrdiff_t = i64;
type clock_t = i32;
type time_t = i64;
}
mod c99 {
type c_longlong = i64;
type c_ulonglong = u64;
type intptr_t = i64;
type uintptr_t = u64;
}
mod posix88 {
type off_t = i64;
type dev_t = u32;
type ino_t = u32;
type pid_t = i32;
type uid_t = u32;
type gid_t = u32;
type useconds_t = u32;
type mode_t = u16;
type ssize_t = i64;
}
mod extra {
}
}
}
#[cfg(target_os = "win32")]
mod os {
#[cfg(target_arch = "x86")]
mod arch {
mod c95 {
type c_char = i8;
type c_schar = i8;
type c_uchar = u8;
type c_short = i16;
type c_ushort = u16;
type c_int = i32;
type c_uint = u32;
type c_long = i32;
type c_ulong = u32;
type size_t = u32;
type ptrdiff_t = i32;
type clock_t = i32;
type time_t = i32;
}
mod c99 {
type c_longlong = i64;
type c_ulonglong = u64;
type intptr_t = i32;
type uintptr_t = u32;
}
mod posix88 {
type off_t = i32;
type dev_t = u32;
type ino_t = i16;
type pid_t = i32;
type useconds_t = u32;
type mode_t = u16;
type ssize_t = i32;
}
mod extra {
}
}
}
#[cfg(target_os = "macos")]
mod os {
#[cfg(target_arch = "x86")]
mod arch {
mod c95 {
type c_char = i8;
type c_schar = i8;
type c_uchar = u8;
type c_short = i16;
type c_ushort = u16;
type c_int = i32;
type c_uint = u32;
type c_long = i32;
type c_ulong = u32;
type size_t = u32;
type ptrdiff_t = i32;
type clock_t = u32;
type time_t = i32;
}
mod c99 {
type c_longlong = i64;
type c_ulonglong = u64;
type intptr_t = i32;
type uintptr_t = u32;
}
mod posix88 {
type off_t = i64;
type dev_t = i32;
type ino_t = u64;
type pid_t = i32;
type uid_t = u32;
type gid_t = u32;
type useconds_t = u32;
type mode_t = u16;
type ssize_t = i32;
}
mod extra {
}
}
#[cfg(target_arch = "x86_64")]
mod arch {
mod c95 {
type c_char = i8;
type c_schar = i8;
type c_uchar = u8;
type c_short = i16;
type c_ushort = u16;
type c_int = i32;
type c_uint = u32;
type c_long = i64;
type c_ulong = u64;
type size_t = u64;
type ptrdiff_t = i64;
type clock_t = u64;
type time_t = i64;
}
mod c99 {
type c_longlong = i64;
type c_ulonglong = u64;
type intptr_t = i64;
type uintptr_t = u64;
}
mod posix88 {
type off_t = i64;
type dev_t = i32;
type ino_t = u64;
type pid_t = i32;
type uid_t = u32;
type gid_t = u32;
type useconds_t = u32;
type mode_t = u16;
type ssize_t = i64;
}
mod extra {
}
}
}
}
mod consts {
// Consts tend to vary per OS so we pull their definitions out
// into this module.
#[cfg(target_os = "win32")]
mod os {
mod c95 {
const EXIT_FAILURE : int = 1;
const EXIT_SUCCESS : int = 0;
const RAND_MAX : int = 32767;
const EOF : int = -1;
const SEEK_SET : int = 0;
const SEEK_CUR : int = 1;
const SEEK_END : int = 2;
const _IOFBF : int = 0;
const _IONBF : int = 4;
const _IOLBF : int = 64;
const BUFSIZ : uint = 512_u;
const FOPEN_MAX : uint = 20_u;
const FILENAME_MAX : uint = 260_u;
const L_tmpnam : uint = 16_u;
const TMP_MAX : uint = 32767_u;
}
mod posix88 {
const O_RDONLY : int = 0;
const O_WRONLY : int = 1;
const O_RDWR : int = 2;
const O_APPEND : int = 8;
const O_CREAT : int = 256;
const O_EXCL : int = 1024;
const O_TRUNC : int = 512;
const S_IFIFO : int = 4096;
const S_IFCHR : int = 8192;
const S_IFBLK : int = 12288;
const S_IFDIR : int = 16384;
const S_IFREG : int = 32768;
const S_IFMT : int = 61440;
const S_IEXEC : int = 64;
const S_IWRITE : int = 128;
const S_IREAD : int = 256;
const S_IRWXU : int = 448;
const S_IXUSR : int = 64;
const S_IWUSR : int = 128;
const S_IRUSR : int = 256;
const F_OK : int = 0;
const R_OK : int = 4;
const W_OK : int = 2;
const X_OK : int = 1;
const STDERR_FILENO : int = 2;
const STDIN_FILENO : int = 0;
const STDOUT_FILENO : int = 1;
}
mod extra {
const O_TEXT : int = 16384;
const O_BINARY : int = 32768;
}
}
#[cfg(target_os = "linux")]
mod os {
mod c95 {
const EXIT_FAILURE : int = 1;
const EXIT_SUCCESS : int = 0;
const RAND_MAX : int = 2147483647;
const EOF : int = -1;
const SEEK_SET : int = 0;
const SEEK_CUR : int = 1;
const SEEK_END : int = 2;
const _IOFBF : int = 0;
const _IONBF : int = 2;
const _IOLBF : int = 1;
const BUFSIZ : uint = 8192_u;
const FOPEN_MAX : uint = 16_u;
const FILENAME_MAX : uint = 4096_u;
const L_tmpnam : uint = 20_u;
const TMP_MAX : uint = 238328_u;
}
mod posix88 {
const O_RDONLY : int = 0;
const O_WRONLY : int = 1;
const O_RDWR : int = 2;
const O_APPEND : int = 1024;
const O_CREAT : int = 64;
const O_EXCL : int = 128;
const O_TRUNC : int = 512;
const S_IFIFO : int = 4096;
const S_IFCHR : int = 8192;
const S_IFBLK : int = 24576;
const S_IFDIR : int = 16384;
const S_IFREG : int = 32768;
const S_IFMT : int = 61440;
const S_IEXEC : int = 64;
const S_IWRITE : int = 128;
const S_IREAD : int = 256;
const S_IRWXU : int = 448;
const S_IXUSR : int = 64;
const S_IWUSR : int = 128;
const S_IRUSR : int = 256;
const F_OK : int = 0;
const R_OK : int = 4;
const W_OK : int = 2;
const X_OK : int = 1;
const F_LOCK : int = 1;
const F_TEST : int = 3;
const F_TLOCK : int = 2;
const F_ULOCK : int = 0;
}
mod extra {
const O_RSYNC : int = 1052672;
const O_DSYNC : int = 4096;
const O_SYNC : int = 1052672;
}
}
#[cfg(target_os = "freebsd")]
mod os {
mod c95 {
const EXIT_FAILURE : int = 1;
const EXIT_SUCCESS : int = 0;
const RAND_MAX : int = 2147483647;
const EOF : int = -1;
const SEEK_SET : int = 0;
const SEEK_CUR : int = 1;
const SEEK_END : int = 2;
const _IOFBF : int = 0;
const _IONBF : int = 2;
const _IOLBF : int = 1;
const BUFSIZ : uint = 1024_u;
const FOPEN_MAX : uint = 20_u;
const FILENAME_MAX : uint = 1024_u;
const L_tmpnam : uint = 1024_u;
const TMP_MAX : uint = 308915776_u;
}
mod posix88 {
const O_RDONLY : int = 0;
const O_WRONLY : int = 1;
const O_RDWR : int = 2;
const O_APPEND : int = 8;
const O_CREAT : int = 512;
const O_EXCL : int = 2048;
const O_TRUNC : int = 1024;
const S_IFIFO : int = 4096;
const S_IFCHR : int = 8192;
const S_IFBLK : int = 24576;
const S_IFDIR : int = 16384;
const S_IFREG : int = 32768;
const S_IFMT : int = 61440;
const S_IEXEC : int = 64;
const S_IWRITE : int = 128;
const S_IREAD : int = 256;
const S_IRWXU : int = 448;
const S_IXUSR : int = 64;
const S_IWUSR : int = 128;
const S_IRUSR : int = 256;
const F_OK : int = 0;
const R_OK : int = 4;
const W_OK : int = 2;
const X_OK : int = 1;
const STDERR_FILENO : int = 2;
const STDIN_FILENO : int = 0;
const STDOUT_FILENO : int = 1;
const F_LOCK : int = 1;
const F_TEST : int = 3;
const F_TLOCK : int = 2;
const F_ULOCK : int = 0;
}
mod extra {
const O_SYNC : int = 128;
}
}
#[cfg(target_os = "macos")]
mod os {
mod c95 {
const EXIT_FAILURE : int = 1;
const EXIT_SUCCESS : int = 0;
const RAND_MAX : int = 2147483647;
const EOF : int = -1;
const SEEK_SET : int = 0;
const SEEK_CUR : int = 1;
const SEEK_END : int = 2;
const _IOFBF : int = 0;
const _IONBF : int = 2;
const _IOLBF : int = 1;
const BUFSIZ : uint = 1024_u;
const FOPEN_MAX : uint = 20_u;
const FILENAME_MAX : uint = 1024_u;
const L_tmpnam : uint = 1024_u;
const TMP_MAX : uint = 308915776_u;
}
mod posix88 {
const O_RDONLY : int = 0;
const O_WRONLY : int = 1;
const O_RDWR : int = 2;
const O_APPEND : int = 8;
const O_CREAT : int = 512;
const O_EXCL : int = 2048;
const O_TRUNC : int = 1024;
const S_IFIFO : int = 4096;
const S_IFCHR : int = 8192;
const S_IFBLK : int = 24576;
const S_IFDIR : int = 16384;
const S_IFREG : int = 32768;
const S_IFMT : int = 61440;
const S_IEXEC : int = 64;
const S_IWRITE : int = 128;
const S_IREAD : int = 256;
const S_IRWXU : int = 448;
const S_IXUSR : int = 64;
const S_IWUSR : int = 128;
const S_IRUSR : int = 256;
const F_OK : int = 0;
const R_OK : int = 4;
const W_OK : int = 2;
const X_OK : int = 1;
const STDERR_FILENO : int = 2;
const STDIN_FILENO : int = 0;
const STDOUT_FILENO : int = 1;
const F_LOCK : int = 1;
const F_TEST : int = 3;
const F_TLOCK : int = 2;
const F_ULOCK : int = 0;
}
mod extra {
const O_DSYNC : int = 4194304;
const O_SYNC : int = 128;
}
}
}
mod funcs {
// Thankfull most of c95 is universally available and does not vary by OS
// or anything. The same is not true of POSIX.
mod c95 {
#[nolink]
#[abi = "cdecl"]
native mod ctype {
fn isalnum(c: c_int) -> c_int;
fn isalpha(c: c_int) -> c_int;
fn iscntrl(c: c_int) -> c_int;
fn isdigit(c: c_int) -> c_int;
fn isgraph(c: c_int) -> c_int;
fn islower(c: c_int) -> c_int;
fn isprint(c: c_int) -> c_int;
fn ispunct(c: c_int) -> c_int;
fn isspace(c: c_int) -> c_int;
fn isupper(c: c_int) -> c_int;
fn isxdigit(c: c_int) -> c_int;
fn tolower(c: c_int) -> c_int;
fn toupper(c: c_int) -> c_int;
}
#[nolink]
#[abi = "cdecl"]
native mod stdio {
fn fopen(filename: *c_char, mode: *c_char) -> *FILE;
fn freopen(filename: *c_char, mode: *c_char,
file: *FILE) -> *FILE;
fn fflush(file: *FILE) -> c_int;
fn fclose(file: *FILE) -> c_int;
fn remove(filename: *c_char) -> c_int;
fn rename(oldname: *c_char, newname: *c_char) -> c_int;
fn tmpfile() -> *FILE;
fn setvbuf(stream: *FILE, buffer: *c_char,
mode: c_int, size: size_t) -> c_int;
fn setbuf(stream: *FILE, buf: *c_char);
// Omitted: printf and scanf variants.
fn fgetc(stream: *FILE) -> c_int;
fn fgets(buf: *c_char, n: c_int, stream: *FILE) -> *c_char;
fn fputc(c: c_int, stream: *FILE) -> c_int;
fn fputs(s: *c_char, stream: *FILE) -> *c_char;
// Omitted: getc, getchar (might be macros).
// Omitted: gets, so ridiculously unsafe that it should not
// survive.
// Omitted: putc, putchar (might be macros).
fn puts(s: *c_char) -> c_int;
fn ungetc(c: c_int, stream: *FILE) -> c_int;
fn fread(ptr: *c_void, size: size_t,
nobj: size_t, stream: *FILE) -> size_t;
fn fwrite(ptr: *c_void, size: size_t,
nobj: size_t, stream: *FILE) -> size_t;
fn fseek(stream: *FILE, offset: c_long, whence: c_int) -> c_int;
fn ftell(stream: *FILE) -> c_long;
fn rewind(stream: *FILE);
fn fgetpos(stream: *FILE, ptr: *fpos_t) -> c_int;
fn fsetpos(stream: *FILE, ptr: *fpos_t) -> c_int;
fn feof(stream: *FILE) -> c_int;
fn ferror(stream: *FILE) -> c_int;
fn perror(s: *c_char);
}
#[nolink]
#[abi = "cdecl"]
native mod stdlib {
fn abs(i: c_int) -> c_int;
fn labs(i: c_long) -> c_long;
// Omitted: div, ldiv (return type incomplete).
fn atof(s: *c_char) -> c_double;
fn atoi(s: *c_char) -> c_int;
fn strtod(s: *c_char, endp: **c_char) -> c_double;
fn strtol(s: *c_char, endp: **c_char, base: c_int) -> c_long;
fn strtoul(s: *c_char, endp: **c_char, base: c_int) -> c_ulong;
fn calloc(nobj: size_t, size: size_t) -> *c_void;
fn malloc(size: size_t) -> *c_void;
fn realloc(p: *c_void, size: size_t) -> *c_void;
fn free(p: *c_void);
fn abort() -> !;
fn exit(status: c_int) -> !;
// Omitted: atexit.
fn system(s: *c_char) -> c_int;
fn getenv(s: *c_char) -> *c_char;
// Omitted: bsearch, qsort
fn rand() -> c_int;
fn srand(seed: c_uint);
}
#[nolink]
#[abi = "cdecl"]
native mod string {
fn strcpy(dst: *c_char, src: *c_char) -> *c_char;
fn strncpy(dst: *c_char, src: *c_char, n: size_t) -> *c_char;
fn strcat(s: *c_char, ct: *c_char) -> *c_char;
fn strncat(s: *c_char, ct: *c_char, n: size_t) -> *c_char;
fn strcmp(cs: *c_char, ct: *c_char) -> c_int;
fn strncmp(cs: *c_char, ct: *c_char, n: size_t) -> c_int;
fn strcoll(cs: *c_char, ct: *c_char) -> c_int;
fn strchr(cs: *c_char, c: c_int) -> *c_char;
fn strrchr(cs: *c_char, c: c_int) -> *c_char;
fn strspn(cs: *c_char, ct: *c_char) -> size_t;
fn strcspn(cs: *c_char, ct: *c_char) -> size_t;
fn strpbrk(cs: *c_char, ct: *c_char) -> *c_char;
fn strstr(cs: *c_char, ct: *c_char) -> *c_char;
fn strlen(cs: *c_char) -> size_t;
fn strerror(n: c_int) -> *c_char;
fn strtok(s: *c_char, t: *c_char) -> *c_char;
fn strxfrm(s: *c_char, ct: *c_char, n: size_t) -> size_t;
fn memcpy(s: *c_void, ct: *c_void, n: size_t) -> *c_void;
fn memmove(s: *c_void, ct: *c_void, n: size_t) -> *c_void;
fn memcmp(cx: *c_void, ct: *c_void, n: size_t) -> c_int;
fn memchr(cx: *c_void, c: c_int, n: size_t) -> *c_void;
fn memset(s: *c_void, c: c_int, n: size_t) -> *c_void;
}
}
// Microsoft helpfully underscore-qualifies all of its POSIX-like symbols
// to make sure you don't use them accidentally. It also randomly deviates
// from the exact signatures you might otherwise expect, and omits much,
// so be careful when trying to write portable code; it won't always work
// with the same POSIX functions and types as other platforms.
#[cfg(target_os = "win32")]
mod posix88 {
#[nolink]
#[abi = "cdecl"]
native mod stat {
#[link_name = "_chmod"]
fn chmod(path: *c_char, mode: c_int) -> c_int;
#[link_name = "_mkdir"]
fn mkdir(path: *c_char) -> c_int;
}
#[nolink]
#[abi = "cdecl"]
native mod stdio {
#[link_name = "_popen"]
fn popen(command: *c_char, mode: *c_char) -> *FILE;
#[link_name = "_pclose"]
fn pclose(stream: *FILE) -> c_int;
}
#[nolink]
#[abi = "cdecl"]
native mod fcntl {
#[link_name = "_open"]
fn open(path: *c_char, oflag: c_int) -> c_int;
#[link_name = "_creat"]
fn creat(path: *c_char, mode: c_int) -> c_int;
}
#[nolink]
#[abi = "cdecl"]
native mod dirent {
// Not supplied at all.
}
#[nolink]
#[abi = "cdecl"]
native mod unistd {
#[link_name = "_access"]
fn access(path: *c_char, amode: c_int) -> c_int;
#[link_name = "_chdir"]
fn chdir(dir: *c_char) -> c_int;
#[link_name = "_close"]
fn close(fd: c_int) -> c_int;
#[link_name = "_dup"]
fn dup(fd: c_int) -> c_int;
#[link_name = "_dup2"]
fn dup2(src: c_int, dst: c_int) -> c_int;
#[link_name = "_execv"]
fn execv(prog: *c_char, argv: **c_char) -> intptr_t;
#[link_name = "_execve"]
fn execve(prog: *c_char, argv: **c_char, envp: **c_char) -> c_int;
#[link_name = "_execvp"]
fn execvp(c: *c_char, argv: **c_char) -> c_int;
#[link_name = "_execvpe"]
fn execvpe(c: *c_char, argv: **c_char, envp: **c_char) -> c_int;
#[link_name = "_getcwd"]
fn getcwd(buf: *c_char, size: size_t) -> *c_char;
#[link_name = "_getpid"]
fn getpid() -> c_int;
#[link_name = "_isatty"]
fn isatty(fd: c_int) -> c_int;
#[link_name = "_lseek"]
fn lseek(fd: c_int, offset: c_long, origin: c_int) -> c_long;
#[link_name = "_pipe"]
fn pipe(fds: *c_int, psize: c_uint, textmode: c_int) -> c_int;
#[link_name = "_read"]
fn read(fd: c_int, buf: *c_void, count: c_uint) -> c_int;
#[link_name = "_rmdir"]
fn rmdir(path: *c_char) -> c_int;
#[link_name = "_unlink"]
fn unlink(c: *c_char) -> c_int;
#[link_name = "_write"]
fn write(fd: c_int, buf: *c_void, count: c_uint) -> c_uint;
}
}
#[cfg(target_os = "linux")]
#[cfg(target_os = "macos")]
#[cfg(target_os = "freebsd")]
mod posix88 {
#[nolink]
#[abi = "cdecl"]
native mod stat {
fn chmod(path: *c_char, mode: mode_t) -> c_int;
fn fchmod(fd: c_int, mode: mode_t) -> c_int;
fn mkdir(path: *c_char, mode: mode_t) -> c_int;
fn mkfifo(ath: *c_char, mode: mode_t) -> c_int;
}
#[nolink]
#[abi = "cdecl"]
native mod stdio {
fn popen(command: *c_char, mode: *c_char) -> *FILE;
fn pclose(stream: *FILE) -> c_int;
}
#[nolink]
#[abi = "cdecl"]
native mod fcntl {
fn open(path: *c_char, oflag: c_int) -> c_int;
fn creat(path: *c_char, mode: mode_t) -> c_int;
fn fcntl(fd: c_int, cmd: c_int) -> c_int;
}
#[nolink]
#[abi = "cdecl"]
native mod dirent {
fn opendir(dirname: *c_char) -> *DIR;
fn closedir(dirp: *DIR) -> c_int;
fn readdir(dirp: *DIR) -> *dirent;
fn rewinddir(dirp: *DIR);
fn seekdir(dirp: *DIR, loc: c_long);
fn telldir(dirp: *DIR) -> c_long;
}
#[nolink]
#[abi = "cdecl"]
native mod unistd {
fn access(path: *c_char, amode: c_int) -> c_int;
fn alarm(seconds: c_uint) -> c_uint;
fn chdir(dir: *c_char) -> c_int;
fn chown(path: *c_char, uid: uid_t, gid: gid_t) -> c_int;
fn close(fd: c_int) -> c_int;
fn dup(fd: c_int) -> c_int;
fn dup2(src: c_int, dst: c_int) -> c_int;
fn execv(prog: *c_char, argv: **c_char) -> c_int;
fn execve(prog: *c_char, argv: **c_char, envp: **c_char) -> c_int;
fn execvp(c: *c_char, argv: **c_char) -> c_int;
fn fork() -> pid_t;
fn fpathconf(filedes: c_int, name: c_int) -> c_long;
fn getcwd(buf: *c_char, size: size_t) -> *c_char;
fn getegid() -> gid_t;
fn geteuid() -> uid_t;
fn getgid() -> gid_t ;
fn getgroups(ngroups_max: c_int, groups: *gid_t) -> c_int;
fn getlogin() -> *c_char;
fn getopt(argc: c_int, argv: **c_char, optstr: *c_char) -> c_int;
fn getpgrp() -> pid_t;
fn getpid() -> pid_t;
fn getppid() -> pid_t;
fn getuid() -> uid_t;
fn isatty(fd: c_int) -> c_int;
fn link(src: *c_char, dst: *c_char) -> c_int;
fn lseek(fd: c_int, offset: off_t, whence: c_int) -> off_t;
fn pathconf(path: *c_char, name: c_int) -> c_long;
fn pause() -> c_int;
fn pipe(fds: *c_int) -> c_int;
fn read(fd: c_int, buf: *c_void, count: size_t) -> ssize_t;
fn rmdir(path: *c_char) -> c_int;
fn setgid(gid: gid_t) -> c_int;
fn setpgid(pid: pid_t, pgid: pid_t) -> c_int;
fn setsid() -> pid_t;
fn setuid(uid: uid_t) -> c_int;
fn sleep(secs: c_uint) -> c_uint;
fn sysconf(name: c_int) -> c_long;
fn tcgetpgrp(fd: c_int) -> pid_t;
fn ttyname(fd: c_int) -> *c_char;
fn unlink(c: *c_char) -> c_int;
fn write(fd: c_int, buf: *c_void, count: size_t) -> ssize_t;
}
}
mod extra {
}
}
// Local Variables:
// mode: rust;
// fill-column: 78;
// indent-tabs-mode: nil
// c-basic-offset: 4
// buffer-file-coding-system: utf-8-unix
// End:

View File

@ -1149,12 +1149,6 @@ mod u8 {
export lt, le, eq, ne, ge, gt;
export hash;
#[nolink]
#[abi = "cdecl"]
native mod libc {
fn memcmp(s1: *u8, s2: *u8, n: ctypes::size_t) -> ctypes::c_int;
}
/*
Function cmp
@ -1164,7 +1158,8 @@ mod u8 {
let a_len = len(a);
let b_len = len(b);
let n = math::min(a_len, b_len) as ctypes::size_t;
let r = libc::memcmp(to_ptr(a), to_ptr(b), n) as int;
let r = libc::memcmp(to_ptr(a) as *libc::c_void,
to_ptr(b) as *libc::c_void, n) as int;
if r != 0 { r } else {
if a_len == b_len {