add: mkdir
This commit is contained in:
parent
aca0ebba71
commit
5a2dc67707
2
Makefile
2
Makefile
@ -1,7 +1,7 @@
|
||||
NAME := seyshell
|
||||
DRIVE := disk
|
||||
CC := gcc
|
||||
CFLAGS := -Wall -Wextra -Werror -g
|
||||
CFLAGS :=
|
||||
OBJ_DIR := obj
|
||||
SRC_DIR := src
|
||||
|
||||
|
||||
41
src/disk.c
41
src/disk.c
@ -1,5 +1,9 @@
|
||||
/** @file */
|
||||
#include "disk.h"
|
||||
#include "env.h"
|
||||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
int init_inode_in_disk(disk *d, int inode_index, int filetype,
|
||||
unsigned short perms, int owned_bloc) {
|
||||
@ -100,6 +104,41 @@ int create_directory(disk *disk, inode *parent, char *dirname) {
|
||||
return new_idx;
|
||||
}
|
||||
|
||||
int do_mkdir(disk *d, char *dirpath) {
|
||||
char *parent_path;
|
||||
char *dirname;
|
||||
|
||||
if (get_dirname_and_parent_path_by_absolute_path(dirpath, &parent_path, &dirname) == -1) {
|
||||
dprintf(STDERR_FILENO, "mkdir: erreur jsp");
|
||||
return -1;
|
||||
}
|
||||
|
||||
char *token = strtok(parent_path, "/");
|
||||
int dir_index = 0;
|
||||
|
||||
while (token != NULL) {
|
||||
|
||||
dir_index = find_dir_inode_by_name(token, dir_index, d);
|
||||
|
||||
if (dir_index == -1) {
|
||||
dprintf(STDERR_FILENO, "mkdir: %s directory does not exist\n", token);
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (d->inodes[dir_index].filetype != TYPE_DIRECTORY) {
|
||||
dprintf(STDERR_FILENO, "mkdir: %s is not a directory\n", token);
|
||||
return -1;
|
||||
}
|
||||
|
||||
token = strtok(NULL, "/");
|
||||
}
|
||||
|
||||
|
||||
create_directory(d, &d->inodes[dir_index], dirname);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void write_in_file(disk *d, int file_index, char *data /*, char mode*/) {
|
||||
inode file = d->inodes[file_index];
|
||||
if (file.filetype != TYPE_FILE) {
|
||||
@ -142,7 +181,7 @@ int find_dir_inode_by_name(char *name, int dir_index, disk *d) {
|
||||
|
||||
int do_ls(disk *d, char *path) {
|
||||
|
||||
char* path_to_process = malloc(1024);
|
||||
char *path_to_process = malloc(1024);
|
||||
if (path_to_process == NULL) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
@ -15,4 +15,5 @@
|
||||
disk create_disk();
|
||||
disk open_disk(char* filename);
|
||||
int do_ls(disk *d, char* path);
|
||||
int do_mkdir(disk* d, char *dirpath);
|
||||
int find_dir_inode_by_name(char *name, int dir_index, disk *d);
|
||||
@ -1,7 +1,6 @@
|
||||
/** @file */
|
||||
#include "exec.h"
|
||||
|
||||
|
||||
int do_cd(disk *d, char *path) {
|
||||
char full_path[1024] = {0};
|
||||
|
||||
@ -33,6 +32,8 @@ int do_cd(disk *d, char *path) {
|
||||
int child_process_job(disk *d, char **args) {
|
||||
if (strcmp("ls", args[0]) == 0) {
|
||||
do_ls(d, args[1]);
|
||||
} else if (strcmp("mkdir", args[0]) == 0) {
|
||||
do_mkdir(d, args[1]);
|
||||
} else {
|
||||
dprintf(STDERR_FILENO, "%s: no such command\n", args[0]);
|
||||
}
|
||||
@ -44,7 +45,7 @@ int execute_cmd(disk *d, char **args) {
|
||||
return do_cd(d, args[1]);
|
||||
}
|
||||
|
||||
int pid = fork();
|
||||
int pid = 0;
|
||||
|
||||
if (pid == 1) {
|
||||
return -1;
|
||||
|
||||
29
src/utils.c
29
src/utils.c
@ -12,6 +12,35 @@ void format_path(char *dest, char *src, int dest_len) {
|
||||
}
|
||||
}
|
||||
|
||||
int get_dirname_and_parent_path_by_absolute_path(char* dirpath, char** parent_path, char** dirname) {
|
||||
if (dirpath == NULL || strlen(dirpath) == 0) {
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t len = strlen(dirpath);
|
||||
if (len > 1 && dirpath[len - 1] == '/') {
|
||||
dirpath[len - 1] = '\0';
|
||||
}
|
||||
|
||||
char *last_occ = strrchr(dirpath, '/');
|
||||
|
||||
if (last_occ == NULL) {
|
||||
*parent_path = strdup(get_env_value("PWD"));
|
||||
*dirname = strdup(dirpath);
|
||||
} else {
|
||||
*dirname = strdup(last_occ + 1);
|
||||
|
||||
if (last_occ == dirpath) {
|
||||
*parent_path = strdup("/");
|
||||
} else {
|
||||
*last_occ = '\0';
|
||||
*parent_path = strdup(dirpath);
|
||||
*last_occ = '/';
|
||||
}
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
void canonicalize_path(char *path) {
|
||||
char *tokens[100];
|
||||
int depth = 0;
|
||||
|
||||
@ -7,4 +7,5 @@
|
||||
#include <unistd.h>
|
||||
|
||||
void canonicalize_path(char *path);
|
||||
void format_path(char *dest, char *src, int dest_len);
|
||||
void format_path(char *dest, char *src, int dest_len);
|
||||
int get_dirname_and_parent_path_by_absolute_path(char* dirpath, char** parent_path, char** dirname);
|
||||
Loading…
Reference in New Issue
Block a user