summaryrefslogtreecommitdiff
path: root/shell/bin/ls.php
blob: aa938c1c3d1ecc95a62d7b1132aa6009fc54c5b6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
class p_ls extends prog {
	public static function main($args, $env) {
		if (count($args)<2) {
			$args[]='.';
		}
		$ret=0;
		$me = array_shift($args);
		foreach ($args as $name) {
			if (file_exists($name)) {
				if (is_dir($name)) {
					@$dh = opendir($name);
					if ($dh === false) {
						echo $me.': can not open directory: `'.$name."'\n";
						$ret++;
					} else {
						if (count($args)>1) { echo $name.":\n"; }
						$files = array();
						while (false !== ($file = readdir($dh))) {
							$files[]="$file";
						}
						sort($files);
						echo implode("\n",$files)."\n";
						closedir($dh);
					}
				} else {
					echo $name."\n";
				}
			} else {
				echo $me.': file does not exist: `'.$name."'\n";
				$ret++;
			}
		}
		return $ret;
	}
}