-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
138 lines (109 loc) · 2.81 KB
/
utils.c
File metadata and controls
138 lines (109 loc) · 2.81 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
#include "utils.h"
#include "shell.h"
#include "command_cd.h"
#include "command_pwd.h"
#include "command_echo.h"
#include "command_ls.h"
#include "command_pinfo.h"
#include "command_repeat.h"
#include "external_commands.h"
// initialise the main variables
void init(){
root_dir = getcwd(NULL, 0);
absolute_path = getcwd(NULL, 0);
prev_path = getcwd(NULL, 0);
logged_user_name = getlogin();
host_name = (char*)malloc(sizeof(char) * (HOST_NAME_MAX + 1));
gethostname(host_name, HOST_NAME_MAX);
}
// check if pre is a prefix of str
bool str_prefix(const char* pre, const char* str){
return strncmp(pre, str, strlen(pre)) == 0;
}
// get relative path
char* get_relative_path(char* path){
char temp_root_dir[PATH_MAX];
char temp_ch = '/';
static char new_path[PATH_MAX] = "";
static char final_path[PATH_MAX] = "~";
strcpy(temp_root_dir, root_dir);
strncat(temp_root_dir, &temp_ch, 1);
if(!strcmp(root_dir, path)){
strcpy(final_path, "~");
}
else if(!str_prefix(temp_root_dir, path)){
strcpy(final_path, path);
}
else{
strncpy(new_path, &path[strlen(root_dir)], (strlen(path) - strlen(root_dir) + 2));
strcat(final_path, new_path);
}
return final_path;
}
char* trim_str(char* orig_str){
char new_orig[PATH_MAX + 1] = "";
strcpy(new_orig, orig_str);
int count = 0;
static char str[1000] = "";
for(int i = 0; i < 1000; i++)
str[i] = 0;
for(int i = 0; new_orig[i] != '\0'; i++){
if(new_orig[i] == '\t')
new_orig[i] = ' ';
}
while(new_orig[count] == ' '){
count++;
}
for(int i = count, j = 0; new_orig[i] != 0; i++, j++){
str[j] = new_orig[i];
}
int new_len = strlen(str);
for(int i = new_len - 1; i > 0; i--){
if(str[i] != ' '){
break;
}
else{
str[i] = 0;
}
}
return(str);
}
void run_command(char* command){
if(str_prefix("cd", command)){
cd(&command[3]);
}
else if(str_prefix("pwd", command)){
pwd();
}
else if(str_prefix("echo", command)){
echo(&command[5]);
}
else if(str_prefix("ls", command)){
ls(&command[2]);
}
else if(str_prefix("pinfo", command)){
pinfo(&command[5]);
}
else if(str_prefix("repeat", command)){
repeat(&command[7]);
}
else{
handle_external(command);
}
return;
}
// parse input string
void parse_input_string(char* str){
const char s[2] = ";";
char *token;
char* trimmed_token;
/* get the first token */
token = strtok(str, s);
/* walk through other tokens */
while( token != NULL ) {
trimmed_token = trim_str(token);
run_command(trimmed_token);
token = strtok(NULL, s);
}
return;
}