I am currently in a Systems Software class and our final project is to implement implement a simple unix like shell environment and file system with a hierarchical directory structure. We have done the easy part of asking the user for a command like 'cd xxx' or 'ls'. And once each command is called, it goes to a function. I know I need a tree like data structure for the directories and files, but I just don't know where to start. I know that a parent can only be a directory. The directory has a name and can take other directories and files. A file only has a name. How do I go about implementing this kind of code? Here is all I have right now:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<iostream>
void makeFS(){
printf("You created and formatted a new filesystem.\n");
}
void listDir(){
printf("Listing all entries in current directory...\n");
}
void exitShell(){
printf("Adios amigo.\n");
exit(0);
}
void makeDir(char name[50]){
printf("Directory [%s] created at !\n", name);
}
void remDir(char cmd[50]){
printf("You entered %s \n", cmd);
}
void changeDir(char *nextName){
printf("Changed directory path to %s\n", nextName);
}
void status(char cmd[50]){
printf("You entered %s \n", cmd);
}
void makeFile(char cmd[50]){
printf("You entered %s \n", cmd);
}
void remFile(char cmd[50]){
printf("You entered %s \n", cmd);
}
int main(int argc, char *argv[]){
char cmd[50];
const char spc[50] = " \n";
char *file, *dir;
char *tok, *nextName;
while (1){
printf("Russ_John_Shell> ");
fgets(cmd, 50, stdin);
//Tokenizes string to determine what command was inputed as well as any file/directory name needed
tok = strtok(cmd, spc);
nextName = strtok(NULL, spc);
//Checks to see whether the string has a file/directory name after the command
if (nextName == NULL){
//mkfs command
if (strcmp(cmd, "mkfs") == 0){
makeFS();
}
//exit command
else if (strcmp(cmd, "exit") == 0){
exitShell();
}
//ls command
else if (strcmp(cmd, "ls") == 0){
listDir();
}
//command not recognized at all
else {
printf("Command not recognized.\n");
}
}
else {
//mkdir command
if (strcmp(cmd, "mkdir") == 0){
makeDir(nextName);
}
//rmdir command
else if (strcmp(cmd, "rmdir") == 0){
remDir(cmd);
}
//cd command
else if (strcmp(cmd, "cd") == 0){
changeDir(nextName);
}
//stat command
else if (strcmp(cmd, "stat") == 0){
status(cmd);
}
//mkfile command
else if (strcmp(cmd, "mkfile") == 0){
makeFile(cmd);
}
//rmfile command
else if (strcmp(cmd, "rmfile") == 0){
remFile(cmd);
}
//command not recognized at all
else {
printf("Command not recognized.\n");
}
}
}
}
Why don't you use boost.filesystem? http://www.boost.org/doc/libs/1_62_0/libs/filesystem/doc/index.htm
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.