| 124 | } |
| 125 | |
| 126 | int poptReadFile(const char * fn, char ** bp, size_t * nbp, int flags) |
| 127 | { |
| 128 | int fdno; |
| 129 | char * b = NULL; |
| 130 | off_t nb = 0; |
| 131 | char * s, * t, * se; |
| 132 | int rc = POPT_ERROR_ERRNO; /* assume failure */ |
| 133 | |
| 134 | fdno = open(fn, O_RDONLY); |
| 135 | if (fdno < 0) |
| 136 | goto exit; |
| 137 | |
| 138 | if ((nb = lseek(fdno, 0, SEEK_END)) == (off_t)-1 |
| 139 | || (uintmax_t)nb >= SIZE_MAX |
| 140 | || lseek(fdno, 0, SEEK_SET) == (off_t)-1 |
| 141 | || (b = calloc(sizeof(*b), (size_t)nb + 1)) == NULL |
| 142 | || read(fdno, (char *)b, (size_t)nb) != (ssize_t)nb) |
| 143 | { |
| 144 | int oerrno = errno; |
| 145 | (void) close(fdno); |
| 146 | if (nb != (off_t)-1 && (uintmax_t)nb >= SIZE_MAX) |
| 147 | errno = -EOVERFLOW; |
| 148 | else |
| 149 | errno = oerrno; |
| 150 | goto exit; |
| 151 | } |
| 152 | if (close(fdno) == -1) |
| 153 | goto exit; |
| 154 | if (b == NULL) { |
| 155 | rc = POPT_ERROR_MALLOC; |
| 156 | goto exit; |
| 157 | } |
| 158 | rc = 0; |
| 159 | |
| 160 | /* Trim out escaped newlines. */ |
| 161 | if (flags & POPT_READFILE_TRIMNEWLINES) |
| 162 | { |
| 163 | for (t = b, s = b, se = b + nb; *s && s < se; s++) { |
| 164 | switch (*s) { |
| 165 | case '\\': |
| 166 | if (s[1] == '\n') { |
| 167 | s++; |
| 168 | continue; |
| 169 | } |
| 170 | /* fallthrough */ |
| 171 | default: |
| 172 | *t++ = *s; |
| 173 | break; |
| 174 | } |
| 175 | } |
| 176 | *t++ = '\0'; |
| 177 | nb = (off_t)(t - b); |
| 178 | } |
| 179 | |
| 180 | exit: |
| 181 | if (rc != 0) { |
| 182 | if (b) |
| 183 | free(b); |
no outgoing calls
no test coverage detected