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", "loop.h": "c",
"exec.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 CC=gcc
CFLAGS= -O3 -Wall CFLAGS= -O3 -Wall
all:debug all:release
r:release
release:dish release:dish
dish: $(OBJ) dish: $(OBJ)
$(CC) $(CFLAGS) -o dish $(OBJ) $(CC) $(CFLAGS) -o dish $(OBJ)
d:
make clean
make debug
debug: CFLAGS += -DDEBUG -g debug: CFLAGS += -DDEBUG -g
debug: dish debug: dish
c: clean
clean: clean:
rm -f $(OBJ) dish 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) { while (1) {
// read a character // read a character
c = getchar(); c = getchar();
printf("%s", &c);
// repleace EOF with a null character and return // repleace EOF with a null character and return
if (c == EOF || c == '\n') { if (c == EOF || c == '\n') {
buffer[position] = '\0'; buffer[position] = '\0';

3
main.c
View file

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