38 lines
706 B
C
38 lines
706 B
C
|
/* see LICENSE file for details on license */
|
||
|
#ifndef TSTATUS_FILE_C
|
||
|
|
||
|
#include "file.h"
|
||
|
|
||
|
void populate_buffer(char *buffer, char pattern) {
|
||
|
/* XXX: we assume the buffer is at least four bytes long */
|
||
|
|
||
|
int i;
|
||
|
for(i = 0; i < 3; i++) *(buffer + i) = pattern;
|
||
|
*(buffer + i + 1) = '\0';
|
||
|
}
|
||
|
|
||
|
int read_file_into_buffer(char *filename, char *buffer, int length) {
|
||
|
int i;
|
||
|
if (!access(filename, F_OK) == 0) {
|
||
|
populate_buffer(buffer, '?');
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
FILE *file = fopen(filename, "r");
|
||
|
i = fread(buffer, 1, length, file);
|
||
|
*(buffer + i - 1) = '\0';
|
||
|
|
||
|
if(ferror(file)) {
|
||
|
populate_buffer(buffer, '!');
|
||
|
return -1;
|
||
|
}
|
||
|
|
||
|
fclose(file);
|
||
|
|
||
|
*(buffer + i) = '\0';
|
||
|
|
||
|
return 0;
|
||
|
}
|
||
|
#endif
|
||
|
#define TSTATUS_FILE_C
|