Skip to content

Commit 9ef3ef5

Browse files
committed
Added Dictionary instead of directly lists to commands
1 parent 6a23c53 commit 9ef3ef5

File tree

1 file changed

+42
-48
lines changed

1 file changed

+42
-48
lines changed

path_traversal.command

+42-48
Original file line numberDiff line numberDiff line change
@@ -1,61 +1,58 @@
11
#!/usr/bin/env python3
22

3-
root = ["", "root"]
4-
dirs = []
5-
path = []
6-
current_path = [] or ["", "root"]
3+
# root = ["", "root"]
4+
# dirs = []
5+
# path = []
6+
# current_path = [] or ["", "root"]
7+
8+
dict_path = {
9+
"root": ["", "root"],
10+
"current_path": [] or ["", "root"],
11+
"dirs": [],
12+
"path": []
13+
}
714

815

916
"""
1017
* mkdir method is used to make/create directories
1118
* [dirs delared global list for directories]
1219
"""
1320
def mkdir():
14-
global dirs
15-
if dir in dirs:
16-
print("ERR: DIRECTORY ALREADY EXISTS")
21+
global dict_path
22+
if dir in dict_path["path"] and dir in dict_path["dirs"]:
23+
print("ERR: DIRECTORY ALREADY EXISTS")
1724
else:
18-
dirs.append(dir)
19-
path.append(dir)
25+
dict_path["dirs"].append(dir)
2026
print("SUCC: CREATED")
27+
dict_path["path"].append(dir)
2128

2229

2330
"""
2431
* ls method is used to list all the directries
2532
* path declared globally
2633
"""
2734
def ls():
28-
global path
29-
if path == root:
30-
path = dirs
31-
path = path[0]
35+
if dict_path["path"] == dict_path["root"]:
36+
dict_path["path"] = dict_path["dirs"]
37+
# dict_path["path"] = dict_path["path"]
3238
print("DIRS: ")
33-
print(*path, sep="\t")
39+
print(*dict_path["path"], sep="\t")
3440

3541

3642
"""
3743
* cd method is used to change the directory
3844
* current_path, dir, path declared globally
3945
"""
4046
def cd():
41-
global current_path, dir, path
47+
global dict_path
4248
if dir == "":
43-
current_path = root
44-
path = root
45-
elif dir in dirs:
46-
current_path.append(dir)
47-
path.clear()
48-
elif dir == "..":
49-
current_path.pop()
50-
print("SUCC: REACHED")
51-
print(*current_path, sep="/")
52-
i = len(dirs) - 1
53-
if dirs[i] in path:
54-
i = i - 1
55-
path.pop()
56-
path.append(dirs[i])
57-
else:
58-
path.append(dirs[i])
49+
dict_path["current_path"] = dict_path["root"]
50+
dict_path["path"] = dict_path["root"]
51+
print("ENTERED IN ROOT DIRECTORY")
52+
elif dir in dict_path["dirs"] and dir in dict_path["path"]:
53+
dict_path["current_path"].append(dir)
54+
dict_path["path"].clear()
55+
print("ENTERED IN DIRECTORY: ", dict_path["current_path"])
5956
else:
6057
print("ERR: INVALID PATH")
6158

@@ -65,22 +62,21 @@ def cd():
6562
* current_path declared globally
6663
"""
6764
def pwd():
68-
global current_path
65+
global dict_path
6966
print("PATH: ")
70-
print(*current_path, sep="/")
67+
print(*dict_path["current_path"], sep="/")
7168

7269

7370
"""
7471
* rm method is used to remove the directories created.
7572
* dirs declared globabally
7673
"""
7774
def rm():
78-
global dirs
79-
if dir in dirs:
80-
dirs.remove(dir)
81-
if dir in path:
82-
print("SUCC: DELETED")
83-
path.remove(dir)
75+
global dict_path
76+
if dir in dict_path["dirs"]:
77+
dict_path["dirs"].remove(dir)
78+
dict_path["path"].remove(dir)
79+
print("SUCC: DELETED")
8480
else:
8581
print("ERR: DIRECTORY DOES NOT EXIST")
8682

@@ -90,13 +86,11 @@ def rm():
9086
* dirs, current_path, path declared globally.
9187
"""
9288
def session_clear():
93-
global dirs
94-
dirs.clear()
95-
global current_path
96-
current_path.clear()
97-
current_path = root
98-
global path
99-
path.clear()
89+
global dict_path
90+
dict_path["dirs"].clear()
91+
dict_path["current_path"].clear()
92+
dict_path["current_path"] = dict_path["root"] or dict_path[""]
93+
dict_path["path"].clear()
10094
print("SUCC: CLEARED: RESET TO ROOT")
10195

10296

@@ -119,14 +113,14 @@ def commands(argument):
119113
# Execute the function
120114
func()
121115
else:
122-
print("ERR: COMMANDS DOES NOT EXIST!")
116+
print("ERR: COMMAND ENTERED DOES NOT EXIST!")
123117

124118

125119
print("INFO - COMMANDS TO USE : mkdir, ls, cd, pwd, rm, session_clear, exit")
126120
print("<Starting your application...>")
127121

128122
while True:
129-
char = input("$: ")
123+
char = input("$:")
130124
command_list = []
131125
command_list.append(char.split(" "))
132126
char = command_list[0][0]

0 commit comments

Comments
 (0)