Mercurial > trustbridge
comparison common/listutil.c @ 22:bc302bbceaf5
Move listutil again
author | Andre Heinecke <aheinecke@intevation.de> |
---|---|
date | Thu, 20 Feb 2014 15:44:09 +0000 |
parents | cinst/listutil.c@f4f957c58e0a |
children | e783fd99a9eb |
comparison
equal
deleted
inserted
replaced
21:dc1e1e9e62ce | 22:bc302bbceaf5 |
---|---|
1 #include "listutil.h" | |
2 | |
3 #include <stdio.h> | |
4 #include <stdlib.h> | |
5 #include <errno.h> | |
6 #include <fcntl.h> | |
7 #include <unistd.h> | |
8 #include <sys/types.h> | |
9 #include <sys/stat.h> | |
10 #include <string.h> | |
11 | |
12 #define MAX_FILESIZE_KB 1024 | |
13 | |
14 void handle_errno() | |
15 { | |
16 printf("Error: %s \n", strerror(errno)); | |
17 } | |
18 | |
19 list_status_t readList(const char *fileName, char **data, size_t *size) | |
20 { | |
21 int fd = -1; | |
22 struct stat fileStat; | |
23 int rc = 0; | |
24 ssize_t bRead = 0; | |
25 | |
26 memset(&fileStat, 0, sizeof(fileStat)); | |
27 | |
28 list_status_t retval = UnknownError; | |
29 | |
30 fd = open(fileName, O_RDONLY); | |
31 if (fd == -1) { | |
32 handle_errno(); | |
33 retval = StatFailed; | |
34 goto cleanup; | |
35 } | |
36 | |
37 rc = fstat(fd, &fileStat); | |
38 if (rc < 0) { | |
39 printf ("Stat failed with rc: %i\n", rc); | |
40 retval = StatFailed; | |
41 goto cleanup; | |
42 } | |
43 | |
44 // Check the size of the file | |
45 if (!fileStat.st_size) { | |
46 printf("Size zero\n"); | |
47 retval = StatFailed; | |
48 goto cleanup; | |
49 } | |
50 | |
51 if (fileStat.st_size / 1024 > MAX_FILESIZE_KB && | |
52 fileStat.st_size > 0) { | |
53 printf("File too large\n"); | |
54 retval = TooLarge; | |
55 goto cleanup; | |
56 } | |
57 | |
58 *size = (size_t) fileStat.st_size; | |
59 | |
60 *data = (char*) malloc(*size); | |
61 | |
62 if (*data == NULL) { | |
63 printf("Malloc failed\n"); | |
64 retval = UnknownError; | |
65 goto cleanup; | |
66 } | |
67 | |
68 bRead = read(fd, *data, *size); | |
69 | |
70 if (bRead < 0 || (size_t) bRead != *size) { | |
71 printf("Read failed\n"); | |
72 if (bRead == -1) { | |
73 handle_errno(); | |
74 } | |
75 retval = UnknownError; | |
76 *size = 0; | |
77 if (*data) { | |
78 free(*data); | |
79 printf("Nulling data\n"); | |
80 *data = NULL; | |
81 } | |
82 goto cleanup; | |
83 } | |
84 | |
85 retval = UnknownValidity; | |
86 cleanup: | |
87 | |
88 if (fd && fd != -1) { | |
89 close(fd); | |
90 fd = -1; | |
91 } | |
92 | |
93 return retval; | |
94 } | |
95 | |
96 list_status_t readAndVerifyList(const char *fileName, char **data, size_t *size) | |
97 { | |
98 // int validSig = 0; | |
99 char * signature = NULL; | |
100 | |
101 list_status_t retval = UnknownError; | |
102 *data = NULL; | |
103 *size = 0; | |
104 | |
105 retval = readList(fileName, data, size); | |
106 | |
107 if (retval != UnknownValidity) { | |
108 printf ("Readlist failed\n"); | |
109 return retval; | |
110 } | |
111 | |
112 if (!data || !*size) { | |
113 // should not have happend if readList works as specified | |
114 return UnknownError; | |
115 } | |
116 | |
117 signature = *data; | |
118 | |
119 if (*signature != 'S') { | |
120 printf("Does not start with S\n"); | |
121 retval = InvalidFormat; | |
122 goto cleanup; | |
123 } | |
124 | |
125 // TODO VERIFIY | |
126 retval = Valid; | |
127 | |
128 // Maybe check if all bytes are < 127 and > 0 | |
129 | |
130 cleanup: | |
131 if (retval != Valid && *data) { | |
132 free(*data); | |
133 *data = NULL; | |
134 *size = 0; | |
135 } | |
136 return retval; | |
137 } | |
138 |