Unix/ls: Difference between revisions

From Rosetta Code
Content added Content deleted
No edit summary
(Add example output and the task written in Rust.)
Line 1: Line 1:
{{draft task}}
{{draft task}}Write a program that will list everything in the current folder, similar to the Unix utility `ls`.
Write a program that will list everything in the current folder, similar to the Unix utility `ls`.

Example output:

For the list of paths:

<pre>/foo/bar
/foo/bar/1
/foo/bar/2</pre>

When the program is executed in `/foo`, it should print:

<pre>bar</pre>

and when the program is executed in `/foo/bar`, it should print:

<pre>1
2</pre>

=={{header|Rust}}==

<pre>
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.")
}
}</pre>

Revision as of 14:14, 5 June 2014

Unix/ls is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Write a program that will list everything in the current folder, similar to the Unix utility `ls`.

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

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.")
	}
}