Я делаю простую файловую систему. И я получаю ошибку сегментации. Я пробовал valgrined с --leak-check = full, но я не знаю, неверны ли какие-либо из моих указателей.
Exemple input in [terminal]:
create fs
touch 123.c text
touch game.c anyText
delete fs
file. c file
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "file.h"
void createFS(Directory* myFileSystem);
void readString(char* operationToBeExecuted);
void append(struct Files** head_ref, char *name, char *data)
{
/* 1. allocate node */
struct Files* new_list = (struct Files*)malloc(sizeof(struct Files));
new_list->oneFile = malloc(sizeof(*(new_list->oneFile)));
new_list->oneFile->name = malloc(sizeof(*(new_list->oneFile->name)));
new_list->oneFile->data = malloc(sizeof(*(new_list->oneFile->data)));
struct Files *last = *head_ref; /* used in step 5*/
/* 2. put in the data */
new_list->oneFile->name = strdup(name);
new_list->oneFile->data = strdup(data);
new_list->oneFile->size = strlen(data);
/* 3. This new node is going to be the last node, so make next of it as NULL*/
new_list->next = NULL;
/* 4. If the Linked List is empty, then make the new node as head */
if (*head_ref == NULL)
{
*head_ref = new_list;
return;
}
/* 5. Else traverse till the last node */
while (last->next != NULL)
last = last->next;
/* 6. Change the next of last node */
last->next = new_list;
return;
}
int main() {
char *operationToBeExecuted = (char*)malloc(101 * sizeof(char));
printf("What to do now?\n");
readString(operationToBeExecuted);
while (strcmp(operationToBeExecuted, "create fs") != 0) {
printf("Please type \"create fs\" to create your FileSystem!\n");
readString(operationToBeExecuted);
}
// Create our Parent Directory
Directory* Directory = NULL;
Directory = (struct Directory*)malloc(sizeof(struct Directory));
createFS(Directory);
readString(operationToBeExecuted);
while(strcmp(operationToBeExecuted, "") == 0) {
printf("Please enter a Proper Value!\n");
readString(operationToBeExecuted);
}
while (strcmp(operationToBeExecuted, "delete fs") != 0) {
char *tempString = strtok(operationToBeExecuted, " ");
/////////////////////////////////////////////////////////////////////////////
if (strcmp(tempString, "touch") == 0) {
char *fileName = strtok(NULL, " ");
char *content = strtok(NULL, " ");
int lenght = strlen(content);
Directory->Files = malloc(sizeof(*(Directory->Files)));
append(&Directory->Files, fileName, content);
printf("%s - ", Directory->Files->oneFile->name); // test
printf("%s - ", Directory->Files->oneFile->data); // test
printf("%d\n", Directory->Files->oneFile->size); // test
//////////////////////////////////////////////////////////////////////////////
} else if (strcmp(tempString, "mkdir") == 0) {
printf("Wow. Let;s MKDIR!\n");
} else {
printf("Invalid operator! Please enter again!\n");
}
readString(operationToBeExecuted);
}
printf("You have successfully deleted your FileSystem!\n");
free(operationToBeExecuted);
free(Directory->Files->oneFile->name);
free(Directory->Files->oneFile->data);
free(Directory->Files->oneFile);
free(Directory->Files);
free(Directory);
}
void createFS(Directory* myFileSystem) {
myFileSystem->name = malloc(sizeof(*(myFileSystem->name)));
myFileSystem->parentDir = malloc(sizeof(*(myFileSystem->parentDir)));
myFileSystem->name = "/";
myFileSystem->parentDir = NULL;
printf("%s\n", myFileSystem->name); // test
}
void readString(char* operationToBeExecuted) {
fgets(operationToBeExecuted, 101, stdin);
operationToBeExecuted[strlen(operationToBeExecuted) - 1] = '\0';
}
file.h file
#ifndef __FILE_H__
#define __FILE_H__
typedef struct Directory {
// The name of the directory
char *name;
// TODO: The list of files of the current directory
struct Files {
struct File *oneFile;
struct Files *next;
} *Files;
// TODO: The list of directories of the current directory
struct Directories {
struct Directory *oneDirectory;
struct Directories *next;
} *Directories;
// The parent directory of the current directory (NULL for the root directory)
struct Directory *parentDir;
} Directory;
// DO NOT MODIFY THIS STRUCTURE
typedef struct File {
// The name of the file
char *name;
// The size of the file
int size;
// The content of the file
char *data;
// The directory in which the file is located
Directory *dir;
} File;
#endif /* __FILE_H__ */
////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ////////////////////////////////////////////////// ///////////////