Skip to content

Commit

Permalink
Merge pull request #8 from AleksArt000/main
Browse files Browse the repository at this point in the history
some changes
  • Loading branch information
AleksArt000 authored Dec 25, 2024
2 parents 1e16e2f + 6a88450 commit 0d4e262
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 41 deletions.
Binary file modified cutils.a
Binary file not shown.
9 changes: 5 additions & 4 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
Expand Down
19 changes: 7 additions & 12 deletions src/mem.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
Expand All @@ -7,6 +8,8 @@

#include "../cutils.h"



#define MAX_PTRS 8192

typedef struct {
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -142,3 +136,4 @@ void dbg_free(void* ptr, char* file, int line)
}
free(ptr);
}

9 changes: 3 additions & 6 deletions src/string.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,19 @@ 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;

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;
Expand Down
7 changes: 3 additions & 4 deletions src/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -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++;
}

Expand Down Expand Up @@ -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));

Expand Down Expand Up @@ -291,3 +289,4 @@ int mvsp(char* old_path,char* new_path)

return 0;
}

23 changes: 8 additions & 15 deletions test.c
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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);

}

Expand All @@ -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() {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);

Expand Down Expand Up @@ -382,8 +377,6 @@ void test_mvlink() {
return;
}



void test_dbg_malloc()
{
char* str = dbg_malloc(10, __FILE__, __LINE__);
Expand Down

0 comments on commit 0d4e262

Please sign in to comment.