6843f6bb047d1847f42ca3858f02b8b42f7ee5c1
Stefan Schuermans add copyright headers and l...

Stefan Schuermans authored 4 years ago

libuptev/src/read_file.c   1) /**
libuptev/src/read_file.c   2)  * UProcTrace: User-space Process Tracing
libuptev/src/read_file.c   3)  * Copyright 2020: Stefan Schuermans, Aachen, Germany <stefan@schuermans.info>
libuptev/src/read_file.c   4)  * Copyleft: GNU LESSER GENERAL PUBLIC LICENSE version 3 (see LICENSE)
libuptev/src/read_file.c   5)  */
libuptev/src/read_file.c   6) 
Stefan Schuermans factor reading file to sepr...

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

liblwptev/src/read_file.c  9) 
liblwptev/src/read_file.c 10) #include <fcntl.h>
liblwptev/src/read_file.c 11) #include <stdlib.h>
liblwptev/src/read_file.c 12) #include <string.h>
liblwptev/src/read_file.c 13) #include <sys/stat.h>
Stefan Schuermans formatting

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

libuptev/src/read_file.c  17) 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 18)   /* it is not possible to get file size before, because this yields zero for
liblwptev/src/read_file.c 19)      files like /proc/self/cmdline */
liblwptev/src/read_file.c 20)   *size = 0;
liblwptev/src/read_file.c 21)   /* open file */
liblwptev/src/read_file.c 22)   int fd = open(pathname, O_RDONLY);
liblwptev/src/read_file.c 23)   if (fd == -1) {
liblwptev/src/read_file.c 24)     return NULL;
liblwptev/src/read_file.c 25)   }
liblwptev/src/read_file.c 26)   /* get initial buffer */
liblwptev/src/read_file.c 27)   size_t sz = 4096;
liblwptev/src/read_file.c 28)   char *data = malloc(sz);
Stefan Schuermans formatting

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

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

Stefan Schuermans authored 4 years ago

libuptev/src/read_file.c  44)     if (len == 0) {
Stefan Schuermans fix read_file: close file o...

Stefan Schuermans authored 3 years ago

libuptev/src/read_file.c  45)       /* end of file -> close file and return data */
Stefan Schuermans factor reading file to sepr...

Stefan Schuermans authored 4 years ago

liblwptev/src/read_file.c 46)       *size = pos;
Stefan Schuermans fix read_file: close file o...

Stefan Schuermans authored 3 years ago

libuptev/src/read_file.c  47)       close(fd);
Stefan Schuermans factor reading file to sepr...

Stefan Schuermans authored 4 years ago

liblwptev/src/read_file.c 48)       return data;
liblwptev/src/read_file.c 49)     }
liblwptev/src/read_file.c 50)     /* data read -> add to buffer */
liblwptev/src/read_file.c 51)     pos += len;
liblwptev/src/read_file.c 52)     /* buffer full ? -> enlarge */
liblwptev/src/read_file.c 53)     if (pos >= sz) {
liblwptev/src/read_file.c 54)       sz *= 2;
liblwptev/src/read_file.c 55)       char *data2 = realloc(data, sz);
liblwptev/src/read_file.c 56)       /* out of memory ? -> cleanup and return failure */
Stefan Schuermans formatting

Stefan Schuermans authored 4 years ago

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