-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMyShell.c
More file actions
86 lines (72 loc) · 2.37 KB
/
MyShell.c
File metadata and controls
86 lines (72 loc) · 2.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
#include "Header.h"
#include "customPwd.h"
#include "customEcho.h"
#include "customLs.h"
#include "customCd.h"
#include "customPinfo.h"
#include "sysCommands.h"
#include "customHistory.h"
#include "Execute.h" // KEEP THIS LAST
int main(){
// initialise dir stuff
// char home_dir[PATH_MAX];
char prev_directory[PATH_MAX];
getcwd(home_dir, PATH_MAX);
getcwd(prev_directory, PATH_MAX);
// int prev_directory_set = 0;
char hostname[HOST_NAME_MAX];
char username[LOGIN_NAME_MAX];
char cwd[PATH_MAX];
cwd[0] = '~'; cwd[1] = '\0';
// initialise linkedlist for bg process
initial = malloc(sizeof(struct bg_prc));
initial->size = 0;
initial->next = NULL;
initial->pid = getpid();
while(1){
gethostname(hostname, HOST_NAME_MAX);
getlogin_r(username, LOGIN_NAME_MAX);
printf("<%s@%s:%s>", username, hostname, cwd);
// NOW WE ATTEMPT TO INPUT COMMANDS FROM THE TERMINAL
char *command;
command = malloc(1024 * sizeof(char));
size_t bufsize = 1024;
int num_char_read = getline(&command, &bufsize, stdin);
// getline returns '-1' when cntr+D [exit command] is used
if(num_char_read == -1){
printf("\nBubyyeeee :)\n");
return 0;
}
// printf("%ld;%s",strlen(command), command);
// getline doens't add \0
command[num_char_read-1] = '\0';
// printf("%ld;%s",strlen(command), command);
saveHistory(command);
// remove intial spaces and tabs
int counter = 0;
while( (command[counter]==' ') || (command[counter]==' ') ){
counter++;
}
for(int i=counter; i<=strlen(command);i++ ){
command[i-counter] = command[i];
}
// WE NOW HAVE THE COMMANDS GIVEN IN A SINLGE INPUT
// TIME TO LOOP THROUGH ALL THE COMMMANDS
char *token;
token = strtok(command, ";");
while (token != NULL){
Execute(token, cwd, home_dir, prev_directory);
// moves to the next command
token = strtok(NULL, ";");
if(token==NULL)
continue;
while( (token[0]==' ') || (token[0]=='\t') ){
for(int i=1;i<strlen(token);i++){
token[i-1] = token[i];
}
token[strlen(token)-1] = '\0';
}
}
}
return 0;
}