feat(exec): add exec
This commit is contained in:
parent
7d7030a2d9
commit
d78af54b6c
6 changed files with 48 additions and 6 deletions
9
.vscode/settings.json
vendored
9
.vscode/settings.json
vendored
|
@ -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": []
|
||||
}
|
||||
]
|
||||
}
|
9
Makefile
9
Makefile
|
@ -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
28
exec.c
Normal 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
4
exec.h
Normal file
|
@ -0,0 +1,4 @@
|
|||
#ifndef __EXEC_H__
|
||||
#define __EXEC_H__
|
||||
int forkExec(char **args);
|
||||
#endif
|
1
line.c
1
line.c
|
@ -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
3
main.c
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue