-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.c
More file actions
206 lines (173 loc) · 5.15 KB
/
Copy pathutils.c
File metadata and controls
206 lines (173 loc) · 5.15 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
//Loading all the necessary header files
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<sys/utsname.h>
#include "utils.h"
//Utility function to remove the leading and preceeding spaces and tabs in a stirng
char* trim(char* command) {
int n = strlen(command);
char *str = (char*)malloc(sizeof(char) * (n + 1));
int count = 0, j, k;
while (isspace(command[count])) {
count++;
}
for (j = count, k = 0; command[j] != '\0'; j++, k++) {
str[k] = command[j];
}
if (!isspace(str[strlen(str) - 1])){
str[k] = '\0';
return str;
}
else{
for(int i = strlen(str) - 1; i >=0; i--){
if (!isspace(str[i])){
str[i+1] = '\0';
return str;
}
}
}
}
//Utility function to tokenize the given strings into respective tokens
char** tokenize(char* str){
int n = strlen(str);
char** toks = (char**)malloc(sizeof(char*) * (n + 1));;
char* tok;
int count = 0;
while(tok = strtok_r(str, " \t\n", &str)){
toks[count] = tok;
count++;
}
toks[count] = NULL;
return toks;
}
//Utility function to add specific characters to preserve a given section in the string
char* addChars(char* str, char c){
int x = 170;
for(int i = 0; str[i] != NULL; i++){
if((i + 1) < strlen(str) && str[i] == '\\' && str[i + 1] == c){
str[i] = (char)x;
str[i + 1] = (char)x;
i++;
}
}
return str;
}
//Utility function to replace the specific characters added in the string
char* replace(char* str, char c){
for(int i = 0; str[i] != NULL; i++){
if((i + 1) < strlen(str) && (int)str[i] == -86 && (int)str[i + 1] == -86){
str[i] = (char)92;
str[i + 1] = c;
i++;
}
}
return str;
}
//Utility function to split the string from a given deliminator
char** parse(char* str, char* delim){
int n = strlen(str);
char** toks = (char**)malloc(sizeof(char*) * (n + 1));
char* tok;
int count = 0;
while(tok = strtok_r(str, delim, &str)){
toks[count] = trim(tok);
count++;
}
toks[count] = NULL;
return toks;
}
//Utility function to check whether a character is present in string or not
int check(char* s, char* delim){
int n = strlen(s);
char* tok = strtok_r(s, delim, &s);
int n_;
if(tok == NULL){
n_ = 0;
}else{
n_ = strlen(tok);
}
if(n == n_){
return 0;
}else{
return 1;
}
}
//Utility function to check if the given string is a number
int isNumber(char* str){
if(strlen(str) == 0){
return -1;
}
for(int i = 0; str[i] != NULL; i++){
if(48 <= (int)str[i] && (int)str[i] <= 57){
continue;
}else{
return -1;
}
}
return 1;
}
//Utility function to count the number of words in a string
int numWords(char* str){
int number = 0;
for(int i = 0; i < strlen(str); i++){
if(str[i] == ' ' || str[i] == '\t' || str[i] == '\n'){
continue;
}
else{
number++;
while(str[i] != ' ' && str[i] != '\t' && str[i] != '\n' && (i < strlen(str))){
i++;
}
i--;
}
}
return number;
}
//Utility function to get the name of the shell
void getShellName(){
char username[maxLength];
getlogin_r(username, maxLength);
char systemName[maxLength];
struct utsname unameData;
uname(&unameData);
strcpy(systemName, unameData.nodename);
strcat(shellName, "<");
strcat(shellName, username);
strcat(shellName, "@");
strcat(shellName, systemName);
strcat(shellName, ":~");
}
//Utility function that displays the instructions on how to use this shell
void printHelp(){
printf("\n");
printf("\t\t************************Brief Instructions************************\n\n");
printf(" 1. Run the command\033[1;33m HISTN\033[0;35m to view the last N commands executed on this shell\n");
printf(" 2. Run the command\033[1;33m !HISTN\033[0;35m to execute the Nth command executed on this shell\n");
printf(" 3. Run the command\033[1;33m pid\033[0;35m to get the process id of this shell\n");
printf(" 4. Run the command\033[1;33m pid all\033[0;35m to get the process ids of all the processes spawned from this shell\n");
printf(" 5. Run the command\033[1;33m pid current\033[0;35m to get the process ids of all the processes spawned from this shell and currently active\n");
printf(" 6. Run the command\033[1;33m HELP\033[0;35m to get the instructions for this shell\n");
printf(" 7. Run the command\033[1;33m STOP\033[0;35m to exit this shell\n\n");
printf("\t\t*******************************************************************\n\n");
return;
}
//Adding colours
void red (){
printf("\033[1;31m");
}
void yellow(){
printf("\033[1;33m");
}
void cyan(){
printf("\033[0;36m");
}
void purple(){
printf("\033[0;35m");
}
void bold(){
printf("\033[1m");
}
void reset (){
printf("\033[0m");
}