Unix/ls: Difference between revisions
(Change <pre> to <lang>) |
(→Tcl: Added implementation, updated task description) |
||
Line 1: | Line 1: | ||
{{draft task}} |
{{draft task}} |
||
Write a program that will list everything in the current folder, similar to the Unix utility |
Write a program that will list everything in the current folder, similar to the Unix utility “<tt>ls</tt>” [http://man7.org/linux/man-pages/man1/ls.1.html] (or the Windows terminal command “<tt>DIR</tt>”). The output must be sorted, but printing extended details and producing multi-column output is not required. |
||
⚫ | |||
⚫ | |||
For the list of paths: |
For the list of paths: |
||
<pre>/foo/bar |
<pre>/foo/bar |
||
/foo/bar/1 |
/foo/bar/1 |
||
Line 11: | Line 9: | ||
When the program is executed in `/foo`, it should print: |
When the program is executed in `/foo`, it should print: |
||
<pre>bar</pre> |
<pre>bar</pre> |
||
and when the program is executed in `/foo/bar`, it should print: |
and when the program is executed in `/foo/bar`, it should print: |
||
<pre>1 |
<pre>1 |
||
2</pre> |
2</pre> |
||
=={{header|Rust}}== |
=={{header|Rust}}== |
||
⚫ | |||
<lang rust> |
|||
⚫ | |||
use std::io::fs; |
use std::io::fs; |
||
Line 39: | Line 32: | ||
} |
} |
||
}</lang> |
}</lang> |
||
=={{header|Tcl}}== |
|||
<lang tcl>puts [join [lsort [glob -nocomplain *]] "\n"]</lang> |
Revision as of 08:56, 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) => { for entry in v.iter() { match entry.filename_str() { Some(str) => println!("{}", str), None => println!("Error: Unable to get filename of path {}", entry.display()) }; } } Err(_) => println!("Error: Unable to get contents of directory.") } }</lang>
Tcl
<lang tcl>puts [join [lsort [glob -nocomplain *]] "\n"]</lang>