FUNIX 代码
#ifndef FUNIX_H
#define FUNIX_H
#include "directory.h"
const int COMMAND_LENGTH = 80;
const int NUM_COMMANDS = 5;
const int MAX_ARGUMENTS = 40;
class Funix
{
Directory *currentDirectory;
int umask;
int time;
void cd(int argCount, const char *arguments[]);
// calls cd() with currentDirectory as one of its parameters
int eXit(int argCount, const char *arguments[]) const;
// checks "exit" command, returns 0 on proper exit
void getCommand(char *command);
// writes prompt and reads command
void ls(int argCount, const char *arguments[]) const;
// calls ls() with currentDirectory as one of its parameters
void mkdir(int argCount, const char *arguments[]);
// calls mkdir() with currentDirectory as one of its parameters
int processCommand(char *command); // returns 0 on proper exit
void setUmask(int argCount, const char *arguments[]);
// checks "umask" command and executes it if it is proper
void writePrompt() const; // shows path and '#'
public:
Funix();
// creates currentDirectory, and sets umask and time
void run();
// reads and processes commands in a loop until proper exit
~Funix();
};
#endif
#ifndef FUNIX_H
#define FUNIX_H
#include "directory.h"
const int COMMAND_LENGTH = 80;
const int NUM_COMMANDS = 5;
const int MAX_ARGUMENTS = 40;
class Funix
{
Directory *currentDirectory;
int umask;
int time;
void cd(int argCount, const char *arguments[]);
// calls cd() with currentDirectory as one of its parameters
int eXit(int argCount, const char *arguments[]) const;
// checks "exit" command, returns 0 on proper exit
void getCommand(char *command);
// writes prompt and reads command
void ls(int argCount, const char *arguments[]) const;
// calls ls() with currentDirectory as one of its parameters
void mkdir(int argCount, const char *arguments[]);
// calls mkdir() with currentDirectory as one of its parameters
int processCommand(char *command); // returns 0 on proper exit
void setUmask(int argCount, const char *arguments[]);
// checks "umask" command and executes it if it is proper
void writePrompt() const; // shows path and '#'
public:
Funix();
// creates currentDirectory, and sets umask and time
void run();
// reads and processes commands in a loop until proper exit
~Funix();
};
#endif
#ifndef DIRECTORY_H
#define DIRECTORY_H
#include "permissions.h"
const int MAX_DIRECTORIES = 3;
class Directory
{
int time;
Directory **subDirectories;
int subDirectoryCount;
Directory *parent;
Permissions permissions;
char *name;
public:
Directory(const char *nam, short umas,
int tim, Directory *paren);
~Directory();
void showPath(Directory *dir) const;
void mkdir(int argCount, const char *arguments[],
short umas, int tim);
void ls(int argCount, const char *arguments[]) const;
Directory* cd(int argCount, const char *arguments[]);
};
#endif // DIRECTORY_H
#include
#include
#include
#include "directory.h"
using namespace std;
Directory::Directory(const char *nam, short umas,
int tim, Directory *paren): time(tim),
subDirectories(NULL),
subDirectoryCount(0), parent(paren)
{
name = new char(strlen(nam) + 1);
strcpy(name, nam);
permissions.set(7, umas);
} // constructor!! oh yeah
Directory::~Directory()
{
delete name;
for (int i = 0; i < subDirectoryCount; i++)
delete subDirectories[i];
delete [] subDirectories;
}
Directory* Directory::cd(int argCount, const char *arguments[])
{
if (argCount != 2)
{
cout<<"usage: cd directoryName\n";
return this;
} // if too many arguments
if (strcmp(arguments[1], "..") == 0)
{
if (parent)
return parent;
else // this is root
return this;
} // if cd ..
for (int i = 0; i < subDirectoryCount; i++)
{
if (strcmp(subDirectories[i]->name, arguments[1]) == 0)
return subDirectories[i];
} // for each subdirectory
cout<<"cd: "<
<<": No such file or directory\n";
return this;
} // cd()
void Directory::ls(int argCount, const char *arguments[]) const
{
if (argCount > 2 || (argCount == 2 && strcmp(arguments[1], "-l") != 0))
cout<<"usage: ls [-l]\n";
else // correct number of arguments
{
if (argCount == 1) // simple ls
{
for (int i = 0; i < subDirectoryCount; i++)
cout<
name<<" "; cout<<"\n"; } // if simple ls else // must be ls -l { for (int i = 0; i < subDirectoryCount; i++) { subDirectories[i]->permissions.print(); cout<<" "<
time<< " "<
name<
name, arguments[1]) == 0) { cout<<"mkdir: cannot create directory '"<
<<"': File exists\n"; return; } // if subdirectory already exists. } // for each subdirectory Directory **subDirectoriesTemp = new Directory *[subDirectoryCount + 1]; memcpy(subDirectoriesTemp,subDirectories,subDirectoryCount*sizeof(Directory *)); delete [] subDirectories; subDirectories = subDirectoriesTemp; subDirectories[subDirectoryCount] = new Directory(arguments[1], umas, tim, this); subDirectoryCount++; } // mkdir() void Directory::showPath(Directory *dir) const { if (dir->parent == NULL) { cout<<"/"; return; } // at root Directory::showPath(dir->parent); cout<
name<<"/"; } // showPath()) #ifndef PERMISSIONS_H #define PERMISSIONS_H class Permissions { short permissions; public: void print() const; void set(short originalPermissions, short umask); }; #endif // PERMISSIONS_H #include
#include "permissions.h" using namespace std; void Permissions::set(short originalPermissions, short umask) { permissions = originalPermissions & ~umask; } // set() void Permissions::print() const { if (permissions & 4) cout<<"r"; else // no read permissions cout<<"-"; if (permissions & 2) cout<<"w"; else // no write permissions cout<<"-"; if (permissions & 1) cout<<"x"; else // no execute permissions cout<<"-"; } // print() #include
#include "funix.h" int main() { Funix *funix = new Funix; funix->run(); delete funix; } // main()