-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbash.c
156 lines (131 loc) · 3.27 KB
/
bash.c
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
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include<unistd.h>
#include<sys/types.h>
#include<sys/wait.h>
#include<readline/readline.h>
#include<readline/history.h>
#define SIZE 1024
#define WORD 1000
#define LIST 100
//PRINTS DE ENTRADA E HELP
void startup()
{
char* user = getenv("USER");
printf("\n\n\n**********************************\n");
printf("\n\n\n BEM VINDO AO MEU TERMINAL\n");
printf("\n %s",user);
printf("\n\n\n***********************************\n");
}
void help()
{
printf("\n\n\n**********************************\n");
printf("\n\n\n MENU DE AJUDA\n");
printf("\n\n COMANDOS:");
printf("\n- cd");
printf("\n- exit");
printf("\n- help");
printf("\n- Qualquer comando UNIX shell");
printf("\n\n\n***********************************\n");
}
void showDir() {
// char* user = getenv("USER");
char* currentDir;
if( (currentDir = getcwd(NULL, 0)) == NULL) {
perror("failed to get current directory\n");
}
getcwd(currentDir, sizeof(currentDir));
//char* dir = strcat(user,"@");
//dir = strcat(dir,currentDir);
printf("\033[1;35m");
printf("\n%s@",getenv("USER"));
printf("\033[1;32m");
printf("%s",currentDir);
printf("\033[0m");
free(currentDir);
}
int inputManager(char* input){
char* temp;
//char* dir = showDir();
// printf("\n%s ",dir);
temp = readline("> ");
if(strlen(temp) != 0){
add_history(temp);
strcpy(input, temp);
return 0;
}
else
return 1;
}
int customCmdManager(char** input){
int cmdTotal = 3;
char* cmdList[cmdTotal];
int cmd = 0;
cmdList[0] = "exit";
cmdList[1] = "help";
cmdList[2] = "cd";
for(int i = 0; i < cmdTotal; i++){
if(strcmp(input[0], cmdList[i]) == 0){
cmd = i+1;
}
}
switch(cmd){
case 1:
exit(0);
case 2:
help();
return 1;
case 3:
chdir(input[1]);
return 1;
default:
break;
}
return 0;
}
void spaceInput(char* input, char** wordlist){
for(int i = 0; i< LIST; i++){
wordlist[i] = strsep(&input, " ");
if (wordlist[i] == NULL)
break;
if (strlen(wordlist[i]) == 0)
i--;
}
}
int parseInput(char* input, char** wordlist){
spaceInput(input,wordlist);
if(customCmdManager(wordlist))
return 0;
return 1;
}
void execCommands(char** wordlist){
pid_t pid = fork();
if(pid == -1){
printf("\nFailed to create a child process");
exit(1);
}
else if(pid == 0){
if(execvp(wordlist[0], wordlist) < 0)
perror("ERROR");
exit(0);
}
else{
wait(NULL);
return;
}
}
int main(void){
char input[WORD], *wordlist[LIST];
int execflag = 0;
startup();
while(1){
showDir();
if(inputManager(input))
continue;
execflag = parseInput(input, wordlist);
if(execflag == 1)
execCommands(wordlist);
}
return 0;
}