-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyshell.c
More file actions
42 lines (33 loc) · 950 Bytes
/
myshell.c
File metadata and controls
42 lines (33 loc) · 950 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <signal.h>
#include "signals.h"
#include "execute.h"
int main(int argc, char *argv[]) {
if(argc>1){
freopen(argv[1], "r", stdin);
}
signal(SIGINT, sigint_handler);
signal(SIGCHLD, sigchld_handler);
char cmdline[MAXLINE];
if (isatty(STDIN_FILENO)) {
printf("myshell> ");
}
while (fgets(cmdline, sizeof(cmdline), stdin) != NULL) {
cmdline[strlen(cmdline) - 1] = '\0';
// Check if cmdline is empty
if(strlen(cmdline) > 0){
// cmdline is nonempty, safe to parse and execute commands
execute_separated_commands(cmdline);
}
// check signal_received flag before printing the prompt
if (isatty(STDIN_FILENO) && !signal_received) {
printf("myshell> ");
}
signal_received = 0;
}
printf("\n");
exit(0);
}