environ #15
43
src/array.c
43
src/array.c
@ -17,10 +17,6 @@
|
||||
#include <string.h>
|
||||
#include "array.h"
|
||||
|
||||
/**
|
||||
* Create a String Array by initializing its structure.
|
||||
* @return Returns a new String Array.
|
||||
*/
|
||||
StringArray *create_string_array() {
|
||||
StringArray *string_array = malloc(sizeof(StringArray));
|
||||
if (string_array == NULL) {
|
||||
@ -32,11 +28,6 @@ StringArray *create_string_array() {
|
||||
return string_array;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert a string into the String Array.
|
||||
* @param string_array The String Array to insert into.
|
||||
* @param string The string to insert into the String Array.
|
||||
*/
|
||||
void insert_string_array(StringArray *string_array, char *string) {
|
||||
if (string_array->array == NULL) {
|
||||
string_array->array = malloc(2 * sizeof(char *));
|
||||
@ -66,11 +57,6 @@ void insert_string_array(StringArray *string_array, char *string) {
|
||||
string_array->size++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Delete a string from the String Array.
|
||||
* @param string_array The String Array to delete from.
|
||||
* @param index The index in the String Array to delete.
|
||||
*/
|
||||
void delete_string_array(StringArray *string_array, int index) {
|
||||
if (string_array->array != NULL && string_array->size > 0 && string_array->size > index) {
|
||||
for (size_t i = index; i < string_array->size - 1; i++) {
|
||||
@ -100,10 +86,6 @@ void delete_string_array(StringArray *string_array, int index) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Free the String Array and all of its strings.
|
||||
* @param string_array The String Array to free.
|
||||
*/
|
||||
void free_string_array(StringArray *string_array) {
|
||||
for (size_t i = 0; i < string_array->size; i++) {
|
||||
if (string_array->array[i] != NULL) {
|
||||
@ -122,10 +104,6 @@ void free_string_array(StringArray *string_array) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Create a new Array List.
|
||||
* @return Returns the newly created Array List.
|
||||
*/
|
||||
ArrayList *create_array_list() {
|
||||
ArrayList *array_list = malloc(sizeof(ArrayList));
|
||||
if (array_list == NULL) {
|
||||
@ -138,12 +116,6 @@ ArrayList *create_array_list() {
|
||||
return array_list;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set a key inside the Array List.
|
||||
* @param array_list The Array List to work on.
|
||||
* @param key The key to insert/update.
|
||||
* @param value The value to insert/update.
|
||||
*/
|
||||
void set_array_list(ArrayList *array_list, char *key, char *value) {
|
||||
if (array_list->keys == NULL) {
|
||||
array_list->keys = create_string_array();
|
||||
@ -165,12 +137,6 @@ void set_array_list(ArrayList *array_list, char *key, char *value) {
|
||||
array_list->size++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get a value based on a key from an Array List.
|
||||
* @param array_list The Array List to work on.
|
||||
* @param key The key to search for.
|
||||
* @return Returns the value if the key is found in the Array List otherwise it returns NULL.
|
||||
*/
|
||||
char *get_array_list(ArrayList *array_list, char *key) {
|
||||
if (array_list->keys != NULL) {
|
||||
for (size_t i = 0; i < array_list->size; i++) {
|
||||
@ -182,11 +148,6 @@ char *get_array_list(ArrayList *array_list, char *key) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/**
|
||||
* Remove a key from the Array List.
|
||||
* @param array_list The Array List to work on.
|
||||
* @param key The key to remove.
|
||||
*/
|
||||
void unset_array_list(ArrayList *array_list, char *key) {
|
||||
if (array_list->keys != NULL) {
|
||||
for (size_t i = 0; i < array_list->size; i++) {
|
||||
@ -200,10 +161,6 @@ void unset_array_list(ArrayList *array_list, char *key) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Free all the memory used in the Array List.
|
||||
* @param array_list The Array List to free.
|
||||
*/
|
||||
void free_array_list(ArrayList *array_list) {
|
||||
if (array_list != NULL) {
|
||||
free_string_array(array_list->keys);
|
||||
|
@ -21,11 +21,6 @@
|
||||
#include "builtins.h"
|
||||
#include "console_line.h"
|
||||
|
||||
/**
|
||||
* Check if the command is a builtin or not.
|
||||
* @param command String with the command name to check.
|
||||
* @return Returns true if it's a builtin or false otherwise.
|
||||
*/
|
||||
bool is_builtin(char *command) {
|
||||
if (strcmp(command, "exit") == 0) {
|
||||
return true;
|
||||
@ -38,10 +33,6 @@ bool is_builtin(char *command) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Run the builtin command.
|
||||
* @param args An array of strings containing the arguments to run.
|
||||
*/
|
||||
void run_builtin(StringArray *args) {
|
||||
if (strcmp(args->array[0], "exit") == 0) {
|
||||
exit_shell(args);
|
||||
@ -54,19 +45,11 @@ void run_builtin(StringArray *args) {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Exit the shell.
|
||||
* @param args The arguments that were used to call exit. This is used to free the memory before exit.
|
||||
*/
|
||||
void exit_shell(StringArray *args) {
|
||||
free_string_array(args);
|
||||
exit(EXIT_SUCCESS);
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the directory to what the user inputs.
|
||||
* @param args The arguments the user input.
|
||||
*/
|
||||
void change_directory(StringArray *args) {
|
||||
if (args->size > 2) {
|
||||
fprintf(stderr, "Too many arguments!\n");
|
||||
|
@ -25,18 +25,10 @@
|
||||
#include "color.h"
|
||||
#include "console_line.h"
|
||||
|
||||
/**
|
||||
* Remove new line from the end of a string.
|
||||
* @param line The string to remove the new line from.
|
||||
*/
|
||||
void remove_new_line(char* line) {
|
||||
line[strcspn(line, "\n")] = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the logged in user's username.
|
||||
* @return Returns the logged in user's username.
|
||||
*/
|
||||
char *get_username() {
|
||||
struct passwd *pass = NULL;
|
||||
pass = getpwuid(getuid());
|
||||
@ -47,10 +39,6 @@ char *get_username() {
|
||||
return pass->pw_name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the hostname of the machine.
|
||||
* @return Returns the hostname.
|
||||
*/
|
||||
char *get_hostname() {
|
||||
char hostname[HOST_NAME_MAX + 1];
|
||||
gethostname(hostname, HOST_NAME_MAX + 1);
|
||||
@ -63,10 +51,6 @@ char *get_hostname() {
|
||||
return result;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the current working directory of the shell.
|
||||
* @return Returns the current working directory.
|
||||
*/
|
||||
char *get_working_directory() {
|
||||
char *cwd = malloc(PATH_MAX * sizeof(char *));
|
||||
getcwd(cwd, PATH_MAX);
|
||||
@ -77,9 +61,6 @@ char *get_working_directory() {
|
||||
return cwd;
|
||||
}
|
||||
|
||||
/**
|
||||
* Print the console line before the user input.
|
||||
*/
|
||||
void print_input_line() {
|
||||
char *username = get_username();
|
||||
char *hostname = get_hostname();
|
||||
@ -95,10 +76,6 @@ void print_input_line() {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Get input from the console.
|
||||
* @return Returns a string input by the user.
|
||||
*/
|
||||
char *get_console_input() {
|
||||
size_t buffer_size = 0;
|
||||
char *line = NULL;
|
||||
|
@ -17,40 +17,126 @@
|
||||
|
||||
#ifndef _MYSHELLIN_ARRAY
|
||||
#define _MYSHELLIN_ARRAY
|
||||
|
||||
/**
|
||||
* This struct houses a dynamically sized string array.
|
||||
*/
|
||||
typedef struct {
|
||||
/**
|
||||
* The array.
|
||||
*/
|
||||
char **array;
|
||||
|
||||
/**
|
||||
* The amount of elements in the array.
|
||||
*/
|
||||
size_t size;
|
||||
} StringArray;
|
||||
|
||||
/**
|
||||
* This struct is used to clean up memory on exit.
|
||||
*/
|
||||
typedef struct {
|
||||
/**
|
||||
* The array of memory to clean.
|
||||
*/
|
||||
void **array;
|
||||
|
||||
/**
|
||||
* The amount of elements in the array.
|
||||
*/
|
||||
size_t size;
|
||||
} CleanArray;
|
||||
|
||||
/**
|
||||
* This struct has an array list with key value pairs based on strings.
|
||||
*/
|
||||
typedef struct {
|
||||
/**
|
||||
* The keys array.
|
||||
*/
|
||||
StringArray *keys;
|
||||
|
||||
/**
|
||||
* The values array.
|
||||
*/
|
||||
StringArray *values;
|
||||
|
||||
/**
|
||||
* The amount of elements in the array list.
|
||||
*/
|
||||
size_t size;
|
||||
} ArrayList;
|
||||
|
||||
/**
|
||||
* A global clean array to use on exit.
|
||||
*/
|
||||
CleanArray clean;
|
||||
|
||||
/**
|
||||
* A global variables array used for environment variables and values.
|
||||
*/
|
||||
ArrayList *variables;
|
||||
|
||||
/**
|
||||
* Create a String Array by initializing its structure.
|
||||
* @return Returns a new String Array.
|
||||
*/
|
||||
StringArray *create_string_array();
|
||||
|
||||
/**
|
||||
* Insert a string into the String Array.
|
||||
* @param string_array The String Array to insert into.
|
||||
* @param string The string to insert into the String Array.
|
||||
*/
|
||||
void insert_string_array(StringArray *string_array, char *string);
|
||||
|
||||
/**
|
||||
* Delete a string from the String Array.
|
||||
* @param string_array The String Array to delete from.
|
||||
* @param index The index in the String Array to delete.
|
||||
*/
|
||||
void delete_string_array(StringArray *string_array, int index);
|
||||
|
||||
/**
|
||||
* Free the String Array and all of its strings.
|
||||
* @param string_array The String Array to free.
|
||||
*/
|
||||
void free_string_array(StringArray *string_array);
|
||||
|
||||
/**
|
||||
* Create a new Array List.
|
||||
* @return Returns the newly created Array List.
|
||||
*/
|
||||
ArrayList *create_array_list();
|
||||
|
||||
/**
|
||||
* Set a key inside the Array List.
|
||||
* @param array_list The Array List to work on.
|
||||
* @param key The key to insert/update.
|
||||
* @param value The value to insert/update.
|
||||
*/
|
||||
void set_array_list(ArrayList *array_list, char *key, char *value);
|
||||
|
||||
/**
|
||||
* Get a value based on a key from an Array List.
|
||||
* @param array_list The Array List to work on.
|
||||
* @param key The key to search for.
|
||||
* @return Returns the value if the key is found in the Array List otherwise it returns NULL.
|
||||
*/
|
||||
char *get_array_list(ArrayList *array_list, char *key);
|
||||
|
||||
/**
|
||||
* Remove a key from the Array List.
|
||||
* @param array_list The Array List to work on.
|
||||
* @param key The key to remove.
|
||||
*/
|
||||
void unset_array_list(ArrayList *array_list, char *key);
|
||||
|
||||
/**
|
||||
* Free all the memory used in the Array List.
|
||||
* @param array_list The Array List to free.
|
||||
*/
|
||||
void free_array_list(ArrayList *array_list);
|
||||
|
||||
#endif
|
||||
|
@ -18,11 +18,30 @@
|
||||
|
||||
#ifndef _MYSHELLIN_BUILTINS
|
||||
#define _MYSHELLIN_BUILTINS
|
||||
|
||||
/**
|
||||
* Check if the command is a builtin or not.
|
||||
* @param command String with the command name to check.
|
||||
* @return Returns true if it's a builtin or false otherwise.
|
||||
*/
|
||||
bool is_builtin(char *command);
|
||||
|
||||
/**
|
||||
* Run the builtin command.
|
||||
* @param args An array of strings containing the arguments to run.
|
||||
*/
|
||||
void run_builtin(StringArray *args);
|
||||
|
||||
/**
|
||||
* Exit the shell.
|
||||
* @param args The arguments that were used to call exit. This is used to free the memory before exit.
|
||||
*/
|
||||
void exit_shell(StringArray *args);
|
||||
|
||||
/**
|
||||
* Change the directory to what the user inputs.
|
||||
* @param args The arguments the user input.
|
||||
*/
|
||||
void change_directory(StringArray *args);
|
||||
|
||||
#endif
|
||||
|
@ -15,17 +15,42 @@
|
||||
|
||||
#ifndef _MYSHELLIN_CONSOLE_LINE
|
||||
#define _MYSHELLIN_CONSOLE_LINE
|
||||
|
||||
#define CONSOLE_BUFFER_SIZE 1024
|
||||
|
||||
/**
|
||||
* Remove new line from the end of a string.
|
||||
* @param line The string to remove the new line from.
|
||||
*/
|
||||
void remove_new_line(char *line);
|
||||
|
||||
/**
|
||||
* Get the logged in user's username.
|
||||
* @return Returns the logged in user's username.
|
||||
*/
|
||||
char *get_username();
|
||||
|
||||
/**
|
||||
* Get the hostname of the machine.
|
||||
* @return Returns the hostname.
|
||||
*/
|
||||
char *get_hostname();
|
||||
|
||||
/**
|
||||
* Get the current working directory of the shell.
|
||||
* @return Returns the current working directory.
|
||||
*/
|
||||
char *get_working_directory();
|
||||
|
||||
/**
|
||||
* Print the console line before the user input.
|
||||
*/
|
||||
void print_input_line();
|
||||
|
||||
/**
|
||||
* Get input from the console.
|
||||
* @return Returns a string input by the user.
|
||||
*/
|
||||
char *get_console_input();
|
||||
|
||||
#endif
|
||||
|
@ -17,5 +17,11 @@
|
||||
|
||||
#ifndef _MYSHELLIN_LAUNCH
|
||||
#define _MYSHELLIN_LAUNCH
|
||||
|
||||
/**
|
||||
* Launch programs from the OS.
|
||||
* @param args The arguments to launch.
|
||||
*/
|
||||
void launch_program(StringArray *args);
|
||||
|
||||
#endif
|
||||
|
@ -15,9 +15,21 @@
|
||||
|
||||
#ifndef _MYSHELLIN_LOOP
|
||||
#define _MYSHELLIN_LOOP
|
||||
|
||||
/**
|
||||
* Add memory address to array to be cleaned up later.
|
||||
* @param data The data to be cleaned up on exit.
|
||||
*/
|
||||
void add_to_cleanup(void *data);
|
||||
|
||||
/**
|
||||
* Cleanup memory when exiting.
|
||||
*/
|
||||
void exit_cleanup();
|
||||
|
||||
/**
|
||||
* This is the loop that checks for user input and acts on it.
|
||||
*/
|
||||
void loop();
|
||||
|
||||
#endif
|
||||
|
@ -20,10 +20,6 @@
|
||||
#include <unistd.h>
|
||||
#include "array.h"
|
||||
|
||||
/**
|
||||
* Launch programs from the OS.
|
||||
* @param args The arguments to launch.
|
||||
*/
|
||||
void launch_program(StringArray *args) {
|
||||
pid_t child = 0;
|
||||
|
||||
|
10
src/loop.c
10
src/loop.c
@ -22,10 +22,6 @@
|
||||
#include "console_line.h"
|
||||
#include "launch.h"
|
||||
|
||||
/**
|
||||
* Add memory address to array to be cleaned up later.
|
||||
* @param data The data to be cleaned up on exit.
|
||||
*/
|
||||
void add_to_cleanup(void *data) {
|
||||
clean.array = realloc(clean.array, (clean.size + 1) * sizeof(void *));
|
||||
if (clean.array == NULL) {
|
||||
@ -35,9 +31,6 @@ void add_to_cleanup(void *data) {
|
||||
clean.array[clean.size++] = data;
|
||||
}
|
||||
|
||||
/**
|
||||
* Cleanup memory when exiting.
|
||||
*/
|
||||
void exit_cleanup() {
|
||||
for (size_t i = 0; i < clean.size; i++) {
|
||||
if (clean.array[i] != NULL) {
|
||||
@ -52,9 +45,6 @@ void exit_cleanup() {
|
||||
free_array_list(variables);
|
||||
}
|
||||
|
||||
/**
|
||||
* This is the loop that checks for user input and acts on it.
|
||||
*/
|
||||
void loop() {
|
||||
clean.size = 0;
|
||||
variables = create_array_list();
|
||||
|
Loading…
Reference in New Issue
Block a user