diff --git a/c-programming/experiments/reverse-ramdisk.c b/c-programming/experiments/reverse-ramdisk.c index 66da5c0..96bda32 100644 --- a/c-programming/experiments/reverse-ramdisk.c +++ b/c-programming/experiments/reverse-ramdisk.c @@ -61,10 +61,11 @@ typedef struct { int ID; char* file_path; #if IS_POSIX - int file; + int #else - FILE* file; + FILE* #endif + file; } TempFile; TempFile* temp_files = NULL; @@ -149,10 +150,11 @@ int tf_free(int ID) { temp_files[index].locked = true; #if IS_POSIX - close(temp_files[index].file); + close #else - fclose(temp_files[index].file); + fclose #endif + (temp_files[index].file); // Delete the file if (remove(temp_files[index].file_path) != 0) { @@ -204,11 +206,13 @@ int tf_write(int ID, size_t offset, void* src, size_t data_size) { return -1; // Set the position + if ( #if IS_POSIX - if (lseek(file, offset, SEEK_SET) == -1) { + lseek #else - if (fseek(file, offset, SEEK_SET) == -1) { + fseek #endif + (file, offset, SEEK_SET) == -1) { LINE_FAIL(-1); return -1; } @@ -261,33 +265,42 @@ int tf_read(int ID, size_t offset, void* dest, size_t data_size) { temp_files[index].locked = true; #if IS_POSIX - int file = temp_files[index].file; - if (file == -1) + int #else - FILE* file = temp_files[index].file; - if (file == NULL) + FILE* #endif + file = temp_files[index].file; + if (file == +#if IS_POSIX + -1 +#else + NULL +#endif + ) return -1; // Read the data from the file void* src = malloc(data_size); if (src == NULL) { #if IS_POSIX - close(file); + close #else - fclose(file); + fclose #endif - LINE_FAIL(-7); + (file); + LINE_FAIL(-8); return -1; } memset(src, 0, data_size); // clear destination // Set the position + if ( #if IS_POSIX - if (lseek(file, offset, SEEK_SET) == -1) { + lseek #else - if (fseek(file, offset, SEEK_SET) == -1) { + fseek #endif + (file, offset, SEEK_SET) == -1) { LINE_FAIL(-1); return -1; }