-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
164 lines (141 loc) · 3.37 KB
/
Copy pathmain.c
File metadata and controls
164 lines (141 loc) · 3.37 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <readline/readline.h>
#include <readline/history.h>
#include <limits.h>
#include <signal.h>
void sigint_handler(int sig) {
write(STDOUT_FILENO, "\n", 1);
rl_on_new_line();
rl_replace_line("", 0);
rl_redisplay();
}
int main(int argc, char **argv) {
for (int i = 1; i < argc; i++) {
if (strcmp(argv[i], "-c") == 0 && i + 1 < argc) {
execlp("sh", "sh", "-c", argv[i+1], NULL);
perror("nautilush: exec failed");
_exit(EXIT_FAILURE);
}
}
signal(SIGINT, sigint_handler);
signal(SIGTSTP, SIG_IGN);
signal(SIGQUIT, SIG_IGN);
signal(SIGTTOU, SIG_IGN);
while (1) {
char *input = NULL;
size_t len = 0;
if (isatty(STDIN_FILENO)) {
char *cwd = getcwd(NULL, 0);
const char *home = getenv("HOME");
char prompt[PATH_MAX + 13]; // Length of "nautilush:~$ " is 13
if (cwd != NULL) {
if (home && strncmp(cwd, home, strlen(home)) == 0) {
snprintf(prompt,sizeof(prompt),"nautilush:~%s$ ", cwd + strlen(home));
} else {
snprintf(prompt, sizeof(prompt),"nautilush:%s$ ", cwd);
}
} else {
snprintf(prompt, sizeof(prompt), "nautilush:$ ");
}
free(cwd);
input = readline(prompt);
if (!input) {
break;
}
if (*input) {
add_history(input);
}
} else {
const ssize_t count = getline(&input, &len, stdin);
if (count == -1) {
free(input);
break;
}
if (input[count - 1] == '\n') {
input[count - 1] = '\0';
}
}
char *cmd = input;
while (isspace((unsigned char)*cmd)) {
cmd++;
}
len = strlen(cmd);
while (len > 0 && isspace((unsigned char)cmd[len - 1])) {
cmd[len - 1] = '\0';
len--;
}
if (*cmd == '\0') {
free(input);
continue;
}
if (strcmp(cmd, "exit") == 0) {
free(input);
break;
}
if (strncmp(cmd, "cd ", 3) == 0) {
cmd += 3;
while (isspace((unsigned char)*cmd)) {
cmd++;
}
if (chdir(cmd) != 0) {
perror("nautilush");
}
free(input);
continue;
}
if (strcmp(cmd, "cd") == 0) {
const char *home = getenv("HOME");
if (home && chdir(home) != 0) {
perror("nautilush");
}
free(input);
continue;
}
pid_t pid = fork();
if (pid == -1) {
perror("nautilush: fork failed");
} else if (pid == 0) {
setpgid(0, 0);
signal(SIGINT, SIG_DFL);
signal(SIGTSTP, SIG_DFL);
signal(SIGQUIT, SIG_DFL);
signal(SIGTTOU, SIG_DFL);
execlp("bash", "bash", "-c", input, NULL);
perror("nautilush: exec failed");
_exit(EXIT_FAILURE);
} else {
setpgid(pid, pid);
if (isatty(STDIN_FILENO)) {
tcsetpgrp(STDIN_FILENO, pid);
}
signal(SIGINT, SIG_IGN);
int status;
waitpid(pid, &status, WUNTRACED);
if (isatty(STDIN_FILENO)) {
tcsetpgrp(STDIN_FILENO, getpgrp());
}
signal(SIGINT, sigint_handler);
if (WIFSIGNALED(status)) {
if (WTERMSIG(status) == SIGINT) {
write(STDOUT_FILENO, "\n", 1);
} else {
if (WCOREDUMP(status)) {
printf("%s (core dumped) %s\n", strsignal(WTERMSIG(status)), input);
} else {
printf("%s %s\n", strsignal(WTERMSIG(status)), input);
}
}
} else if (WIFSTOPPED(status)) {
// [?] because bash has job numbers, nautilush does not
printf("\n[?]+ Stopped %s\n", input);
}
}
free(input);
}
return EXIT_SUCCESS;
}