From cc9d6f352698e9f1eb59e00a6068feef272c0006 Mon Sep 17 00:00:00 2001 From: "Begerad, Stefan" Date: Fri, 20 Jan 2023 11:44:46 +0100 Subject: [PATCH] feat: file2char --- file2char/main.c | 119 ++++++++++++++++++++++++++++++++++++++++++++ file2char/makefile | 19 +++++++ file2char/readme.md | 23 +++++++++ 3 files changed, 161 insertions(+) create mode 100644 file2char/main.c create mode 100644 file2char/makefile create mode 100644 file2char/readme.md diff --git a/file2char/main.c b/file2char/main.c new file mode 100644 index 0000000..8f7bb31 --- /dev/null +++ b/file2char/main.c @@ -0,0 +1,119 @@ +/*to use exit*/ +#include +/*to use *print**/ +#include + +/* + * load_file reads the file identified by 'path' into a character buffer + * pointed at by 'buf'. + * On success, the size of the file is returned. + * On failure, exit(EXIT_FAILURE) is returned and. + * + * WARNING: load_file malloc()s memory to '*buf' which must be freed by + * the caller. + */ +int load_file(char const* path, char **buf){ + /*declarations*/ + FILE *fp; + long lSize; + + /*fopen() + *filename + *mode + *return: set errno value in error case + */ + fp=fopen(path,"r"); + + if(fp){ + fprintf(stdout,"file opened.\n"); + + /*set offset to file end*/ + /*fseek() + *stream + *offset + *origin + */ + fseek(fp,0L,SEEK_END); + if (ferror(fp)){ + printf ("Error seeking file\n"); + } + + /*get file size*/ + lSize = ftell( fp ); + fprintf(stdout,"lSize: %ld.\n",lSize); + + /*set offset to file start*/ + fseek(fp,0,SEEK_SET); + if (ferror(fp)){ + printf ("Error seeking file\n"); + } + + /*allocate memory for entire content + null termination*/ + *buf=(char *)malloc((lSize+1)*sizeof(char)); + if(!*buf){ + fclose(fp); + fputs("Error allocating memory",stderr); + exit(EXIT_FAILURE); + } + fprintf(stdout,"mem allocated.\n"); + + /*copy file into buffer*/ + /*fread() + *ptr + *size + *count + *stream + */ + if( 1!=fread(*buf,lSize,1,fp)){ + fclose(fp); + free(*buf); + fputs("Error reading entire file",stderr); + exit(EXIT_FAILURE); + } + fprintf(stdout,"file read.\n"); + + /*close file*/ + if(EOF==fclose(fp)){ + free(*buf); + exit(EXIT_FAILURE); + } + fprintf(stdout,"file closed.\n"); + + (*buf)[lSize] = '\0'; + fprintf(stdout,"mem null terminated.\n"); + + /*TODO Cleanup debugging!*/ + /* + for (int i = 0; i < lSize; i++) { + printf("buffer[%d] == %c\n", i, buffer[i]); + } + */ + printf("buffer = %s\n", *buf); + + }else{ + perror(path); + exit(EXIT_FAILURE); + } + /*done*/ + return lSize; +} + +int main(int argc, char** argv){ + /*declaration*/ + int fileSize; + char *buf; + + fprintf(stdout,"main() Started...\n"); + + if (argc != 2) { + fprintf(stderr, "Usage: %s \n", argv[0]); + exit(EXIT_FAILURE); + } + + + fileSize = load_file(argv[1], &buf); + free(buf); + fprintf(stdout,"main() fileSize: %d.\n",fileSize); + fprintf(stdout,"main() Done.\n"); + return 0; +} diff --git a/file2char/makefile b/file2char/makefile new file mode 100644 index 0000000..bcd60d7 --- /dev/null +++ b/file2char/makefile @@ -0,0 +1,19 @@ +#target: dependency_1 dependency_2 dependency_3 ... +# command +# +RM = /bin/rm -f +OBJ = main.o +EXE = main +CC = /usr/bin/gcc +CFLAGS = -Wall +# +all: $(EXE) +# +$(EXE): $(OBJ) + $(CC) $(CFLAGS) $(OBJ) -o $(EXE) +# +main.o: main.c + $(CC) -c main.c -o main.o +# +clean: + $(RM) $(OBJ) $(EXE) *~ diff --git a/file2char/readme.md b/file2char/readme.md new file mode 100644 index 0000000..ac2cb44 --- /dev/null +++ b/file2char/readme.md @@ -0,0 +1,23 @@ +* build + +``` +make clean all +``` + +* cleanup + +``` +make clean +``` + +* run + +``` +./main +``` + +* run Valgrind tool `Memcheck` + +``` +valgrind --leak-check=yes -s ./main +```