feat(exec): add exec

This commit is contained in:
iridiumR 2022-09-18 13:43:33 +08:00
parent 7d7030a2d9
commit d78af54b6c
No known key found for this signature in database
GPG Key ID: 5574BE4450D55618
6 changed files with 48 additions and 6 deletions

View File

@ -5,5 +5,12 @@
"loop.h": "c",
"exec.h": "c"
},
"editor.tabSize": 2
"editor.tabSize": 2,
"makefile.launchConfigurations": [
{
"cwd": "/home/ir/dev/priCode/dish",
"binaryPath": "/home/ir/dev/priCode/dish/dish",
"binaryArgs": []
}
]
}

View File

@ -2,16 +2,21 @@ OBJ=main.o line.o loop.o exec.o
CC=gcc
CFLAGS= -O3 -Wall
all:debug
all:release
r:release
release:dish
dish: $(OBJ)
$(CC) $(CFLAGS) -o dish $(OBJ)
d:
make clean
make debug
debug: CFLAGS += -DDEBUG -g
debug: dish
c: clean
clean:
rm -f $(OBJ) dish

28
exec.c Normal file
View File

@ -0,0 +1,28 @@
#include <stdio.h>
#include <stdlib.h>
#include <sys/types.h>
#include <unistd.h>
int forkExec(char **args) {
pid_t pid;
int status;
#ifdef DEBUG
printf("debug: fork enter.\n");
#endif
pid = fork();
if (pid == 0) {
/* child process */
if (execvp(args[0], args) == -1) {
perror("dish");
exit(EXIT_FAILURE);
}
} else if (pid < 0) {
/* fork error */
perror("dish");
} else {
/* parent process: wait child process*/
pid=wait(&status);
}
return WEXITSTATUS(status);
}

4
exec.h Normal file
View File

@ -0,0 +1,4 @@
#ifndef __EXEC_H__
#define __EXEC_H__
int forkExec(char **args);
#endif

1
line.c
View File

@ -17,7 +17,6 @@ char *readLine(void) {
while (1) {
// read a character
c = getchar();
printf("%s", &c);
// repleace EOF with a null character and return
if (c == EOF || c == '\n') {
buffer[position] = '\0';

3
main.c
View File

@ -5,7 +5,7 @@
#include "loop.h"
void intHandler(int dummy) {
printf("\nend!\n");
printf("\nSIGINT exit.\n");
exit(EXIT_FAILURE);
}
@ -13,7 +13,6 @@ int main(int argc, char *argv[]) {
signal(SIGINT, intHandler);
printf("Hello, World!\n");
#ifdef DEBUG
printf("DEBUG is defined\n");
#endif