wrote a makefile and added background job support
This commit is contained in:
@@ -5,6 +5,19 @@
|
||||
#include<string.h>
|
||||
#include<readline/readline.h>
|
||||
#include<readline/history.h>
|
||||
#include<signal.h>
|
||||
void handleSIGINT(int sig) {
|
||||
printf("\n");
|
||||
}
|
||||
int detectBackgrount(char **args) {
|
||||
for (int i = 0; args[i] != NULL; i++) {
|
||||
if (strcmp(args[i], "&") == 0) {
|
||||
args[i] = NULL;
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
int seperatePipe(char **args) {
|
||||
for(int i = 0; args[i] != NULL; i++) {
|
||||
if (strcmp(args[i], "|") == 0) return i;
|
||||
@@ -22,6 +35,7 @@ void executePipe(char **args, int pipeIndex) {
|
||||
dup2(fd[1], STDOUT_FILENO);
|
||||
close(fd[0]);
|
||||
close(fd[1]);
|
||||
signal(SIGINT, SIG_DFL);
|
||||
execvp(cmd1[0], cmd1);
|
||||
perror("tinysh");
|
||||
exit(1);
|
||||
@@ -31,6 +45,7 @@ void executePipe(char **args, int pipeIndex) {
|
||||
dup2(fd[0], STDIN_FILENO);
|
||||
close(fd[0]);
|
||||
close(fd[1]);
|
||||
signal(SIGINT, SIG_DFL);
|
||||
execvp(cmd2[0], cmd2);
|
||||
perror("tinysh");
|
||||
exit(1);
|
||||
@@ -62,13 +77,14 @@ char **parse(char *line) {
|
||||
return args;
|
||||
}
|
||||
int main() {
|
||||
signal(SIGINT, handleSIGINT);
|
||||
while(1) {
|
||||
char *user = getenv("USER");
|
||||
char dir[1024];
|
||||
getcwd(dir, sizeof(dir));
|
||||
char host[1024];
|
||||
gethostname(host, sizeof(host));
|
||||
char prompt[2048];
|
||||
char prompt[2051];
|
||||
snprintf(prompt, sizeof(prompt), "%s@%s %s$ ", user, host, dir);
|
||||
char *line = readline(prompt);
|
||||
if(line == NULL) return 0;
|
||||
@@ -76,6 +92,7 @@ int main() {
|
||||
char **parsed = parse(line);
|
||||
if(parsed[0] == NULL) { free(parsed); free(line); continue; }
|
||||
if(handleBuiltins(parsed)) { free(line); free(parsed); continue; }
|
||||
int background = detectBackgrount(parsed);
|
||||
int pipeIndex = seperatePipe(parsed);
|
||||
if (pipeIndex != -1) {
|
||||
executePipe(parsed, pipeIndex);
|
||||
@@ -89,7 +106,7 @@ int main() {
|
||||
perror("tinysh");
|
||||
exit(1);
|
||||
}
|
||||
} else if(pid > 0) {
|
||||
} else if(pid > 0 && background != 0) {
|
||||
wait(NULL);
|
||||
}
|
||||
free(parsed);
|
||||
|
||||
Reference in New Issue
Block a user