Add signal handling

This commit is contained in:
g1n 2021-09-22 14:32:26 +03:00
parent 0edcd01321
commit 05426d867b
1 changed files with 13 additions and 1 deletions

View File

@ -5,6 +5,7 @@
#include <sys/wait.h>
#include <pwd.h>
#include <libgen.h>
#include <signal.h>
int position = 0;
char pwd[128]; // FIXME: change to PATH_MAX
@ -12,6 +13,7 @@ char hostname[1024]; // Hostname of machine
struct passwd *pw; // Struct of passwd for uid
char *username; // Current username
char *homedir; // HOME directory of current user
pid_t pid; // FIXME: this shouldn't be global i think
char **split_line(char *line, char *delim) {
int bufsize = 64;
@ -36,7 +38,7 @@ char **split_line(char *line, char *delim) {
int execute(char **command, int status) {
status = 0;
pid_t pid, wpid;
pid_t wpid;
pid = fork();
if (pid == 0) {
@ -127,6 +129,8 @@ int parse_command(char **command, int status) {
return status;
}
void init() { // Some initial tasks
getcwd(pwd, sizeof(pwd)); // Get directory where shell was started
gethostname(hostname, sizeof(hostname)); // Get hostname
@ -140,6 +144,13 @@ void print_ps() {
printf("%s@%s %s> ", username, hostname, basename(pwd));
}
void signal_handler(int sig) {
if (pid == 0)
raise(sig);
else
printf("\n");
}
int main(int argc, char *argv[]) {
char *line = NULL;
char script_line[100]; // FIXME
@ -149,6 +160,7 @@ int main(int argc, char *argv[]) {
char** args;
int status = 0; // FIXME
int command_status = 0; // FIXME
signal(SIGINT, signal_handler);
if (argc >= 2) {
FILE *script = fopen(argv[1], "r");
if (script == NULL) {