unix文件系统

时间:2016-11-29 04:34:33

标签: c++ unix filesystems directory-structure

我目前在系统软件类中,我们的最终项目是实现一个简单的unix,如shell环境和带有分层目录结构的文件系统。我们已经轻松地向用户询问了诸如< cd xxx'之类的命令。或者' ls'。一旦调用每个命令,它就转到一个函数。我知道我需要一个树状目录和文件的数据结构,但我不知道从哪里开始。我知道父母只能是一个目录。该目录有一个名称,可以采取其他目录和文件。文件只有一个名称。我该如何实现这种代码?这就是我现在所拥有的一切:

#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");
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)