tstatus/file.c

38 lines
706 B
C
Raw Normal View History

2022-09-21 12:50:10 -05:00
/* 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