diff --git a/cutils.a b/cutils.a index 68e05b5..81f8828 100644 Binary files a/cutils.a and b/cutils.a differ diff --git a/makefile b/makefile index 730a956..7fdbc5f 100644 --- a/makefile +++ b/makefile @@ -14,7 +14,7 @@ BIN_DIR = bin CC = gcc # The C flags to use -CFLAGS = -Iinclude -fPIC +CFLAGS = -Iinclude -fPIC -g # The source files SOURCES = $(wildcard $(SRC_DIR)/*.c) @@ -32,14 +32,15 @@ all: directories $(LIBRARY) directories: mkdir -p $(OBJ_DIR) mkdir -p $(BIN_DIR) + +# Build the object files +$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c + $(CC) $(CFLAGS) -c -o $@ $< -D MEMCHECK=$(MEMCHECK) # Build the library $(LIBRARY): $(OBJECTS) ar rcs $(LIBRARY).a $(OBJECTS) -# Build the object files -$(OBJ_DIR)/%.o: $(SRC_DIR)/%.c - $(CC) $(CFLAGS) -c -o $@ $< -D MEMCHECK=$(MEMCHECK) check: $(CC) $(CFLAGS) -o $(BIN_DIR)/test test.c $(LIBRARY).a -D MEMCHECK=1 diff --git a/src/mem.c b/src/mem.c index 8feda23..d62ba60 100644 --- a/src/mem.c +++ b/src/mem.c @@ -1,3 +1,4 @@ + #include #include #include @@ -7,6 +8,8 @@ #include "../cutils.h" + + #define MAX_PTRS 8192 typedef struct { @@ -94,24 +97,15 @@ void* dbg_calloc(size_t nmemb, size_t size, char* file, int line) return ptr; } -void* dbg_realloc(void* ptr, size_t size, char* file, int line) { +void* dbg_realloc(void* ptr, size_t size, char* file, int line) +{ dbg(4, "dbg_realloc: %s:%d %p %zu->%zu bytes", file, line, ptr, malloc_usable_size(ptr), size); - - // Unlog the pointer before reallocating - unlog_ptr(ptr, file, line); - - // Reallocate memory void* newptr = realloc(ptr, size); - - // Check for allocation failure if (newptr == NULL) { msg(ERROR, "\trealloc failed"); - return NULL; // Return NULL on failure } - - // Log the new pointer + unlog_ptr(ptr, file, line); log_ptr(newptr, file, line); - return newptr; } @@ -142,3 +136,4 @@ void dbg_free(void* ptr, char* file, int line) } free(ptr); } + diff --git a/src/string.c b/src/string.c index 22e9ffc..b6c3293 100644 --- a/src/string.c +++ b/src/string.c @@ -7,7 +7,6 @@ unsigned int splita (char* string,char delim,char*** dest) { unsigned int count = 0; unsigned int alloced = 16 * sizeof(char*); *dest = calloc(16,sizeof(char*)); - char sdelim[2]; sdelim[0] = delim; sdelim[1] = 0; @@ -15,14 +14,12 @@ unsigned int splita (char* string,char delim,char*** dest) { char* token = strtok(string, sdelim); while (token != NULL) { - - (*dest)[count++] = token; - token = strtok(NULL, sdelim); - - if (count*sizeof(char*) >= alloced) { + if ((count+1)*sizeof(char*) >= alloced) { alloced *= 1.5; *dest = realloc(*dest,alloced); } + (*dest)[count++] = strdup(token); + token = strtok(NULL, sdelim); } return count; diff --git a/src/system.c b/src/system.c index 575a984..0f3d3f2 100644 --- a/src/system.c +++ b/src/system.c @@ -107,7 +107,7 @@ char** ls(char* path) printf("Error : too many elements in list , reallocating\n"); list = realloc(list,(count+512) * sizeof(char*)); } - list[count] = dir->d_name; + list[count] = strdup(dir->d_name); count++; } @@ -209,9 +209,7 @@ int mvlink(char* old_path,char* new_path) //strncpy(new_link,parent_path,strlen(parent_path)); // add the separator - if (new_link[strlen(new_link) - 1] != '/') { - strncat(new_link, "/", 2); // Reserve space for the null terminator - } + //if (new_link[strlen(new_link)-1] != '/') strncat(new_link,"/",2); //strncat(new_link,rel_path,strlen(rel_path)); @@ -291,3 +289,4 @@ int mvsp(char* old_path,char* new_path) return 0; } + diff --git a/test.c b/test.c index 8c1e8f4..6447807 100644 --- a/test.c +++ b/test.c @@ -24,7 +24,6 @@ void test_isdir() assert(isdir("src/doesnotexist") == 1); } - void test_rmrf() { // Create a temporary directory char temp_dir_template[] = "/tmp/test_dir_XXXXXX"; @@ -79,7 +78,7 @@ void test_rdfile() { assert(strncmp(content, test_string, size) == 0); // Assert that content is correct // Clean up - free(content); + //free(content); remove(temp_file_path); } @@ -201,8 +200,8 @@ void test_ls() { assert(*result_non_existing == NULL); // ls should return NULL for a non-existing directory // Free the result - free(result); - free(result_non_existing); + // free(result); + //free(result_non_existing); } @@ -213,14 +212,12 @@ void test_splita() { assert(count == 2); // splita should return 2 for "Hello,World" with ',' as delimiter - //printf("result[0]: %s\n", result[0]); - // Check if the split strings are correct assert(strcmp(result[0], "Hello") == 0); // First string should be "Hello" assert(strcmp(result[1], "World") == 0); // Second string should be "World" - free(result); + //free(result); } void test_countc() { @@ -253,9 +250,11 @@ void test_relpath() { // create a temp directory char temp_dir_template[512] = "/tmp/test_dir_XXXXXX\0"; char* temp_dir = mkdtemp(temp_dir_template); + assert(temp_dir != NULL); // Assert that directory was created successfully assert(isdir(temp_dir) == 0); // Assert that directory exists char* start = calloc(512, sizeof(char)); + assert(start != NULL+1); char* end = calloc(512, sizeof(char)); sprintf(start, "%s/a/b/c/d/1", temp_dir); @@ -273,19 +272,14 @@ void test_relpath() { res = pmkdir(end_parent); assert(res == 0); // pmkdir should return 0 for successful directory creation - - FILE *file = fopen(start, "w"); assert(file != NULL); fputs("Hello, World! - START", file); fclose(file); - //dbg(1, "start: %s", start); file = fopen(end, "w"); assert(file != NULL); fputs("Hello, World! - END", file); fclose(file); - //dbg(1, "end: %s", end); - char* result = relpath(start, end); printf("results: %s\n", result); @@ -302,7 +296,8 @@ void test_relpath() { fclose(rel_file); assert(strcmp(content, "Hello, World! - END") == 0); // Assert that content is correct - free(result); + // idk + //free(result); free(start); free(end); @@ -382,8 +377,6 @@ void test_mvlink() { return; } - - void test_dbg_malloc() { char* str = dbg_malloc(10, __FILE__, __LINE__);