]> git.lizzy.rs Git - rust.git/blob - src/libstd/os.rs
Fix bug in `match`ing struct patterns
[rust.git] / src / libstd / os.rs
1 // Copyright 2012-2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 /*!
12  * Higher-level interfaces to libc::* functions and operating system services.
13  *
14  * In general these take and return rust types, use rust idioms (enums,
15  * closures, vectors) rather than C idioms, and do more extensive safety
16  * checks.
17  *
18  * This module is not meant to only contain 1:1 mappings to libc entries; any
19  * os-interface code that is reasonably useful and broadly applicable can go
20  * here. Including utility routines that merely build on other os code.
21  *
22  * We assume the general case is that users do not care, and do not want to
23  * be made to care, which operating system they are on. While they may want
24  * to special case various special cases -- and so we will not _hide_ the
25  * facts of which OS the user is on -- they should be given the opportunity
26  * to write OS-ignorant code by default.
27  */
28
29 #[allow(missing_doc)];
30
31 use cast;
32 use clone::Clone;
33 use container::Container;
34 use io;
35 use iterator::{IteratorUtil, range};
36 use libc;
37 use libc::{c_char, c_void, c_int, size_t};
38 use libc::FILE;
39 use local_data;
40 use option::{Some, None};
41 use os;
42 use prelude::*;
43 use ptr;
44 use str;
45 use to_str;
46 use unstable::finally::Finally;
47 use vec;
48
49 pub use libc::fclose;
50 pub use os::consts::*;
51
52 /// Delegates to the libc close() function, returning the same return value.
53 pub fn close(fd: c_int) -> c_int {
54     unsafe {
55         libc::close(fd)
56     }
57 }
58
59 pub mod rustrt {
60     use libc::{c_char, c_int};
61     use libc;
62
63     extern {
64         pub fn rust_get_argc() -> c_int;
65         pub fn rust_get_argv() -> **c_char;
66         pub fn rust_path_is_dir(path: *libc::c_char) -> c_int;
67         pub fn rust_path_exists(path: *libc::c_char) -> c_int;
68         pub fn rust_set_exit_status(code: libc::intptr_t);
69     }
70 }
71
72 pub static TMPBUF_SZ : uint = 1000u;
73 static BUF_BYTES : uint = 2048u;
74
75 pub fn getcwd() -> Path {
76     let buf = [0 as libc::c_char, ..BUF_BYTES];
77     unsafe {
78         if(0 as *libc::c_char == libc::getcwd(
79             &buf[0],
80             BUF_BYTES as libc::size_t)) {
81             fail!();
82         }
83         Path(str::raw::from_c_str(&buf[0]))
84     }
85 }
86
87 // FIXME: move these to str perhaps? #2620
88
89 pub fn fill_charp_buf(f: &fn(*mut c_char, size_t) -> bool) -> Option<~str> {
90     let mut buf = [0 as c_char, .. TMPBUF_SZ];
91     do buf.as_mut_buf |b, sz| {
92         if f(b, sz as size_t) {
93             unsafe {
94                 Some(str::raw::from_buf(b as *u8))
95             }
96         } else {
97             None
98         }
99     }
100 }
101
102 #[cfg(windows)]
103 pub mod win32 {
104     use libc;
105     use vec;
106     use str;
107     use option::{None, Option};
108     use option;
109     use os::TMPBUF_SZ;
110     use libc::types::os::arch::extra::DWORD;
111
112     pub fn fill_utf16_buf_and_decode(f: &fn(*mut u16, DWORD) -> DWORD)
113         -> Option<~str> {
114         unsafe {
115             let mut n = TMPBUF_SZ as DWORD;
116             let mut res = None;
117             let mut done = false;
118             while !done {
119                 let mut k: DWORD = 0;
120                 let mut buf = vec::from_elem(n as uint, 0u16);
121                 do buf.as_mut_buf |b, _sz| {
122                     k = f(b, TMPBUF_SZ as DWORD);
123                     if k == (0 as DWORD) {
124                         done = true;
125                     } else if (k == n &&
126                                libc::GetLastError() ==
127                                libc::ERROR_INSUFFICIENT_BUFFER as DWORD) {
128                         n *= (2 as DWORD);
129                     } else {
130                         done = true;
131                     }
132                 }
133                 if k != 0 && done {
134                     let sub = buf.slice(0, k as uint);
135                     res = option::Some(str::from_utf16(sub));
136                 }
137             }
138             return res;
139         }
140     }
141
142     pub fn as_utf16_p<T>(s: &str, f: &fn(*u16) -> T) -> T {
143         let mut t = s.to_utf16();
144         // Null terminate before passing on.
145         t.push(0u16);
146         t.as_imm_buf(|buf, _len| f(buf))
147     }
148 }
149
150 /*
151 Accessing environment variables is not generally threadsafe.
152 Serialize access through a global lock.
153 */
154 fn with_env_lock<T>(f: &fn() -> T) -> T {
155     use unstable::finally::Finally;
156
157     unsafe {
158         return do (|| {
159             rust_take_env_lock();
160             f()
161         }).finally {
162             rust_drop_env_lock();
163         };
164     }
165
166     extern {
167         #[fast_ffi]
168         fn rust_take_env_lock();
169         #[fast_ffi]
170         fn rust_drop_env_lock();
171     }
172 }
173
174 /// Returns a vector of (variable, value) pairs for all the environment
175 /// variables of the current process.
176 pub fn env() -> ~[(~str,~str)] {
177     unsafe {
178         #[cfg(windows)]
179         unsafe fn get_env_pairs() -> ~[~str] {
180             use libc::funcs::extra::kernel32::{
181                 GetEnvironmentStringsA,
182                 FreeEnvironmentStringsA
183             };
184             let ch = GetEnvironmentStringsA();
185             if (ch as uint == 0) {
186                 fail!("os::env() failure getting env string from OS: %s", os::last_os_error());
187             }
188             let mut curr_ptr: uint = ch as uint;
189             let mut result = ~[];
190             while(*(curr_ptr as *libc::c_char) != 0 as libc::c_char) {
191                 let env_pair = str::raw::from_c_str(
192                     curr_ptr as *libc::c_char);
193                 result.push(env_pair);
194                 curr_ptr +=
195                     libc::strlen(curr_ptr as *libc::c_char) as uint
196                     + 1;
197             }
198             FreeEnvironmentStringsA(ch);
199             result
200         }
201         #[cfg(unix)]
202         unsafe fn get_env_pairs() -> ~[~str] {
203             extern {
204                 fn rust_env_pairs() -> **libc::c_char;
205             }
206             let environ = rust_env_pairs();
207             if (environ as uint == 0) {
208                 fail!("os::env() failure getting env string from OS: %s", os::last_os_error());
209             }
210             let mut result = ~[];
211             ptr::array_each(environ, |e| {
212                 let env_pair = str::raw::from_c_str(e);
213                 debug!("get_env_pairs: %s",
214                        env_pair);
215                 result.push(env_pair);
216             });
217             result
218         }
219
220         fn env_convert(input: ~[~str]) -> ~[(~str, ~str)] {
221             let mut pairs = ~[];
222             for p in input.iter() {
223                 let vs: ~[&str] = p.splitn_iter('=', 1).collect();
224                 debug!("splitting: len: %u",
225                     vs.len());
226                 assert_eq!(vs.len(), 2);
227                 pairs.push((vs[0].to_owned(), vs[1].to_owned()));
228             }
229             pairs
230         }
231         do with_env_lock {
232             let unparsed_environ = get_env_pairs();
233             env_convert(unparsed_environ)
234         }
235     }
236 }
237
238 #[cfg(unix)]
239 /// Fetches the environment variable `n` from the current process, returning
240 /// None if the variable isn't set.
241 pub fn getenv(n: &str) -> Option<~str> {
242     unsafe {
243         do with_env_lock {
244             let s = n.as_c_str(|s| libc::getenv(s as *libc::c_char));
245             if ptr::null::<u8>() == cast::transmute(s) {
246                 None
247             } else {
248                 Some(str::raw::from_buf(cast::transmute(s)))
249             }
250         }
251     }
252 }
253
254 #[cfg(windows)]
255 /// Fetches the environment variable `n` from the current process, returning
256 /// None if the variable isn't set.
257 pub fn getenv(n: &str) -> Option<~str> {
258     unsafe {
259         do with_env_lock {
260             use os::win32::{as_utf16_p, fill_utf16_buf_and_decode};
261             do as_utf16_p(n) |u| {
262                 do fill_utf16_buf_and_decode() |buf, sz| {
263                     libc::GetEnvironmentVariableW(u, buf, sz)
264                 }
265             }
266         }
267     }
268 }
269
270
271 #[cfg(unix)]
272 /// Sets the environment variable `n` to the value `v` for the currently running
273 /// process
274 pub fn setenv(n: &str, v: &str) {
275     unsafe {
276         do with_env_lock {
277             do n.to_str().as_c_str |nbuf| {
278                 do v.to_str().as_c_str |vbuf| {
279                     libc::funcs::posix01::unistd::setenv(nbuf, vbuf, 1);
280                 }
281             }
282         }
283     }
284 }
285
286
287 #[cfg(windows)]
288 /// Sets the environment variable `n` to the value `v` for the currently running
289 /// process
290 pub fn setenv(n: &str, v: &str) {
291     unsafe {
292         do with_env_lock {
293             use os::win32::as_utf16_p;
294             do as_utf16_p(n) |nbuf| {
295                 do as_utf16_p(v) |vbuf| {
296                     libc::SetEnvironmentVariableW(nbuf, vbuf);
297                 }
298             }
299         }
300     }
301 }
302
303 /// Remove a variable from the environment entirely
304 pub fn unsetenv(n: &str) {
305     #[cfg(unix)]
306     fn _unsetenv(n: &str) {
307         unsafe {
308             do with_env_lock {
309                 do n.to_str().as_c_str |nbuf| {
310                     libc::funcs::posix01::unistd::unsetenv(nbuf);
311                 }
312             }
313         }
314     }
315     #[cfg(windows)]
316     fn _unsetenv(n: &str) {
317         unsafe {
318             do with_env_lock {
319                 use os::win32::as_utf16_p;
320                 do as_utf16_p(n) |nbuf| {
321                     libc::SetEnvironmentVariableW(nbuf, ptr::null());
322                 }
323             }
324         }
325     }
326
327     _unsetenv(n);
328 }
329
330 pub fn fdopen(fd: c_int) -> *FILE {
331     do "r".as_c_str |modebuf| {
332         unsafe {
333             libc::fdopen(fd, modebuf)
334         }
335     }
336 }
337
338
339 // fsync related
340
341 #[cfg(windows)]
342 pub fn fsync_fd(fd: c_int, _level: io::fsync::Level) -> c_int {
343     unsafe {
344         use libc::funcs::extra::msvcrt::*;
345         return commit(fd);
346     }
347 }
348
349 #[cfg(target_os = "linux")]
350 #[cfg(target_os = "android")]
351 pub fn fsync_fd(fd: c_int, level: io::fsync::Level) -> c_int {
352     unsafe {
353         use libc::funcs::posix01::unistd::*;
354         match level {
355           io::fsync::FSync
356           | io::fsync::FullFSync => return fsync(fd),
357           io::fsync::FDataSync => return fdatasync(fd)
358         }
359     }
360 }
361
362 #[cfg(target_os = "macos")]
363 pub fn fsync_fd(fd: c_int, level: io::fsync::Level) -> c_int {
364     unsafe {
365         use libc::consts::os::extra::*;
366         use libc::funcs::posix88::fcntl::*;
367         use libc::funcs::posix01::unistd::*;
368         match level {
369           io::fsync::FSync => return fsync(fd),
370           _ => {
371             // According to man fnctl, the ok retval is only specified to be
372             // !=-1
373             if (fcntl(F_FULLFSYNC as c_int, fd) == -1 as c_int)
374                 { return -1 as c_int; }
375             else
376                 { return 0 as c_int; }
377           }
378         }
379     }
380 }
381
382 #[cfg(target_os = "freebsd")]
383 pub fn fsync_fd(fd: c_int, _l: io::fsync::Level) -> c_int {
384     unsafe {
385         use libc::funcs::posix01::unistd::*;
386         return fsync(fd);
387     }
388 }
389
390 pub struct Pipe {
391     input: c_int,
392     out: c_int
393 }
394
395 #[cfg(unix)]
396 pub fn pipe() -> Pipe {
397     unsafe {
398         let mut fds = Pipe {input: 0 as c_int,
399                             out: 0 as c_int };
400         assert_eq!(libc::pipe(&mut fds.input), (0 as c_int));
401         return Pipe {input: fds.input, out: fds.out};
402     }
403 }
404
405
406
407 #[cfg(windows)]
408 pub fn pipe() -> Pipe {
409     unsafe {
410         // Windows pipes work subtly differently than unix pipes, and their
411         // inheritance has to be handled in a different way that I do not
412         // fully understand. Here we explicitly make the pipe non-inheritable,
413         // which means to pass it to a subprocess they need to be duplicated
414         // first, as in core::run.
415         let mut fds = Pipe {input: 0 as c_int,
416                     out: 0 as c_int };
417         let res = libc::pipe(&mut fds.input, 1024 as ::libc::c_uint,
418                              (libc::O_BINARY | libc::O_NOINHERIT) as c_int);
419         assert_eq!(res, 0 as c_int);
420         assert!((fds.input != -1 as c_int && fds.input != 0 as c_int));
421         assert!((fds.out != -1 as c_int && fds.input != 0 as c_int));
422         return Pipe {input: fds.input, out: fds.out};
423     }
424 }
425
426 fn dup2(src: c_int, dst: c_int) -> c_int {
427     unsafe {
428         libc::dup2(src, dst)
429     }
430 }
431
432 /// Returns the proper dll filename for the given basename of a file.
433 pub fn dll_filename(base: &str) -> ~str {
434     fmt!("%s%s%s", DLL_PREFIX, base, DLL_SUFFIX)
435 }
436
437 /// Optionally returns the filesystem path to the current executable which is
438 /// running. If any failure occurs, None is returned.
439 pub fn self_exe_path() -> Option<Path> {
440
441     #[cfg(target_os = "freebsd")]
442     fn load_self() -> Option<~str> {
443         unsafe {
444             use libc::funcs::bsd44::*;
445             use libc::consts::os::extra::*;
446             do fill_charp_buf() |buf, sz| {
447                 let mib = ~[CTL_KERN as c_int,
448                            KERN_PROC as c_int,
449                            KERN_PROC_PATHNAME as c_int, -1 as c_int];
450                 let mut sz = sz;
451                 sysctl(vec::raw::to_ptr(mib), mib.len() as ::libc::c_uint,
452                        buf as *mut c_void, &mut sz, ptr::null(),
453                        0u as size_t) == (0 as c_int)
454             }
455         }
456     }
457
458     #[cfg(target_os = "linux")]
459     #[cfg(target_os = "android")]
460     fn load_self() -> Option<~str> {
461         unsafe {
462             use libc::funcs::posix01::unistd::readlink;
463
464             let mut path_str = str::with_capacity(TMPBUF_SZ);
465             let len = do path_str.as_c_str |buf| {
466                 let buf = buf as *mut c_char;
467                 do "/proc/self/exe".as_c_str |proc_self_buf| {
468                     readlink(proc_self_buf, buf, TMPBUF_SZ as size_t)
469                 }
470             };
471             if len == -1 {
472                 None
473             } else {
474                 str::raw::set_len(&mut path_str, len as uint);
475                 Some(path_str)
476             }
477         }
478     }
479
480     #[cfg(target_os = "macos")]
481     fn load_self() -> Option<~str> {
482         unsafe {
483             do fill_charp_buf() |buf, sz| {
484                 let mut sz = sz as u32;
485                 libc::funcs::extra::_NSGetExecutablePath(
486                     buf, &mut sz) == (0 as c_int)
487             }
488         }
489     }
490
491     #[cfg(windows)]
492     fn load_self() -> Option<~str> {
493         unsafe {
494             use os::win32::fill_utf16_buf_and_decode;
495             do fill_utf16_buf_and_decode() |buf, sz| {
496                 libc::GetModuleFileNameW(0u as libc::DWORD, buf, sz)
497             }
498         }
499     }
500
501     do load_self().map |pth| {
502         Path(*pth).dir_path()
503     }
504 }
505
506
507 /**
508  * Returns the path to the user's home directory, if known.
509  *
510  * On Unix, returns the value of the 'HOME' environment variable if it is set
511  * and not equal to the empty string.
512  *
513  * On Windows, returns the value of the 'HOME' environment variable if it is
514  * set and not equal to the empty string. Otherwise, returns the value of the
515  * 'USERPROFILE' environment variable if it is set and not equal to the empty
516  * string.
517  *
518  * Otherwise, homedir returns option::none.
519  */
520 pub fn homedir() -> Option<Path> {
521     return match getenv("HOME") {
522         Some(ref p) => if !p.is_empty() {
523           Some(Path(*p))
524         } else {
525           secondary()
526         },
527         None => secondary()
528     };
529
530     #[cfg(unix)]
531     fn secondary() -> Option<Path> {
532         None
533     }
534
535     #[cfg(windows)]
536     fn secondary() -> Option<Path> {
537         do getenv("USERPROFILE").chain |p| {
538             if !p.is_empty() {
539                 Some(Path(p))
540             } else {
541                 None
542             }
543         }
544     }
545 }
546
547 /**
548  * Returns the path to a temporary directory.
549  *
550  * On Unix, returns the value of the 'TMPDIR' environment variable if it is
551  * set and non-empty and '/tmp' otherwise.
552  *
553  * On Windows, returns the value of, in order, the 'TMP', 'TEMP',
554  * 'USERPROFILE' environment variable  if any are set and not the empty
555  * string. Otherwise, tmpdir returns the path to the Windows directory.
556  */
557 pub fn tmpdir() -> Path {
558     return lookup();
559
560     fn getenv_nonempty(v: &str) -> Option<Path> {
561         match getenv(v) {
562             Some(x) =>
563                 if x.is_empty() {
564                     None
565                 } else {
566                     Some(Path(x))
567                 },
568             _ => None
569         }
570     }
571
572     #[cfg(unix)]
573     fn lookup() -> Path {
574         getenv_nonempty("TMPDIR").unwrap_or_default(Path("/tmp"))
575     }
576
577     #[cfg(windows)]
578     fn lookup() -> Path {
579         getenv_nonempty("TMP").or(
580             getenv_nonempty("TEMP").or(
581                 getenv_nonempty("USERPROFILE").or(
582                    getenv_nonempty("WINDIR")))).unwrap_or_default(Path("C:\\Windows"))
583     }
584 }
585
586 /// Recursively walk a directory structure
587 pub fn walk_dir(p: &Path, f: &fn(&Path) -> bool) -> bool {
588     let r = list_dir(p);
589     r.iter().advance(|q| {
590         let path = &p.push(*q);
591         f(path) && (!path_is_dir(path) || walk_dir(path, |p| f(p)))
592     })
593 }
594
595 /// Indicates whether a path represents a directory
596 pub fn path_is_dir(p: &Path) -> bool {
597     unsafe {
598         do p.to_str().as_c_str |buf| {
599             rustrt::rust_path_is_dir(buf) != 0 as c_int
600         }
601     }
602 }
603
604 /// Indicates whether a path exists
605 pub fn path_exists(p: &Path) -> bool {
606     unsafe {
607         do p.to_str().as_c_str |buf| {
608             rustrt::rust_path_exists(buf) != 0 as c_int
609         }
610     }
611 }
612
613 /**
614  * Convert a relative path to an absolute path
615  *
616  * If the given path is relative, return it prepended with the current working
617  * directory. If the given path is already an absolute path, return it
618  * as is.
619  */
620 // NB: this is here rather than in path because it is a form of environment
621 // querying; what it does depends on the process working directory, not just
622 // the input paths.
623 pub fn make_absolute(p: &Path) -> Path {
624     if p.is_absolute {
625         (*p).clone()
626     } else {
627         getcwd().push_many(p.components)
628     }
629 }
630
631
632 /// Creates a directory at the specified path
633 pub fn make_dir(p: &Path, mode: c_int) -> bool {
634     return mkdir(p, mode);
635
636     #[cfg(windows)]
637     fn mkdir(p: &Path, _mode: c_int) -> bool {
638         unsafe {
639             use os::win32::as_utf16_p;
640             // FIXME: turn mode into something useful? #2623
641             do as_utf16_p(p.to_str()) |buf| {
642                 libc::CreateDirectoryW(buf, cast::transmute(0))
643                     != (0 as libc::BOOL)
644             }
645         }
646     }
647
648     #[cfg(unix)]
649     fn mkdir(p: &Path, mode: c_int) -> bool {
650         do p.to_str().as_c_str |buf| {
651             unsafe {
652                 libc::mkdir(buf, mode as libc::mode_t) == (0 as c_int)
653             }
654         }
655     }
656 }
657
658 /// Creates a directory with a given mode.
659 /// Returns true iff creation
660 /// succeeded. Also creates all intermediate subdirectories
661 /// if they don't already exist, giving all of them the same mode.
662
663 // tjc: if directory exists but with different permissions,
664 // should we return false?
665 pub fn mkdir_recursive(p: &Path, mode: c_int) -> bool {
666     if path_is_dir(p) {
667         return true;
668     }
669     else if p.components.is_empty() {
670         return false;
671     }
672     else if p.components.len() == 1 {
673         // No parent directories to create
674         path_is_dir(p) || make_dir(p, mode)
675     }
676     else {
677         mkdir_recursive(&p.pop(), mode) && make_dir(p, mode)
678     }
679 }
680
681 /// Lists the contents of a directory
682 pub fn list_dir(p: &Path) -> ~[~str] {
683     if p.components.is_empty() && !p.is_absolute() {
684         // Not sure what the right behavior is here, but this
685         // prevents a bounds check failure later
686         return ~[];
687     }
688     unsafe {
689         #[cfg(target_os = "linux")]
690         #[cfg(target_os = "android")]
691         #[cfg(target_os = "freebsd")]
692         #[cfg(target_os = "macos")]
693         unsafe fn get_list(p: &Path) -> ~[~str] {
694             use libc::{dirent_t};
695             use libc::{opendir, readdir, closedir};
696             extern {
697                 fn rust_list_dir_val(ptr: *dirent_t) -> *libc::c_char;
698             }
699             let input = p.to_str();
700             let mut strings = ~[];
701             let input_ptr = ::cast::transmute(&input[0]);
702             debug!("os::list_dir -- BEFORE OPENDIR");
703             let dir_ptr = opendir(input_ptr);
704             if (dir_ptr as uint != 0) {
705         debug!("os::list_dir -- opendir() SUCCESS");
706                 let mut entry_ptr = readdir(dir_ptr);
707                 while (entry_ptr as uint != 0) {
708                     strings.push(str::raw::from_c_str(rust_list_dir_val(
709                         entry_ptr)));
710                     entry_ptr = readdir(dir_ptr);
711                 }
712                 closedir(dir_ptr);
713             }
714             else {
715         debug!("os::list_dir -- opendir() FAILURE");
716             }
717             debug!(
718                 "os::list_dir -- AFTER -- #: %?",
719                      strings.len());
720             strings
721         }
722         #[cfg(windows)]
723         unsafe fn get_list(p: &Path) -> ~[~str] {
724             use libc::consts::os::extra::INVALID_HANDLE_VALUE;
725             use libc::{wcslen, free};
726             use libc::funcs::extra::kernel32::{
727                 FindFirstFileW,
728                 FindNextFileW,
729                 FindClose,
730             };
731             use os::win32::{
732                 as_utf16_p
733             };
734             use rt::global_heap::malloc_raw;
735
736             #[nolink]
737             extern {
738                 fn rust_list_dir_wfd_size() -> libc::size_t;
739                 fn rust_list_dir_wfd_fp_buf(wfd: *libc::c_void) -> *u16;
740             }
741             fn star(p: &Path) -> Path { p.push("*") }
742             do as_utf16_p(star(p).to_str()) |path_ptr| {
743                 let mut strings = ~[];
744                 let wfd_ptr = malloc_raw(rust_list_dir_wfd_size() as uint);
745                 let find_handle =
746                     FindFirstFileW(
747                         path_ptr,
748                         ::cast::transmute(wfd_ptr));
749                 if find_handle as libc::c_int != INVALID_HANDLE_VALUE {
750                     let mut more_files = 1 as libc::c_int;
751                     while more_files != 0 {
752                         let fp_buf = rust_list_dir_wfd_fp_buf(wfd_ptr);
753                         if fp_buf as uint == 0 {
754                             fail!("os::list_dir() failure: got null ptr from wfd");
755                         }
756                         else {
757                             let fp_vec = vec::from_buf(
758                                 fp_buf, wcslen(fp_buf) as uint);
759                             let fp_str = str::from_utf16(fp_vec);
760                             strings.push(fp_str);
761                         }
762                         more_files = FindNextFileW(
763                             find_handle,
764                             ::cast::transmute(wfd_ptr));
765                     }
766                     FindClose(find_handle);
767                     free(wfd_ptr)
768                 }
769                 strings
770             }
771         }
772         do get_list(p).consume_iter().filter |filename| {
773             "." != *filename && ".." != *filename
774         }.collect()
775     }
776 }
777
778 /**
779  * Lists the contents of a directory
780  *
781  * This version prepends each entry with the directory.
782  */
783 pub fn list_dir_path(p: &Path) -> ~[Path] {
784     list_dir(p).map(|f| p.push(*f))
785 }
786
787 /// Removes a directory at the specified path, after removing
788 /// all its contents. Use carefully!
789 pub fn remove_dir_recursive(p: &Path) -> bool {
790     let mut error_happened = false;
791     do walk_dir(p) |inner| {
792         if !error_happened {
793             if path_is_dir(inner) {
794                 if !remove_dir_recursive(inner) {
795                     error_happened = true;
796                 }
797             }
798             else {
799                 if !remove_file(inner) {
800                     error_happened = true;
801                 }
802             }
803         }
804         true
805     };
806     // Directory should now be empty
807     !error_happened && remove_dir(p)
808 }
809
810 /// Removes a directory at the specified path
811 pub fn remove_dir(p: &Path) -> bool {
812    return rmdir(p);
813
814     #[cfg(windows)]
815     fn rmdir(p: &Path) -> bool {
816         unsafe {
817             use os::win32::as_utf16_p;
818             return do as_utf16_p(p.to_str()) |buf| {
819                 libc::RemoveDirectoryW(buf) != (0 as libc::BOOL)
820             };
821         }
822     }
823
824     #[cfg(unix)]
825     fn rmdir(p: &Path) -> bool {
826         do p.to_str().as_c_str |buf| {
827             unsafe {
828                 libc::rmdir(buf) == (0 as c_int)
829             }
830         }
831     }
832 }
833
834 /// Changes the current working directory to the specified path, returning
835 /// whether the change was completed successfully or not.
836 pub fn change_dir(p: &Path) -> bool {
837     return chdir(p);
838
839     #[cfg(windows)]
840     fn chdir(p: &Path) -> bool {
841         unsafe {
842             use os::win32::as_utf16_p;
843             return do as_utf16_p(p.to_str()) |buf| {
844                 libc::SetCurrentDirectoryW(buf) != (0 as libc::BOOL)
845             };
846         }
847     }
848
849     #[cfg(unix)]
850     fn chdir(p: &Path) -> bool {
851         do p.to_str().as_c_str |buf| {
852             unsafe {
853                 libc::chdir(buf) == (0 as c_int)
854             }
855         }
856     }
857 }
858
859 /// Copies a file from one location to another
860 pub fn copy_file(from: &Path, to: &Path) -> bool {
861     return do_copy_file(from, to);
862
863     #[cfg(windows)]
864     fn do_copy_file(from: &Path, to: &Path) -> bool {
865         unsafe {
866             use os::win32::as_utf16_p;
867             return do as_utf16_p(from.to_str()) |fromp| {
868                 do as_utf16_p(to.to_str()) |top| {
869                     libc::CopyFileW(fromp, top, (0 as libc::BOOL)) !=
870                         (0 as libc::BOOL)
871                 }
872             }
873         }
874     }
875
876     #[cfg(unix)]
877     fn do_copy_file(from: &Path, to: &Path) -> bool {
878         unsafe {
879             let istream = do from.to_str().as_c_str |fromp| {
880                 do "rb".as_c_str |modebuf| {
881                     libc::fopen(fromp, modebuf)
882                 }
883             };
884             if istream as uint == 0u {
885                 return false;
886             }
887             // Preserve permissions
888             let from_mode = from.get_mode().expect("copy_file: couldn't get permissions \
889                                                     for source file");
890
891             let ostream = do to.to_str().as_c_str |top| {
892                 do "w+b".as_c_str |modebuf| {
893                     libc::fopen(top, modebuf)
894                 }
895             };
896             if ostream as uint == 0u {
897                 fclose(istream);
898                 return false;
899             }
900             let bufsize = 8192u;
901             let mut buf = vec::with_capacity::<u8>(bufsize);
902             let mut done = false;
903             let mut ok = true;
904             while !done {
905                 do buf.as_mut_buf |b, _sz| {
906                   let nread = libc::fread(b as *mut c_void, 1u as size_t,
907                                           bufsize as size_t,
908                                           istream);
909                   if nread > 0 as size_t {
910                       if libc::fwrite(b as *c_void, 1u as size_t, nread,
911                                       ostream) != nread {
912                           ok = false;
913                           done = true;
914                       }
915                   } else {
916                       done = true;
917                   }
918               }
919             }
920             fclose(istream);
921             fclose(ostream);
922
923             // Give the new file the old file's permissions
924             if do to.to_str().as_c_str |to_buf| {
925                 libc::chmod(to_buf, from_mode as libc::mode_t)
926             } != 0 {
927                 return false; // should be a condition...
928             }
929             return ok;
930         }
931     }
932 }
933
934 /// Deletes an existing file
935 pub fn remove_file(p: &Path) -> bool {
936     return unlink(p);
937
938     #[cfg(windows)]
939     fn unlink(p: &Path) -> bool {
940         unsafe {
941             use os::win32::as_utf16_p;
942             return do as_utf16_p(p.to_str()) |buf| {
943                 libc::DeleteFileW(buf) != (0 as libc::BOOL)
944             };
945         }
946     }
947
948     #[cfg(unix)]
949     fn unlink(p: &Path) -> bool {
950         unsafe {
951             do p.to_str().as_c_str |buf| {
952                 libc::unlink(buf) == (0 as c_int)
953             }
954         }
955     }
956 }
957
958 #[cfg(unix)]
959 /// Returns the platform-specific value of errno
960 pub fn errno() -> int {
961     #[cfg(target_os = "macos")]
962     #[cfg(target_os = "freebsd")]
963     fn errno_location() -> *c_int {
964         #[nolink]
965         extern {
966             fn __error() -> *c_int;
967         }
968         unsafe {
969             __error()
970         }
971     }
972
973     #[cfg(target_os = "linux")]
974     #[cfg(target_os = "android")]
975     fn errno_location() -> *c_int {
976         #[nolink]
977         extern {
978             fn __errno_location() -> *c_int;
979         }
980         unsafe {
981             __errno_location()
982         }
983     }
984
985     unsafe {
986         (*errno_location()) as int
987     }
988 }
989
990 #[cfg(windows)]
991 /// Returns the platform-specific value of errno
992 pub fn errno() -> uint {
993     use libc::types::os::arch::extra::DWORD;
994
995     #[link_name = "kernel32"]
996     #[abi = "stdcall"]
997     extern "stdcall" {
998         fn GetLastError() -> DWORD;
999     }
1000
1001     unsafe {
1002         GetLastError() as uint
1003     }
1004 }
1005
1006 /// Get a string representing the platform-dependent last error
1007 pub fn last_os_error() -> ~str {
1008     #[cfg(unix)]
1009     fn strerror() -> ~str {
1010         #[cfg(target_os = "macos")]
1011         #[cfg(target_os = "android")]
1012         #[cfg(target_os = "freebsd")]
1013         fn strerror_r(errnum: c_int, buf: *mut c_char, buflen: size_t)
1014                       -> c_int {
1015             #[nolink]
1016             extern {
1017                 fn strerror_r(errnum: c_int, buf: *mut c_char, buflen: size_t)
1018                               -> c_int;
1019             }
1020             unsafe {
1021                 strerror_r(errnum, buf, buflen)
1022             }
1023         }
1024
1025         // GNU libc provides a non-compliant version of strerror_r by default
1026         // and requires macros to instead use the POSIX compliant variant.
1027         // So we just use __xpg_strerror_r which is always POSIX compliant
1028         #[cfg(target_os = "linux")]
1029         fn strerror_r(errnum: c_int, buf: *mut c_char, buflen: size_t) -> c_int {
1030             #[nolink]
1031             extern {
1032                 fn __xpg_strerror_r(errnum: c_int,
1033                                     buf: *mut c_char,
1034                                     buflen: size_t)
1035                                     -> c_int;
1036             }
1037             unsafe {
1038                 __xpg_strerror_r(errnum, buf, buflen)
1039             }
1040         }
1041
1042         let mut buf = [0 as c_char, ..TMPBUF_SZ];
1043         unsafe {
1044             let err = strerror_r(errno() as c_int, &mut buf[0],
1045                                  TMPBUF_SZ as size_t);
1046             if err < 0 {
1047                 fail!("strerror_r failure");
1048             }
1049
1050             str::raw::from_c_str(&buf[0])
1051         }
1052     }
1053
1054     #[cfg(windows)]
1055     fn strerror() -> ~str {
1056         use libc::types::os::arch::extra::DWORD;
1057         use libc::types::os::arch::extra::LPSTR;
1058         use libc::types::os::arch::extra::LPVOID;
1059
1060         #[link_name = "kernel32"]
1061         #[abi = "stdcall"]
1062         extern "stdcall" {
1063             fn FormatMessageA(flags: DWORD,
1064                               lpSrc: LPVOID,
1065                               msgId: DWORD,
1066                               langId: DWORD,
1067                               buf: LPSTR,
1068                               nsize: DWORD,
1069                               args: *c_void)
1070                               -> DWORD;
1071         }
1072
1073         static FORMAT_MESSAGE_FROM_SYSTEM: DWORD = 0x00001000;
1074         static FORMAT_MESSAGE_IGNORE_INSERTS: DWORD = 0x00000200;
1075
1076         let mut buf = [0 as c_char, ..TMPBUF_SZ];
1077
1078         // This value is calculated from the macro
1079         // MAKELANGID(LANG_SYSTEM_DEFAULT, SUBLANG_SYS_DEFAULT)
1080         let langId = 0x0800 as DWORD;
1081         let err = errno() as DWORD;
1082         unsafe {
1083             let res = FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM |
1084                                      FORMAT_MESSAGE_IGNORE_INSERTS,
1085                                      ptr::mut_null(), err, langId,
1086                                      &mut buf[0], TMPBUF_SZ as DWORD,
1087                                      ptr::null());
1088             if res == 0 {
1089                 fail!("[%?] FormatMessage failure", errno());
1090             }
1091
1092             str::raw::from_c_str(&buf[0])
1093         }
1094     }
1095
1096     strerror()
1097 }
1098
1099 /**
1100  * Sets the process exit code
1101  *
1102  * Sets the exit code returned by the process if all supervised tasks
1103  * terminate successfully (without failing). If the current root task fails
1104  * and is supervised by the scheduler then any user-specified exit status is
1105  * ignored and the process exits with the default failure status
1106  */
1107 pub fn set_exit_status(code: int) {
1108     use rt;
1109     use rt::OldTaskContext;
1110
1111     if rt::context() == OldTaskContext {
1112         unsafe {
1113             rustrt::rust_set_exit_status(code as libc::intptr_t);
1114         }
1115     } else {
1116         rt::util::set_exit_status(code);
1117     }
1118 }
1119
1120 unsafe fn load_argc_and_argv(argc: c_int, argv: **c_char) -> ~[~str] {
1121     let mut args = ~[];
1122     for i in range(0u, argc as uint) {
1123         args.push(str::raw::from_c_str(*argv.offset(i as int)));
1124     }
1125     args
1126 }
1127
1128 /**
1129  * Returns the command line arguments
1130  *
1131  * Returns a list of the command line arguments.
1132  */
1133 #[cfg(target_os = "macos")]
1134 pub fn real_args() -> ~[~str] {
1135     unsafe {
1136         let (argc, argv) = (*_NSGetArgc() as c_int,
1137                             *_NSGetArgv() as **c_char);
1138         load_argc_and_argv(argc, argv)
1139     }
1140 }
1141
1142 #[cfg(target_os = "linux")]
1143 #[cfg(target_os = "android")]
1144 #[cfg(target_os = "freebsd")]
1145 pub fn real_args() -> ~[~str] {
1146     use rt;
1147     use rt::TaskContext;
1148
1149     if rt::context() == TaskContext {
1150         match rt::args::clone() {
1151             Some(args) => args,
1152             None => fail!("process arguments not initialized")
1153         }
1154     } else {
1155         unsafe {
1156             let argc = rustrt::rust_get_argc();
1157             let argv = rustrt::rust_get_argv();
1158             load_argc_and_argv(argc, argv)
1159         }
1160     }
1161 }
1162
1163 #[cfg(windows)]
1164 pub fn real_args() -> ~[~str] {
1165     let mut nArgs: c_int = 0;
1166     let lpArgCount: *mut c_int = &mut nArgs;
1167     let lpCmdLine = unsafe { GetCommandLineW() };
1168     let szArgList = unsafe { CommandLineToArgvW(lpCmdLine, lpArgCount) };
1169
1170     let mut args = ~[];
1171     for i in range(0u, nArgs as uint) {
1172         unsafe {
1173             // Determine the length of this argument.
1174             let ptr = *szArgList.offset(i as int);
1175             let mut len = 0;
1176             while *ptr.offset(len as int) != 0 { len += 1; }
1177
1178             // Push it onto the list.
1179             args.push(vec::raw::buf_as_slice(ptr, len,
1180                                              str::from_utf16));
1181         }
1182     }
1183
1184     unsafe {
1185         LocalFree(cast::transmute(szArgList));
1186     }
1187
1188     return args;
1189 }
1190
1191 type LPCWSTR = *u16;
1192
1193 #[cfg(windows)]
1194 #[link_name="kernel32"]
1195 #[abi="stdcall"]
1196 extern "stdcall" {
1197     fn GetCommandLineW() -> LPCWSTR;
1198     fn LocalFree(ptr: *c_void);
1199 }
1200
1201 #[cfg(windows)]
1202 #[link_name="shell32"]
1203 #[abi="stdcall"]
1204 extern "stdcall" {
1205     fn CommandLineToArgvW(lpCmdLine: LPCWSTR, pNumArgs: *mut c_int) -> **u16;
1206 }
1207
1208 struct OverriddenArgs {
1209     val: ~[~str]
1210 }
1211
1212 static overridden_arg_key: local_data::Key<@OverriddenArgs> = &local_data::Key;
1213
1214 /// Returns the arguments which this program was started with (normally passed
1215 /// via the command line).
1216 ///
1217 /// The return value of the function can be changed by invoking the
1218 /// `os::set_args` function.
1219 pub fn args() -> ~[~str] {
1220     match local_data::get(overridden_arg_key, |k| k.map(|&k| *k)) {
1221         None => real_args(),
1222         Some(args) => args.val.clone()
1223     }
1224 }
1225
1226 /// For the current task, overrides the task-local cache of the arguments this
1227 /// program had when it started. These new arguments are only available to the
1228 /// current task via the `os::args` method.
1229 pub fn set_args(new_args: ~[~str]) {
1230     let overridden_args = @OverriddenArgs {
1231         val: new_args.clone()
1232     };
1233     local_data::set(overridden_arg_key, overridden_args);
1234 }
1235
1236 // FIXME #6100 we should really use an internal implementation of this - using
1237 // the POSIX glob functions isn't portable to windows, probably has slight
1238 // inconsistencies even where it is implemented, and makes extending
1239 // functionality a lot more difficult
1240 // FIXME #6101 also provide a non-allocating version - each_glob or so?
1241 /// Returns a vector of Path objects that match the given glob pattern
1242 #[cfg(target_os = "linux")]
1243 #[cfg(target_os = "android")]
1244 #[cfg(target_os = "freebsd")]
1245 #[cfg(target_os = "macos")]
1246 pub fn glob(pattern: &str) -> ~[Path] {
1247     #[cfg(target_os = "linux")]
1248     #[cfg(target_os = "android")]
1249     fn default_glob_t () -> libc::glob_t {
1250         libc::glob_t {
1251             gl_pathc: 0,
1252             gl_pathv: ptr::null(),
1253             gl_offs: 0,
1254             __unused1: ptr::null(),
1255             __unused2: ptr::null(),
1256             __unused3: ptr::null(),
1257             __unused4: ptr::null(),
1258             __unused5: ptr::null(),
1259         }
1260     }
1261
1262     #[cfg(target_os = "freebsd")]
1263     fn default_glob_t () -> libc::glob_t {
1264         libc::glob_t {
1265             gl_pathc: 0,
1266             __unused1: 0,
1267             gl_offs: 0,
1268             __unused2: 0,
1269             gl_pathv: ptr::null(),
1270             __unused3: ptr::null(),
1271             __unused4: ptr::null(),
1272             __unused5: ptr::null(),
1273             __unused6: ptr::null(),
1274             __unused7: ptr::null(),
1275             __unused8: ptr::null(),
1276         }
1277     }
1278
1279     #[cfg(target_os = "macos")]
1280     fn default_glob_t () -> libc::glob_t {
1281         libc::glob_t {
1282             gl_pathc: 0,
1283             __unused1: 0,
1284             gl_offs: 0,
1285             __unused2: 0,
1286             gl_pathv: ptr::null(),
1287             __unused3: ptr::null(),
1288             __unused4: ptr::null(),
1289             __unused5: ptr::null(),
1290             __unused6: ptr::null(),
1291             __unused7: ptr::null(),
1292             __unused8: ptr::null(),
1293         }
1294     }
1295
1296     let mut g = default_glob_t();
1297     do pattern.as_c_str |c_pattern| {
1298         unsafe { libc::glob(c_pattern, 0, ptr::null(), &mut g) }
1299     };
1300     do(|| {
1301         let paths = unsafe {
1302             vec::raw::from_buf_raw(g.gl_pathv, g.gl_pathc as uint)
1303         };
1304         do paths.map |&c_str| {
1305             Path(unsafe { str::raw::from_c_str(c_str) })
1306         }
1307     }).finally {
1308         unsafe { libc::globfree(&mut g) };
1309     }
1310 }
1311
1312 /// Returns a vector of Path objects that match the given glob pattern
1313 #[cfg(target_os = "win32")]
1314 pub fn glob(_pattern: &str) -> ~[Path] {
1315     fail!("glob() is unimplemented on Windows")
1316 }
1317
1318 #[cfg(target_os = "macos")]
1319 extern {
1320     // These functions are in crt_externs.h.
1321     pub fn _NSGetArgc() -> *c_int;
1322     pub fn _NSGetArgv() -> ***c_char;
1323 }
1324
1325 // Round up `from` to be divisible by `to`
1326 fn round_up(from: uint, to: uint) -> uint {
1327     let r = if from % to == 0 {
1328         from
1329     } else {
1330         from + to - (from % to)
1331     };
1332     if r == 0 {
1333         to
1334     } else {
1335         r
1336     }
1337 }
1338
1339 #[cfg(unix)]
1340 pub fn page_size() -> uint {
1341     unsafe {
1342         libc::sysconf(libc::_SC_PAGESIZE) as uint
1343     }
1344 }
1345
1346 #[cfg(windows)]
1347 pub fn page_size() -> uint {
1348   unsafe {
1349     let mut info = libc::SYSTEM_INFO::new();
1350     libc::GetSystemInfo(&mut info);
1351
1352     return info.dwPageSize as uint;
1353   }
1354 }
1355
1356 pub struct MemoryMap {
1357     data: *mut u8,
1358     len: size_t,
1359     kind: MemoryMapKind
1360 }
1361
1362 pub enum MemoryMapKind {
1363     MapFile(*c_void),
1364     MapVirtual
1365 }
1366
1367 pub enum MapOption {
1368     MapReadable,
1369     MapWritable,
1370     MapExecutable,
1371     MapAddr(*c_void),
1372     MapFd(c_int),
1373     MapOffset(uint)
1374 }
1375
1376 pub enum MapError {
1377     // Linux-specific errors
1378     ErrFdNotAvail,
1379     ErrInvalidFd,
1380     ErrUnaligned,
1381     ErrNoMapSupport,
1382     ErrNoMem,
1383     ErrUnknown(libc::c_int),
1384
1385     // Windows-specific errors
1386     ErrUnsupProt,
1387     ErrUnsupOffset,
1388     ErrNeedRW,
1389     ErrAlreadyExists,
1390     ErrVirtualAlloc(uint),
1391     ErrCreateFileMappingW(uint),
1392     ErrMapViewOfFile(uint)
1393 }
1394
1395 impl to_str::ToStr for MapError {
1396     fn to_str(&self) -> ~str {
1397         match *self {
1398             ErrFdNotAvail => ~"fd not available for reading or writing",
1399             ErrInvalidFd => ~"Invalid fd",
1400             ErrUnaligned => ~"Unaligned address, invalid flags, \
1401                               negative length or unaligned offset",
1402             ErrNoMapSupport=> ~"File doesn't support mapping",
1403             ErrNoMem => ~"Invalid address, or not enough available memory",
1404             ErrUnknown(code) => fmt!("Unknown error=%?", code),
1405             ErrUnsupProt => ~"Protection mode unsupported",
1406             ErrUnsupOffset => ~"Offset in virtual memory mode is unsupported",
1407             ErrNeedRW => ~"File mapping should be at least readable/writable",
1408             ErrAlreadyExists => ~"File mapping for specified file already exists",
1409             ErrVirtualAlloc(code) => fmt!("VirtualAlloc failure=%?", code),
1410             ErrCreateFileMappingW(code) => fmt!("CreateFileMappingW failure=%?", code),
1411             ErrMapViewOfFile(code) => fmt!("MapViewOfFile failure=%?", code)
1412         }
1413     }
1414 }
1415
1416 #[cfg(unix)]
1417 impl MemoryMap {
1418     pub fn new(min_len: uint, options: ~[MapOption]) -> Result<~MemoryMap, MapError> {
1419         use libc::off_t;
1420
1421         let mut addr: *c_void = ptr::null();
1422         let mut prot: c_int = 0;
1423         let mut flags: c_int = libc::MAP_PRIVATE;
1424         let mut fd: c_int = -1;
1425         let mut offset: off_t = 0;
1426         let len = round_up(min_len, page_size()) as size_t;
1427
1428         for &o in options.iter() {
1429             match o {
1430                 MapReadable => { prot |= libc::PROT_READ; },
1431                 MapWritable => { prot |= libc::PROT_WRITE; },
1432                 MapExecutable => { prot |= libc::PROT_EXEC; },
1433                 MapAddr(addr_) => {
1434                     flags |= libc::MAP_FIXED;
1435                     addr = addr_;
1436                 },
1437                 MapFd(fd_) => {
1438                     flags |= libc::MAP_FILE;
1439                     fd = fd_;
1440                 },
1441                 MapOffset(offset_) => { offset = offset_ as off_t; }
1442             }
1443         }
1444         if fd == -1 { flags |= libc::MAP_ANON; }
1445
1446         let r = unsafe {
1447             libc::mmap(addr, len, prot, flags, fd, offset)
1448         };
1449         if r == libc::MAP_FAILED {
1450             Err(match errno() as c_int {
1451                 libc::EACCES => ErrFdNotAvail,
1452                 libc::EBADF => ErrInvalidFd,
1453                 libc::EINVAL => ErrUnaligned,
1454                 libc::ENODEV => ErrNoMapSupport,
1455                 libc::ENOMEM => ErrNoMem,
1456                 code => ErrUnknown(code)
1457             })
1458         } else {
1459             Ok(~MemoryMap {
1460                data: r as *mut u8,
1461                len: len,
1462                kind: if fd == -1 {
1463                    MapVirtual
1464                } else {
1465                    MapFile(ptr::null())
1466                }
1467             })
1468         }
1469     }
1470 }
1471
1472 #[cfg(unix)]
1473 impl Drop for MemoryMap {
1474     fn drop(&self) {
1475         unsafe {
1476             match libc::munmap(self.data as *c_void, self.len) {
1477                 0 => (),
1478                 -1 => error!(match errno() as c_int {
1479                     libc::EINVAL => ~"invalid addr or len",
1480                     e => fmt!("unknown errno=%?", e)
1481                 }),
1482                 r => error!(fmt!("Unexpected result %?", r))
1483             }
1484         }
1485     }
1486 }
1487
1488 #[cfg(windows)]
1489 impl MemoryMap {
1490     pub fn new(min_len: uint, options: ~[MapOption]) -> Result<~MemoryMap, MapError> {
1491         use libc::types::os::arch::extra::{LPVOID, DWORD, SIZE_T, HANDLE};
1492
1493         let mut lpAddress: LPVOID = ptr::mut_null();
1494         let mut readable = false;
1495         let mut writable = false;
1496         let mut executable = false;
1497         let mut fd: c_int = -1;
1498         let mut offset: uint = 0;
1499         let len = round_up(min_len, page_size()) as SIZE_T;
1500
1501         for &o in options.iter() {
1502             match o {
1503                 MapReadable => { readable = true; },
1504                 MapWritable => { writable = true; },
1505                 MapExecutable => { executable = true; }
1506                 MapAddr(addr_) => { lpAddress = addr_ as LPVOID; },
1507                 MapFd(fd_) => { fd = fd_; },
1508                 MapOffset(offset_) => { offset = offset_; }
1509             }
1510         }
1511
1512         let flProtect = match (executable, readable, writable) {
1513             (false, false, false) if fd == -1 => libc::PAGE_NOACCESS,
1514             (false, true, false) => libc::PAGE_READONLY,
1515             (false, true, true) => libc::PAGE_READWRITE,
1516             (true, false, false) if fd == -1 => libc::PAGE_EXECUTE,
1517             (true, true, false) => libc::PAGE_EXECUTE_READ,
1518             (true, true, true) => libc::PAGE_EXECUTE_READWRITE,
1519             _ => return Err(ErrUnsupProt)
1520         };
1521
1522         if fd == -1 {
1523             if offset != 0 {
1524                 return Err(ErrUnsupOffset);
1525             }
1526             let r = unsafe {
1527                 libc::VirtualAlloc(lpAddress,
1528                                    len,
1529                                    libc::MEM_COMMIT | libc::MEM_RESERVE,
1530                                    flProtect)
1531             };
1532             match r as uint {
1533                 0 => Err(ErrVirtualAlloc(errno())),
1534                 _ => Ok(~MemoryMap {
1535                    data: r as *mut u8,
1536                    len: len,
1537                    kind: MapVirtual
1538                 })
1539             }
1540         } else {
1541             let dwDesiredAccess = match (readable, writable) {
1542                 (true, true) => libc::FILE_MAP_ALL_ACCESS,
1543                 (true, false) => libc::FILE_MAP_READ,
1544                 (false, true) => libc::FILE_MAP_WRITE,
1545                 _ => {
1546                     return Err(ErrNeedRW);
1547                 }
1548             };
1549             unsafe {
1550                 let hFile = libc::get_osfhandle(fd) as HANDLE;
1551                 let mapping = libc::CreateFileMappingW(hFile,
1552                                                        ptr::mut_null(),
1553                                                        flProtect,
1554                                                        (len >> 32) as DWORD,
1555                                                        (len & 0xffff_ffff) as DWORD,
1556                                                        ptr::null());
1557                 if mapping == ptr::mut_null() {
1558                     return Err(ErrCreateFileMappingW(errno()));
1559                 }
1560                 if errno() as c_int == libc::ERROR_ALREADY_EXISTS {
1561                     return Err(ErrAlreadyExists);
1562                 }
1563                 let r = libc::MapViewOfFile(mapping,
1564                                             dwDesiredAccess,
1565                                             (offset >> 32) as DWORD,
1566                                             (offset & 0xffff_ffff) as DWORD,
1567                                             0);
1568                 match r as uint {
1569                     0 => Err(ErrMapViewOfFile(errno())),
1570                     _ => Ok(~MemoryMap {
1571                        data: r as *mut u8,
1572                        len: len,
1573                        kind: MapFile(mapping as *c_void)
1574                     })
1575                 }
1576             }
1577         }
1578     }
1579 }
1580
1581 #[cfg(windows)]
1582 impl Drop for MemoryMap {
1583     fn drop(&self) {
1584         use libc::types::os::arch::extra::{LPCVOID, HANDLE};
1585
1586         unsafe {
1587             match self.kind {
1588                 MapVirtual => match libc::VirtualFree(self.data as *mut c_void,
1589                                                       self.len,
1590                                                       libc::MEM_RELEASE) {
1591                     0 => error!(fmt!("VirtualFree failed: %?", errno())),
1592                     _ => ()
1593                 },
1594                 MapFile(mapping) => {
1595                     if libc::UnmapViewOfFile(self.data as LPCVOID) != 0 {
1596                         error!(fmt!("UnmapViewOfFile failed: %?", errno()));
1597                     }
1598                     if libc::CloseHandle(mapping as HANDLE) != 0 {
1599                         error!(fmt!("CloseHandle failed: %?", errno()));
1600                     }
1601                 }
1602             }
1603         }
1604     }
1605 }
1606
1607 pub mod consts {
1608
1609     #[cfg(unix)]
1610     pub use os::consts::unix::*;
1611
1612     #[cfg(windows)]
1613     pub use os::consts::windows::*;
1614
1615     #[cfg(target_os = "macos")]
1616     pub use os::consts::macos::*;
1617
1618     #[cfg(target_os = "freebsd")]
1619     pub use os::consts::freebsd::*;
1620
1621     #[cfg(target_os = "linux")]
1622     pub use os::consts::linux::*;
1623
1624     #[cfg(target_os = "android")]
1625     pub use os::consts::android::*;
1626
1627     #[cfg(target_os = "win32")]
1628     pub use os::consts::win32::*;
1629
1630     #[cfg(target_arch = "x86")]
1631     pub use os::consts::x86::*;
1632
1633     #[cfg(target_arch = "x86_64")]
1634     pub use os::consts::x86_64::*;
1635
1636     #[cfg(target_arch = "arm")]
1637     pub use os::consts::arm::*;
1638
1639     #[cfg(target_arch = "mips")]
1640     use os::consts::mips::*;
1641
1642     pub mod unix {
1643         pub static FAMILY: &'static str = "unix";
1644     }
1645
1646     pub mod windows {
1647         pub static FAMILY: &'static str = "windows";
1648     }
1649
1650     pub mod macos {
1651         pub static SYSNAME: &'static str = "macos";
1652         pub static DLL_PREFIX: &'static str = "lib";
1653         pub static DLL_SUFFIX: &'static str = ".dylib";
1654         pub static EXE_SUFFIX: &'static str = "";
1655     }
1656
1657     pub mod freebsd {
1658         pub static SYSNAME: &'static str = "freebsd";
1659         pub static DLL_PREFIX: &'static str = "lib";
1660         pub static DLL_SUFFIX: &'static str = ".so";
1661         pub static EXE_SUFFIX: &'static str = "";
1662     }
1663
1664     pub mod linux {
1665         pub static SYSNAME: &'static str = "linux";
1666         pub static DLL_PREFIX: &'static str = "lib";
1667         pub static DLL_SUFFIX: &'static str = ".so";
1668         pub static EXE_SUFFIX: &'static str = "";
1669     }
1670
1671     pub mod android {
1672         pub static SYSNAME: &'static str = "android";
1673         pub static DLL_PREFIX: &'static str = "lib";
1674         pub static DLL_SUFFIX: &'static str = ".so";
1675         pub static EXE_SUFFIX: &'static str = "";
1676     }
1677
1678     pub mod win32 {
1679         pub static SYSNAME: &'static str = "win32";
1680         pub static DLL_PREFIX: &'static str = "";
1681         pub static DLL_SUFFIX: &'static str = ".dll";
1682         pub static EXE_SUFFIX: &'static str = ".exe";
1683     }
1684
1685
1686     pub mod x86 {
1687         pub static ARCH: &'static str = "x86";
1688     }
1689     pub mod x86_64 {
1690         pub static ARCH: &'static str = "x86_64";
1691     }
1692     pub mod arm {
1693         pub static ARCH: &'static str = "arm";
1694     }
1695     pub mod mips {
1696         pub static ARCH: &'static str = "mips";
1697     }
1698 }
1699
1700 #[cfg(test)]
1701 mod tests {
1702     use libc::{c_int, c_void, size_t};
1703     use libc;
1704     use option::Some;
1705     use option;
1706     use os::{env, getcwd, getenv, make_absolute, real_args};
1707     use os::{remove_file, setenv, unsetenv};
1708     use os;
1709     use path::Path;
1710     use rand::RngUtil;
1711     use rand;
1712     use run;
1713     use str::StrSlice;
1714     use vec::CopyableVector;
1715     use libc::consts::os::posix88::{S_IRUSR, S_IWUSR, S_IXUSR};
1716
1717
1718     #[test]
1719     pub fn last_os_error() {
1720         debug!(os::last_os_error());
1721     }
1722
1723     #[test]
1724     pub fn test_args() {
1725         let a = real_args();
1726         assert!(a.len() >= 1);
1727     }
1728
1729     fn make_rand_name() -> ~str {
1730         let mut rng = rand::rng();
1731         let n = ~"TEST" + rng.gen_str(10u);
1732         assert!(getenv(n).is_none());
1733         n
1734     }
1735
1736     #[test]
1737     fn test_setenv() {
1738         let n = make_rand_name();
1739         setenv(n, "VALUE");
1740         assert_eq!(getenv(n), option::Some(~"VALUE"));
1741     }
1742
1743     #[test]
1744     fn test_unsetenv() {
1745         let n = make_rand_name();
1746         setenv(n, "VALUE");
1747         unsetenv(n);
1748         assert_eq!(getenv(n), option::None);
1749     }
1750
1751     #[test]
1752     #[ignore(cfg(windows))]
1753     #[ignore]
1754     fn test_setenv_overwrite() {
1755         let n = make_rand_name();
1756         setenv(n, "1");
1757         setenv(n, "2");
1758         assert_eq!(getenv(n), option::Some(~"2"));
1759         setenv(n, "");
1760         assert_eq!(getenv(n), option::Some(~""));
1761     }
1762
1763     // Windows GetEnvironmentVariable requires some extra work to make sure
1764     // the buffer the variable is copied into is the right size
1765     #[test]
1766     #[ignore(cfg(windows))]
1767     #[ignore]
1768     fn test_getenv_big() {
1769         let mut s = ~"";
1770         let mut i = 0;
1771         while i < 100 {
1772             s = s + "aaaaaaaaaa";
1773             i += 1;
1774         }
1775         let n = make_rand_name();
1776         setenv(n, s);
1777         debug!(s.clone());
1778         assert_eq!(getenv(n), option::Some(s));
1779     }
1780
1781     #[test]
1782     fn test_self_exe_path() {
1783         let path = os::self_exe_path();
1784         assert!(path.is_some());
1785         let path = path.unwrap();
1786         debug!(path.clone());
1787
1788         // Hard to test this function
1789         assert!(path.is_absolute);
1790     }
1791
1792     #[test]
1793     #[ignore]
1794     fn test_env_getenv() {
1795         let e = env();
1796         assert!(e.len() > 0u);
1797         for p in e.iter() {
1798             let (n, v) = (*p).clone();
1799             debug!(n.clone());
1800             let v2 = getenv(n);
1801             // MingW seems to set some funky environment variables like
1802             // "=C:=C:\MinGW\msys\1.0\bin" and "!::=::\" that are returned
1803             // from env() but not visible from getenv().
1804             assert!(v2.is_none() || v2 == option::Some(v));
1805         }
1806     }
1807
1808     #[test]
1809     fn test_env_setenv() {
1810         let n = make_rand_name();
1811
1812         let mut e = env();
1813         setenv(n, "VALUE");
1814         assert!(!e.contains(&(n.clone(), ~"VALUE")));
1815
1816         e = env();
1817         assert!(e.contains(&(n, ~"VALUE")));
1818     }
1819
1820     #[test]
1821     fn test() {
1822         assert!((!Path("test-path").is_absolute));
1823
1824         debug!("Current working directory: %s", getcwd().to_str());
1825
1826         debug!(make_absolute(&Path("test-path")));
1827         debug!(make_absolute(&Path("/usr/bin")));
1828     }
1829
1830     #[test]
1831     #[cfg(unix)]
1832     fn homedir() {
1833         let oldhome = getenv("HOME");
1834
1835         setenv("HOME", "/home/MountainView");
1836         assert_eq!(os::homedir(), Some(Path("/home/MountainView")));
1837
1838         setenv("HOME", "");
1839         assert!(os::homedir().is_none());
1840
1841         for s in oldhome.iter() { setenv("HOME", *s) }
1842     }
1843
1844     #[test]
1845     #[cfg(windows)]
1846     fn homedir() {
1847
1848         let oldhome = getenv("HOME");
1849         let olduserprofile = getenv("USERPROFILE");
1850
1851         setenv("HOME", "");
1852         setenv("USERPROFILE", "");
1853
1854         assert!(os::homedir().is_none());
1855
1856         setenv("HOME", "/home/MountainView");
1857         assert_eq!(os::homedir(), Some(Path("/home/MountainView")));
1858
1859         setenv("HOME", "");
1860
1861         setenv("USERPROFILE", "/home/MountainView");
1862         assert_eq!(os::homedir(), Some(Path("/home/MountainView")));
1863
1864         setenv("HOME", "/home/MountainView");
1865         setenv("USERPROFILE", "/home/PaloAlto");
1866         assert_eq!(os::homedir(), Some(Path("/home/MountainView")));
1867
1868         oldhome.iter().advance(|s| { setenv("HOME", *s); true });
1869         olduserprofile.iter().advance(|s| { setenv("USERPROFILE", *s); true });
1870     }
1871
1872     #[test]
1873     fn tmpdir() {
1874         assert!(!os::tmpdir().to_str().is_empty());
1875     }
1876
1877     // Issue #712
1878     #[test]
1879     fn test_list_dir_no_invalid_memory_access() {
1880         os::list_dir(&Path("."));
1881     }
1882
1883     #[test]
1884     fn list_dir() {
1885         let dirs = os::list_dir(&Path("."));
1886         // Just assuming that we've got some contents in the current directory
1887         assert!(dirs.len() > 0u);
1888
1889         for dir in dirs.iter() {
1890             debug!((*dir).clone());
1891         }
1892     }
1893
1894     #[test]
1895     fn list_dir_empty_path() {
1896         let dirs = os::list_dir(&Path(""));
1897         assert!(dirs.is_empty());
1898     }
1899
1900     #[test]
1901     #[cfg(not(windows))]
1902     fn list_dir_root() {
1903         let dirs = os::list_dir(&Path("/"));
1904         assert!(dirs.len() > 1);
1905     }
1906     #[test]
1907     #[cfg(windows)]
1908     fn list_dir_root() {
1909         let dirs = os::list_dir(&Path("C:\\"));
1910         assert!(dirs.len() > 1);
1911     }
1912
1913
1914     #[test]
1915     fn path_is_dir() {
1916         assert!((os::path_is_dir(&Path("."))));
1917         assert!((!os::path_is_dir(&Path("test/stdtest/fs.rs"))));
1918     }
1919
1920     #[test]
1921     fn path_exists() {
1922         assert!((os::path_exists(&Path("."))));
1923         assert!((!os::path_exists(&Path(
1924                      "test/nonexistent-bogus-path"))));
1925     }
1926
1927     #[test]
1928     fn copy_file_does_not_exist() {
1929       assert!(!os::copy_file(&Path("test/nonexistent-bogus-path"),
1930                             &Path("test/other-bogus-path")));
1931       assert!(!os::path_exists(&Path("test/other-bogus-path")));
1932     }
1933
1934     #[test]
1935     fn copy_file_ok() {
1936         unsafe {
1937           let tempdir = getcwd(); // would like to use $TMPDIR,
1938                                   // doesn't seem to work on Linux
1939           assert!((tempdir.to_str().len() > 0u));
1940           let input = tempdir.push("in.txt");
1941           let out = tempdir.push("out.txt");
1942
1943           /* Write the temp input file */
1944             let ostream = do input.to_str().as_c_str |fromp| {
1945                 do "w+b".as_c_str |modebuf| {
1946                     libc::fopen(fromp, modebuf)
1947                 }
1948           };
1949           assert!((ostream as uint != 0u));
1950           let s = ~"hello";
1951           let mut buf = s.as_bytes_with_null().to_owned();
1952           let len = buf.len();
1953           do buf.as_mut_buf |b, _len| {
1954               assert_eq!(libc::fwrite(b as *c_void, 1u as size_t,
1955                                       (s.len() + 1u) as size_t, ostream),
1956                          len as size_t)
1957           }
1958           assert_eq!(libc::fclose(ostream), (0u as c_int));
1959           let in_mode = input.get_mode();
1960           let rs = os::copy_file(&input, &out);
1961           if (!os::path_exists(&input)) {
1962             fail!("%s doesn't exist", input.to_str());
1963           }
1964           assert!((rs));
1965           let rslt = run::process_status("diff", [input.to_str(), out.to_str()]);
1966           assert_eq!(rslt, 0);
1967           assert_eq!(out.get_mode(), in_mode);
1968           assert!((remove_file(&input)));
1969           assert!((remove_file(&out)));
1970         }
1971     }
1972
1973     #[test]
1974     fn recursive_mkdir_slash() {
1975         let path = Path("/");
1976         assert!(os::mkdir_recursive(&path,  (S_IRUSR | S_IWUSR | S_IXUSR) as i32));
1977     }
1978
1979     #[test]
1980     fn recursive_mkdir_empty() {
1981         let path = Path("");
1982         assert!(!os::mkdir_recursive(&path, (S_IRUSR | S_IWUSR | S_IXUSR) as i32));
1983     }
1984
1985     #[test]
1986     fn memory_map_rw() {
1987         use result::{Ok, Err};
1988
1989         let chunk = match os::MemoryMap::new(16, ~[
1990             os::MapReadable,
1991             os::MapWritable
1992         ]) {
1993             Ok(chunk) => chunk,
1994             Err(msg) => fail!(msg.to_str())
1995         };
1996         assert!(chunk.len >= 16);
1997
1998         unsafe {
1999             *chunk.data = 0xBE;
2000             assert!(*chunk.data == 0xBE);
2001         }
2002     }
2003
2004     #[test]
2005     fn memory_map_file() {
2006         use result::{Ok, Err};
2007         use os::*;
2008         use libc::*;
2009
2010         #[cfg(unix)]
2011         fn lseek_(fd: c_int, size: uint) {
2012             unsafe {
2013                 assert!(lseek(fd, size as off_t, SEEK_SET) == size as off_t);
2014             }
2015         }
2016         #[cfg(windows)]
2017         fn lseek_(fd: c_int, size: uint) {
2018            unsafe {
2019                assert!(lseek(fd, size as c_long, SEEK_SET) == size as c_long);
2020            }
2021         }
2022
2023         let path = tmpdir().push("mmap_file.tmp");
2024         let size = page_size() * 2;
2025         remove_file(&path);
2026
2027         let fd = unsafe {
2028             let fd = do path.to_str().as_c_str |path| {
2029                 open(path, O_CREAT | O_RDWR | O_TRUNC, S_IRUSR | S_IWUSR)
2030             };
2031             lseek_(fd, size);
2032             do "x".as_c_str |x| {
2033                 assert!(write(fd, x as *c_void, 1) == 1);
2034             }
2035             fd
2036         };
2037         let chunk = match MemoryMap::new(size / 2, ~[
2038             MapReadable,
2039             MapWritable,
2040             MapFd(fd),
2041             MapOffset(size / 2)
2042         ]) {
2043             Ok(chunk) => chunk,
2044             Err(msg) => fail!(msg.to_str())
2045         };
2046         assert!(chunk.len > 0);
2047
2048         unsafe {
2049             *chunk.data = 0xbe;
2050             assert!(*chunk.data == 0xbe);
2051             close(fd);
2052         }
2053     }
2054
2055     // More recursive_mkdir tests are in extra::tempfile
2056 }