c84264bb398c51438bb5230fc6b4a2a75a5bd923
Stefan Schuermans factor reading file to sepr...

Stefan Schuermans authored 4 years ago

liblwptev/src/read_file.c  1) #include "cleaner.h"
Stefan Schuermans formatting

Stefan Schuermans authored 4 years ago

libuptev/src/read_file.c   2) #include "stringlist.h"
Stefan Schuermans factor reading file to sepr...

Stefan Schuermans authored 4 years ago

liblwptev/src/read_file.c  3) 
liblwptev/src/read_file.c  4) #include <fcntl.h>
liblwptev/src/read_file.c  5) #include <stdlib.h>
liblwptev/src/read_file.c  6) #include <string.h>
liblwptev/src/read_file.c  7) #include <sys/stat.h>
Stefan Schuermans formatting

Stefan Schuermans authored 4 years ago

libuptev/src/read_file.c   8) #include <sys/types.h>
Stefan Schuermans factor reading file to sepr...

Stefan Schuermans authored 4 years ago

liblwptev/src/read_file.c  9) #include <unistd.h>
liblwptev/src/read_file.c 10) 
Stefan Schuermans formatting

Stefan Schuermans authored 4 years ago

libuptev/src/read_file.c  11) char *uptev_read_file(char const *pathname, size_t *size) {
Stefan Schuermans factor reading file to sepr...

Stefan Schuermans authored 4 years ago

liblwptev/src/read_file.c 12)   /* it is not possible to get file size before, because this yields zero for
liblwptev/src/read_file.c 13)      files like /proc/self/cmdline */
liblwptev/src/read_file.c 14)   *size = 0;
liblwptev/src/read_file.c 15)   /* open file */
liblwptev/src/read_file.c 16)   int fd = open(pathname, O_RDONLY);
liblwptev/src/read_file.c 17)   if (fd == -1) {
liblwptev/src/read_file.c 18)     return NULL;
liblwptev/src/read_file.c 19)   }
liblwptev/src/read_file.c 20)   /* get initial buffer */
liblwptev/src/read_file.c 21)   size_t sz = 4096;
liblwptev/src/read_file.c 22)   char *data = malloc(sz);
Stefan Schuermans formatting

Stefan Schuermans authored 4 years ago

libuptev/src/read_file.c  23)   if (!data) {
Stefan Schuermans factor reading file to sepr...

Stefan Schuermans authored 4 years ago

liblwptev/src/read_file.c 24)     close(fd);
liblwptev/src/read_file.c 25)     return NULL;
liblwptev/src/read_file.c 26)   }
liblwptev/src/read_file.c 27)   /* read file contents - potentially iteratively */
liblwptev/src/read_file.c 28)   size_t pos = 0;
liblwptev/src/read_file.c 29)   while (1) {
liblwptev/src/read_file.c 30)     /* read file contents */
liblwptev/src/read_file.c 31)     ssize_t len = read(fd, data + pos, sz - pos);
liblwptev/src/read_file.c 32)     /* error -> cleanup and return failure */
liblwptev/src/read_file.c 33)     if (len < 0) {
liblwptev/src/read_file.c 34)       free(data);
liblwptev/src/read_file.c 35)       close(fd);
liblwptev/src/read_file.c 36)       return NULL;
liblwptev/src/read_file.c 37)     }
Stefan Schuermans formatting

Stefan Schuermans authored 4 years ago

libuptev/src/read_file.c  38)     if (len == 0) {
Stefan Schuermans factor reading file to sepr...

Stefan Schuermans authored 4 years ago

liblwptev/src/read_file.c 39)       /* end of file -> return data */
liblwptev/src/read_file.c 40)       *size = pos;
liblwptev/src/read_file.c 41)       return data;
liblwptev/src/read_file.c 42)     }
liblwptev/src/read_file.c 43)     /* data read -> add to buffer */
liblwptev/src/read_file.c 44)     pos += len;
liblwptev/src/read_file.c 45)     /* buffer full ? -> enlarge */
liblwptev/src/read_file.c 46)     if (pos >= sz) {
liblwptev/src/read_file.c 47)       sz *= 2;
liblwptev/src/read_file.c 48)       char *data2 = realloc(data, sz);
liblwptev/src/read_file.c 49)       /* out of memory ? -> cleanup and return failure */
Stefan Schuermans formatting

Stefan Schuermans authored 4 years ago

libuptev/src/read_file.c  50)       if (!data2) {