2023-12-27 20:05:59 +05:30
|
|
|
/*
|
2024-01-25 23:03:58 +05:30
|
|
|
* reverse-ramdisk.c
|
|
|
|
*
|
2024-01-04 00:32:31 +05:30
|
|
|
* C programming idea: Handling temporary files like memory allocations
|
|
|
|
* (allocating -> creating empty file, using -> locking for R/W,
|
|
|
|
* freeing -> deleting).
|
|
|
|
*
|
|
|
|
* 20% AI, 80% human (the code is tested and reviewed)
|
|
|
|
*
|
|
|
|
* Warning: The current result is quick and dirty. Not for educational or
|
|
|
|
* production purposes.
|
|
|
|
*
|
|
|
|
* GCC/Clang/TCC: Compile with -DTEST to set macro TEST as defined, with
|
|
|
|
* -DDEBUG to enable debug mode
|
|
|
|
*
|
2024-01-07 00:58:06 +05:30
|
|
|
* To-Do: Test: Fix the bug with invalid next size to reallocate
|
|
|
|
* To-Do: Test: Automate the test verification
|
2024-01-25 23:03:58 +05:30
|
|
|
*
|
|
|
|
* Author: Intel A80486DX2-66
|
|
|
|
* License: Creative Commons Zero 1.0 Universal
|
2024-01-04 00:32:31 +05:30
|
|
|
*/
|
2023-12-27 20:05:59 +05:30
|
|
|
|
|
|
|
#include <errno.h>
|
|
|
|
#include <inttypes.h>
|
2023-12-27 23:55:39 +05:30
|
|
|
#include <math.h>
|
2024-01-03 22:35:14 +05:30
|
|
|
#include <stdbool.h>
|
2023-12-27 20:05:59 +05:30
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2023-12-28 02:32:52 +05:30
|
|
|
|
2023-12-28 03:09:59 +05:30
|
|
|
#if _POSIX_VERSION >= 200112L
|
2023-12-28 02:32:52 +05:30
|
|
|
# include <fcntl.h>
|
|
|
|
# include <unistd.h>
|
|
|
|
# define IS_POSIX 1
|
|
|
|
#else
|
|
|
|
# define IS_POSIX 0
|
|
|
|
#endif
|
2023-12-27 20:05:59 +05:30
|
|
|
|
2023-12-27 21:41:51 +05:30
|
|
|
#ifdef DEBUG
|
2024-01-03 23:51:29 +05:30
|
|
|
# define LINE_FAIL(x) printf("failed on line %d\n", __LINE__ + x)
|
2024-01-07 00:56:22 +05:30
|
|
|
#else
|
|
|
|
# define LINE_FAIL(x)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(DEBUG) || defined(TEST)
|
2024-01-04 00:27:49 +05:30
|
|
|
# define DBG_PRINT(...) do { \
|
|
|
|
printf(__VA_ARGS__); \
|
|
|
|
fflush(stdout); \
|
|
|
|
} while (0)
|
2023-12-27 21:41:51 +05:30
|
|
|
#else
|
2024-01-04 00:27:49 +05:30
|
|
|
# define DBG_PRINT(...)
|
2023-12-27 21:41:51 +05:30
|
|
|
#endif
|
|
|
|
|
2024-01-03 23:51:29 +05:30
|
|
|
#define RETREAT(s) do { \
|
2023-12-27 21:41:51 +05:30
|
|
|
perror(s); \
|
|
|
|
exit(EXIT_FAILURE); \
|
|
|
|
} while (0)
|
|
|
|
|
2023-12-28 02:54:29 +05:30
|
|
|
typedef struct {
|
2024-01-03 22:35:14 +05:30
|
|
|
bool locked;
|
2023-12-28 02:54:29 +05:30
|
|
|
int ID;
|
|
|
|
char* file_path;
|
2024-01-03 22:21:43 +05:30
|
|
|
#if IS_POSIX
|
2023-12-28 02:54:29 +05:30
|
|
|
int file;
|
|
|
|
#else
|
2023-12-27 20:05:59 +05:30
|
|
|
FILE* file;
|
2023-12-28 02:54:29 +05:30
|
|
|
#endif
|
2024-01-03 22:21:43 +05:30
|
|
|
} TempFile;
|
2023-12-27 20:05:59 +05:30
|
|
|
|
|
|
|
TempFile* temp_files = NULL;
|
|
|
|
size_t num_temp_files = 0;
|
|
|
|
|
2023-12-27 21:41:00 +05:30
|
|
|
int tf_alloc(size_t n, size_t type_size);
|
|
|
|
int tf_free(int ID);
|
2023-12-30 18:34:25 +05:30
|
|
|
int tf_write(int ID, size_t offset, void* src, size_t data_size);
|
2023-12-27 21:41:00 +05:30
|
|
|
int tf_read(int ID, size_t offset, void* dest, size_t data_size);
|
|
|
|
|
2023-12-27 20:05:59 +05:30
|
|
|
int tf_alloc(size_t n, size_t type_size) {
|
2024-01-04 00:27:49 +05:30
|
|
|
DBG_PRINT("tf_alloc(%zu, %zu)\n", n, type_size);
|
|
|
|
|
2023-12-27 20:05:59 +05:30
|
|
|
// Create an empty file
|
2023-12-27 20:25:20 +05:30
|
|
|
size_t len_digit;
|
|
|
|
if (num_temp_files == 0)
|
|
|
|
len_digit = 1;
|
|
|
|
else
|
2024-01-03 23:37:45 +05:30
|
|
|
len_digit = (size_t) floor(log10((double) num_temp_files)) + 1;
|
2023-12-27 20:25:20 +05:30
|
|
|
size_t file_path_len = len_digit + strlen("tf_.tmp");
|
|
|
|
char* file_path = malloc((file_path_len + 1) * sizeof(char));
|
|
|
|
if (file_path == NULL) {
|
2024-01-03 23:51:29 +05:30
|
|
|
LINE_FAIL(-2);
|
2023-12-27 20:25:20 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2023-12-28 02:34:22 +05:30
|
|
|
int res = snprintf(file_path, file_path_len + 1, "tf_%" PRIuMAX ".tmp",
|
2023-12-27 23:59:16 +05:30
|
|
|
(uintmax_t) num_temp_files);
|
2023-12-27 20:25:20 +05:30
|
|
|
if ((size_t) res != file_path_len) {
|
2024-01-03 23:51:29 +05:30
|
|
|
LINE_FAIL(-2);
|
2023-12-27 20:25:20 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-12-28 02:54:29 +05:30
|
|
|
#if IS_POSIX
|
|
|
|
int file = open(file_path, O_RDWR | O_CREAT);
|
|
|
|
if (file == -1) {
|
|
|
|
#else
|
2023-12-27 20:05:59 +05:30
|
|
|
FILE* file = fopen(file_path, "w+b");
|
|
|
|
if (file == NULL) {
|
2023-12-28 02:54:29 +05:30
|
|
|
#endif
|
2024-01-03 23:51:29 +05:30
|
|
|
LINE_FAIL(-2);
|
2023-12-27 20:05:59 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allocate memory for the TempFile struct
|
2023-12-27 20:25:20 +05:30
|
|
|
TempFile* temp_file = malloc(sizeof(TempFile));
|
2023-12-27 20:05:59 +05:30
|
|
|
if (temp_file == NULL) {
|
2024-01-03 23:51:29 +05:30
|
|
|
LINE_FAIL(-2);
|
2023-12-27 20:05:59 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-12-28 00:28:50 +05:30
|
|
|
// Assign the ID, file path, file handler
|
2024-01-03 22:35:14 +05:30
|
|
|
temp_file->locked = false;
|
2023-12-27 20:05:59 +05:30
|
|
|
temp_file->ID = num_temp_files;
|
|
|
|
temp_file->file_path = strdup(file_path);
|
|
|
|
temp_file->file = file;
|
|
|
|
|
2023-12-28 00:15:53 +05:30
|
|
|
// Allocate/reallocate memory for the temp_files structure
|
|
|
|
if (temp_files == NULL)
|
|
|
|
temp_files = malloc(sizeof(TempFile));
|
|
|
|
else
|
2024-01-04 00:13:22 +05:30
|
|
|
temp_files = realloc(temp_files, num_temp_files * sizeof(TempFile));
|
2023-12-27 20:05:59 +05:30
|
|
|
if (temp_files == NULL) {
|
2024-01-03 23:51:29 +05:30
|
|
|
LINE_FAIL(-2);
|
2023-12-27 20:05:59 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-12-28 02:37:29 +05:30
|
|
|
// Add the temp file to the array
|
|
|
|
temp_files[num_temp_files++] = *temp_file;
|
|
|
|
|
2023-12-27 20:05:59 +05:30
|
|
|
return temp_file->ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
int tf_free(int ID) {
|
2024-01-04 00:27:49 +05:30
|
|
|
DBG_PRINT("tf_free(%d)\n", ID);
|
|
|
|
|
2023-12-27 20:25:20 +05:30
|
|
|
size_t index = (size_t) ID;
|
2023-12-27 20:05:59 +05:30
|
|
|
|
2024-01-03 22:35:14 +05:30
|
|
|
if (temp_files[index].locked) {
|
|
|
|
errno = EBUSY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
temp_files[index].locked = true;
|
|
|
|
|
2023-12-28 02:54:29 +05:30
|
|
|
#if IS_POSIX
|
|
|
|
close(temp_files[index].file);
|
|
|
|
#else
|
2023-12-27 20:05:59 +05:30
|
|
|
fclose(temp_files[index].file);
|
2023-12-28 02:54:29 +05:30
|
|
|
#endif
|
2023-12-27 20:05:59 +05:30
|
|
|
|
|
|
|
// Delete the file
|
|
|
|
if (remove(temp_files[index].file_path) != 0) {
|
2024-01-03 23:51:29 +05:30
|
|
|
LINE_FAIL(-1);
|
2023-12-27 20:05:59 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
free(temp_files[index].file_path);
|
|
|
|
|
|
|
|
// Shift the remaining temp files in the array
|
2024-01-03 23:40:22 +05:30
|
|
|
for (size_t i = index; i < num_temp_files - 1; i++)
|
|
|
|
temp_files[i] = temp_files[i + 1];
|
2023-12-27 20:05:59 +05:30
|
|
|
|
|
|
|
// Reallocate memory for the temp_files array
|
2024-01-03 23:33:43 +05:30
|
|
|
if (--num_temp_files > 0) {
|
2024-01-04 00:27:49 +05:30
|
|
|
DBG_PRINT("num_temp_files = %zu\n", num_temp_files);
|
2024-01-03 23:33:43 +05:30
|
|
|
if ((temp_files = realloc(temp_files, num_temp_files
|
|
|
|
* sizeof(TempFile))) == NULL) {
|
2024-01-03 23:51:29 +05:30
|
|
|
LINE_FAIL(-2);
|
2024-01-03 23:33:43 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2023-12-27 20:05:59 +05:30
|
|
|
}
|
|
|
|
|
2024-01-03 22:35:14 +05:30
|
|
|
temp_files[index].locked = false;
|
|
|
|
|
2023-12-27 20:05:59 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2023-12-30 18:34:25 +05:30
|
|
|
int tf_write(int ID, size_t offset, void* src, size_t data_size) {
|
2024-01-04 00:27:49 +05:30
|
|
|
DBG_PRINT("tf_write(%d, %zu, %p, %zu)\n", ID, offset, src, data_size);
|
|
|
|
|
2023-12-27 20:25:20 +05:30
|
|
|
size_t index = (size_t) ID;
|
2023-12-27 20:05:59 +05:30
|
|
|
|
2024-01-03 22:35:14 +05:30
|
|
|
if (temp_files[index].locked) {
|
|
|
|
errno = EBUSY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
temp_files[index].locked = true;
|
|
|
|
|
2023-12-28 02:54:29 +05:30
|
|
|
#if IS_POSIX
|
|
|
|
// Check file handler for -1
|
|
|
|
int file = temp_files[index].file;
|
|
|
|
if (file == -1)
|
|
|
|
#else
|
2023-12-27 20:25:20 +05:30
|
|
|
// Check file handler for NULL
|
2023-12-27 20:05:59 +05:30
|
|
|
FILE* file = temp_files[index].file;
|
|
|
|
if (file == NULL)
|
2023-12-28 02:54:29 +05:30
|
|
|
#endif
|
2023-12-27 20:05:59 +05:30
|
|
|
return -1;
|
|
|
|
|
2023-12-27 20:25:20 +05:30
|
|
|
// Set the position
|
2023-12-28 02:54:29 +05:30
|
|
|
#if IS_POSIX
|
|
|
|
if (lseek(file, offset, SEEK_SET) == -1) {
|
|
|
|
#else
|
|
|
|
if (fseek(file, offset, SEEK_SET) == -1) {
|
|
|
|
#endif
|
2024-01-03 23:51:29 +05:30
|
|
|
LINE_FAIL(-1);
|
2023-12-28 02:54:29 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2023-12-27 20:05:59 +05:30
|
|
|
|
|
|
|
// Write the data to the file
|
2023-12-28 02:54:29 +05:30
|
|
|
#if IS_POSIX
|
2024-01-04 00:19:23 +05:30
|
|
|
ssize_t
|
2023-12-28 02:54:29 +05:30
|
|
|
#else
|
2024-01-04 00:19:23 +05:30
|
|
|
size_t
|
|
|
|
#endif
|
|
|
|
bytes_written =
|
|
|
|
#if IS_POSIX
|
|
|
|
write(file, src, data_size);
|
|
|
|
#else
|
|
|
|
fwrite(src, 1, data_size, file);
|
2023-12-28 02:54:29 +05:30
|
|
|
#endif
|
2023-12-27 20:05:59 +05:30
|
|
|
|
2023-12-28 02:54:29 +05:30
|
|
|
if (
|
|
|
|
#if IS_POSIX
|
|
|
|
(size_t)
|
|
|
|
#endif
|
|
|
|
bytes_written != data_size) {
|
|
|
|
errno = EIO;
|
2023-12-27 20:05:59 +05:30
|
|
|
return -1;
|
2023-12-28 02:54:29 +05:30
|
|
|
}
|
2023-12-27 20:05:59 +05:30
|
|
|
|
2023-12-28 02:54:29 +05:30
|
|
|
#if IS_POSIX
|
|
|
|
if (fsync(file) == -1) {
|
2024-01-03 23:51:29 +05:30
|
|
|
LINE_FAIL(-1);
|
2023-12-28 00:00:42 +05:30
|
|
|
return -1;
|
|
|
|
}
|
2023-12-28 02:54:29 +05:30
|
|
|
#else
|
|
|
|
fflush(file);
|
|
|
|
#endif
|
2023-12-28 00:00:42 +05:30
|
|
|
|
2024-01-03 22:35:14 +05:30
|
|
|
temp_files[index].locked = false;
|
|
|
|
|
2023-12-27 20:05:59 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int tf_read(int ID, size_t offset, void* dest, size_t data_size) {
|
2024-01-04 00:27:49 +05:30
|
|
|
DBG_PRINT("tf_read(%d, %zu, %p, %zu)\n", ID, offset, dest, data_size);
|
|
|
|
|
2023-12-27 20:25:20 +05:30
|
|
|
size_t index = (size_t) ID;
|
2023-12-27 20:05:59 +05:30
|
|
|
|
2024-01-03 22:35:14 +05:30
|
|
|
if (temp_files[index].locked) {
|
|
|
|
errno = EBUSY;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
temp_files[index].locked = true;
|
|
|
|
|
2023-12-28 02:54:29 +05:30
|
|
|
#if IS_POSIX
|
|
|
|
int file = temp_files[index].file;
|
|
|
|
if (file == -1)
|
|
|
|
#else
|
2023-12-27 20:05:59 +05:30
|
|
|
FILE* file = temp_files[index].file;
|
|
|
|
if (file == NULL)
|
2023-12-28 02:54:29 +05:30
|
|
|
#endif
|
2023-12-27 20:05:59 +05:30
|
|
|
return -1;
|
|
|
|
|
|
|
|
// Read the data from the file
|
2023-12-30 18:34:25 +05:30
|
|
|
void* src = malloc(data_size);
|
|
|
|
if (src == NULL) {
|
2023-12-28 02:54:29 +05:30
|
|
|
#if IS_POSIX
|
|
|
|
close(file);
|
|
|
|
#else
|
2023-12-27 20:05:59 +05:30
|
|
|
fclose(file);
|
2023-12-28 02:54:29 +05:30
|
|
|
#endif
|
2024-01-03 23:51:29 +05:30
|
|
|
LINE_FAIL(-7);
|
2023-12-27 20:05:59 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-12-30 18:34:25 +05:30
|
|
|
memset(src, 0, data_size); // clear destination
|
2023-12-28 02:54:29 +05:30
|
|
|
// Set the position
|
|
|
|
#if IS_POSIX
|
|
|
|
if (lseek(file, offset, SEEK_SET) == -1) {
|
|
|
|
#else
|
|
|
|
if (fseek(file, offset, SEEK_SET) == -1) {
|
|
|
|
#endif
|
2024-01-03 23:51:29 +05:30
|
|
|
LINE_FAIL(-1);
|
2023-12-28 02:54:29 +05:30
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// read bytes
|
|
|
|
#if IS_POSIX
|
2023-12-30 18:34:25 +05:30
|
|
|
ssize_t bytes_read = read(file, src, data_size);
|
2023-12-28 02:54:29 +05:30
|
|
|
#else
|
2023-12-30 18:34:25 +05:30
|
|
|
size_t bytes_read = fread(src, 1, data_size, file);
|
2023-12-28 02:54:29 +05:30
|
|
|
#endif
|
2023-12-30 18:34:25 +05:30
|
|
|
memcpy(dest, src, data_size);
|
2023-12-27 20:05:59 +05:30
|
|
|
|
2023-12-30 18:34:25 +05:30
|
|
|
free(src); // Free the allocated memory
|
2023-12-27 20:05:59 +05:30
|
|
|
|
2023-12-28 02:54:29 +05:30
|
|
|
if (
|
|
|
|
#if IS_POSIX
|
|
|
|
(size_t)
|
|
|
|
#endif
|
|
|
|
bytes_read != data_size) {
|
2023-12-27 20:05:59 +05:30
|
|
|
errno = EIO;
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2023-12-27 20:25:20 +05:30
|
|
|
#ifdef DEBUG
|
2024-01-03 23:56:13 +05:30
|
|
|
printf("Read: ID = %d, src = %p, size = %zu -> '",
|
|
|
|
ID, dest, data_size);
|
2023-12-27 20:05:59 +05:30
|
|
|
for (size_t i = 0; i < data_size; i++)
|
2024-01-04 00:00:07 +05:30
|
|
|
printf("0x%02" PRIX8 "%c", *((uint8_t*)((uint8_t*)dest + i)),
|
|
|
|
i == (data_size - 1) ? '\'' : ' ');
|
|
|
|
printf("\n");
|
2023-12-27 23:59:16 +05:30
|
|
|
fflush(stdout);
|
2023-12-27 20:25:20 +05:30
|
|
|
#endif
|
2024-01-03 22:35:14 +05:30
|
|
|
|
|
|
|
temp_files[index].locked = false;
|
|
|
|
|
2023-12-27 20:05:59 +05:30
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef TEST
|
2023-12-27 21:08:05 +05:30
|
|
|
int main(void) {
|
2024-01-04 00:27:49 +05:30
|
|
|
DBG_PRINT("started\n");
|
|
|
|
|
2024-01-07 00:53:13 +05:30
|
|
|
#define ARRAY_1_LEN 4
|
|
|
|
#define ARRAY_2_LEN 16
|
|
|
|
|
|
|
|
int ID_1 = tf_alloc(ARRAY_1_LEN, sizeof(int));
|
|
|
|
int ID_2 = tf_alloc(ARRAY_2_LEN, sizeof(uint16_t));
|
2024-01-04 00:27:49 +05:30
|
|
|
if (ID_1 == -1 || ID_2 == -1)
|
2024-01-03 23:51:29 +05:30
|
|
|
RETREAT("tf_alloc");
|
2024-01-04 00:27:49 +05:30
|
|
|
DBG_PRINT("allocated memory\n");
|
2023-12-27 20:05:59 +05:30
|
|
|
|
2024-01-07 00:53:13 +05:30
|
|
|
int test_data_1[ARRAY_1_LEN] = {123, 456, 789, -123};
|
2024-01-04 00:27:49 +05:30
|
|
|
DBG_PRINT("initialized array 1\n");
|
|
|
|
|
2024-01-07 00:53:13 +05:30
|
|
|
uint16_t test_data_2[ARRAY_2_LEN];
|
|
|
|
for (size_t i = 0; i < ARRAY_2_LEN; i++)
|
2024-01-04 00:27:49 +05:30
|
|
|
test_data_2[i] = 1 << i;
|
|
|
|
DBG_PRINT("initialized array 2\n");
|
2023-12-27 20:05:59 +05:30
|
|
|
|
2024-01-07 00:53:13 +05:30
|
|
|
for (size_t i = 0; i < ARRAY_1_LEN; i++)
|
2024-01-04 00:27:49 +05:30
|
|
|
if (tf_write(ID_1, i * sizeof(int), &test_data_1[i], sizeof(int)) == -1)
|
|
|
|
RETREAT("tf_write");
|
|
|
|
DBG_PRINT("wrote array 1\n");
|
|
|
|
|
2024-01-07 00:53:13 +05:30
|
|
|
for (size_t i = 0; i < ARRAY_2_LEN; i++)
|
2024-01-04 00:27:49 +05:30
|
|
|
if (tf_write(ID_2, i * sizeof(uint16_t), &test_data_2[i],
|
|
|
|
sizeof(uint16_t)) == -1)
|
2024-01-03 23:51:29 +05:30
|
|
|
RETREAT("tf_write");
|
2024-01-04 00:27:49 +05:30
|
|
|
DBG_PRINT("wrote array 2\n");
|
2023-12-27 20:05:59 +05:30
|
|
|
|
|
|
|
// round-trip
|
2024-01-04 00:27:49 +05:30
|
|
|
test_data_1[0] = 111;
|
|
|
|
test_data_1[1] = 222;
|
|
|
|
test_data_1[2] = 333;
|
|
|
|
test_data_1[3] = 444;
|
|
|
|
DBG_PRINT("filled array 1 with garbage\n");
|
|
|
|
|
|
|
|
for (size_t i = 0; i < 16; i++)
|
|
|
|
test_data_2[i] ^= 1;
|
|
|
|
DBG_PRINT("filled array 2 with garbage\n");
|
2023-12-27 20:05:59 +05:30
|
|
|
|
2024-01-07 00:53:13 +05:30
|
|
|
for (size_t i = 0; i < ARRAY_1_LEN; i++)
|
2024-01-04 00:27:49 +05:30
|
|
|
if (tf_read(ID_1, i * sizeof(int), &test_data_1[i], sizeof(int)) == -1)
|
2024-01-03 23:51:29 +05:30
|
|
|
RETREAT("tf_read");
|
2024-01-04 00:27:49 +05:30
|
|
|
DBG_PRINT("restored array 1\n");
|
2023-12-27 20:05:59 +05:30
|
|
|
|
2024-01-07 00:53:13 +05:30
|
|
|
for (size_t i = 0; i < ARRAY_2_LEN; i++)
|
2024-01-04 00:27:49 +05:30
|
|
|
if (tf_read(ID_2, i * sizeof(uint16_t), &test_data_2[i],
|
|
|
|
sizeof(uint16_t)) == -1)
|
|
|
|
RETREAT("tf_read");
|
|
|
|
DBG_PRINT("restored array 2\n");
|
2023-12-27 20:05:59 +05:30
|
|
|
|
2024-01-04 00:27:49 +05:30
|
|
|
DBG_PRINT("Values (1): ");
|
2024-01-07 00:53:13 +05:30
|
|
|
for (size_t i = 0; i < ARRAY_1_LEN; i++)
|
|
|
|
DBG_PRINT("%d%c", test_data_1[i], i == (ARRAY_1_LEN - 1) ? '\n' : ' ');
|
2024-01-04 00:27:49 +05:30
|
|
|
DBG_PRINT("Values (2): ");
|
2024-01-07 00:53:13 +05:30
|
|
|
for (size_t i = 0; i < ARRAY_2_LEN; i++)
|
|
|
|
DBG_PRINT("%d%c", test_data_2[i], i == (ARRAY_2_LEN - 1) ? '\n' : ' ');
|
2024-01-04 00:27:49 +05:30
|
|
|
|
|
|
|
tf_free(ID_1);
|
|
|
|
tf_free(ID_2);
|
|
|
|
DBG_PRINT("freed both files\n");
|
2023-12-27 20:05:59 +05:30
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|