WIP: NOT WORKING: read_file()

This commit is contained in:
Adrien Bourmault 2023-02-23 21:54:22 +01:00
parent df294bd780
commit 0396f8039b
No known key found for this signature in database
GPG Key ID: 6EB408FE0ACEC664
4 changed files with 23 additions and 10 deletions

View File

@ -42,7 +42,7 @@ NTHREADS= $(shell nproc)
CC=gcc CC=gcc
WARNINGS= -Wall WARNINGS= -Wall
DEBUG= -ggdb -fno-omit-frame-pointer -fdiagnostics-color=always DEBUG= -ggdb -fno-omit-frame-pointer -fdiagnostics-color=always
OPTIMIZE= -O2 OPTIMIZE= -O3
INCLUDE= $(shell pkg-config --cflags glib-2.0 libxml-2.0 gtk4) INCLUDE= $(shell pkg-config --cflags glib-2.0 libxml-2.0 gtk4)
LIBS= $(shell pkg-config --libs glib-2.0 libxml-2.0 gtk4) -lGL -lGLU -lm -lepoxy -lX11 -lGLEW LIBS= $(shell pkg-config --libs glib-2.0 libxml-2.0 gtk4) -lGL -lGLU -lm -lepoxy -lX11 -lGLEW
@ -121,6 +121,12 @@ run: build_system
@$(BINDIR)/gem-graph-client @$(BINDIR)/gem-graph-client
@echo -e ${CL2}[$@] ${CL}done.${CL3} @echo -e ${CL2}[$@] ${CL}done.${CL3}
debug: build_system
@echo -e ${CL2}[$@] ${CL}executing...${CL3}
@gdb $(BINDIR)/gem-graph-client
@echo -e ${CL2}[$@] ${CL}done.${CL3}
all: build_system all: build_system
@echo -e ${CL2}[$@] ${CL}done.${CL3} @echo -e ${CL2}[$@] ${CL}done.${CL3}

View File

@ -49,17 +49,19 @@ enum
*/ */
static inline bool read_file(char *filename, char **contents) static inline bool read_file(char *filename, char **contents)
{ {
int fd = open(filename, O_RDONLY); int fd;
int filesize;
fd = open(filename, O_RDONLY);
if(fd < 0) { if(fd < 0) {
printf("Couldn't read file: %s\n",filename); printf("Couldn't read file: %s\n",filename);
return false; return false;
} }
int filesize = lseek(fd, 0, SEEK_END) +1 ; filesize = lseek(fd, 0, SEEK_END) +1 ;
*contents = malloc(sizeof(char) * filesize); *contents = calloc(filesize, sizeof(char));
if (errno) { if (contents == NULL) {
perror("Not enough memory to allocate file"); perror("Not enough memory to allocate file");
return false; return false;
} }
@ -67,7 +69,7 @@ static inline bool read_file(char *filename, char **contents)
lseek(fd, 0, SEEK_SET); lseek(fd, 0, SEEK_SET);
read(fd,*contents,filesize); read(fd,*contents,filesize);
*contents[filesize-1]='\0'; *(contents+filesize-1)='\0';
close(fd); close(fd);

View File

@ -10,6 +10,7 @@
"gcc-toolchain" "gcc-toolchain"
"pkg-config" "pkg-config"
"findutils" "findutils"
"gdb"
"make" "make"
"gtk" "gtk"
"libxml2" "libxml2"

View File

@ -46,7 +46,7 @@
/* /*
* Dynamic array of ptr to dynamically allocated gl_area_entry * Dynamic array of ptr to dynamically allocated gl_area_entry
*/ */
static struct gl_area_entry **gl_area_array = NULL; struct gl_area_entry **gl_area_array = NULL;
/* -------------------------------------------------------------------------- */ /* -------------------------------------------------------------------------- */
@ -495,7 +495,8 @@ bool graphics_init(void *gl_area)
gl_area_array = calloc(2, sizeof(struct gl_area_entry *)); gl_area_array = calloc(2, sizeof(struct gl_area_entry *));
// check if an error occured during allocation // check if an error occured during allocation
if (errno) { if (gl_area_array == NULL) {
printf("gl_area_array : %p", gl_area_array);
perror("Not enough memory to allocate gl_area_array"); perror("Not enough memory to allocate gl_area_array");
return false; return false;
} }
@ -505,16 +506,19 @@ bool graphics_init(void *gl_area)
gl_area_array = realloc(gl_area_array, gl_area_array = realloc(gl_area_array,
(array_size + 1) (array_size + 1)
* sizeof(struct gl_area_entry *)); * sizeof(struct gl_area_entry *));
if (errno) { if (gl_area_array == NULL) {
perror("Not enough memory to allocate gl_area_array"); perror("Not enough memory to allocate gl_area_array");
return false; return false;
} }
explicit_bzero(gl_area_array + array_size * sizeof(struct gl_area_entry *),
sizeof(struct gl_area_entry *));
} }
// Alloc new entry // Alloc new entry
gl_area_array[array_size] = calloc(1, sizeof(struct gl_area_entry)); gl_area_array[array_size] = calloc(1, sizeof(struct gl_area_entry));
if (errno) { if (gl_area_array[array_size] == NULL) {
perror("Not enough memory to allocate gl_area_array entry"); perror("Not enough memory to allocate gl_area_array entry");
return false; return false;
} }