From baa73e1a3987bf5c8ac5410168684e37bea62194 Mon Sep 17 00:00:00 2001 From: Vincent Barrilliot Date: Fri, 12 Nov 2021 12:17:11 +0000 Subject: [PATCH] Add support for finding file position, or positionning from and of file, as this is available in other OSes --- src/dev/channel.h | 1 + src/dev/fsys.c | 26 ++++++++++++++++---------- 2 files changed, 17 insertions(+), 10 deletions(-) diff --git a/src/dev/channel.h b/src/dev/channel.h index 781e935..8ed8cfd 100644 --- a/src/dev/channel.h +++ b/src/dev/channel.h @@ -38,6 +38,7 @@ #define CDEV_SEEK_ABSOLUTE 0 #define CDEV_SEEK_RELATIVE 1 +#define CDEV_SEEK_END 2 /* * Structure defining a channel diff --git a/src/dev/fsys.c b/src/dev/fsys.c index c86103b..a6e1c24 100644 --- a/src/dev/fsys.c +++ b/src/dev/fsys.c @@ -544,23 +544,29 @@ short fchan_flush(t_channel * chan) { } /** - * attempt to move the "cursor" position in the channel + * Attempt to move the "cursor" position in the channel */ short fchan_seek(t_channel * chan, long position, short base) { FIL * file; - FRESULT result; - int total_written; + FSIZE_t new_position; file = fchan_to_file(chan); if (file) { - if (base == CDEV_SEEK_ABSOLUTE) { - result = f_lseek(file, position); - return fatfs_to_foenix(result); - } else if (base == CDEV_SEEK_RELATIVE) { - long current = f_tell(file); - result = f_lseek(file, current + position); - return fatfs_to_foenix(result); + switch (base) { + case CDEV_SEEK_ABSOLUTE: + new_position = position; + break; + case CDEV_SEEK_RELATIVE: + new_position = f_tell(file) + position; + break; + case CDEV_SEEK_END: + new_position = f_size(file) - position; + break; + default: + return ERR_GENERAL; } + + return fatfs_to_foenix(f_lseek(file, position)); } return ERR_BADCHANNEL;