factor reading file to seprate file
Stefan Schuermans

Stefan Schuermans commited on 2020-05-17 19:05:35
Showing 4 changed files, with 75 additions and 57 deletions.

... ...
@@ -22,6 +22,8 @@ add_library(
22 22
   src/macros.h
23 23
   src/proc_begin.c
24 24
   src/proc_end.c
25
+  src/read_file.c
26
+  src/read_file.h
25 27
   src/stringlist.c
26 28
   src/stringlist.h
27 29
   src/symlink.c
... ...
@@ -0,0 +1,59 @@
1
+#include "stringlist.h"
2
+#include "cleaner.h"
3
+
4
+#include <fcntl.h>
5
+#include <stdlib.h>
6
+#include <string.h>
7
+#include <sys/types.h>
8
+#include <sys/stat.h>
9
+#include <unistd.h>
10
+
11
+char * lwptev_read_file(char const *pathname, size_t *size) {
12
+  /* it is not possible to get file size before, because this yields zero for
13
+     files like /proc/self/cmdline */
14
+  *size = 0;
15
+  /* open file */
16
+  int fd = open(pathname, O_RDONLY);
17
+  if (fd == -1) {
18
+    return NULL;
19
+  }
20
+  /* get initial buffer */
21
+  size_t sz = 4096;
22
+  char *data = malloc(sz);
23
+  if (! data) {
24
+    close(fd);
25
+    return NULL;
26
+  }
27
+  /* read file contents - potentially iteratively */
28
+  size_t pos = 0;
29
+  while (1) {
30
+    /* read file contents */
31
+    ssize_t len = read(fd, data + pos, sz - pos);
32
+    /* error -> cleanup and return failure */
33
+    if (len < 0) {
34
+      free(data);
35
+      close(fd);
36
+      return NULL;
37
+    }
38
+    if (len == 0 ) {
39
+      /* end of file -> return data */
40
+      *size = pos;
41
+      return data;
42
+    }
43
+    /* data read -> add to buffer */
44
+    pos += len;
45
+    /* buffer full ? -> enlarge */
46
+    if (pos >= sz) {
47
+      sz *= 2;
48
+      char *data2 = realloc(data, sz);
49
+      /* out of memory ? -> cleanup and return failure */
50
+      if (! data2) {
51
+        free(data);
52
+        close(fd);
53
+        return NULL;
54
+      }
55
+      /* use new buffer */
56
+      data = data2;
57
+    }
58
+  }
59
+}
... ...
@@ -0,0 +1,12 @@
1
+#ifndef LWPTEV_READ_FILE_H
2
+#define LWPTEV_READ_FILE_H
3
+
4
+/**
5
+ * @brief read file contents
6
+ * @param[in] pathname path to file containing zero-terminated string list
7
+ * @param[out] *size size of file contents
8
+ * @return pointer to malloc-ed file contents or NULL
9
+ */
10
+char * lwptev_read_file(char const *pathname, size_t *size);
11
+
12
+#endif /* #ifndef LWPTEV_READ_FILE_H */
... ...
@@ -1,5 +1,6 @@
1 1
 #include "stringlist.h"
2 2
 #include "cleaner.h"
3
+#include "read_file.h"
3 4
 
4 5
 #include <fcntl.h>
5 6
 #include <stdlib.h>
... ...
@@ -8,62 +9,6 @@
8 9
 #include <sys/stat.h>
9 10
 #include <unistd.h>
10 11
 
11
-/**
12
- * @brief read file contents
13
- * @param[in] pathname path to file containing zero-terminated string list
14
- * @param[out] *size size of file contents
15
- * @return pointer to malloc-ed file contents or NULL
16
- */
17
-static char * lwptev_stringlist_read_file(char const *pathname, size_t *size) {
18
-  /* it is not possible to get file size before, because this yields zero for
19
-     files like /proc/self/cmdline */
20
-  *size = 0;
21
-  /* open file */
22
-  int fd = open(pathname, O_RDONLY);
23
-  if (fd == -1) {
24
-    return NULL;
25
-  }
26
-  /* get initial buffer */
27
-  size_t sz = 4096;
28
-  char *data = malloc(sz);
29
-  if (! data) {
30
-    close(fd);
31
-    return NULL;
32
-  }
33
-  /* read file contents - potentially iteratively */
34
-  size_t pos = 0;
35
-  while (1) {
36
-    /* read file contents */
37
-    ssize_t len = read(fd, data + pos, sz - pos);
38
-    /* error -> cleanup and return failure */
39
-    if (len < 0) {
40
-      free(data);
41
-      close(fd);
42
-      return NULL;
43
-    }
44
-    if (len == 0 ) {
45
-      /* end of file -> return data */
46
-      *size = pos;
47
-      return data;
48
-    }
49
-    /* data read -> add to buffer */
50
-    pos += len;
51
-    /* buffer full ? -> enlarge */
52
-    if (pos >= sz) {
53
-      sz *= 2;
54
-      char *data2 = realloc(data, sz);
55
-      /* out of memory ? -> cleanup and return failure */
56
-      if (! data2) {
57
-        free(data);
58
-        close(fd);
59
-        return NULL;
60
-      }
61
-      /* use new buffer */
62
-      data = data2;
63
-    }
64
-  }
65
-}
66
-
67 12
 /**
68 13
  * @brief make array with pointers to strings
69 14
  * @param[in] data pointer to string list
... ...
@@ -101,7 +46,7 @@ int lwptev_stringlist_read(char const *pathname, size_t *n, char ***strs,
101 46
   *strs = NULL;
102 47
   /* read file contents */
103 48
   size_t sz;
104
-  char *data = lwptev_stringlist_read_file(pathname, &sz);
49
+  char *data = lwptev_read_file(pathname, &sz);
105 50
   if (! data) {
106 51
     lwptev_cleaner_cleanup(cleaner);
107 52
     return -1;
108 53