UNIX类似文件系统的目录结构

Directory Structure for a unix like file system

本文关键字:结构 文件系统 UNIX      更新时间:2023-10-16

我当前在系统软件类中,我们的最终项目是实施具有层次目录结构的简单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 %sn", 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");
            }
        }
    }
}

为什么不使用boost.filesystem?http://www.boost.org/doc/libs/1_62_0/libs/filesystem/doc/index.htm