diff --git a/CMakeLists.txt b/CMakeLists.txt index 93a8881..9eb0b9f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,8 +3,7 @@ cmake_minimum_required(VERSION 3.25) # Setting a variable to use for both the project name and the resulting binary executable set(REPO timer) project(${REPO}) -set(PROJECT_NAME ${REPO}) - +set(PROJECT_NAME ${REPO}) # Set the C standard to C99 @@ -14,7 +13,6 @@ set(CMAKE_C_STANDARD 99) set(CPP_FLAGS "-Wall -Wextra -pedantic") - # Add the src directory to the include path include_directories(${PROJECT_SOURCE_DIR}/include) @@ -25,24 +23,22 @@ file(GLOB SOURCES ${PROJECT_SOURCE_DIR}/src/*.c) add_executable(${PROJECT_NAME} ${PROJECT_SOURCE_DIR}/driver.c ${SOURCES}) - # Set the output directory to the build/bin directory set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/build/bin) set_target_properties(${PROJECT_NAME} - PROPERTIES - COMPILE_FLAGS ${CPP_FLAGS} - RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/build/bin -) + PROPERTIES + COMPILE_FLAGS ${CPP_FLAGS} + RUNTIME_OUTPUT_DIRECTORY ${PROJECT_SOURCE_DIR}/build/bin + ) # Set the object directory to the build/obj directory set_property( - TARGET ${PROJECT_NAME} - PROPERTY - CMAKE_OBJECT_PATH_PREFIX ${PROJECT_SOURCE_DIR}/build/obj/ + TARGET ${PROJECT_NAME} + PROPERTY + CMAKE_OBJECT_PATH_PREFIX ${PROJECT_SOURCE_DIR}/build/obj/ ) - # Set the cmake binary directory to the build/cmake directory set(CMAKE_BINARY_DIR ${PROJECT_SOURCE_DIR}/build/cmake) @@ -50,7 +46,6 @@ set(CMAKE_BINARY_DIR ${PROJECT_SOURCE_DIR}/build/cmake) include_directories(${PROJECT_SOURCE_DIR}/build/cmake) - # Add any other dependencies here, such as libraries or additional build options target_link_libraries(${PROJECT_NAME} PRIVATE ao) # -lao diff --git a/driver.c b/driver.c index d0b07ae..f086bcf 100644 --- a/driver.c +++ b/driver.c @@ -8,16 +8,16 @@ volatile bool allow_input = false; -void* input_thread(); +void *input_thread(void *arg); -int main (int argc, char** argv) { +int main(int argc, char **argv) { // used to capture user input prior to timer going off pthread_t input_thread_id; pthread_create(&input_thread_id, NULL, input_thread, NULL); if (argc == 1 || argc > 3) { - FAULT: + FAULT: display_help(); return 1; } @@ -28,12 +28,14 @@ int main (int argc, char** argv) { goto FAULT; } - char* alarm = "/usr/local/share/timer/sound/alarm.mp3"; - AUDIO* a = audio_init(alarm); - if(!a) return 1; + char *alarm = "/usr/local/share/timer/sound/alarm.mp3"; // install + // char *alarm = "sound/alarm.mp3"; // debugging + + AUDIO *a = audio_init(alarm); + if (!a) return 1; int t = timer(argv); - if(t) return 1; + if (t) return 1; play_alarm(a); pthread_join(input_thread_id, NULL); @@ -44,7 +46,9 @@ int main (int argc, char** argv) { return 0; } -void* input_thread() { +void *input_thread(void *arg) { + (void) arg; // to avoid unused variable warning lol + int c; while (!allow_input) { c = getchar(); @@ -56,10 +60,8 @@ void* input_thread() { return NULL; } - /* TODO: # dnf install mpg123-devel # dnf install libao-devel */ - diff --git a/include/audio.h b/include/audio.h index 5517842..6eb755a 100644 --- a/include/audio.h +++ b/include/audio.h @@ -5,18 +5,24 @@ #include typedef struct AUDIO { - mpg123_handle* mh; - ao_device* dev; - ao_sample_format format; - int err; + mpg123_handle *mh; + ao_device *dev; + ao_sample_format format; + int err; } AUDIO; -AUDIO* audio_init (char* file); -void init_handle (AUDIO* a); -void init_err (AUDIO* a, char* mp3); -void init_format (AUDIO* a); -void init_dev (AUDIO* a); -void play_alarm (AUDIO* a); -void audio_clean (AUDIO* a); +AUDIO *audio_init(char *file); + +void init_handle(AUDIO *a); + +void init_err(AUDIO *a, char *mp3); + +void init_format(AUDIO *a); + +void init_dev(AUDIO *a); + +void play_alarm(AUDIO *a); + +void audio_clean(AUDIO *a); #endif diff --git a/include/general.h b/include/general.h index 2dfddde..4e36448 100644 --- a/include/general.h +++ b/include/general.h @@ -3,16 +3,21 @@ #define CYAN "\033[1m\033[36m" #define CLEAR "\033[0m" - -void display_help (); -int check_arguments (int argc, char** argv); -void clear_line(); + +void display_help(void); + +int check_arguments(int argc, char **argv); + +void clear_line(void); #undef is_second -int is_second(char* s); -int is_minute(char* s); -int is_hour(char* s); -int is_format(char* s); +int is_second(char *s); + +int is_minute(char *s); + +int is_hour(char *s); + +int is_format(char *s); #endif diff --git a/include/keyboard.h b/include/keyboard.h index 71c2cff..1ae15ce 100644 --- a/include/keyboard.h +++ b/include/keyboard.h @@ -2,6 +2,6 @@ #define TIMER_KEYBOARD_INCLUDED // Function to check if a key has been pressed -int kbhit(); +int kbhit(void); #endif diff --git a/include/leak_detector_c.h b/include/leak_detector_c.h index 0cb208d..22fb424 100644 --- a/include/leak_detector_c.h +++ b/include/leak_detector_c.h @@ -2,37 +2,42 @@ #ifndef LEAK_DETECTOR_C_H #define LEAK_DETECTOR_C_H -#define FILE_NAME_LENGTH 256 -#define OUTPUT_FILE "leak_info.txt" -#define malloc(size) xmalloc (size, __FILE__, __LINE__) -#define calloc(elements, size) xcalloc (elements, size, __FILE__, __LINE__) -#define free(mem_ref) xfree(mem_ref) - -struct _MEM_INFO -{ - void *address; - unsigned int size; - char file_name[FILE_NAME_LENGTH]; - unsigned int line; +#define FILE_NAME_LENGTH 256 +#define OUTPUT_FILE "leak_info.txt" +#define malloc(size) xmalloc (size, __FILE__, __LINE__) +#define calloc(elements, size) xcalloc (elements, size, __FILE__, __LINE__) +#define free(mem_ref) xfree(mem_ref) + +struct _MEM_INFO { + void *address; + unsigned int size; + char file_name[FILE_NAME_LENGTH]; + unsigned int line; }; typedef struct _MEM_INFO MEM_INFO; struct _MEM_LEAK { - MEM_INFO mem_info; - struct _MEM_LEAK * next; + MEM_INFO mem_info; + struct _MEM_LEAK *next; }; typedef struct _MEM_LEAK MEM_LEAK; void add(MEM_INFO alloc_info); + void erase(unsigned pos); + void clear(void); -void * xmalloc(unsigned int size, const char * file, unsigned int line); -void * xcalloc(unsigned int elements, unsigned int size, const char * file, unsigned int line); -void xfree(void * mem_ref); +void *xmalloc(unsigned int size, const char *file, unsigned int line); + +void *xcalloc(unsigned int elements, unsigned int size, const char *file, unsigned int line); + +void xfree(void *mem_ref); + +void add_mem_info(void *mem_ref, unsigned int size, const char *file, unsigned int line); + +void remove_mem_info(void *mem_ref); -void add_mem_info (void * mem_ref, unsigned int size, const char * file, unsigned int line); -void remove_mem_info (void * mem_ref); void report_mem_leak(void); #endif diff --git a/include/number.h b/include/number.h index 0ee202c..dddf708 100644 --- a/include/number.h +++ b/include/number.h @@ -1,6 +1,6 @@ #ifndef TIMER_NUMBER_INCLUDED #define TIMER_NUMBER_INCLUDED -int is_num(char* str); +int is_num(char *str); -#endif \ No newline at end of file +#endif diff --git a/include/strtonum.h b/include/strtonum.h index 55c0155..30b44f6 100644 --- a/include/strtonum.h +++ b/include/strtonum.h @@ -22,8 +22,8 @@ long long strtonum(const char *numstr, long long minval, long long maxval, const char **errstrp); -#define INVALID 1 -#define TOOSMALL 2 -#define TOOLARGE 3 +#define INVALID 1 +#define TOOSMALL 2 +#define TOOLARGE 3 -#endif \ No newline at end of file +#endif diff --git a/include/timer.h b/include/timer.h index 29eeffa..6d54adb 100644 --- a/include/timer.h +++ b/include/timer.h @@ -1,12 +1,13 @@ #ifndef TIMER_TIMER_INCLUDED #define TIMER_TIMER_INCLUDED -#define SECOND 1 +#define SECOND 1 #define MINUTE 2 -#define HOUR 3 +#define HOUR 3 #define FORMAT 4 -int timer (char** str); -int get_time_in_s(char* str, char* num); +int timer(char **str); -#endif \ No newline at end of file +int get_time_in_s(char *str, char *num); + +#endif diff --git a/install.sh b/install.sh index 0e89278..b656b37 100644 --- a/install.sh +++ b/install.sh @@ -1,25 +1,25 @@ #!/bin/bash # Generate Binary Executable -chmod +x run.sh -./run.sh +chmod +x run.sh +./run.sh # Detect the user's shell SHELL_CONFIG_FILE="" if [[ -n "$BASH_VERSION" ]]; then - SHELL_CONFIG_FILE=~/.bashrc + SHELL_CONFIG_FILE=~/.bashrc elif [[ -n "$ZSH_VERSION" ]]; then - SHELL_CONFIG_FILE=~/.zshrc + SHELL_CONFIG_FILE=~/.zshrc else - echo "Unsupported shell. Please add /usr/local/bin to your PATH manually." - exit 1 + echo "Unsupported shell. Please add /usr/local/bin to your PATH manually." + exit 1 fi # Check if /usr/local/bin is in PATH if [[ ":$PATH:" != *":/usr/local/bin:"* ]]; then - echo "Adding /usr/local/bin to PATH" - echo 'export PATH=$PATH:/usr/local/bin' >> "$SHELL_CONFIG_FILE" - source "$SHELL_CONFIG_FILE" + echo "Adding /usr/local/bin to PATH" + echo 'export PATH=$PATH:/usr/local/bin' >>"$SHELL_CONFIG_FILE" + source "$SHELL_CONFIG_FILE" fi # Move timer executable diff --git a/run.sh b/run.sh index 0d6bf5d..87d2f5e 100644 --- a/run.sh +++ b/run.sh @@ -2,39 +2,33 @@ project="timer" -if [ "$1" == "build" ] || [ "$1" == "make" ] -then +if [ "$1" == "build" ] || [ "$1" == "make" ]; then + cmake -B build/cmake/ -S . + make -C build/cmake/ +elif [ "$1" == "rebuild" ]; then + rm -rf build/bin/* build/cmake/* + cmake -B build/cmake/ -S . + make -C build/cmake/ +elif [ "$1" == "clean" ]; then + rm -rf build/ +elif [ "$1" == "help" ]; then + echo -e "Build project:\t ./run build" + echo -e "Rebuild project: ./run rebuild" + echo -e "Clean project:\t ./run clean" + echo -e "Run project:\t ./run" +else + if [ -e build/bin/$project ]; then + exec build/bin/$project "$@" + else + echo "No binary executable named \"$project\" found in the directory \"build/bin/\"." + echo "Building..." + cmake -B build/cmake/ -S . make -C build/cmake/ -elif [ "$1" == "rebuild" ] -then - rm -rf build/bin/* build/cmake/* - cmake -B build/cmake/ -S . - make -C build/cmake/ -elif [ "$1" == "clean" ] -then - rm -rf build/ -elif [ "$1" == "help" ] -then - echo -e "Build project:\t ./run build" - echo -e "Rebuild project: ./run rebuild" - echo -e "Clean project:\t ./run clean" - echo -e "Run project:\t ./run" -else - if [ -e build/bin/$project ] - then - exec build/bin/$project "$@" - else - echo "No binary executable named \"$project\" found in the directory \"build/bin/\"." - echo "Building..." - - cmake -B build/cmake/ -S . - make -C build/cmake/ - if [ -e build/bin/$project ] - then - exec build/bin/$project "$@" - else - echo "Project built, but no binary executable found. Verify your project names match between the shell script and CMakeLists.txt" - fi + if [ -e build/bin/$project ]; then + exec build/bin/$project "$@" + else + echo "Project built, but no binary executable found. Verify your project names match between the shell script and CMakeLists.txt" fi + fi fi diff --git a/src/audio.c b/src/audio.c index cd206f8..43970b5 100644 --- a/src/audio.c +++ b/src/audio.c @@ -3,52 +3,53 @@ #include "audio.h" #include "keyboard.h" -AUDIO* audio_init (char* file) { +AUDIO *audio_init(char *file) { - mpg123_init(); + mpg123_init(); ao_initialize(); - AUDIO* a = (AUDIO*) malloc(sizeof(AUDIO)); + AUDIO *a = (AUDIO *) malloc(sizeof(AUDIO)); init_handle(a); if (!a->mh) goto handle; init_err(a, file); - if(a->err) goto err; + if (a->err) goto err; init_format(a); init_dev(a); if (!a->dev) goto dev; - if(0) { - dev: - err: + if (0) { + dev: + + err: free(a->mh); - handle: + handle: return NULL; } return a; } -void init_handle (AUDIO* a) { +void init_handle(AUDIO *a) { - mpg123_handle* mh = mpg123_new(NULL, NULL); + mpg123_handle *mh = mpg123_new(NULL, NULL); if (mh == NULL) { fprintf(stderr, "Error initializing mpg123\n"); - a->mh = NULL;; + a->mh = NULL; } a->mh = mh; } -void init_err (AUDIO* a, char* mp3) { - - int err; +void init_err(AUDIO *a, char *mp3) { + + int err; - err = mpg123_open(a->mh, mp3); + err = mpg123_open(a->mh, mp3); if (err != MPG123_OK) { fprintf(stderr, "Cannot open file: %s\n", mpg123_strerror(a->mh)); a->err = 1; @@ -57,7 +58,7 @@ void init_err (AUDIO* a, char* mp3) { a->err = err; } -void init_format (AUDIO* a) { +void init_format(AUDIO *a) { int channels, encoding; long rate; @@ -73,8 +74,8 @@ void init_format (AUDIO* a) { a->format = format; } -void init_dev (AUDIO* a) { - ao_device* dev = ao_open_live(ao_default_driver_id(), &a->format, NULL); +void init_dev(AUDIO *a) { + ao_device *dev = ao_open_live(ao_default_driver_id(), &a->format, NULL); if (dev == NULL) { fprintf(stderr, "Error opening audio device\n"); a->dev = NULL; @@ -83,9 +84,9 @@ void init_dev (AUDIO* a) { a->dev = dev; } -void play_alarm (AUDIO* a) { +void play_alarm(AUDIO *a) { size_t buffer_size = mpg123_outblock(a->mh); - char* buffer = (char*) malloc(buffer_size); + char *buffer = (char *) malloc(buffer_size); do {// Play alarm until [ENTER] is used size_t done; @@ -104,10 +105,10 @@ void play_alarm (AUDIO* a) { free(buffer); } -void audio_clean (AUDIO* a) { - if(a->dev) +void audio_clean(AUDIO *a) { + if (a->dev) ao_close(a->dev); - if(a->mh) { + if (a->mh) { mpg123_close(a->mh); mpg123_delete(a->mh); } diff --git a/src/general.c b/src/general.c index 31b49d4..6ddcf13 100644 --- a/src/general.c +++ b/src/general.c @@ -5,44 +5,45 @@ #include "number.h" #include "strtonum.h" -void display_help () { +void display_help(void) { printf( - "General timer options:\n" - "\t--help\t\t\tDisplays this screen.\n" - "\t-s, --second\t\tInput second(s) before alarm rings.\n" - "\t-m, --minute\t\tInput minute(s) before alarm rings.\n" - "\t-h, --hour\t\tInput hour(s) before alarm rings.\n" - "\t-f, --format\t\tFor more specific timers.\n" - "\t\t\t\t\t\tFormat: hhmmss\n" - "\t\t\t\t\t\tExample: -f 031530\n" - "\t\t\t\t\t\t\t will run for 03 hours, 15 minutes, 30 seconds\n" + "General timer options:\n" + "\t--help\t\t\tDisplays this screen.\n" + "\t-s, --second\t\tInput second(s) before alarm rings.\n" + "\t-m, --minute\t\tInput minute(s) before alarm rings.\n" + "\t-h, --hour\t\tInput hour(s) before alarm rings.\n" + "\t-f, --format\t\tFor more specific timers.\n" + "\t\t\t\t│ Format: hhmmss\n" + "\t\t\t\t│ Example: -f 031530\n" + "\t\t\t\t└ Equates: 03 hours, 15 minutes, 30 seconds\n" ); } -int check_arguments(int argc, char** argv) { +int check_arguments(int argc, char **argv) { // TODO: CLEAN THIS UP! - if( - strcmp(argv[1], "--help") != 0 && - !is_second(argv[1]) && - !is_minute(argv[1]) && - !is_hour(argv[1]) && - !is_format(argv[1]) - ) goto FAULT; + if ( + strcmp(argv[1], "--help") != 0 && + !is_second(argv[1]) && + !is_minute(argv[1]) && + !is_hour(argv[1]) && + !is_format(argv[1]) + ) + goto FAULT; if (strcmp(argv[1], "--help") == 0) return -1; if (argc < 3) return -1; // failed to input a number, only entered flag - const char* errstr; + const char *errstr; - if (!is_num(argv[2])) + if (!is_num(argv[2])) return -2; if (is_format(argv[1])) { // TODO: should allow the use of unlimited hours? // for now, it's left to double digit hours - if (strlen(argv[2]) != 6) + if (strlen(argv[2]) != 6) return -1; strtonum(argv[2], 1, 999999, &errstr); } else { @@ -51,51 +52,51 @@ int check_arguments(int argc, char** argv) { if (errstr) { printf("ERROR: STRTONUM; number is %s\n", errstr); - FAULT: + FAULT: return 1; } return 0; } -void clear_line() { +void clear_line(void) { printf("\r\033[K"); } -int is_second(char* s) { +int is_second(char *s) { if (strcmp(s, "-s") != 0 && strcmp(s, "--second") != 0 - ) { + ) { return 0; } return 1; } -int is_minute(char* s) { +int is_minute(char *s) { if (strcmp(s, "-m") != 0 && strcmp(s, "--minute") != 0 - ) { + ) { return 0; } return 1; } -int is_hour(char* s) { +int is_hour(char *s) { if (strcmp(s, "-h") != 0 && strcmp(s, "--hour") != 0 - ) { + ) { return 0; } return 1; } -int is_format(char* s) { +int is_format(char *s) { if (strcmp(s, "-f") != 0 && strcmp(s, "--format") != 0 - ) { + ) { return 0; } diff --git a/src/keyboard.c b/src/keyboard.c index 50fc3df..f935e6f 100644 --- a/src/keyboard.c +++ b/src/keyboard.c @@ -6,7 +6,7 @@ #include "keyboard.h" // Function to check if a key has been pressed -int kbhit() { +int kbhit(void) { struct termios oldt, newt; int ch; int oldf; @@ -23,10 +23,10 @@ int kbhit() { tcsetattr(STDIN_FILENO, TCSANOW, &oldt); fcntl(STDIN_FILENO, F_SETFL, oldf); - if(ch != EOF) { + if (ch != EOF) { ungetc(ch, stdin); return 1; } return 0; -} \ No newline at end of file +} diff --git a/src/leak_detector_c.c b/src/leak_detector_c.c index 8d0fac1..e8766dc 100644 --- a/src/leak_detector_c.c +++ b/src/leak_detector_c.c @@ -1,203 +1,157 @@ -#include -#include -#include -#include "leak_detector_c.h" - -#undef malloc -#undef calloc -#undef free - - -static MEM_LEAK * ptr_start = NULL; -static MEM_LEAK * ptr_next = NULL; - - -//void * xmalloc (unsigned int size, const char * file, unsigned int line); -//void * xcalloc (unsigned int elements, unsigned int size, const char * file, unsigned int line); -//void xfree(void * mem_ref); -/* - * adds allocated memory info. into the list - * - */ -void add(MEM_INFO alloc_info) -{ - - MEM_LEAK * mem_leak_info = NULL; - mem_leak_info = (MEM_LEAK *) malloc (sizeof(MEM_LEAK)); - mem_leak_info->mem_info.address = alloc_info.address; - mem_leak_info->mem_info.size = alloc_info.size; - strcpy(mem_leak_info->mem_info.file_name, alloc_info.file_name); - mem_leak_info->mem_info.line = alloc_info.line; - mem_leak_info->next = NULL; - - if (ptr_start == NULL) - { - ptr_start = mem_leak_info; - ptr_next = ptr_start; - } - else { - ptr_next->next = mem_leak_info; - ptr_next = ptr_next->next; - } +#include +#include +#include +#include "leak_detector_c.h" + +#undef malloc +#undef calloc +#undef free + +static MEM_LEAK *ptr_start = NULL; +static MEM_LEAK *ptr_next = NULL; + +// Adds allocated memory info. into the list +void add(MEM_INFO alloc_info) { + + //void * xmalloc (unsigned int size, const char * file, unsigned int line); + //void * xcalloc (unsigned int elements, unsigned int size, const char * file, unsigned int line); + //void xfree(void * mem_ref); + + MEM_LEAK *mem_leak_info = NULL; + mem_leak_info = (MEM_LEAK *) malloc(sizeof(MEM_LEAK)); + mem_leak_info->mem_info.address = alloc_info.address; + mem_leak_info->mem_info.size = alloc_info.size; + strcpy(mem_leak_info->mem_info.file_name, alloc_info.file_name); + mem_leak_info->mem_info.line = alloc_info.line; + mem_leak_info->next = NULL; + + if (ptr_start == NULL) { + ptr_start = mem_leak_info; + ptr_next = ptr_start; + } else { + ptr_next->next = mem_leak_info; + ptr_next = ptr_next->next; + } } -/* - * erases memory info. from the list - * - */ -void erase(unsigned pos) -{ - - unsigned index = 0; - MEM_LEAK * alloc_info, * temp; - - if(pos == 0) - { - MEM_LEAK * temp = ptr_start; - ptr_start = ptr_start->next; - free(temp); - } - else - { - for(index = 0, alloc_info = ptr_start; index < pos; - alloc_info = alloc_info->next, ++index) - { - if(pos == index + 1) - { - temp = alloc_info->next; - alloc_info->next = temp->next; - free(temp); - break; - } - } - } +// Erases memory info. from the list +void erase(unsigned pos) { + + unsigned index; + MEM_LEAK *alloc_info, *temp; + + if (pos == 0) { + MEM_LEAK *temp = ptr_start; + ptr_start = ptr_start->next; + free(temp); + } else { + for (index = 0, alloc_info = ptr_start; index < pos; + alloc_info = alloc_info->next, ++index) { + if (pos == index + 1) { + temp = alloc_info->next; + alloc_info->next = temp->next; + free(temp); + break; + } + } + } } -/* - * deletes all the elements from the list - */ -void clear() -{ - MEM_LEAK * temp = ptr_start; - MEM_LEAK * alloc_info = ptr_start; - - while(alloc_info != NULL) - { - alloc_info = alloc_info->next; - free(temp); - temp = alloc_info; - } +// Deletes all the elements from the list +void clear(void) { + MEM_LEAK *temp = ptr_start; + MEM_LEAK *alloc_info = ptr_start; + + while (alloc_info != NULL) { + alloc_info = alloc_info->next; + free(temp); + temp = alloc_info; + } } -/* - * replacement of malloc - */ -void * xmalloc (unsigned int size, const char * file, unsigned int line) -{ - void * ptr = malloc (size); - if (ptr != NULL) - { - add_mem_info(ptr, size, file, line); - } - return ptr; +// Replacement of malloc +void *xmalloc(unsigned int size, const char *file, unsigned int line) { + void *ptr = malloc(size); + if (ptr != NULL) { + add_mem_info(ptr, size, file, line); + } + return ptr; } -/* - * replacement of calloc - */ -void * xcalloc (unsigned int elements, unsigned int size, const char * file, unsigned int line) -{ - unsigned total_size; - void * ptr = calloc(elements , size); - if(ptr != NULL) - { - total_size = elements * size; - add_mem_info (ptr, total_size, file, line); - } - return ptr; +// Replacement of calloc +void *xcalloc(unsigned int elements, unsigned int size, const char *file, unsigned int line) { + unsigned total_size; + void *ptr = calloc(elements, size); + if (ptr != NULL) { + total_size = elements * size; + add_mem_info(ptr, total_size, file, line); + } + return ptr; } -/* - * replacement of free - */ -void xfree(void * mem_ref) -{ - remove_mem_info(mem_ref); - free(mem_ref); +// Replacement of free +void xfree(void *mem_ref) { + remove_mem_info(mem_ref); + free(mem_ref); } -/* - * gets the allocated memory info and adds it to a list - * - */ -void add_mem_info (void * mem_ref, unsigned int size, const char * file, unsigned int line) -{ - MEM_INFO mem_alloc_info; - - /* fill up the structure with all info */ - memset( &mem_alloc_info, 0, sizeof ( mem_alloc_info ) ); - mem_alloc_info.address = mem_ref; - mem_alloc_info.size = size; - strncpy(mem_alloc_info.file_name, file, FILE_NAME_LENGTH); - mem_alloc_info.line = line; - - /* add the above info to a list */ - add(mem_alloc_info); +// Gets the allocated memory info and adds it to a list +void add_mem_info(void *mem_ref, unsigned int size, const char *file, unsigned int line) { + MEM_INFO mem_alloc_info; + + // fill up the structure with all info + memset(&mem_alloc_info, 0, sizeof(mem_alloc_info)); + mem_alloc_info.address = mem_ref; + mem_alloc_info.size = size; + strncpy(mem_alloc_info.file_name, file, FILE_NAME_LENGTH); + mem_alloc_info.line = line; + + // add the above info to a list + add(mem_alloc_info); } -/* - * if the allocated memory info is part of the list, removes it - * - */ -void remove_mem_info (void * mem_ref) -{ - unsigned short index; - MEM_LEAK * leak_info = ptr_start; - - /* check if allocate memory is in our list */ - for(index = 0; leak_info != NULL; ++index, leak_info = leak_info->next) - { - if ( leak_info->mem_info.address == mem_ref ) - { - erase ( index ); - break; - } - } +// If the allocated memory info is part of the list, removes it +void remove_mem_info(void *mem_ref) { + unsigned short index; + MEM_LEAK *leak_info = ptr_start; + + /* check if allocate memory is in our list */ + for (index = 0; leak_info != NULL; ++index, leak_info = leak_info->next) { + if (leak_info->mem_info.address == mem_ref) { + erase(index); + break; + } + } } -/* - * writes all info of the unallocated memory into a file - */ -void report_mem_leak(void) -{ - MEM_LEAK * leak_info; - - FILE * fp_write = fopen (OUTPUT_FILE, "wt"); - char info[1024]; - - if(fp_write != NULL) - { - sprintf(info, "%s\n", "Memory Leak Summary"); - fwrite(info, (strlen(info) + 1) , 1, fp_write); - sprintf(info, "%s\n", "-----------------------------------"); - fwrite(info, (strlen(info) + 1) , 1, fp_write); - - for(leak_info = ptr_start; leak_info != NULL; leak_info = leak_info->next) - { - sprintf(info, "address : %p\n", leak_info->mem_info.address); - fwrite(info, (strlen(info) + 1) , 1, fp_write); - sprintf(info, "size : %d bytes\n", leak_info->mem_info.size); - fwrite(info, (strlen(info) + 1) , 1, fp_write); - sprintf(info, "file : %s\n", leak_info->mem_info.file_name); - fwrite(info, (strlen(info) + 1) , 1, fp_write); - sprintf(info, "line : %d\n", leak_info->mem_info.line); - fwrite(info, (strlen(info) + 1) , 1, fp_write); - sprintf(info, "%s\n", "-----------------------------------"); - fwrite(info, (strlen(info) + 1) , 1, fp_write); - } - } - clear(); +// Writes all info of the unallocated memory into a file +void report_mem_leak(void) { + MEM_LEAK *leak_info; + + FILE *fp_write = fopen(OUTPUT_FILE, "wt"); + char info[1024]; + + if (fp_write != NULL) { + sprintf(info, "%s\n", "Memory Leak Summary"); + fwrite(info, (strlen(info) + 1), 1, fp_write); + sprintf(info, "%s\n", "-----------------------------------"); + fwrite(info, (strlen(info) + 1), 1, fp_write); + + for (leak_info = ptr_start; leak_info != NULL; leak_info = leak_info->next) { + sprintf(info, "address : %p\n", leak_info->mem_info.address); + fwrite(info, (strlen(info) + 1), 1, fp_write); + sprintf(info, "size : %d bytes\n", leak_info->mem_info.size); + fwrite(info, (strlen(info) + 1), 1, fp_write); + sprintf(info, "file : %s\n", leak_info->mem_info.file_name); + fwrite(info, (strlen(info) + 1), 1, fp_write); + sprintf(info, "line : %d\n", leak_info->mem_info.line); + fwrite(info, (strlen(info) + 1), 1, fp_write); + sprintf(info, "%s\n", "-----------------------------------"); + fwrite(info, (strlen(info) + 1), 1, fp_write); + } + } + clear(); } diff --git a/src/number.c b/src/number.c index a102b02..24fd727 100644 --- a/src/number.c +++ b/src/number.c @@ -2,14 +2,14 @@ #include "number.h" -int is_num(char* str) { +int is_num(char *str) { int l = strlen(str); - char* p = str; + char *p = str; for (int i = 0; i < l; i++, p++) { if (*p < '0' || *p > '9') return 0; } return 1; -} \ No newline at end of file +} diff --git a/src/strtonum.c b/src/strtonum.c index d53688f..968c0c7 100644 --- a/src/strtonum.c +++ b/src/strtonum.c @@ -31,10 +31,10 @@ long long strtonum(const char *numstr, long long minval, long long maxval, const const char *errstr; int err; } ev[4] = { - { NULL, 0 }, - { "invalid", EINVAL }, - { "too small", ERANGE }, - { "too large", ERANGE }, + {NULL, 0}, + {"invalid", EINVAL}, + {"too small", ERANGE}, + {"too large", ERANGE}, }; ev[0].err = errno; @@ -58,4 +58,4 @@ long long strtonum(const char *numstr, long long minval, long long maxval, const ll = 0; return (ll); -} \ No newline at end of file +} diff --git a/src/timer.c b/src/timer.c index f74f067..2238147 100644 --- a/src/timer.c +++ b/src/timer.c @@ -1,5 +1,4 @@ #include -#include #include #include #include @@ -8,7 +7,7 @@ #include "timer.h" #include "strtonum.h" -int timer (char** str) { +int timer(char **str) { int duration = get_time_in_s(str[1], str[2]); @@ -27,7 +26,7 @@ int timer (char** str) { // Delay for one second sleep(1); - } + } clear_line(); printf("Time remaining: 00:00:00"); @@ -37,29 +36,37 @@ int timer (char** str) { return 0; } -int get_time_in_s(char* str, char* num) { +int get_time_in_s(char *str, char *num) { int duration; - const char* errstr; - char h[3], m[3], s[3]; + const char *errstr; + char h[3], m[3], s[3]; - if(is_format(str)) { + if (is_format(str)) { goto format; } duration = (int) strtonum(num, 0, 0, &errstr); - if(is_minute(str)) + if (is_minute(str)) duration *= 60; - if(is_hour(str)) + if (is_hour(str)) duration *= 3600; - if(0) { - format: - h[0]=num[0];h[1]=num[1];h[2]='\0'; - m[0]=num[2];m[1]=num[3];m[2]='\0'; - s[0]=num[4];s[1]=num[5];s[2]='\0'; + if (0) { + format: + h[0] = num[0]; + h[1] = num[1]; + h[2] = '\0'; + + m[0] = num[2]; + m[1] = num[3]; + m[2] = '\0'; + + s[0] = num[4]; + s[1] = num[5]; + s[2] = '\0'; - duration = (int) strtonum(h, 0, 99, &errstr) * 3600; + duration = (int) strtonum(h, 0, 99, &errstr) * 3600; duration += (int) strtonum(m, 0, 99, &errstr) * 60; duration += (int) strtonum(s, 0, 99, &errstr); } diff --git a/uninstall.sh b/uninstall.sh index b652f70..aac83fe 100644 --- a/uninstall.sh +++ b/uninstall.sh @@ -2,4 +2,4 @@ sudo rm -rf /usr/local/bin/timer # Remove sound files -sudo rm -rf /usr/local/share/timer/ \ No newline at end of file +sudo rm -rf /usr/local/share/timer/