Unix/ls: Difference between revisions
→Tcl: Added implementation, updated task description |
→{{header|Rust}}: Add in sorting of filenames. |
||
Line 22: | Line 22: | ||
match fs::readdir(&cwd) { |
match fs::readdir(&cwd) { |
||
Ok(v) => { |
Ok(v) => { |
||
let mut filenames = Vec::new(); |
|||
for entry in v.iter() { |
for entry in v.iter() { |
||
match entry.filename_str() { |
match entry.filename_str() { |
||
Some(str) => |
Some(str) => filenames.push(str), |
||
None => |
None => fail!(format!("unable to get filename of path {}", entry.display())) |
||
}; |
}; |
||
} |
|||
filenames.sort(); |
|||
for filename in filenames.iter() { |
|||
println!("{}", filename); |
|||
} |
} |
||
} |
} |
||
Err( |
Err(e) => fail!(e) |
||
} |
} |
||
}</lang> |
}</lang> |
Revision as of 14:34, 6 June 2014
Write a program that will list everything in the current folder, similar to the Unix utility “ls” [1] (or the Windows terminal command “DIR”). The output must be sorted, but printing extended details and producing multi-column output is not required.
- Example output
For the list of paths:
/foo/bar /foo/bar/1 /foo/bar/2
When the program is executed in `/foo`, it should print:
bar
and when the program is executed in `/foo/bar`, it should print:
1 2
Rust
<lang rust>use std::os; use std::io::fs;
fn main() { let cwd = os::getcwd(); match fs::readdir(&cwd) { Ok(v) => { let mut filenames = Vec::new(); for entry in v.iter() { match entry.filename_str() { Some(str) => filenames.push(str), None => fail!(format!("unable to get filename of path {}", entry.display())) }; }
filenames.sort(); for filename in filenames.iter() { println!("{}", filename); } } Err(e) => fail!(e) } }</lang>
Tcl
<lang tcl>puts [join [lsort [glob -nocomplain *]] "\n"]</lang>