FILE: optimize allocation

This commit is contained in:
hasufell 2014-06-08 03:05:37 +02:00
parent 75db8761d3
commit 7a7072675e
No known key found for this signature in database
GPG Key ID: 220CD1C5BDEED020
1 changed files with 5 additions and 5 deletions

View File

@ -54,9 +54,14 @@ read_file(char const * const filename)
int fd = 0;
size_t str_size = 0;
ssize_t n;
size_t file_length = 0;
string *result_string;
fd = open(filename, O_RDONLY);
file_length = lseek(fd, 0, SEEK_END) + 1;
lseek(fd, 0, SEEK_SET);
cstring = malloc(sizeof(char) * file_length);
if (fd != -1) {
/* read and copy chunks */
@ -70,11 +75,6 @@ read_file(char const * const filename)
str_size += n; /* count total bytes read */
REALLOC( /* allocate correct size */
cstring, /* pointer to realloc */
str_size /* total bytes read */
+ 1); /* space for trailing NULL byte */
/* append buffer to string */
memcpy(cstring + (str_size - n), buf, (size_t)n);
}