src/file_helper.c
/* This function returns one of the READALL_ constants above.
   If the return value is zero == READALL_OK, then:
     (*dataptr) points to a dynamically allocated buffer, with
     (*sizeptr) chars read from the file.
     The buffer is allocated for one extra char, which is NULL,
     and automatically appended after the data.
   Initial values of (*dataptr) and (*sizeptr) are ignored.
*/
This function returns one of the READALL_ constants above.
If the return value is zero == READALL_OK, then:
(*dataptr) points to a dynamically allocated buffer, with
(*sizeptr) chars read from the file.
The buffer is allocated for one extra char, which is NULL,
and automatically appended after the data.
Initial values of (*dataptr) and (*sizeptr) are ignored.
This function returns one of the READALL_ constants above . If the return value is zero == READALL_OK then : ( * dataptr ) points to a dynamically allocated buffer with ( * sizeptr ) chars read from the file . The buffer is allocated for one extra char which is NULL and automatically appended after the data . Initial values of ( * dataptr ) and ( * sizeptr ) are ignored .
int readFile(FILE *in, char **dataptr, size_t *sizeptr)
{
    char  *data = NULL, *temp;
    size_t size = 0;
    size_t used = 0;
    size_t n;
    if (in == NULL || dataptr == NULL || sizeptr == NULL)
        return READALL_INVALID;
    if (ferror(in))
        return READALL_ERROR;
    while (1) {
        if (used + READALL_CHUNK + 1 > size) {
            size = used + READALL_CHUNK + 1;
            if (size <= used) {
                free(data);
                return READALL_TOOMUCH;
            }
            temp = realloc(data, size);
            if (temp == NULL) {
                free(data);
                return READALL_NOMEM;
            }
            data = temp;
        }
        n = fread(data + used, 1, READALL_CHUNK, in);
        if (n == 0)
            break;
        used += n;
    }
    if (ferror(in)) {
        free(data);
        return READALL_ERROR;
    }
    temp = realloc(data, used + 1);
    if (temp == NULL) {
        free(data);
        return READALL_NOMEM;
    }
    data = temp;
    data[used] = '\0';
    *dataptr = data;
    *sizeptr = used;
    return READALL_OK;
}
int readFile ( FILE * in , char * * dataptr , size_t * sizeptr ) { char * data = NULL , * temp ; size_t size = 0 ; size_t used = 0 ; size_t n ; if ( in == NULL || dataptr == NULL || sizeptr == NULL ) return READALL_INVALID ; if ( ferror ( in ) ) return READALL_ERROR ; while ( 1 ) { if ( used + READALL_CHUNK + 1 > size ) { size = used + READALL_CHUNK + 1 ; if ( size <= used ) { free ( data ) ; return READALL_TOOMUCH ; } temp = realloc ( data , size ) ; if ( temp == NULL ) { free ( data ) ; return READALL_NOMEM ; } data = temp ; } n = fread ( data + used , 1 , READALL_CHUNK , in ) ; if ( n == 0 ) break ; used += n ; } if ( ferror ( in ) ) { free ( data ) ; return READALL_ERROR ; } temp = realloc ( data , used + 1 ) ; if ( temp == NULL ) { free ( data ) ; return READALL_NOMEM ; } data = temp ; data [ used ] = ' \0 ' ; * dataptr = data ; * sizeptr = used ; return READALL_OK ; }
This function returns one of the READALL_ constants above.
This function returns one of the READALL_ constants above .
/* None of the parameters can be NULL. */ /* A read error already occurred? */ /* Overflow check. Some ANSI C compilers
               may optimize this away, though. */