From e87126c708a2d3b04fc91365b52ec0b8cd2da718 Mon Sep 17 00:00:00 2001 From: Brandon Rodriguez <brodriguez8774@gmail.com> Date: Wed, 4 Oct 2017 20:10:45 -0400 Subject: [PATCH] Create basic project structure Includes linked list and basic file read in --- LinkedList.c | 72 ++++++++++++++++++++ LinkedList.h | 29 ++++++++ Main.c | 182 +++++++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 283 insertions(+) create mode 100644 LinkedList.c create mode 100644 LinkedList.h create mode 100644 Main.c diff --git a/LinkedList.c b/LinkedList.c new file mode 100644 index 0000000..779f9f7 --- /dev/null +++ b/LinkedList.c @@ -0,0 +1,72 @@ +/** + * Brandon Rodriguez + * CS 3240 + * 10-04-16 + */ + + +/** + * Standard linked list, in C. + */ + + +// Import headers. +#include "apue.h" +#include "HelperHeader.h" +#include "LinkedList.h" + + +/** + * Creates a new node with provided data. + */ +directory_node* create_node(char* directory_name, char* absolute_path) { + directory_node* new_node = calloc(1, sizeof(node_t)); + new_node->directory_name = copy_string(directory_name); + new_node->absolute_path = copy_string(absolute_path); + return new_node; +} + + + +/** + * Enqueues node from tail of list. + * + * Return: 1 on success or 0 on failure. + */ +int enqueue_node(char* directory_name, char* absolute_path) { + directory_node* a_node = create_node(directory_name, absolute_path); + if (a_node == NULL) { + return 0; + } + + // Check if directories already exist within queue. + if (head_node == NULL) { + head_node = a_node; + tail_node = a_node; + } else { + tail_node->next_node = a_node; + tail_node = a_node; + } + return 1; +} + + +/** + * Dequeues node from head of list. + * + * Return: Noded on success or null on failure. + */ +directory_node* dequeue_node() { + directory_node* a_node; + + // Check that there are nodes to return. + if (head_node == NULL) { + return NULL; + } + + // Return current head node. + a_node = head_node; + head_node = head_node->next_node; + a_node->next_node = NULL; + return a_node; +} diff --git a/LinkedList.h b/LinkedList.h new file mode 100644 index 0000000..980177a --- /dev/null +++ b/LinkedList.h @@ -0,0 +1,29 @@ +/** + * Brandon Rodriguez + * CS 3240 + * 10-04-17 + */ + + +/** + * A personal header for linked list implementation. + */ + + +// Variables. +typedef struct directory_node directory_node; + +struct directory_node { + char* directory_name; + char* absolute_path; + directory_node* next_node; +} node_t; +directory_node* head_node; +directory_node* tail_node; + + +// Prototypes. + +directory_node* create_node(char* directory_name, char* absolute_path); +int enqueue_node(char* directory_name, char* absolute_path); +directory_node* dequeue_node(); diff --git a/Main.c b/Main.c new file mode 100644 index 0000000..847bc37 --- /dev/null +++ b/Main.c @@ -0,0 +1,182 @@ +/** + * Brandon Rodriguez + * CS 3240 + * 10-04-16 + * a2 (Assignment 2) + */ + + +/** + * Description: + * Read from a provided directory path (relative or absolute). + * For all files and folders in said path, print out information, + * using a "breadth-first" approach. + */ + + +/** + * Known Issues: + * + */ + + +#define _BSD_SOURCE + +// Import headers. +#include <stdio.h> +#include <sys/stat.h> +#include <sys/types.h> +#include <unistd.h> +#include "apue.h" +#include "HelperHeader.h" +#include "LinkedList.h" + + +// Define Vars. +#define BUFFER_SIZE 4096 + + +// Variables. +struct stat read_buffer; +char* root_directory; + + +// Method Declaration. +void set_root_directory(); +void print_file_type(); +char* get_path_end(); +void exit_program(); + + +/** + * Program's main. + * Initializes and runs program. + * + * Return 0 on normal exit or 1 on error. + */ +int main(int argc, char* argv[]) { + // Check number of args provided. + // Should have one argument (plus program call). + if (argc != 2) { + if (argc < 2) { + printf("Please provide directory to list.\n"); + return 1; + } else { + printf("Too many args provided. Please provide only a single directory.\n"); + return 1; + } + } else { + int stat_value = lstat(argv[1], &read_buffer); + if (stat_value < 0) { + err_sys("Failed to stat file."); + } + } + + set_root_directory(argv[1]); + printf("%s\n", root_directory); + print_file_type(argv[1]); + + + // int return_int = enqueue_node("./TestingDirectory", "~/home/brodriguez8774/Classes/CS3240/Assignments/a2"); + // if (return_int == 0) { + // err_msg("Failed to create node."); + // } + + exit_program(); + return 0; +} + + +/** + * Sets absolute root directory for program. + */ +void set_root_directory(char* argv) { + char* current_dir; + char* root_end_dir; + + // Allocate memory. + if (root_directory != NULL) { + free(root_directory); + } + root_directory = calloc(1, BUFFER_SIZE); + + // Set absolute path. + if ((getcwd(root_directory, BUFFER_SIZE)) == NULL) { + err_sys("Failed to set root directory."); + } + + // Double check that path includes current directory also. + // Necessary for consistency between both relative and absolute paths. + current_dir = get_path_end(argv); + root_end_dir = get_path_end(root_directory); + if (current_dir != root_end_dir) { + // Append directory to path. + strcat(root_directory, current_dir); + } + + free(current_dir); + free(root_end_dir); +} + + +/** + * Parses provided path to find the ending directory. + */ +char* get_path_end(char* path) { + char* char_pointer = path; + + // First get null terminator of string. + while (*char_pointer != '\0') { + ++char_pointer; + } + + // Now work backwards from end to get last directory in path. + --char_pointer; + --char_pointer; + while (*char_pointer != '/') { + --char_pointer; + } + + char_pointer = copy_string(char_pointer); + + return char_pointer; +} + + +/** + * Prints mode of provided file. + */ +void print_file_type(char* argv) { + if (S_ISREG(read_buffer.st_mode)) { + printf("%s - Regular File\n", argv); + } else if (S_ISDIR(read_buffer.st_mode)) { + printf("%s - Directory\n", argv); + } else if (S_ISCHR(read_buffer.st_mode)) { + printf("%s - Char Special\n", argv); + } else if (S_ISBLK(read_buffer.st_mode)) { + printf("%s - Block Special\n", argv); + } else if (S_ISFIFO(read_buffer.st_mode)) { + printf("%s - Named Pipe\n", argv); + } else if (S_ISLNK(read_buffer.st_mode)) { + printf("%s - Symbolic Link\n", argv); + } else if (S_ISSOCK(read_buffer.st_mode)) { + printf("%s - Socket\n", argv); + } else { + printf("Unknown file mode.\n"); + } +} + + +/** + * Frees memory to exit program cleanly. + */ +void exit_program() { + free(root_directory); + directory_node * a_node; + while (head_node != NULL) { + a_node = dequeue_node(); + free(a_node->directory_name); + free(a_node->absolute_path); + free(a_node); + } +} -- GitLab