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

Commit ea8166c

Browse files
author
NerdOfCode
committed
Minor Updates
Switching to over to using strncat instead of strcat... My bad for not using 'strncat' from the beginning.
1 parent 4cc454b commit ea8166c

3 files changed

Lines changed: 50 additions & 51 deletions

File tree

Bin/cmd_src/rm.c

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@
66

77
int main(int argc, char **argv){
88

9+
if(argc < 2){
10+
puts("rm: [OPT] [FILENAME]");
11+
return -1;
12+
}
13+
914
//Will hold the exit value of the remove() function
1015
int remove_status = 0;
1116

Src/globals.h

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#define FALSE 0
3030

3131
//Used for displaying debug info for specific functions and commands (Experimental)
32+
//Default:0
3233
#define DEBUG 0
3334

3435
//Used for User-command logging
@@ -74,8 +75,6 @@
7475
//Default path to command Bin
7576
#define CMD_BIN DEFAULT_LOCATION"Bin/"
7677

77-
//Platform detection
78-
//On the wish list for now :)
7978
#if defined(__linux__)
8079
#define HOSTNAME "USER"
8180
#endif

Src/shell.c

Lines changed: 44 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -61,21 +61,18 @@ int update_new_cd();
6161
int log_command();
6262

6363
//Globals
64-
char remove_char_result[128];
65-
char *home_dir;
6664
char *logged_in_user;
6765

68-
struct adv_desc{
66+
struct user_access{
6967
bool pwd_allowed;
7068
bool whoami_allowed;
7169
} adv_desc_access;
7270

73-
int mini_kernel_panic_counter;
71+
unsigned int mini_kernel_panic_counter;
7472

7573
int main ( int argc, char argv[64] ){
7674

7775
char input[64] = "";
78-
char *string_compare = "";
7976
char *hostname = "";
8077

8178
//Test if user is allowed to use pwd and if allowed show the working directory
@@ -87,12 +84,12 @@ int main ( int argc, char argv[64] ){
8784
short int return_whoami_test_value = 0;
8885

8986
pwd_test = malloc(64 * sizeof(char));
90-
strcat(pwd_test,CMD_BIN);
91-
strcat(pwd_test,"pwd");
87+
strncat(pwd_test,CMD_BIN, sizeof(CMD_BIN));
88+
strncat(pwd_test,"pwd", sizeof(pwd_test));
9289

9390
whoami_test = malloc(64 * sizeof(char));
94-
strcat(whoami_test,CMD_BIN);
95-
strcat(whoami_test,"whoami");
91+
strncat(whoami_test,CMD_BIN, sizeof(CMD_BIN));
92+
strncat(whoami_test,"whoami", sizeof(whoami_test));
9693

9794
//Run our startup function
9895
start_up();
@@ -143,18 +140,18 @@ int main ( int argc, char argv[64] ){
143140
printf(YELLOW_TEXT "%s@%s[%s]: " RESET, logged_in_user,hostname,short_pwd);
144141
//printf("CD BUFFER: %s\n",cd_buffer);
145142

146-
if(fgets(input,64,stdin) == NULL)
143+
if(fgets(input,sizeof(input),stdin) == NULL)
147144
if(DEBUG)
148145
puts("Error retrieving input.");
149146

150147
}else if(adv_desc_access.whoami_allowed == TRUE){
151148
printf(YELLOW_TEXT "%s@%s: " RESET, logged_in_user,hostname);
152-
if(fgets(input,64,stdin) == NULL)
149+
if(fgets(input,sizeof(input),stdin) == NULL)
153150
if(DEBUG)
154151
puts("Error retrieving input.");
155152
}else{
156153
printf(YELLOW_TEXT "Command: " RESET);
157-
if(fgets(input,64,stdin) == NULL)
154+
if(fgets(input,sizeof(input),stdin) == NULL)
158155
if(DEBUG)
159156
puts("Error retrieving input.");
160157
}
@@ -172,14 +169,14 @@ int main ( int argc, char argv[64] ){
172169

173170
//Check to see if user wants to exit before re-running loop
174171
//Have to check for newline too, because of fgets for input
175-
if(strncmp(input,"exit\n",sizeof(input)) == 0){
172+
if(strncmp(input,"exit\n",sizeof("exit\n")) == 0){
176173
clean_up();
177174
exit(1);
178-
}else if(strncmp(input,"help\n",sizeof(input)) == 0){
175+
}else if(strncmp(input,"help\n",sizeof("help\n")) == 0){
179176
help_commands();
180-
}else if(strncmp(input,"cmds\n",sizeof(input)) == 0){
177+
}else if(strncmp(input,"cmds\n",sizeof("cmds\n")) == 0){
181178
commands();
182-
}else if(strncmp(input," \n",sizeof(input)) == 0){
179+
}else if(strncmp(input," \n",sizeof(" \n")) == 0){
183180
printf("He - He\n");
184181
}else{
185182
if(check_empty_beginning(input) <= -1){
@@ -211,7 +208,7 @@ void change_to_home_dir( void ){
211208
short int ret = 0;
212209

213210
char current_user_home[64] = "/home/";
214-
strcat(current_user_home,logged_in_user);
211+
strncat(current_user_home,logged_in_user,sizeof(current_user_home));
215212

216213
//chdir() return -1 on error and 0 on success
217214
ret = chdir(current_user_home);
@@ -227,18 +224,18 @@ int start_up( void ){
227224

228225
char home[64] = "/home/";
229226

230-
strcat(home, getenv("USER"));
227+
strncat(home, getenv("USER"), sizeof(home));
231228

232-
strcat(home, "/");
229+
strncat(home, "/", sizeof(home));
233230

234-
strcat(home, RSHELL_DIR);
231+
strncat(home, RSHELL_DIR, sizeof(home));
235232

236-
strcat(home, "/");
233+
strncat(home, "/", sizeof(home));
237234

238235
//Just to be sure
239236
mkdir(home,0755);
240237

241-
strcat(home, USER_CD_LOG);
238+
strncat(home, USER_CD_LOG, sizeof(home));
242239

243240
//Overwrite
244241
fptr = fopen(home, "w");
@@ -263,15 +260,15 @@ int clean_up( void ){
263260

264261
char home[64] = "/home/";
265262

266-
strcat(home, getenv("USER"));
263+
strncat(home, getenv("USER"), sizeof(home));
267264

268-
strcat(home, "/");
265+
strncat(home, "/", sizeof(home));
269266

270-
strcat(home, RSHELL_DIR);
267+
strncat(home, RSHELL_DIR, sizeof(home));
271268

272-
strcat(home, "/");
269+
strncat(home, "/", sizeof(home));
273270

274-
strcat(home, USER_CD_LOG);
271+
strncat(home, USER_CD_LOG, sizeof(home));
275272

276273
//Get rid of users cwd
277274
fptr = fopen(home, "w");
@@ -311,6 +308,8 @@ void commands(){
311308
char *remove_char_until(char specified_buffer[128],char remove_char[2]){
312309
//Begin removal process below
313310

311+
static char remove_char_result[128] = "";
312+
314313
int highest = 0,i = 0;
315314

316315
for(i = 0; i <= strlen(specified_buffer);++i){
@@ -362,12 +361,12 @@ int parseCommand(char input[64]){
362361
//Check for any allocation errors before saving input
363362
if(filename_ptr == NULL || command_ptr == NULL){
364363
fprintf(stderr, RED_TEXT"Failed to fork... Not enough memory...\n"RESET);
365-
return 0;
364+
return -1;
366365
}
367366

368367
//Just in case, check for an empty command
369368
if(input[0] == '\n'){
370-
return 0;
369+
return -1;
371370
}
372371

373372
//Prevent user from escaping by running something like: 'ls && exec /bin/bash'
@@ -377,7 +376,7 @@ int parseCommand(char input[64]){
377376
for(int i = 0; i <= strlen(input); i++){
378377
if(input[i] == '&' || input[i] == '|' || input[i] == ';'){
379378
fprintf(stderr,"Illegal character detected...\n");
380-
return 0;
379+
return -255;
381380
}
382381
}
383382

@@ -397,8 +396,8 @@ int parseCommand(char input[64]){
397396
//Obliterate filename_ptr
398397
//And check if command exists relative to its filename
399398
memset(filename_ptr, 0, sizeof(filename_ptr));
400-
strcat(filename_ptr,CMD_BIN);
401-
strcat(filename_ptr,command_ptr);
399+
strncat(filename_ptr,CMD_BIN, sizeof(CMD_BIN));
400+
strncat(filename_ptr,command_ptr, sizeof(CMD_BIN));
402401

403402
//Only remove newline if command has arguments
404403
if(command_args){
@@ -425,8 +424,8 @@ int parseCommand(char input[64]){
425424

426425
//Reset to default users args
427426
memset(filename_ptr, 0, 64);
428-
strcat(filename_ptr, CMD_BIN);
429-
strcat(filename_ptr, input);
427+
strncat(filename_ptr, CMD_BIN, sizeof(CMD_BIN));
428+
strncat(filename_ptr, input, sizeof(CMD_BIN));
430429
if(system(filename_ptr) == -1)
431430
if(DEBUG)
432431
printf("Error executing: %s\n",filename_ptr);
@@ -476,15 +475,15 @@ int update_new_cd( int update ){
476475
char cwd[1024] = "";
477476
char cwd_file[64] = "/home/";
478477

479-
strcat(cwd_file, getenv("USER"));
478+
strncat(cwd_file, getenv("USER"), sizeof(cwd_file));
480479

481-
strcat(cwd_file, "/");
480+
strncat(cwd_file, "/", sizeof(cwd_file));
482481

483-
strcat(cwd_file,RSHELL_DIR);
482+
strncat(cwd_file,RSHELL_DIR, sizeof(cwd_file));
484483

485-
strcat(cwd_file, "/");
484+
strncat(cwd_file, "/", sizeof(cwd_file));
486485

487-
strcat(cwd_file, USER_CD_LOG);
486+
strncat(cwd_file, USER_CD_LOG, sizeof(cwd_file));
488487

489488
fptr = fopen(cwd_file, "r");
490489

@@ -501,11 +500,7 @@ int update_new_cd( int update ){
501500

502501
if(strncmp(cd_buffer,"../",sizeof("../")) || strncmp(cd_buffer,"..",sizeof("../"))){
503502
//Write over file
504-
//LINUX SYSTEM DEPENDENT
505-
if(HOSTNAME != "USER"){
506-
puts("This system is not supported...");
507-
exit(1);
508-
}
503+
509504
//Delete old cwd file
510505
if(truncate(cwd_file, 0) == -1)
511506
printf("Error: unable to overwrite old 'cwd' file.");
@@ -533,15 +528,15 @@ int log_command(char *command){
533528
//Convert Log File to include home directory
534529
char home_dir_log[64] = "/home/";
535530

536-
strcat(home_dir_log,logged_in_user);
531+
strncat(home_dir_log,logged_in_user, sizeof(home_dir_log));
537532

538-
strcat(home_dir_log,"/");
533+
strncat(home_dir_log,"/", sizeof(home_dir_log));
539534

540-
strcat(home_dir_log,RSHELL_DIR);
535+
strncat(home_dir_log,RSHELL_DIR, sizeof(home_dir_log));
541536

542-
strcat(home_dir_log, "/");
537+
strncat(home_dir_log, "/", sizeof(home_dir_log));
543538

544-
strcat(home_dir_log, LOG_FILE);
539+
strncat(home_dir_log, LOG_FILE, sizeof(home_dir_log));
545540

546541
FILE *fptr = fopen(home_dir_log,"ab+");
547542

0 commit comments

Comments
 (0)