Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
289 views
in Technique[技术] by (71.8m points)

c - How to make directory structure? (Basic DOS commands such as mkdir, cd dir, cd.., dir...)

I would like to make a program with simple DOS commands using liked lists/trees and stack. I already made some functions such as md-make directory,cd-cd dir, cdBack-cd.., delete and push and pop. I would like to make function to list all the directories but I don't know how. I kinda got lost in my program. I commented my simple menu because I would first like to make it work. I'm trying to add a new directory into my file but it does not work. I would be glad if someone could help. Thanks in advance!

My code:

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include <stdlib.h>
#include<string.h>
#define MAX_DIR_LENGTH (256)

typedef struct dir* Position;
typedef struct stack* StackPosition;

struct dir {
    char name[MAX_DIR_LENGTH];
    Position sibling;
    Position child;
    Position next;
};
struct stack {
    Position dir;
    StackPosition next;
};

Position createDir(char* name) {
    Position dir = (Position)malloc(sizeof(dir));
    if (!dir) {
        printf("Memory allocation failed!
");
        return NULL;
    }
    strcpy(dir->name, name);
    dir->child = NULL;
    dir->sibling = NULL;
}

void push(char x, StackPosition S) {
    StackPosition q;
    q = (StackPosition)malloc(sizeof(struct stack));

    q->dir = x;

    q->next = S->next;
    S->next = q;
}
int pop(StackPosition S) {
    int x = -1;
    StackPosition temp;
    if (S->next != NULL) {
        x = S->next->dir;
        temp = S->next;
        S->next = temp->next;
        free(temp);
    }
    return x;
}

int md(Position current, char* name) {
    if (current->child == NULL) {
        current->child = createDir(name);

        //if (!dir)return-1;
    }
    else {
        Position dir = createDir(name);
        if (!dir)return -1;

        if (strcmp(current->child->name, name) > 0) {
            dir->sibling = current->child;
            current->child = dir;
        }
        else {
            Position tmp = current->child;
            while (strcmp(tmp->sibling->name, name) < 0) {
                tmp = tmp->sibling;
            }
            dir->next = tmp->next;
            tmp->next = dir;
        }
    }

    //return 0;
}
Position cd(Position current, char* name, StackPosition stack) {
    Position p = current->child;

    while (p != NULL && strcmp(p->name, name) != 0) {
        p = p->sibling;
    }
    if (!p) {
        printf("Directory not found, please try again!
");
        return current;
    }

    push(name, stack);
    return p;
}
Position cdBack(StackPosition stack, Position current) {
    if (stack->next != NULL) {
        StackPosition first = stack->next;
        Position result = first->dir;
        stack->next = first->next;
        free(first);
        return result;
    }
    return current;
}
void delete(Position current) {
    if (!current)return;

    delete(current->sibling);
    delete(current->child);
    free(current);
}

int main() {
    char c;
    char ime[15];

    Position root = NULL;
    Position current = NULL;
    struct stack stack;
    
    stack.next = NULL;
    root = createDir("C:");
    current = root;

    md(current, "John");
    current = cdBack(&stack, current);
    /*while (1)
    {
        printf("1->mkdir
2->cd dir
3->cd..
4->dir
5->Izlaz
Vas izbor: ");
        scanf("%c", &c);

        switch (c) {
        case'1':
            printf("Unesi ime:");
            scanf("%s", ime);
            

            break;
        case'2':
            break;
        case'3':
            break;
        case'4':
            break;
        case'5':
            return 0;
            break;
        }
    }*/
}
question from:https://stackoverflow.com/questions/65644213/how-to-make-directory-structure-basic-dos-commands-such-as-mkdir-cd-dir-cd

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...