summaryrefslogtreecommitdiff
path: root/Source/Applications/Shell/Shell-fs.ns.cpp
blob: b7e585af01522181669df1cf273353a2698d3ed2 (plain) (blame)
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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#include "Shell.ns.h"

namespace Shell {

void ls(Vector<String>& args) {
	FSNode d = cwd;
	if (args.size() == 2) {
		FSNode n = FS::find(args[1], cwd);
		d = FSNode(0);
		if (!n.valid())
			outvt << "No such directory : " << args[1] << "\n";
		else if (n.type() != NT_DIRECTORY)
			outvt << "Not a directory : " << args[1] << "\n";
		else
			d = n;
	}	
	if (d.valid()) outvt << "Contents of directory " << d.path() << " :\n";
	if (!d.valid()) return;
	for (u32int i = 0; i < d.getLength(); i++) {
		FSNode n = d.getChild(i);
		if (!n.valid()) {
			outvt << " [inacessible file]\n";	//This is a file we are not supposed to be able to read
			continue;
		}
		String perm = "rwxrwxrwx";
		u32int p = n.getPerm();
		for (u32int i = 0; i < 9; i++) {
			if (((p >> i) & 1) == 0) perm[9 - i] = "-";
		}
		if (n.type() == NT_FILE) {
			outvt << " FILE " << perm << " " << n.getName();
			outvt.setCsrCol(30);
			outvt << (s32int)n.getLength() << " bytes.\n";
		} else if (n.type() == NT_DIRECTORY) {
			outvt << " DIR  " << perm << " " << n.getName() << "/";
			outvt.setCsrCol(30);
			outvt << (s32int)n.getLength() << " items.\n";
		}
	}
}

void cd(Vector<String>& args) {
	if (args.size() != 2) {
		outvt << "Invalid argument count.\n";
	} else {
		FSNode ncwd = FS::find(args[1], cwd);
		if (!ncwd.valid()) {
			outvt << "No such directory : " << args[1] << "\n";
		} else if (ncwd.type() == NT_DIRECTORY) {
			ncwd.setCwd();
			cwd = ncwd;
		} else {
			outvt << "Not a directory.\n";
		}
	}
}

void pwd(Vector<String>& args) {
	outvt << "Current directory : " << cwd.path() << "\n";
}

void rm(Vector<String>& args) {
	if (args.size() == 1) outvt << "No file to remove.\n";
	for (u32int i = 1; i < args.size(); i++) {
		if (!FS::find(args[i], cwd).remove()) {
			outvt << "Error while removing file " << args[i] << "\n";
		}
	}
}

void mkdir(Vector<String>& args) {
	if (args.size() == 1) outvt << "No directory to create.\n";
	for (u32int i = 1; i < args.size(); i++) {
		if (!FS::mkdir(args[i], cwd).valid()) {
			outvt << "Error while creating directory " << args[i] << "\n";
		}
	}
}

}