add: exec command

This commit is contained in:
Guamss 2026-03-25 14:39:08 +01:00
parent 5c37658bcc
commit 936980df04
5 changed files with 52 additions and 13 deletions

View File

@ -1,3 +1,28 @@
#include "exec.h" #include "exec.h"
#include <stdio.h>
#include <unistd.h>
void execute_cmd(void) {} int is_builtin_cmd(char *executable) {
(void)executable;
return 0;
}
int execute_cmd(char **args) {
if (!is_builtin_cmd(args[0])) {
int pid = fork();
if (pid == -1)
return -1;
if (pid == 0) {
// Ce que va exécuter l'enfant
int status_code = execvp(args[0], args);
perror("seyshell");
return status_code;
} else {
// Ce que va exécuter le parent
int status;
waitpid(pid, &status, WUNTRACED);
}
}
return 1;
}

View File

@ -1,4 +1,6 @@
#pragma once #pragma once
#include <stdio.h>
#include <sys/wait.h>
int is_builtin_cmd(void); int is_builtin_cmd(char *executable);
void execute_cmd(void); int execute_cmd(char **args);

View File

@ -5,11 +5,8 @@
int main(int argc, char *argv[], char *envp[]) { int main(int argc, char *argv[], char *envp[]) {
(void)argc; (void)argc;
(void)argv; (void)argv;
(void)envp;
int index = 0;
while (envp[index]) {
printf("%s\n", envp[index++]);
}
shell_loop(); shell_loop();
return EXIT_SUCCESS; return EXIT_SUCCESS;

View File

@ -3,6 +3,7 @@
#include "exec.h" #include "exec.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h>
#include <unistd.h> #include <unistd.h>
char *read_line() { char *read_line() {
@ -29,8 +30,23 @@ char *read_line() {
} }
char **split_line(char *line) { char **split_line(char *line) {
(void)line; char **args = malloc(BUFSIZE * sizeof(char *));
return NULL; int i = 0;
if (args == NULL) {
dprintf(STDERR_FILENO, "Allocation error");
exit(EXIT_FAILURE);
}
char *token = strtok(line, " ");
while (token != NULL) {
args[i] = token;
i++;
token = strtok(NULL, " ");
}
args[i++] = NULL;
return args;
} }
void shell_loop(void) { void shell_loop(void) {
@ -41,11 +57,10 @@ void shell_loop(void) {
do { do {
dprintf(STDOUT_FILENO, "> "); dprintf(STDOUT_FILENO, "> ");
line = read_line(); line = read_line();
dprintf(STDOUT_FILENO, "%s\n", line);
args = split_line(line); args = split_line(line);
execute_cmd(); status = execute_cmd(args);
// free(line); free(line);
free(args); free(args);
} while (status); } while (status);
} }