-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
executable file
·74 lines (62 loc) · 1.55 KB
/
main.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
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include "execute.h"
#include "parse.h"
#include "read.h"
#define MAX_PATH 256
#define MAX_STR 64
/* reads, parses and executes, no pipes for now */
int main() {
char *line;
char **command;
char *dir = (char*)malloc(MAX_PATH*sizeof(char));
/*main loop */
while( 1 ){
getcwd(dir, 256);
printf("[ %s ]$ ", dir);
fflush(NULL); /* get ready to take input */
line = read_line();
command = parse_line(line);
/* checking if any redirection exists */
int redirect_flag = 0;
int len = -1;
while( command[++len] != NULL );
for( int i = 0; i < len; i++ ) {
if( !strcmp(command[i], ">") ) {
redirect_flag= 1;
}
}
/* built-in commands, will move them somewhere, this is very bad */
if ( !strcmp(command[0], "cd") ) {
chdir(command[1]);
}
else if ( !strcmp(command[0], "exit") ) {
return 1;
}
/*if redirection exists, quickly parse the command
* and send it to new execute_redir function
*/
else if( redirect_flag ){
char **first_part = malloc(20 * sizeof(char*));
for( int i = 0; i < 20; i++ ) {
first_part[i] = malloc(64*sizeof(char));
}
char *second_part = malloc(64*sizeof(char));
int counter = 0;
while( strcmp(command[counter], ">") ) {
strcpy(first_part[counter], command[counter]);
counter++;
}
first_part[counter] = 0x00;
strcpy(second_part, command[counter + 1]);
execute_redir(first_part, second_part);
}
else {
execute(command);
}
free(line);
free(command);
}
free(dir);
}