-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
76 lines (71 loc) · 1.44 KB
/
Copy pathshell.c
File metadata and controls
76 lines (71 loc) · 1.44 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
#include <stdio.h>
#include <stdlib.h>
#include <signal.h>
#include <unistd.h>
#include "parser.h"
#include "stack.h"
#include "process.h"
void handle_sigint(int signum)
{
write(STDOUT_FILENO, "\n> ", 3);
}
int main()
{
signal(SIGINT, handle_sigint);
signal(SIGTSTP, SIG_IGN);
pid_t shell_pid = getpid();
tcsetpgrp(STDIN_FILENO, shell_pid);
Stack *jobStack = malloc(sizeof(Stack));
jobStack->head = NULL;
while (1)
{
printf("> ");
long long bufLen = 1;
long long idx = 0;
char *bufTemp = malloc(sizeof(char));
bool breakpt = false;
while (1)
{
char c;
if (scanf("%c", &c) == EOF)
{
breakpt = true;
break;
}
if (c == '\n')
{
bufTemp[idx] = '\0';
idx++;
break;
}
bufTemp[idx] = c;
idx++;
if (idx == bufLen)
{
char *temp = malloc(2 * bufLen * sizeof(char));
for (int i = 0; i < bufLen; i++)
temp[i] = bufTemp[i];
free(bufTemp);
bufTemp = temp;
bufLen *= 2;
}
}
if (breakpt)
{
free(bufTemp);
exitShell(jobStack);
break;
}
char *buf = malloc(idx * sizeof(char));
for (int i = 0; i < idx; i++)
buf[i] = bufTemp[i];
free(bufTemp);
InputParse *parsedInput = parseInput(buf);
if (parsedInput != NULL)
{
createProcess(parsedInput, jobStack, shell_pid);
}
free(buf);
}
freeStack(jobStack);
}