add: exec command
This commit is contained in:
parent
5c37658bcc
commit
936980df04
2
Makefile
2
Makefile
@ -1,5 +1,5 @@
|
||||
NAME := seyshell
|
||||
CC := gcc
|
||||
CC := gcc
|
||||
CFLAGS := -Wall -Wextra -Werror -g
|
||||
OBJ_DIR := obj
|
||||
SRC_DIR := src
|
||||
|
||||
27
src/exec.c
27
src/exec.c
@ -1,3 +1,28 @@
|
||||
#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;
|
||||
}
|
||||
@ -1,4 +1,6 @@
|
||||
#pragma once
|
||||
#include <stdio.h>
|
||||
#include <sys/wait.h>
|
||||
|
||||
int is_builtin_cmd(void);
|
||||
void execute_cmd(void);
|
||||
int is_builtin_cmd(char *executable);
|
||||
int execute_cmd(char **args);
|
||||
@ -5,11 +5,8 @@
|
||||
int main(int argc, char *argv[], char *envp[]) {
|
||||
(void)argc;
|
||||
(void)argv;
|
||||
(void)envp;
|
||||
|
||||
int index = 0;
|
||||
while (envp[index]) {
|
||||
printf("%s\n", envp[index++]);
|
||||
}
|
||||
shell_loop();
|
||||
|
||||
return EXIT_SUCCESS;
|
||||
|
||||
@ -3,6 +3,7 @@
|
||||
#include "exec.h"
|
||||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
#include <unistd.h>
|
||||
|
||||
char *read_line() {
|
||||
@ -29,8 +30,23 @@ char *read_line() {
|
||||
}
|
||||
|
||||
char **split_line(char *line) {
|
||||
(void)line;
|
||||
return NULL;
|
||||
char **args = malloc(BUFSIZE * sizeof(char *));
|
||||
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) {
|
||||
@ -41,11 +57,10 @@ void shell_loop(void) {
|
||||
do {
|
||||
dprintf(STDOUT_FILENO, "> ");
|
||||
line = read_line();
|
||||
dprintf(STDOUT_FILENO, "%s\n", line);
|
||||
args = split_line(line);
|
||||
execute_cmd();
|
||||
status = execute_cmd(args);
|
||||
|
||||
// free(line);
|
||||
free(line);
|
||||
free(args);
|
||||
} while (status);
|
||||
}
|
||||
|
||||
Loading…
Reference in New Issue
Block a user