Skip to content

Commit 9839267

Browse files
committed
---
yaml --- r: 6323 b: refs/heads/master c: 25bc37c h: refs/heads/master i: 6321: c60213e 6319: 60e714d v: v3
1 parent 18f88a4 commit 9839267

File tree

8 files changed

+193
-139
lines changed

8 files changed

+193
-139
lines changed

[refs]

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,2 @@
11
---
2-
refs/heads/master: b27a88e99c2ab011ebc2fcaa8a8943bcfc0b3065
2+
refs/heads/master: 25bc37cef9ac13b0fc8b51bd4a285ac1f54cbc1f

trunk/src/lib/ctypes.rs

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@ Module: ctypes
44
Definitions useful for C interop
55
*/
66

7-
/* Type: size_t */
7+
type c_int = i32;
8+
type long = int;
9+
type unsigned = u32;
10+
type ulong = uint;
11+
12+
type intptr_t = uint;
13+
type uintptr_t = uint;
14+
type uint32_t = u32;
15+
816
type size_t = uint;
9-
/* Type: ssize_t */
1017
type ssize_t = int;
11-
/* Type: uint32_t */
12-
type uint32_t = u32;
13-
/* Type: uintptr_t */
14-
type uintptr_t = uint;
18+
type off_t = uint;
19+
20+
type fd_t = i32; // not actually a C type, but should be.
21+
type pid_t = i32;

trunk/src/lib/generic_os.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ fn setenv(n: str, v: str) {
4848
let _: () =
4949
str::as_buf(v,
5050
{|vbuf|
51-
os::libc::setenv(nbuf, vbuf, 1);
51+
os::libc::setenv(nbuf, vbuf, 1i32);
5252
});
5353
});
5454
}

trunk/src/lib/io.rs

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import ctypes::fd_t;
2+
import ctypes::c_int;
13

24
#[abi = "cdecl"]
35
native mod rustrt {
@@ -6,7 +8,6 @@ native mod rustrt {
68
fn rust_get_stderr() -> os::libc::FILE;
79
}
810

9-
1011
// Reading
1112

1213
// FIXME This is all buffered. We might need an unbuffered variant as well
@@ -49,8 +50,12 @@ type reader =
4950
fn tell() -> uint;
5051
};
5152

52-
fn convert_whence(whence: seek_style) -> int {
53-
ret alt whence { seek_set. { 0 } seek_cur. { 1 } seek_end. { 2 } };
53+
fn convert_whence(whence: seek_style) -> i32 {
54+
ret alt whence {
55+
seek_set. { 0i32 }
56+
seek_cur. { 1i32 }
57+
seek_end. { 2i32 }
58+
};
5459
}
5560

5661
resource FILE_res(f: os::libc::FILE) { os::libc::fclose(f); }
@@ -64,11 +69,11 @@ obj FILE_buf_reader(f: os::libc::FILE, res: option::t<@FILE_res>) {
6469
vec::unsafe::set_len::<u8>(buf, read);
6570
ret buf;
6671
}
67-
fn read_byte() -> int { ret os::libc::fgetc(f); }
68-
fn unread_byte(byte: int) { os::libc::ungetc(byte, f); }
69-
fn eof() -> bool { ret os::libc::feof(f) != 0; }
72+
fn read_byte() -> int { ret os::libc::fgetc(f) as int; }
73+
fn unread_byte(byte: int) { os::libc::ungetc(byte as i32, f); }
74+
fn eof() -> bool { ret os::libc::feof(f) != 0i32; }
7075
fn seek(offset: int, whence: seek_style) {
71-
assert (os::libc::fseek(f, offset, convert_whence(whence)) == 0);
76+
assert (os::libc::fseek(f, offset, convert_whence(whence)) == 0i32);
7277
}
7378
fn tell() -> uint { ret os::libc::ftell(f) as uint; }
7479
}
@@ -247,14 +252,14 @@ obj FILE_writer(f: os::libc::FILE, res: option::t<@FILE_res>) {
247252
if nout < 1u { log_err "error dumping buffer"; }
248253
}
249254
fn seek(offset: int, whence: seek_style) {
250-
assert (os::libc::fseek(f, offset, convert_whence(whence)) == 0);
255+
assert (os::libc::fseek(f, offset, convert_whence(whence)) == 0i32);
251256
}
252257
fn tell() -> uint { ret os::libc::ftell(f) as uint; }
253258
}
254259

255-
resource fd_res(fd: int) { os::libc::close(fd); }
260+
resource fd_res(fd: fd_t) { os::libc::close(fd); }
256261

257-
obj fd_buf_writer(fd: int, res: option::t<@fd_res>) {
262+
obj fd_buf_writer(fd: fd_t, res: option::t<@fd_res>) {
258263
fn write(v: [u8]) unsafe {
259264
let len = vec::len::<u8>(v);
260265
let count = 0u;
@@ -282,7 +287,7 @@ obj fd_buf_writer(fd: int, res: option::t<@fd_res>) {
282287

283288
fn file_buf_writer(path: str,
284289
flags: [fileflag]) -> result::t<buf_writer, str> {
285-
let fflags: int =
290+
let fflags: i32 =
286291
os::libc_constants::O_WRONLY | os::libc_constants::O_BINARY;
287292
for f: fileflag in flags {
288293
alt f {
@@ -299,7 +304,7 @@ fn file_buf_writer(path: str,
299304
os::libc_constants::S_IRUSR |
300305
os::libc_constants::S_IWUSR)
301306
});
302-
ret if fd < 0 {
307+
ret if fd < 0i32 {
303308
log_err sys::last_os_error();
304309
result::err("error opening " + path)
305310
} else {
@@ -385,9 +390,14 @@ fn buffered_file_buf_writer(path: str) -> result::t<buf_writer, str> {
385390

386391

387392
// FIXME it would be great if this could be a const
393+
<<<<<<< HEAD
388394
// Problem seems to be that new_writer is not pure
389395
fn stdout() -> writer { ret new_writer(fd_buf_writer(1, option::none)); }
390396
fn stderr() -> writer { ret new_writer(fd_buf_writer(2, option::none)); }
397+
=======
398+
fn stdout() -> writer { ret new_writer(fd_buf_writer(1i32, option::none)); }
399+
fn stderr() -> writer { ret new_writer(fd_buf_writer(2i32, option::none)); }
400+
>>>>>>> refactor all unix types
391401

392402
fn print(s: str) { stdout().write_str(s); }
393403
fn println(s: str) { stdout().write_str(s + "\n"); }

trunk/src/lib/linux_os.rs

Lines changed: 59 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -4,82 +4,90 @@ Module: os
44
TODO: Restructure and document
55
*/
66

7+
import ctypes::*;
8+
9+
export libc;
10+
export libc_constants;
11+
export pipe;
12+
export fd_FILE;
13+
export close;
14+
export fclose;
15+
export waitpid;
16+
export getcwd;
17+
export exec_suffix;
18+
export target_os;
19+
export dylib_filename;
20+
export get_exe_path;
21+
722
// FIXME Somehow merge stuff duplicated here and macosx_os.rs. Made difficult
823
// by https://github.com/graydon/rust/issues#issue/268
24+
925
#[link_name = ""]
1026
#[abi = "cdecl"]
1127
native mod libc {
12-
fn read(fd: int, buf: *u8, count: uint) -> int;
13-
fn write(fd: int, buf: *u8, count: uint) -> int;
14-
fn fread(buf: *u8, size: uint, n: uint, f: libc::FILE) -> uint;
15-
fn fwrite(buf: *u8, size: uint, n: uint, f: libc::FILE) -> uint;
16-
fn open(s: str::sbuf, flags: int, mode: uint) -> int;
17-
fn close(fd: int) -> int;
28+
fn read(fd: fd_t, buf: *u8, count: size_t) -> ssize_t;
29+
fn write(fd: fd_t, buf: *u8, count: size_t) -> ssize_t;
30+
fn fread(buf: *u8, size: size_t, n: size_t, f: libc::FILE) -> size_t;
31+
fn fwrite(buf: *u8, size: size_t, n: size_t, f: libc::FILE) -> size_t;
32+
fn open(s: str::sbuf, flags: c_int, mode: unsigned) -> fd_t;
33+
fn close(fd: fd_t) -> int;
1834
type FILE;
1935
fn fopen(path: str::sbuf, mode: str::sbuf) -> FILE;
20-
fn fdopen(fd: int, mode: str::sbuf) -> FILE;
36+
fn fdopen(fd: fd_t, mode: str::sbuf) -> FILE;
2137
fn fclose(f: FILE);
22-
fn fgetc(f: FILE) -> int;
23-
fn ungetc(c: int, f: FILE);
24-
fn feof(f: FILE) -> int;
25-
fn fseek(f: FILE, offset: int, whence: int) -> int;
26-
fn ftell(f: FILE) -> int;
38+
fn fgetc(f: FILE) -> c_int;
39+
fn ungetc(c: c_int, f: FILE);
40+
fn feof(f: FILE) -> c_int;
41+
fn fseek(f: FILE, offset: long, whence: c_int) -> c_int;
42+
fn ftell(f: FILE) -> long;
2743
type dir;
2844
fn opendir(d: str::sbuf) -> dir;
29-
fn closedir(d: dir) -> int;
45+
fn closedir(d: dir) -> c_int;
3046
type dirent;
3147
fn readdir(d: dir) -> dirent;
3248
fn getenv(n: str::sbuf) -> str::sbuf;
33-
fn setenv(n: str::sbuf, v: str::sbuf, overwrite: int) -> int;
34-
fn unsetenv(n: str::sbuf) -> int;
35-
fn pipe(buf: *mutable int) -> int;
36-
fn waitpid(pid: int, &status: int, options: int) -> int;
37-
fn readlink(path: str::sbuf, buf: str::sbuf,
38-
bufsize: ctypes::size_t) -> ctypes::ssize_t;
49+
fn setenv(n: str::sbuf, v: str::sbuf, overwrite: c_int) -> c_int;
50+
fn unsetenv(n: str::sbuf) -> c_int;
51+
fn pipe(buf: *mutable fd_t) -> c_int;
52+
fn waitpid(pid: pid_t, &status: c_int, options: c_int) -> pid_t;
3953
}
4054

4155
mod libc_constants {
42-
const O_RDONLY: int = 0;
43-
const O_WRONLY: int = 1;
44-
const O_RDWR: int = 2;
45-
const O_APPEND: int = 1024;
46-
const O_CREAT: int = 64;
47-
const O_EXCL: int = 128;
48-
const O_TRUNC: int = 512;
49-
const O_TEXT: int = 0; // nonexistent in linux libc
50-
const O_BINARY: int = 0; // nonexistent in linux libc
51-
52-
const S_IRUSR: uint = 256u;
53-
const S_IWUSR: uint = 128u;
54-
}
56+
const O_RDONLY: c_int = 0;
57+
const O_WRONLY: c_int = 1;
58+
const O_RDWR: c_int = 2;
59+
const O_APPEND: c_int = 1024;
60+
const O_CREAT: c_int = 64;
61+
const O_EXCL: c_int = 128;
62+
const O_TRUNC: c_int = 512;
63+
const O_TEXT: c_int = 0; // nonexistent in linux libc
64+
const O_BINARY: c_int = 0; // nonexistent in linux libc
5565

56-
// FIXME turn into constants
57-
fn exec_suffix() -> str { ret ""; }
58-
fn target_os() -> str { ret "linux"; }
59-
60-
fn dylib_filename(base: str) -> str { ret "lib" + base + ".so"; }
66+
const S_IRUSR: unsigned = 256u;
67+
const S_IWUSR: unsigned = 128u;
68+
}
6169

62-
fn pipe() -> {in: int, out: int} {
63-
let fds = {mutable in: 0, mutable out: 0};
64-
assert (os::libc::pipe(ptr::mut_addr_of(fds.in)) == 0);
70+
fn pipe() -> {in: fd_t, out: fd_t} {
71+
let fds = {mutable in: 0i32, mutable out: 0i32};
72+
assert (os::libc::pipe(ptr::mut_addr_of(fds.in)) == 0i32);
6573
ret {in: fds.in, out: fds.out};
6674
}
6775

68-
fn fd_FILE(fd: int) -> libc::FILE {
76+
fn fd_FILE(fd: fd_t) -> libc::FILE {
6977
ret str::as_buf("r", {|modebuf| libc::fdopen(fd, modebuf) });
7078
}
7179

72-
fn close(fd: int) -> int {
80+
fn close(fd: fd_t) -> int {
7381
libc::close(fd)
7482
}
7583

7684
fn fclose(file: libc::FILE) {
7785
libc::fclose(file)
7886
}
7987

80-
fn waitpid(pid: int) -> int {
81-
let status = 0;
82-
assert (os::libc::waitpid(pid, status, 0) != -1);
88+
fn waitpid(pid: pid_t) -> i32 {
89+
let status = 0i32;
90+
assert (os::libc::waitpid(pid, status, 0i32) != -1i32);
8391
ret status;
8492
}
8593

@@ -90,6 +98,12 @@ native mod rustrt {
9098

9199
fn getcwd() -> str { ret rustrt::rust_getcwd(); }
92100

101+
fn exec_suffix() -> str { ret ""; }
102+
103+
fn target_os() -> str { ret "linux"; }
104+
105+
fn dylib_filename(base: str) -> str { ret "lib" + base + ".so"; }
106+
93107
/// Returns the directory containing the running program
94108
/// followed by a path separator
95109
fn get_exe_path() -> option::t<fs::path> {

0 commit comments

Comments
 (0)