diff --git a/Main.c b/Main.c index dd9750bfb36de884f1bdbff8e919bc8dfe487c75..c7411d5dc397208a68ba7efb1b1e6a231b94d69e 100644 --- a/Main.c +++ b/Main.c @@ -106,8 +106,7 @@ int main(int argc, char* argv[]) { } while (head_node != NULL) { - printf("\n"); - printf("\n%s\n", head_node->absolute_path); + printf("\n\n%s\n", head_node->absolute_path); // Change current directory to current node's path. return_int = change_directory(head_node->absolute_path); @@ -119,7 +118,7 @@ int main(int argc, char* argv[]) { free(temp_node->absolute_path); free(temp_node); } - printf("\n\n"); + printf("\n"); exit_program(); return 0; @@ -179,7 +178,7 @@ int change_directory(char* given_path) { // First read in node directory. return_int = lstat(given_path, &stat_buffer); if (return_int < 0) { - err_sys("Failed to stat file."); + err_sys("Failed to stat file.\n"); } // First, ensure that it is, infact, a directory. @@ -189,7 +188,7 @@ int change_directory(char* given_path) { // Change into directory. return_int = chdir(given_path); if (return_int < 0) { - err_sys("Failed to change directory."); + err_sys("Failed to change directory.\n"); return 0; } return 1; @@ -215,45 +214,51 @@ void list_directory(char* given_path) { struct stat stat_buffer; DIR* dir_pointer = opendir(given_path); - // Loop until no more files to read in current directory.. - while (dir_pointer != NULL) { - if ((dir_file = readdir(dir_pointer)) != NULL) { + if ((access(given_path, R_OK) == 0)) { - // New file found. Get type. - return_int = lstat(dir_file->d_name, &stat_buffer); - if (return_int < 0) { - err_sys("Failed to stat file."); - } - - // Check if directory. - if (S_ISDIR(stat_buffer.st_mode)) { + // Loop until no more files to read in current directory.. + while (dir_pointer != NULL) { + if ((dir_file = readdir(dir_pointer)) != NULL) { - // Check for "."" or "..". - if ((strcmp(dir_file->d_name, ".") != 0) && (strcmp(dir_file->d_name, "..") != 0)) { + // New file found. Get type. + return_int = lstat(dir_file->d_name, &stat_buffer); + if (return_int < 0) { + err_sys("Failed to stat file.\n"); + } - // Is directory and not symbolic. Add to queue. - temp_string = copy_string_with_buffer(given_path, BUFFER_SIZE); - strcat(temp_string, "/"); - strcat(temp_string, dir_file->d_name); - return_int = enqueue_node(temp_string); - if (return_int == 0) { - err_msg("Failed to queue node."); + // Check if directory. + if (S_ISDIR(stat_buffer.st_mode)) { + + // Check for "."" or "..". + if ((strcmp(dir_file->d_name, ".") != 0) && (strcmp(dir_file->d_name, "..") != 0)) { + + // Is directory and not symbolic. Add to queue. + temp_string = copy_string_with_buffer(given_path, BUFFER_SIZE); + strcat(temp_string, "/"); + strcat(temp_string, dir_file->d_name); + return_int = enqueue_node(temp_string); + if (return_int == 0) { + err_msg("Failed to queue node.\n"); + } + free(temp_string); } - free(temp_string); } - } - print_file_type(dir_file->d_name, stat_buffer); - //print_file_type_full(dir_file->d_name, stat_buffer); - } else { + print_file_type(dir_file->d_name, stat_buffer); + //print_file_type_full(dir_file->d_name, stat_buffer); + } else { - // End of files in directory. Closing stream. - return_int = closedir(dir_pointer); - if (return_int < 0) { - err_msg("Failed to properly close directory."); + // End of files in directory. Closing stream. + return_int = closedir(dir_pointer); + if (return_int < 0) { + err_msg("Failed to properly close directory.\n"); + } + dir_pointer = NULL; } - dir_pointer = NULL; } + + } else { + printf("No read permission. Cannot read directory files.\n"); } }