Skip to content

Commit

Permalink
fixed untracked files and small memory checking errors
Browse files Browse the repository at this point in the history
  • Loading branch information
palkh committed Jul 22, 2024
1 parent dd9b120 commit 06c2ca3
Show file tree
Hide file tree
Showing 17 changed files with 261 additions and 12 deletions.
Binary file removed bin/test
Binary file not shown.
Binary file modified cutils.a
Binary file not shown.
4 changes: 4 additions & 0 deletions cutils.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ int isdir (const char *d);
int pmkdir (const char *dir);
// move a file and create the dir if it doesn't exist
int mvsp(char* old_path,char* new_path);
// move a symlink
int mvlink(char* old_path,char* new_path);
// get the relative path between two paths
char* relpath(char* start,char* end);
// LIST file in a dir
char** ls(char* path);
// exec a shell command and return the output
Expand Down
4 changes: 2 additions & 2 deletions makefile
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ SOURCES = $(wildcard $(SRC_DIR)/*.c)
OBJECTS = $(SOURCES:$(SRC_DIR)/%.c=$(OBJ_DIR)/%.o)

# memory checking
MEMCHECK = 0
MEMCHECK = 1

# The default target
all: $(LIBRARY)
Expand All @@ -37,7 +37,7 @@ $(OBJ_DIR)/%.o: $(SRC_DIR)/%.c
$(CC) $(CFLAGS) -c -o $@ $< -D MEMCHECK=$(MEMCHECK)

check:
$(CC) $(CFLAGS) -o $(BIN_DIR)/test test.c $(LIBRARY).a
$(CC) $(CFLAGS) -o $(BIN_DIR)/test test.c $(LIBRARY).a -D MEMCHECK=$(MEMCHECK)
bin/test

clean:
Expand Down
Binary file removed obj/exec.o
Binary file not shown.
Binary file removed obj/file.o
Binary file not shown.
Binary file removed obj/mem.o
Binary file not shown.
Binary file removed obj/msg.o
Binary file not shown.
Binary file removed obj/string.o
Binary file not shown.
Binary file removed obj/system.o
Binary file not shown.
1 change: 1 addition & 0 deletions src/exec.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include <stdio.h>
#include <stdlib.h>

#include "../cutils.h"

char* exec(const char* cmd)
{
Expand Down
3 changes: 2 additions & 1 deletion src/file.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
#include "unistd.h"
#include "stdlib.h"
#include "stdio.h"

#include "../cutils.h"

long rdfile(const char* filePath,char** buffer)
{
(*buffer) = 0;
Expand Down
20 changes: 13 additions & 7 deletions src/mem.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
#include "../cutils.h"

#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <malloc.h>

#define MEMCHECK 0

#include "../cutils.h"



#define MAX_PTRS 4096

typedef struct {
Expand Down Expand Up @@ -74,7 +80,7 @@ void* dbg_malloc(size_t size, char* file, int line)
dbg(4, "dbg_malloc: %s:%d - %zu bytes", file, line, size);
void* ptr = malloc(size);
if (ptr == NULL) {
dbg(1, "dbg_malloc: malloc failed");
msg(ERROR, "\tmalloc failed");
}
log_ptr(ptr, file, line);
return ptr;
Expand All @@ -85,7 +91,7 @@ void* dbg_calloc(size_t nmemb, size_t size, char* file, int line)
dbg(4, "dbg_calloc: %s:%d - %zu bytes", file, line, nmemb * size);
void* ptr = calloc(nmemb, size);
if (ptr == NULL) {
dbg(1, "dbg_calloc: calloc failed");
msg(ERROR, "\tcalloc failed");
}
log_ptr(ptr, file, line);
return ptr;
Expand All @@ -96,7 +102,7 @@ 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);
void* newptr = realloc(ptr, size);
if (newptr == NULL) {
dbg(1, "dbg_realloc: realloc failed");
msg(ERROR, "\trealloc failed");
}
unlog_ptr(ptr, file, line);
log_ptr(newptr, file, line);
Expand All @@ -109,7 +115,7 @@ char* dbg_strdup(char* str, char* file, int line)
dbg(4, "dbg_strdup: %s:%d - %zu bytes", file, line, size);
char* newstr = strdup(str);
if (newstr == NULL) {
dbg(1, "dbg_strdup: strdup failed");
msg(ERROR, "\tstrdup failed");
}
log_ptr(newstr, file, line);
return newstr;
Expand All @@ -123,9 +129,9 @@ void dbg_free(void* ptr, char* file, int line)
char* pos = find_dfree((char*) ptr);
if (pos != NULL)
{
dbg(1, "dbg_free: trying to free already freed at %s pointer --> %p", pos, ptr);
msg(WARNING, "\ttrying to free %p already freed at %s ",ptr, pos);
} else {
dbg(1, "dbg_free: trying to free unallocated pointer --> %p", ptr);
msg(WARNING, "\ttrying to free unallocated pointer --> %p", ptr);
}
}
free(ptr);
Expand Down
1 change: 1 addition & 0 deletions src/remove.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#include <ftw.h>
#include <unistd.h>

#include "../cutils.h"

// Callback function used by nftw to unlink files and directories
/*
Expand Down
2 changes: 2 additions & 0 deletions src/string.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#include "stdlib.h"
#include "string.h"

#include "../cutils.h"

unsigned int splita (char* string,char delim,char*** dest) {
unsigned int count = 0;
unsigned int alloced = 16 * sizeof(char*);
Expand Down
88 changes: 88 additions & 0 deletions src/system.c
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,94 @@ int pmkdir (const char *dir)

}

char* relpath(char* start,char* end) {
printf("Start: %p\n",start);
printf("End: %p\n",end);
// get the relative path between old_path and link
// first get the common prefix
int i = 0;
while (start[i] == end[i]) i++;
// then get the path from old_path to the common prefix
char* part_path = start+i;
// count the number of '/' in part_path
dbg(3,"Part path: %s\n",part_path);
// sanitize by removing the last '/'
if (part_path[strlen(part_path)-1] == '/') part_path[strlen(part_path)-1] = '\0';

int count = 0;
for (int j = 0; j < strlen(part_path); j++) {
if (part_path[j] == '/') count++;
}
// create the relative path
char rel_path[2048] = {0};
for (int j = 0; j < count; j++) {
strncat(rel_path,"../",4);
}
// add the remaining part of the link
strncat(rel_path,end+i,strlen(end)-i);
//
return strdup(rel_path);

}

int mvlink(char* old_path,char* new_path)
{
dbg(3,"Moving link %s to %s\n",old_path,new_path);

// read the link in old_path
char link[2048]; // 2048 is the max length of a path
ssize_t len = readlink(old_path, link, sizeof(link)-1);
// check for overflow
if (len == -1) {
msg(ERROR,"Error reading link\n");
return -1;
}

link[len] = '\0';

char* rel_path = relpath(old_path,link);


dbg(3,"%s -> %s\n",old_path,link);
dbg(3,"Relative path: %s\n",rel_path);

//get parent dir of new_path
char* parent_path = calloc(strlen(new_path)+1,sizeof(char));
strncpy(parent_path,new_path,strrchr(new_path, '/')-new_path);


char* new_link = calloc(strlen(parent_path)+strlen(rel_path)+1,sizeof(char));
strncpy(new_link,parent_path,strlen(parent_path));
// add the separator
if (new_link[strlen(new_link)-1] != '/') strncat(new_link,"/",1);
strncat(new_link,rel_path,strlen(rel_path));


dbg(3,"Getting absolute path of %s\n",new_link);
// allocate memory for the absolute path
char* new_link_abs = realpath(new_link,NULL);

if (new_link_abs == NULL) {
msg(ERROR,"Error getting absolute path\n");
perror("realpath");
return -1;
}

dbg(3,"%s -> %s\n",new_path,new_link_abs);
if (symlink(new_link_abs,new_path) != 0) {
msg(ERROR,"Error creating link\n");
return -1;
}

free(rel_path);
free(new_link);
free(new_link_abs);
free(parent_path);


return 0;
}


int mvsp(char* old_path,char* new_path)
{
Expand Down
Loading

0 comments on commit 06c2ca3

Please sign in to comment.