Skip to content

Commit 9b4db17

Browse files
committed
libcore: Implement a sys::args() on Mac
1 parent 1229d1c commit 9b4db17

File tree

2 files changed

+36
-5
lines changed

2 files changed

+36
-5
lines changed

src/libcore/os.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -732,6 +732,30 @@ pub fn set_exit_status(code: int) {
732732
rustrt::rust_set_exit_status(code as libc::intptr_t);
733733
}
734734

735+
/**
736+
* Returns the command line arguments
737+
*
738+
* Returns a list of the command line arguments.
739+
*/
740+
#[cfg(target_os = "macos")]
741+
pub fn args() -> ~[~str] {
742+
unsafe {
743+
let (argc, argv) = (*_NSGetArgc() as uint, *_NSGetArgv());
744+
let mut args = ~[];
745+
for uint::range(0, argc) |i| {
746+
vec::push(&mut args, str::raw::from_c_str(*argv.offset(i)));
747+
}
748+
return args;
749+
}
750+
}
751+
752+
#[cfg(target_os = "macos")]
753+
extern {
754+
// These functions are in crt_externs.h.
755+
pub fn _NSGetArgc() -> *c_int;
756+
pub fn _NSGetArgv() -> ***c_char;
757+
}
758+
735759
#[cfg(unix)]
736760
pub fn family() -> ~str { ~"unix" }
737761

src/libcore/ptr.rs

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,23 +47,23 @@ pub pure fn mut_addr_of<T>(val: &T) -> *mut T {
4747

4848
/// Calculate the offset from a pointer
4949
#[inline(always)]
50-
pub fn offset<T>(ptr: *T, count: uint) -> *T {
50+
pub pure fn offset<T>(ptr: *T, count: uint) -> *T {
5151
unsafe {
5252
(ptr as uint + count * sys::size_of::<T>()) as *T
5353
}
5454
}
5555

5656
/// Calculate the offset from a const pointer
5757
#[inline(always)]
58-
pub fn const_offset<T>(ptr: *const T, count: uint) -> *const T {
58+
pub pure fn const_offset<T>(ptr: *const T, count: uint) -> *const T {
5959
unsafe {
6060
(ptr as uint + count * sys::size_of::<T>()) as *T
6161
}
6262
}
6363

6464
/// Calculate the offset from a mut pointer
6565
#[inline(always)]
66-
pub fn mut_offset<T>(ptr: *mut T, count: uint) -> *mut T {
66+
pub pure fn mut_offset<T>(ptr: *mut T, count: uint) -> *mut T {
6767
(ptr as uint + count * sys::size_of::<T>()) as *mut T
6868
}
6969

@@ -176,18 +176,25 @@ pub fn ref_eq<T>(thing: &a/T, other: &b/T) -> bool {
176176
to_uint(thing) == to_uint(other)
177177
}
178178

179-
pub trait Ptr {
179+
pub trait Ptr<T> {
180180
pure fn is_null() -> bool;
181181
pure fn is_not_null() -> bool;
182+
pure fn offset(count: uint) -> self;
182183
}
183184

184185
/// Extension methods for pointers
185-
impl<T> *T: Ptr {
186+
impl<T> *T: Ptr<T> {
186187
/// Returns true if the pointer is equal to the null pointer.
188+
#[inline(always)]
187189
pure fn is_null() -> bool { is_null(self) }
188190

189191
/// Returns true if the pointer is not equal to the null pointer.
192+
#[inline(always)]
190193
pure fn is_not_null() -> bool { is_not_null(self) }
194+
195+
/// Calculates the offset from a pointer.
196+
#[inline(always)]
197+
pure fn offset(count: uint) -> *T { offset(self, count) }
191198
}
192199

193200
// Equality for pointers

0 commit comments

Comments
 (0)