Skip to content

Commit 2df1688

Browse files
committed
libcore: Implement os::args() on Windows
1 parent 5e7ff92 commit 2df1688

File tree

1 file changed

+46
-1
lines changed

1 file changed

+46
-1
lines changed

src/libcore/os.rs

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -761,7 +761,46 @@ fn real_args() -> ~[~str] {
761761

762762
#[cfg(windows)]
763763
fn real_args() -> ~[~str] {
764-
fail // Needs implementing.
764+
let mut nArgs: c_int = 0;
765+
let lpArgCount = ptr::to_mut_unsafe_ptr(&mut nArgs);
766+
let lpCmdLine = GetCommandLineW();
767+
let szArgList = CommandLineToArgvW(lpCmdLine, lpArgCount);
768+
769+
let mut args = ~[];
770+
for uint::range(0, nArgs as uint) |i| {
771+
unsafe {
772+
// Determine the length of this argument.
773+
let ptr = *szArgList.offset(i);
774+
let mut len = 0;
775+
while *ptr.offset(len) != 0 { len += 1; }
776+
777+
// Push it onto the list.
778+
vec::push(&mut args, vec::raw::form_slice(ptr, len, str::from_utf16));
779+
}
780+
}
781+
782+
unsafe {
783+
LocalFree(cast::transmute(szArgList));
784+
}
785+
786+
return args;
787+
}
788+
789+
type LPCWSTR = *u16;
790+
791+
#[cfg(windows)]
792+
#[link_name="kernel32"]
793+
#[abi="stdcall"]
794+
extern {
795+
fn GetCommandLineW() -> LPCWSTR;
796+
fn LocalFree(ptr: *c_void);
797+
}
798+
799+
#[cfg(windows)]
800+
#[link_name="shell32"]
801+
#[abi="stdcall"]
802+
extern {
803+
fn CommandLineToArgvW(lpCmdLine: LPCWSTR, pNumArgs: *mut c_int) -> **u16;
765804
}
766805

767806
struct OverriddenArgs {
@@ -845,6 +884,12 @@ mod tests {
845884
log(debug, last_os_error());
846885
}
847886

887+
#[test]
888+
pub fn test_args() {
889+
let a = real_args();
890+
assert a.len() >= 1;
891+
}
892+
848893
fn make_rand_name() -> ~str {
849894
let rng: rand::Rng = rand::Rng();
850895
let n = ~"TEST" + rng.gen_str(10u);

0 commit comments

Comments
 (0)