summaryrefslogtreecommitdiff
path: root/main.c
diff options
context:
space:
mode:
authorAbdellah El Morabit <nsrddyn@gmail.com>2025-02-22 13:16:03 +0100
committerAbdellah El Morabit <nsrddyn@gmail.com>2025-02-22 13:16:03 +0100
commitcdbd4471aadb918c6631f9cd18bff4b98fbef99c (patch)
treeaa2eba1daaca27255a4df3bb92ffe7fd4b645a06 /main.c
parente6b16a313f3ab539f5baba4ab027778165a2f535 (diff)
i think everything works except for the new features im planning on adding and the remove file/ directory permissions but i dont know how to do that yet
Diffstat (limited to 'main.c')
-rw-r--r--main.c67
1 files changed, 39 insertions, 28 deletions
diff --git a/main.c b/main.c
index ffe953d..9eecd21 100644
--- a/main.c
+++ b/main.c
@@ -3,6 +3,8 @@
#include <string.h>
#include <dirent.h>
#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
#define MAX_HISTORY 100
#define MAX_COMMAND_LENGTH 255 // not setting a macro for the argument because they are included in the command
@@ -26,61 +28,56 @@ void exec_command(char *input);
// shell commands functions prototypes
void list(const char *path);
void make_dir(const char *path);
-void remove_dir(const char *path);
void remove_file(const char *path);
void copy_files(const char *arg);
void move_files(const char *arg);
void print_cdirectory(const char *arg);
void change_directory(const char *path);
+void change_ownership(const char *path);
void clear(const char *arg);
void echo(const char *arg);
+void exit_(const char *arg);
struct exec_command CommandsList[] = {
{"ls", list},
{"mkdir", make_dir},
- {"rmdir", remove_dir},
{"rm", remove_file},
{"cp", copy_files},
{"mv", move_files},
+ {"chown", change_ownership},
{"pwd", print_cdirectory},
{"cd", change_directory},
{"clr", clear},
- {"echo", echo}
+ {"echo", echo},
+ {"exit", exit_}
};
// Code functions
struct command split_command(char *input) {
- struct command NEW_COMMAND = {};
+ struct command user_command;
char *command = strtok(input, " ");
char *argument = strtok(NULL, " ");
- if (command != NULL) {
- NEW_COMMAND.com = strdup(command);
- } else {
- perror("failed, null pointer detected, no initial command has been inserted: see command");
- free(command);
- }
+ user_command.com = strdup(command);
if (argument != NULL) {
- NEW_COMMAND.arg = strdup(argument);
+ user_command.arg = strdup(argument);
} else {
- perror("failed, null pointer detected, no initial command has been inserted: see argument");
- free(argument);
- NEW_COMMAND.arg = NULL;
+ user_command.arg = NULL;
}
- return NEW_COMMAND;
+ return user_command;
}
void exec_command(char *input) {
- const struct command user_command = split_command(input);
+ struct command user_command = split_command(input);
+
user_command.com[strcspn(user_command.com, "\n")] = '\0';
- if (user_command.arg != NULL) {
+ if (user_command.arg != NULL)
user_command.arg[strcspn(user_command.arg, "\n")] = '\0';
- }
- for (int i = 0; i < sizeof(*CommandsList) / sizeof(CommandsList->cmd[0]); i++) {
- if (strcmp(user_command.com, &CommandsList->cmd[i]) == 0) {
+ for (int i = 0; i < sizeof(CommandsList) / sizeof(CommandsList[0]); i++) {
+ if (strcmp(user_command.com, CommandsList[i].cmd) == 0) {
CommandsList[i].func(user_command.arg);
}
}
@@ -92,8 +89,7 @@ void exec_command(char *input) {
void list(const char *path) {
struct dirent *entry;
if (path == NULL) {
- perror("No path found\n");
- return;
+ path = ".";
}
DIR *dP = opendir(path);
@@ -111,15 +107,25 @@ void list(const char *path) {
}
void make_dir(const char *path) {
- printf("made a new directory");
+ if (mkdir(path, 0755) == 0)
+ perror("failed to make directory");
}
-void remove_dir(const char *path) {
- printf("removed a directory");
-}
void remove_file(const char *path) {
- printf("remove_file");
+ if (unlink(path) != 0) {
+ perror("failed to delete file");
+ }
+}
+
+void change_ownership(const char *path)
+{
+ if((chmod(path,S_IRWXU) != 0))
+ {
+ perror("failed to get ownership of the file");
+ } else {
+ printf("file permissions updated succssfully");
+ }
}
void copy_files(const char *arg) {
@@ -140,6 +146,9 @@ void echo(const char *arg) {
printf("%s", arg);
}
+void exit_(const char *arg){
+ exit(0);
+}
void print_cdirectory(const char *arg) {
char *current_working_directory = getcwd(NULL, 0);
printf("%s", current_working_directory);
@@ -149,6 +158,8 @@ void change_directory(const char *path) {
if (path == NULL) {
printf("No path found\n");
}
+ else if (chdir(path) != 0)
+ perror("path not found");
}
int main(void) {
@@ -157,7 +168,7 @@ int main(void) {
while (1) {
printf("\n$ ");
- char *input = malloc(sizeof(char *) * MAX_COMMAND_LENGTH);
+ char *input = malloc(sizeof(char) * MAX_COMMAND_LENGTH);
fgets(input, MAX_COMMAND_LENGTH, stdin);
exec_command(input);
free(input);