Skip to content
This repository was archived by the owner on May 17, 2019. It is now read-only.

Commit 688c675

Browse files
author
NerdOfCode
committed
Optimizing
Optimizing shell by patching `gcc -O` warnings...
1 parent d1255ca commit 688c675

2 files changed

Lines changed: 54 additions & 37 deletions

File tree

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ clean:
99
shell:
1010
rm -f Src/shell~
1111

12-
$C -std=gnu11 -o Src/shell Src/shell.c
12+
$C -std=gnu11 -O -o Src/shell Src/shell.c
1313

1414
commands:
1515

Src/shell.c

Lines changed: 53 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,6 @@
1919
2020
#######################################################################################
2121
Author: NerdOfCode
22-
Tested on: Ubuntu Server 16.04
23-
Status: Working --> ^^^
2422
License: Apache-2.0
2523
Updated on: 10/28/18
2624
#######################################################################################
@@ -67,10 +65,12 @@ char remove_char_result[128];
6765
char *home_dir;
6866
char *logged_in_user;
6967

70-
bool pwd_allowed = FALSE;
71-
bool whoami_allowed = FALSE;
68+
struct adv_desc{
69+
bool pwd_allowed;
70+
bool whoami_allowed;
71+
} adv_desc_access;
7272

73-
int mini_kernel_panic_counter = 0;
73+
int mini_kernel_panic_counter;
7474

7575
int main ( int argc, char argv[64] ){
7676

@@ -80,11 +80,11 @@ int main ( int argc, char argv[64] ){
8080

8181
//Test if user is allowed to use pwd and if allowed show the working directory
8282
//Also test if user is allowed to use hostname and whomai
83-
char *pwd_test;
84-
short int return_pwd_test_value;
83+
char *pwd_test = "";
84+
short int return_pwd_test_value = 0;
8585

86-
char *whoami_test;
87-
short int return_whoami_test_value;
86+
char *whoami_test = "";
87+
short int return_whoami_test_value = 0;
8888

8989
pwd_test = malloc(64 * sizeof(char));
9090
strcat(pwd_test,CMD_BIN);
@@ -101,17 +101,20 @@ int main ( int argc, char argv[64] ){
101101
warn_user();
102102

103103
if(access(pwd_test,F_OK) == 0){
104-
//EXITSTATUS(system(CMD_BIN"pwd"));
105104
return_pwd_test_value = system(CMD_BIN"pwd -none");
106105
if(WEXITSTATUS(return_pwd_test_value) == 0){
107-
pwd_allowed = TRUE;
106+
adv_desc_access.pwd_allowed = TRUE;
107+
}else if(return_pwd_test_value == -1){
108+
puts("Error accessing 'working directory'.");
108109
}
109110
}
110111

111112
if(access(whoami_test,F_OK) == 0){
112113
return_whoami_test_value = system(CMD_BIN"whoami -none");
113114
if(WEXITSTATUS(return_whoami_test_value) == 1){
114-
whoami_allowed = TRUE;
115+
adv_desc_access.whoami_allowed = TRUE;
116+
}else if(return_pwd_test_value == -1){
117+
puts("Error accessing 'working directory'.");
115118
}
116119
}
117120

@@ -127,31 +130,33 @@ int main ( int argc, char argv[64] ){
127130
start_up();
128131

129132
while(1){
130-
if(pwd_allowed == TRUE && whoami_allowed == TRUE){
133+
if(adv_desc_access.pwd_allowed == TRUE && adv_desc_access.whoami_allowed == TRUE){
131134
char pwd_buffer[128];
132135
char *short_pwd;
133136
//Just in case, run a function that changes the dir to the newly written one!
134137
update_new_cd(0);
135-
getcwd(pwd_buffer, sizeof(pwd_buffer));
138+
if(getcwd(pwd_buffer, sizeof(pwd_buffer))==NULL)
139+
if(DEBUG)
140+
puts("Error getting 'current working directory'.");
136141
//Remove all characters up to last one...
137142
short_pwd = remove_char_until(pwd_buffer, "/");
138143
printf(YELLOW_TEXT "%s@%s[%s]: " RESET, logged_in_user,hostname,short_pwd);
139144
//printf("CD BUFFER: %s\n",cd_buffer);
140145

141-
//TODO --> Real Time History Check
142-
//Send all key calls directly to stdin
143-
//system("/bin/stty raw");
144-
//Send STTY back to normal behavior
145-
//system("/bin/stty cooked");
146+
if(fgets(input,64,stdin) == NULL)
147+
if(DEBUG)
148+
puts("Error retrieving input.");
146149

147-
fgets(input,64,stdin);
148-
149-
}else if(whoami_allowed == TRUE){
150+
}else if(adv_desc_access.whoami_allowed == TRUE){
150151
printf(YELLOW_TEXT "%s@%s: " RESET, logged_in_user,hostname);
151-
fgets(input,64,stdin);
152+
if(fgets(input,64,stdin) == NULL)
153+
if(DEBUG)
154+
puts("Error retrieving input.");
152155
}else{
153156
printf(YELLOW_TEXT "Command: " RESET);
154-
fgets(input,64,stdin);
157+
if(fgets(input,64,stdin) == NULL)
158+
if(DEBUG)
159+
puts("Error retrieving input.");
155160
}
156161

157162

@@ -201,15 +206,15 @@ void change_to_home_dir( void ){
201206
//TODO
202207
//Create a better method for the following
203208
//Assume users directory is /home/logged_in_user
204-
209+
205210
//For chdir() return code
206211
short int return_status = 0;
207212

208213
char current_user_home[64] = "/home/";
209214
strcat(current_user_home,logged_in_user);
210-
return_status = chdir(current_user_home);
211215

212-
if(return_status != 0)
216+
//chdir() return -1 on error and 0 on success
217+
if(chdir(current_user_home) == -1)
213218
puts(RED_TEXT"Error: 1005"RESET);
214219

215220
}
@@ -417,12 +422,16 @@ int parseCommand(char input[64]){
417422
if(input != command_ptr){
418423

419424
//Reset to default users args
420-
memset(filename_ptr, 0, 128);
425+
memset(filename_ptr, 0, 64);
421426
strcat(filename_ptr, CMD_BIN);
422427
strcat(filename_ptr, input);
423-
system(filename_ptr);
428+
if(system(filename_ptr) == -1)
429+
if(DEBUG)
430+
printf("Error executing: %s\n",filename_ptr);
424431
}else{
425-
system(filename_ptr);
432+
if(system(filename_ptr) == -1 )
433+
if(DEBUG)
434+
printf("Error executing: %s\n",filename_ptr);
426435
}
427436
}else{
428437
//TODO
@@ -431,14 +440,17 @@ int parseCommand(char input[64]){
431440

432441
if(access(filename_ptr, F_OK) == 0){
433442

434-
system(filename_ptr);
443+
if(system(filename_ptr) == -1)
444+
if(DEBUG)
445+
puts("Error checking alias.");
435446

436447
}else{
437448

438449
puts(RED_TEXT"Command Not Found!"RESET);
439450
if(DEBUG){
440451
//Will show the pathway to file
441-
system(filename_ptr);
452+
//We can safely ignore return value
453+
int xyz = system(filename_ptr);
442454
}
443455
}
444456
}
@@ -475,7 +487,8 @@ int update_new_cd( int update ){
475487
fptr = fopen(cwd_file, "r");
476488

477489
if(fptr != NULL){
478-
fscanf(fptr, "%s", cd_buffer);
490+
if(fscanf(fptr, "%s", cd_buffer) == EOF)
491+
printf("Error: reading change directory");
479492
}else{
480493
return -1;
481494
}
@@ -491,13 +504,17 @@ int update_new_cd( int update ){
491504
puts("This system is not supported...");
492505
exit(1);
493506
}
494-
truncate(cwd_file, 0);
507+
//Delete old cwd file
508+
if(truncate(cwd_file, 0) == -1)
509+
printf("Error: unable to overwrite old 'cwd' file.");
495510

496-
chdir(cd_buffer);
511+
if(chdir(cd_buffer) == -1)
512+
puts(RED_TEXT"Error: 1005"RESET);
497513
memset(cd_buffer, 0, sizeof(cd_buffer));
498514
}else{
499515
//We should probably add a security mechanism here at a later date... 4/17/18
500-
chdir(cd_buffer);
516+
if(chdir(cd_buffer) == -1)
517+
puts(RED_TEXT"Error: 1005"RESET);
501518
}
502519

503520

0 commit comments

Comments
 (0)