@@ -30,7 +30,7 @@ export close, fclose, fsync_fd, waitpid;
30
30
export env, getenv, setenv, fdopen, pipe;
31
31
export getcwd, dll_filename, self_exe_path;
32
32
export exe_suffix, dll_suffix, sysname, arch, family;
33
- export homedir, list_dir, list_dir_path, path_is_dir, path_exists,
33
+ export homedir, tmpdir , list_dir, list_dir_path, path_is_dir, path_exists,
34
34
make_absolute, make_dir, remove_dir, change_dir, remove_file,
35
35
copy_file;
36
36
export last_os_error;
@@ -464,6 +464,43 @@ fn homedir() -> option<Path> {
464
464
}
465
465
}
466
466
467
+ /**
468
+ * Returns the path to a temporary directory, if known.
469
+ *
470
+ * On Unix, returns the value of the 'TMPDIR' environment variable if it is
471
+ * set and non-empty and '/tmp' otherwise.
472
+ *
473
+ * On Windows, returns the value of, in order, the 'TMP', 'TEMP',
474
+ * 'USERPROFILE' environment variable if any are set and not the empty
475
+ * string. Otherwise, tmpdir returns option::none.
476
+ */
477
+ fn tmpdir ( ) -> option < Path > {
478
+ return lookup ( ) ;
479
+
480
+ fn getenv_nonempty ( v : Path ) -> option < Path > {
481
+ match getenv ( v) {
482
+ some( x) =>
483
+ if str:: is_empty ( x) {
484
+ none
485
+ } else {
486
+ some ( x)
487
+ } ,
488
+ _ => none
489
+ }
490
+ }
491
+
492
+ #[ cfg( unix) ]
493
+ fn lookup ( ) -> option < Path > {
494
+ option:: or ( getenv_nonempty ( ~"TMPDIR ") , some ( ~"/tmp") )
495
+ }
496
+
497
+ #[ cfg( windows) ]
498
+ fn lookup ( ) -> option < Path > {
499
+ option:: or ( getenv_nonempty ( ~"TMP ") ,
500
+ option:: or ( getenv_nonempty ( ~"TEMP ") ,
501
+ getenv_nonempty ( ~"USERPROFILE ") ) )
502
+ }
503
+ }
467
504
/// Recursively walk a directory structure
468
505
fn walk_dir( p : Path , f : fn ( Path ) -> bool ) {
469
506
0 commit comments