-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfunix.cpp
More file actions
184 lines (146 loc) · 4.12 KB
/
Copy pathfunix.cpp
File metadata and controls
184 lines (146 loc) · 4.12 KB
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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Author: Sean Davis
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <fstream>
#include "funix.h"
#include "directory.h"
using namespace std;
Funix::Funix() : umask(022)
// creates currentDirectory, and sets umask and time
{
currentDirectory = new Directory("/", umask, NULL);
ifstream inf("directories.txt");
if (inf)
{
inf >> *this;
inf.close();
} // if directories.txt exists
} // Funix()
Funix::~Funix()
{
const char *arguments[2] = {"cd", ".."};
Directory *next = currentDirectory->cd(2, arguments);
while(currentDirectory != next)
{
currentDirectory = next;
next = currentDirectory->cd(2, arguments);
} // while currentDirectory not root
delete currentDirectory;
} // ~Funix())
int Funix::eXit(int argCount, const char *arguments[])
{
// checks "exit" command, returns 0 on proper exit
if (argCount > 1)
{
cout << "usage: exit\n";
return 1;
} // if more than one argument
const char *arguments2[2] = {"cd", ".."};
Directory *next = currentDirectory->cd(2, arguments2);
while(currentDirectory != next)
{
currentDirectory = next;
next = currentDirectory->cd(2, arguments2);
} // while currentDirectory not root
ofstream outf("directories.txt");
outf << *this;
outf.close();
return 0;
} // eXit()
void Funix::getCommand(char *command) // writes prompt and reads command
{
writePrompt();
cin.getline(command, COMMAND_LENGTH);
} // getCommand()
int Funix::processCommand(char *command) // returns 0 on proper exit
{
static const char *commands[] = {"cd", "exit", "ls", "mkdir", "umask",
"chmod", "cp"};
const char *arguments[MAX_ARGUMENTS];
char *ptr;
int argCount = 0, commandNum;
ptr = strtok(command, " ");
while (ptr)
{
arguments[argCount++] = ptr;
ptr = strtok(NULL, " ");
} // while more arguments in the command line
if (argCount > 0)
{
for (commandNum = 0; commandNum < NUM_COMMANDS; commandNum++)
{
if (strcmp(arguments[0], commands[commandNum]) == 0)
break;
} // for each possible command
switch (commandNum)
{
case 0: currentDirectory = currentDirectory->cd(argCount, arguments);
break;
case 1: return eXit(argCount, arguments);
case 2: currentDirectory->ls(argCount, arguments); break;
case 3: currentDirectory->mkdir(argCount, arguments, umask); break;
case 4: setUmask(argCount, arguments); break;
case 5: currentDirectory->chmod(argCount, arguments); break;
default: cout << arguments[0] << ": Command not found.\n";
} // switch on commandNum
} // if at least one argument
return 1;
} // processCommand()
void Funix::run()
// reads and processes commands until proper exit
{
char command[COMMAND_LENGTH];
getCommand(command);
while (processCommand(command))
getCommand(command);
} // run()
void Funix::setUmask(int argCount, const char *arguments[])
// checks "umask" command and executes it if it is proper
{
short newUmask = 0;
if (argCount == 1)
{
cout << oct << umask << dec << endl;
return;
} // if only "umask" on commandline
if (argCount != 2)
{
cout << "umask: Too many arguments.\n";
return;
} // if more than 2 arguments
if (strlen(arguments[1]) > 3)
{
cout << "umask: Improper mask.\n";
return;
} // if umask value too long.
for (int i = 0; arguments[1][i] != '\0'; i++ )
{
if (arguments[1][i] < '0' || arguments[1][i] > '7')
{
cout << "umask: Improper mask.\n";
return;
} // if incorrect octal
else // valid octal digit
newUmask = newUmask * 8 + arguments[1][i] - '0';
} // for each digit in argument
umask = newUmask;
} // umask()
void Funix::writePrompt() const // shows path and '#'
{
currentDirectory->showPath();
cout << " # ";
} // writePrompt()
ostream& operator<< (ostream &os, const Funix &rhs)
{
os << rhs.umask << endl;
os << *rhs.currentDirectory;
return os;
} // operator<<
istream& operator>> (istream &is, Funix &rhs)
{
is >> rhs.umask;
is.ignore(10, '\n');
is >> *rhs.currentDirectory;
return is;
} // operator>>