From b5e603e0ea1b0e0fab187a932a5029fcc8afce56 Mon Sep 17 00:00:00 2001 From: patacongo Date: Mon, 1 Sep 2008 20:35:41 +0000 Subject: [PATCH] Fix fseek/ftell; add fsetpos/fgetpos git-svn-id: https://nuttx.svn.sourceforge.net/svnroot/nuttx/trunk@862 7fd9a85b-ad96-42d3-883c-3090e2eb8679 --- nuttx/ChangeLog | 6 ++ nuttx/Documentation/NuttX.html | 5 + nuttx/Documentation/NuttxUserGuide.html | 75 +++++++++++++- nuttx/TODO | 59 +++++++++-- nuttx/fs/fat/fs_fat32.c | 122 +++++++++++----------- nuttx/fs/fat/fs_fat32.h | 3 +- nuttx/include/stdio.h | 46 ++++----- nuttx/include/sys/types.h | 1 + nuttx/lib/Makefile | 5 +- nuttx/lib/lib_fgetpos.c | 125 +++++++++++++++++++++++ nuttx/lib/lib_fseek.c | 28 ++++- nuttx/lib/lib_fsetpos.c | 116 +++++++++++++++++++++ nuttx/lib/lib_ftell.c | 129 ++++++++++++++++++++++++ nuttx/lib/lib_ungetc.c | 62 ++++++------ 14 files changed, 648 insertions(+), 134 deletions(-) create mode 100644 nuttx/lib/lib_fgetpos.c create mode 100644 nuttx/lib/lib_fsetpos.c create mode 100644 nuttx/lib/lib_ftell.c diff --git a/nuttx/ChangeLog b/nuttx/ChangeLog index 67e48d3a8..75dd9c163 100644 --- a/nuttx/ChangeLog +++ b/nuttx/ChangeLog @@ -435,3 +435,9 @@ writing to the socket. 0.3.14 2008-xx-xx Gregory Nutt + * FAT FS now uses position variable in struct file. This simplifies operations + like ftell(). + * fseek() needs to discard bytes buffered by ungetc(). + * Corrected ftell() return value. + * Added fsetpos() and fgetpos(). + diff --git a/nuttx/Documentation/NuttX.html b/nuttx/Documentation/NuttX.html index 90d8f112f..0b33ac548 100644 --- a/nuttx/Documentation/NuttX.html +++ b/nuttx/Documentation/NuttX.html @@ -1062,6 +1062,11 @@ buildroot-0.1.0 2007-03-09 <spudmonkey@racsa.co.cr>
    nuttx-0.3.14 2008-xx-xx Gregory Nutt <spudmonkey@racsa.co.cr> + * FAT FS now uses position variable in struct file. This simplifies operations + like ftell(). + * fseek() needs to discard bytes buffered by ungetc(). + * Corrected ftell() return value. + * Added fsetpos() and fgetpos(). pascal-0.1.3 2008-xx-xx Gregory Nutt <spudmonkey@racsa.co.cr> diff --git a/nuttx/Documentation/NuttxUserGuide.html b/nuttx/Documentation/NuttxUserGuide.html index 0feb1ef0e..d97281e78 100644 --- a/nuttx/Documentation/NuttxUserGuide.html +++ b/nuttx/Documentation/NuttxUserGuide.html @@ -21,7 +21,7 @@ User's Manual

    Gregory Nutt

    -Last Update: August 22, 2008 +Last Update: September 1, 2008

    1.0 Introduction

    @@ -5910,11 +5910,14 @@ interface of the same name.

    2.11.2 Driver Operations

    +
         #include <fcntl.h>
         int open(const char *path, int oflag, ...);
       
    +
    +
         #include <unistd.h>
         int     close(int fd);
      @@ -5925,13 +5928,17 @@ interface of the same name.
         int     unlink(const char *path);
         ssize_t write(int fd, const void *buf, size_t nbytes);
       
    +
    +
         #include <sys/ioctl.h>
         int     ioctl(int fd, int req, unsigned long arg);
       
    +

    2.11.3 Directory Operations

    +
         #include <dirent.h>
         int        closedir(DIR *dirp);
      @@ -5942,11 +5949,15 @@ interface of the same name.
         void       seekdir(FAR DIR *dirp, int loc);
         int        telldir(FAR DIR *dirp);
       
    +
    + +
         #include <unistd.h>
         int       chdir(FAR const char *path);
         FAR char *getcwd(FAR char *buf, size_t size);
       
    +

    2.11.4 Standard I/O

      @@ -5956,13 +5967,16 @@ interface of the same name.
         int    feof(FILE *stream);                               /* Prototyped but not implemented */
         int    ferror(FILE *stream);                             /* Prototyped but not implemented */
         int    fgetc(FILE *stream);
      +  int    fgetpos(FILE *stream, fpos_t *pos);
         char  *fgets(char *s, int n, FILE *stream);
         FILE  *fopen(const char *path, const char *type);
         int    fprintf(FILE *stream, const char *format, ...);
         int    fputc(int c, FILE *stream);
         int    fputs(const char *s, FILE *stream);
         size_t fread(void *ptr, size_t size, size_t n_items, FILE *stream);
      -  int    fseek(FILE *stream, long int offset, int whence); /* Prototyped but not implemented */
      +  int    fseek(FILE *stream, long int offset, int whence);
      +  int    fsetpos(FILE *stream, fpos_t *pos);
      +  long   ftell(FILE *stream);
         size_t fwrite(const void *ptr, size_t size, size_t n_items, FILE *stream);
         char  *gets(char *s);
       
      @@ -6973,29 +6987,57 @@ notify a task when a message is available on a queue.
       

      Index

      - + - +
      +
    • accept
    • bind
    • +
    • chdir
    • clock_getres
    • clock_gettime
    • Clocks
    • clock_settime
    • +
    • close
    • +
    • closedir
    • connect
    • Data structures
    • Directory operations
    • Driver operations
    • +
    • dup
    • +
    • dup2
    • exit
    • FAT File System Support
    • +
    • fclose
    • +
    • fdopen
    • +
    • feof
    • +
    • ferror
    • File system, interfaces
    • File system, overview
    • +
    • fflush
    • +
    • fgetc
    • +
    • fgetpos
    • +
    • fgets
    • +
    • fopen
    • +
    • fprintf
    • +
    • fputc
    • +
    • fputs
    • +
    • fread
    • +
    • fseek
    • +
    • fsetpos
    • +
    • fstat(
    • +
    • ftell
    • +
    • fwrite
    • +
    • getcwd
    • getpid
    • +
    • gets
    • getsockopt
    • gmtime_r
    • Introduction +
    • ioctl
    • kill
    • listen
    • localtime_r
    • +
    • lseek
    • Named Message Queue Interfaces +
    • mkdir
    • mkfatfs
    • mkfifo
    • mktime
    • @@ -7010,8 +7052,13 @@ notify a task when a message is available on a queue.
    • mq_timedsend
    • mq_unlink
    • Network Interfaces
    • +
    • open
    • +
    • opendir
    • OS Interfaces
    • pipe
    • +
      +
    • printf
    • Pthread Interfaces
    • pthread_attr_destroy
    • pthread_attr_getinheritsched
    • @@ -7052,8 +7099,6 @@ notify a task when a message is available on a queue.
    • pthread_mutexattr_getpshared
    • pthread_mutexattr_gettype
    • pthread_mutexattr_init
    • -
    • pthread_mutexattr_setpshared
    • pthread_mutexattr_settype
    • pthread_mutex_destroy
    • @@ -7070,10 +7115,19 @@ notify a task when a message is available on a queue.
    • pthread_sigmask
    • pthread_testcancelstate
    • pthread_yield
    • +
    • puts
    • +
    • read
    • +
    • readdir
    • +
    • readdir_r
    • recv
    • recvfrom
    • +
    • rename
    • +
    • rmdir
    • +
    • rewinddir
    • sched_getparam
    • sched_get_priority_max
    • +
    • sched_get_priority_min
    • sched_get_rr_interval
    • sched_lockcount
    • @@ -7093,6 +7147,7 @@ notify a task when a message is available on a queue.
    • sem_unlink
    • sem_wait
    • sched_getscheduler
    • +
    • seekdir
    • send
    • sendto
    • setsockopt
    • @@ -7110,7 +7165,10 @@ notify a task when a message is available on a queue.
    • sigtimedwait
    • sigwaitinfo
    • socket
    • +
    • sprintf
    • Standard I/O
    • +
    • stat
    • +
    • statfs
    • task_activate
    • Task Control Interfaces
    • task_create
    • @@ -7119,18 +7177,25 @@ notify a task when a message is available on a queue.
    • task_restart
    • Task Scheduling Interfaces
    • Task Switching Interfaces +
    • telldir
    • timer_create
    • timer_delete
    • timer_getoverrun
    • timer_gettime
    • Timers
    • timer_settime
    • +
    • ungetc
    • +
    • unlink
    • +
    • vfprintf
    • +
    • vprintf
    • +
    • vsprintf
    • Watchdog Timer Interfaces
    • wd_cancel
    • wd_create
    • wd_delete
    • wd_gettime
    • wd_start
    • +
    • write
    • diff --git a/nuttx/TODO b/nuttx/TODO index 700722de4..1fde6da6f 100644 --- a/nuttx/TODO +++ b/nuttx/TODO @@ -7,21 +7,22 @@ NuttX TODO List (Last updated July 31, 2008) (1) Signals (sched/, arch/) (1) pthreads (sched/) (1) C++ Support - (10) Network (net/, netutils/) + (12) Network (net/, netutils/) (2) USB (drivers/usbdev) (3) Libraries (lib/) (4) File system/Generic drivers (fs/, drivers/) (1) Pascal add-on (pcode/) (2) Documentation (Documentation/) (3) Build system - (0) Applications & Tests (examples/) + (2) NuttShell (NSH) (examples/nsh) + (1) Other Applications & Tests (examples/) (1) ARM (arch/arm/) (1) ARM/C5471 (arch/arm/src/c5471/) (1) ARM/DM320 (arch/arm/src/dm320/) (2) ARM/LPC214x (arch/arm/src/lpc214x/) (4) pjrc-8052 / MCS51 (arch/pjrc-8051/) - (6) z80/z8 (arch/z80/) - (7) z16 (arch/z16/) + (7) z80/z8 (arch/z80/) + (8) z16 (arch/z16/) o Task/Scheduler (sched/) ^^^^^^^^^^^^^^^^^^^^^^^ @@ -183,6 +184,23 @@ o Network (net/, netutils/) Status: Open Priority: Low + Description: At present, there cannot be two concurrent active TCP send + operations in progress. This is because the uIP ACK logic will + support only one transfer at a time. The solution is simple: + A mutex will be needed to make sure that each send that is + started is able to be the exclusive sender until all of the + data to be sent has been ACKed. + Status: Open. There is some temporary logic to examples/nsh that does + this same fix and that temporary logic should be removed when + send() is fixed. + Priority: Medium-Low. This is an important issue for applications that + send on the same TCP socket from multiple threads. + + Description: Some application-level interface to the ICMP logic is needed + to support ping from the target. + Status: Open + Priority: Low + o USB (drivers/usbdev) ^^^^^^^^^^^^^^^^^^^^ @@ -272,8 +290,27 @@ o Build system Status: Open Priority: Medium (maybe higher for z80 target) -o Applications & Tests (examples/) - ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ +o NuttShell (NSH) (examples/nsh) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + Description: When the telnetd front end is received, each TCP packet + received causes a prompt (nsh >) to be presented. The + prompt should only be presented when the user enters a + carriage return. + Status: Open + Priority: Low + + Description: Here are some commands that would be good to have in NSH: + ping + Status: Open + Priority: Low + +o Other Applications & Tests (examples/) + ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ + + Description: The redirection test (part of examples/pipe) terminates + incorrectly on the Cywgin-based simulation platform (but works + fine on the Linux-based simulation platform). o ARM (arch/arm/) ^^^^^^^^^^^^^^^ @@ -405,6 +442,11 @@ o z80/z8 (arch/z80) Status: Open Priority: Medium + Description: Upgrade to the ZDS-II Z8Encore! 4.11.0 toolchain + Status: Open + Priority: Low + + o z16 (arch/z16) ^^^^^^^^^^^^^^^^ @@ -491,4 +533,9 @@ o z16 (arch/z16) Status: Open Priority: Low + Description: Upgrade to the ZDS-II ZNEO 4.11.1 toolchain + Status: Open + Priority: Low + + diff --git a/nuttx/fs/fat/fs_fat32.c b/nuttx/fs/fat/fs_fat32.c index d4be115fd..f7942e548 100644 --- a/nuttx/fs/fat/fs_fat32.c +++ b/nuttx/fs/fat/fs_fat32.c @@ -74,15 +74,15 @@ * Private Function Prototypes ****************************************************************************/ -static int fat_open(FAR struct file *filp, const char *relpath, +static int fat_open(FAR struct file *filep, const char *relpath, int oflags, mode_t mode); -static int fat_close(FAR struct file *filp); -static ssize_t fat_read(FAR struct file *filp, char *buffer, size_t buflen); -static ssize_t fat_write(FAR struct file *filp, const char *buffer, +static int fat_close(FAR struct file *filep); +static ssize_t fat_read(FAR struct file *filep, char *buffer, size_t buflen); +static ssize_t fat_write(FAR struct file *filep, const char *buffer, size_t buflen); -static off_t fat_seek(FAR struct file *filp, off_t offset, int whence); -static int fat_ioctl(FAR struct file *filp, int cmd, unsigned long arg); -static int fat_sync(FAR struct file *filp); +static off_t fat_seek(FAR struct file *filep, off_t offset, int whence); +static int fat_ioctl(FAR struct file *filep, int cmd, unsigned long arg); +static int fat_sync(FAR struct file *filep); static int fat_opendir(struct inode *mountpt, const char *relpath, struct internal_dir_s *dir); @@ -149,7 +149,7 @@ const struct mountpt_operations fat_operations = * Name: fat_open ****************************************************************************/ -static int fat_open(FAR struct file *filp, const char *relpath, +static int fat_open(FAR struct file *filep, const char *relpath, int oflags, mode_t mode) { struct fat_dirinfo_s dirinfo; @@ -160,13 +160,13 @@ static int fat_open(FAR struct file *filp, const char *relpath, /* Sanity checks */ - DEBUGASSERT(filp->f_priv == NULL && filp->f_inode != NULL); + DEBUGASSERT(filep->f_priv == NULL && filep->f_inode != NULL); /* Get the mountpoint inode reference from the file structure and the * mountpoint private data from the inode structure */ - inode = filp->f_inode; + inode = filep->f_inode; fs = inode->i_private; DEBUGASSERT(fs != NULL); @@ -320,7 +320,7 @@ static int fat_open(FAR struct file *filp, const char *relpath, /* Attach the private date to the struct file instance */ - filp->f_priv = ff; + filep->f_priv = ff; /* Then insert the new instance into the mountpoint structure. * It needs to be there (1) to handle error conditions that effect @@ -337,7 +337,7 @@ static int fat_open(FAR struct file *filp, const char *relpath, if ((oflags & (O_APPEND|O_WRONLY)) == (O_APPEND|O_WRONLY)) { - ssize_t offset = (ssize_t)fat_seek(filp, ff->ff_size, SEEK_SET); + ssize_t offset = (ssize_t)fat_seek(filep, ff->ff_size, SEEK_SET); if (offset < 0) { free(ff); @@ -363,7 +363,7 @@ errout_with_semaphore: * Name: fat_close ****************************************************************************/ -static int fat_close(FAR struct file *filp) +static int fat_close(FAR struct file *filep) { struct inode *inode; struct fat_mountpt_s *fs; @@ -372,12 +372,12 @@ static int fat_close(FAR struct file *filp) /* Sanity checks */ - DEBUGASSERT(filp->f_priv != NULL && filp->f_inode != NULL); + DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL); /* Recover our private data from the struct file instance */ - ff = filp->f_priv; - inode = filp->f_inode; + ff = filep->f_priv; + inode = filep->f_inode; fs = inode->i_private; DEBUGASSERT(fs != NULL); @@ -388,7 +388,7 @@ static int fat_close(FAR struct file *filp) /* Synchronize the file buffers and disk content; update times */ - ret = fat_sync(filp); + ret = fat_sync(filep); /* Then deallocate the memory structures created when the open method * was called. @@ -404,7 +404,7 @@ static int fat_close(FAR struct file *filp) /* Then free the file structure itself. */ free(ff); - filp->f_priv = NULL; + filep->f_priv = NULL; return ret; } @@ -412,7 +412,7 @@ static int fat_close(FAR struct file *filp) * Name: fat_read ****************************************************************************/ -static ssize_t fat_read(FAR struct file *filp, char *buffer, size_t buflen) +static ssize_t fat_read(FAR struct file *filep, char *buffer, size_t buflen) { struct inode *inode; struct fat_mountpt_s *fs; @@ -428,12 +428,12 @@ static ssize_t fat_read(FAR struct file *filp, char *buffer, size_t buflen) /* Sanity checks */ - DEBUGASSERT(filp->f_priv != NULL && filp->f_inode != NULL); + DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL); /* Recover our private data from the struct file instance */ - ff = filp->f_priv; - inode = filp->f_inode; + ff = filep->f_priv; + inode = filep->f_inode; fs = inode->i_private; DEBUGASSERT(fs != NULL); @@ -457,7 +457,7 @@ static ssize_t fat_read(FAR struct file *filp, char *buffer, size_t buflen) /* Get the number of bytes left in the file */ - bytesleft = ff->ff_size - ff->ff_position; + bytesleft = ff->ff_size - filep->f_pos; /* Truncate read count so that it does not exceed the number * of bytes left in the file. @@ -478,7 +478,7 @@ static ssize_t fat_read(FAR struct file *filp, char *buffer, size_t buflen) { /* Get offset into the sector where we begin the read */ - int sectorindex = ff->ff_position & SEC_NDXMASK(fs); + int sectorindex = filep->f_pos & SEC_NDXMASK(fs); bytesread = 0; /* Check if the current read stream happens to lie on a @@ -505,7 +505,7 @@ static ssize_t fat_read(FAR struct file *filp, char *buffer, size_t buflen) { /* No.. Handle the case of the first sector of the file */ - if (ff->ff_position == 0) + if (filep->f_pos == 0) { /* Get the first cluster of the file */ @@ -602,10 +602,10 @@ static ssize_t fat_read(FAR struct file *filp, char *buffer, size_t buflen) /* Set up for the next sector read */ - userbuffer += bytesread; - ff->ff_position += bytesread; - readsize += bytesread; - buflen -= bytesread; + userbuffer += bytesread; + filep->f_pos += bytesread; + readsize += bytesread; + buflen -= bytesread; } fat_semgive(fs); @@ -620,7 +620,7 @@ errout_with_semaphore: * Name: fat_write ****************************************************************************/ -static ssize_t fat_write(FAR struct file *filp, const char *buffer, +static ssize_t fat_write(FAR struct file *filep, const char *buffer, size_t buflen) { struct inode *inode; @@ -636,12 +636,12 @@ static ssize_t fat_write(FAR struct file *filp, const char *buffer, /* Sanity checks */ - DEBUGASSERT(filp->f_priv != NULL && filp->f_inode != NULL); + DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL); /* Recover our private data from the struct file instance */ - ff = filp->f_priv; - inode = filp->f_inode; + ff = filep->f_priv; + inode = filep->f_inode; fs = inode->i_private; DEBUGASSERT(fs != NULL); @@ -681,7 +681,7 @@ static ssize_t fat_write(FAR struct file *filp, const char *buffer, { /* Get offset into the sector where we begin the read */ - int sectorindex = ff->ff_position & SEC_NDXMASK(fs); + int sectorindex = filep->f_pos & SEC_NDXMASK(fs); /* Check if the current read stream happens to lie on a * sector boundary. @@ -707,7 +707,7 @@ static ssize_t fat_write(FAR struct file *filp, const char *buffer, { /* No.. Handle the case of the first sector of the file */ - if (ff->ff_position == 0) + if (filep->f_pos == 0) { /* Check the first cluster of the file. Zero means that * the file is empty -- perhaps the file was truncated or @@ -816,12 +816,12 @@ static ssize_t fat_write(FAR struct file *filp, const char *buffer, * We will first have to read the full sector in memory as * part of a read-modify-write operation. NOTE we don't * have to read the data on a rare case: When we are extending - * the file (ff->ff_position == ff->ff_size) -AND- the new data + * the file (filep->f_pos == ff->ff_size) -AND- the new data * happens to be aligned at the beginning of the sector * (sectorindex == 0). */ - if (ff->ff_position < ff->ff_size || sectorindex != 0) + if (filep->f_pos < ff->ff_size || sectorindex != 0) { ff->ff_currentsector = writesector; ret = fat_ffcacheread(fs, ff, writesector); @@ -846,17 +846,17 @@ static ssize_t fat_write(FAR struct file *filp, const char *buffer, /* Set up for the next write */ - userbuffer += writesize; - ff->ff_position += writesize; + userbuffer += writesize; + filep->f_pos += writesize; byteswritten += writesize; - buflen -= writesize; + buflen -= writesize; } /* The transfer has completed without error. Update the file size */ - if (ff->ff_position > ff->ff_size) + if (filep->f_pos > ff->ff_size) { - ff->ff_size = ff->ff_position; + ff->ff_size = filep->f_pos; } fat_semgive(fs); @@ -871,7 +871,7 @@ errout_with_semaphore: * Name: fat_seek ****************************************************************************/ -static off_t fat_seek(FAR struct file *filp, off_t offset, int whence) +static off_t fat_seek(FAR struct file *filep, off_t offset, int whence) { struct inode *inode; struct fat_mountpt_s *fs; @@ -884,12 +884,12 @@ static off_t fat_seek(FAR struct file *filp, off_t offset, int whence) /* Sanity checks */ - DEBUGASSERT(filp->f_priv != NULL && filp->f_inode != NULL); + DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL); /* Recover our private data from the struct file instance */ - ff = filp->f_priv; - inode = filp->f_inode; + ff = filep->f_priv; + inode = filep->f_inode; fs = inode->i_private; DEBUGASSERT(fs != NULL); @@ -904,7 +904,7 @@ static off_t fat_seek(FAR struct file *filp, off_t offset, int whence) case SEEK_CUR: /* The offset is set to its current location plus * offset bytes. */ - position = offset + ff->ff_position; + position = offset + filep->f_pos; break; case SEEK_END: /* The offset is set to the size of the file plus @@ -946,7 +946,7 @@ static off_t fat_seek(FAR struct file *filp, off_t offset, int whence) /* Set file position to the beginning of the file */ - ff->ff_position = 0; + filep->f_pos = 0; ff->ff_sectorsincluster = 1; /* Move file position if necessary */ @@ -1044,8 +1044,8 @@ static off_t fat_seek(FAR struct file *filp, off_t offset, int whence) /* Otherwise, update the position and continue looking */ - ff->ff_position += clustersize; - position -= clustersize; + filep->f_pos += clustersize; + position -= clustersize; } /* We get here after we have found the sector containing @@ -1078,15 +1078,15 @@ static off_t fat_seek(FAR struct file *filp, off_t offset, int whence) /* And save the new file position */ - ff->ff_position += position; + filep->f_pos += position; } } /* If we extended the size of the file, then mark the file as modified. */ - if ((ff->ff_oflags & O_WROK) != 0 && ff->ff_position > ff->ff_size) + if ((ff->ff_oflags & O_WROK) != 0 && filep->f_pos > ff->ff_size) { - ff->ff_size = ff->ff_position; + ff->ff_size = filep->f_pos; ff->ff_bflags |= FFBUFF_MODIFIED; } @@ -1102,7 +1102,7 @@ errout_with_semaphore: * Name: fat_ioctl ****************************************************************************/ -static int fat_ioctl(FAR struct file *filp, int cmd, unsigned long arg) +static int fat_ioctl(FAR struct file *filep, int cmd, unsigned long arg) { struct inode *inode; struct fat_mountpt_s *fs; @@ -1111,12 +1111,12 @@ static int fat_ioctl(FAR struct file *filp, int cmd, unsigned long arg) /* Sanity checks */ - DEBUGASSERT(filp->f_priv != NULL && filp->f_inode != NULL); + DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL); /* Recover our private data from the struct file instance */ - ff = filp->f_priv; - inode = filp->f_inode; + ff = filep->f_priv; + inode = filep->f_inode; fs = inode->i_private; DEBUGASSERT(fs != NULL); @@ -1145,7 +1145,7 @@ static int fat_ioctl(FAR struct file *filp, int cmd, unsigned long arg) * ****************************************************************************/ -static int fat_sync(FAR struct file *filp) +static int fat_sync(FAR struct file *filep) { struct inode *inode; struct fat_mountpt_s *fs; @@ -1156,12 +1156,12 @@ static int fat_sync(FAR struct file *filp) /* Sanity checks */ - DEBUGASSERT(filp->f_priv != NULL && filp->f_inode != NULL); + DEBUGASSERT(filep->f_priv != NULL && filep->f_inode != NULL); /* Recover our private data from the struct file instance */ - ff = filp->f_priv; - inode = filp->f_inode; + ff = filep->f_priv; + inode = filep->f_inode; fs = inode->i_private; DEBUGASSERT(fs != NULL); diff --git a/nuttx/fs/fat/fs_fat32.h b/nuttx/fs/fat/fs_fat32.h index deb3b7766..9cad293bb 100644 --- a/nuttx/fs/fat/fs_fat32.h +++ b/nuttx/fs/fat/fs_fat32.h @@ -513,8 +513,7 @@ struct fat_file_s uint16 ff_dirindex; /* Index into ff_dirsector to directory entry */ uint32 ff_currentcluster; /* Current cluster being accessed */ size_t ff_dirsector; /* Sector containing the directory entry */ - size_t ff_position; /* File position for read/write/seek in bytes */ - size_t ff_size; /* Size of the file in bytes */ + off_t ff_size; /* Size of the file in bytes */ size_t ff_startcluster; /* Start cluster of file on media */ size_t ff_currentsector; /* Current sector in the file buffer */ ubyte *ff_buffer; /* File buffer (for partial sector accesses) */ diff --git a/nuttx/include/stdio.h b/nuttx/include/stdio.h index 92bfdf10f..0a1cc9371 100644 --- a/nuttx/include/stdio.h +++ b/nuttx/include/stdio.h @@ -1,7 +1,7 @@ -/************************************************************ - * stdio.h +/**************************************************************************** + * include/stdio.h * - * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -14,7 +14,7 @@ * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. - * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * 3. Neither the name NuttX nor the names of its contributors may be * used to endorse or promote products derived from this software * without specific prior written permission. * @@ -31,14 +31,14 @@ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - ************************************************************/ + ****************************************************************************/ #ifndef __STDIO_H #define __STDIO_H -/************************************************************ +/**************************************************************************** * Included Files - ************************************************************/ + ****************************************************************************/ #include @@ -50,13 +50,13 @@ #include -/************************************************************ +/**************************************************************************** * Definitions - ************************************************************/ + ****************************************************************************/ -/* File System Definitions **********************************/ +/* File System Definitions **************************************************/ -/* File system error values *********************************/ +/* File system error values *************************************************/ #define EOF (-1) @@ -74,24 +74,23 @@ #define putchar(c) fputc(c, stdout) #define getc(s) fgetc(s) #define getchar() fgetc(stdin) -#define ftell(s) fseek((s),0,SEEK_CUR) #define rewind(s) ((void)fseek((s),0,SEEK_SET)) -/************************************************************ +/**************************************************************************** * Public Type Definitions - ************************************************************/ + ****************************************************************************/ /* Streams */ typedef FAR struct file_struct FILE; -/************************************************************ +/**************************************************************************** * Public Variables - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Public Function Prototypes - ************************************************************/ + ****************************************************************************/ #undef EXTERN #if defined(__cplusplus) @@ -108,16 +107,17 @@ EXTERN int fflush(FILE *stream); EXTERN int feof(FILE *stream); EXTERN int ferror(FILE *stream); EXTERN int fgetc(FILE *stream); +EXTERN int fgetpos(FILE *stream, fpos_t *pos); EXTERN char *fgets(char *s, int n, FILE *stream); EXTERN FILE *fopen(const char *path, const char *type); EXTERN int fprintf(FILE *stream, const char *format, ...); EXTERN int fputc(int c, FILE *stream); EXTERN int fputs(const char *s, FILE *stream); -EXTERN size_t fread(void *ptr, size_t size, size_t n_items, - FILE *stream); -EXTERN int fseek(FILE *stream, long int offset, int whence); -EXTERN size_t fwrite(const void *ptr, size_t size, - size_t n_items, FILE *stream); +EXTERN size_t fread(void *ptr, size_t size, size_t n_items, FILE *stream); +EXTERN int fseek(FAR FILE *stream, long int offset, int whence); +EXTERN int fsetpos(FILE *stream, fpos_t *pos); +EXTERN long ftell(FAR FILE *stream); +EXTERN size_t fwrite(const void *ptr, size_t size, size_t n_items, FILE *stream); EXTERN char *gets(char *s); EXTERN int printf(const char *format, ...); diff --git a/nuttx/include/sys/types.h b/nuttx/include/sys/types.h index 99736e41e..85eba55d4 100644 --- a/nuttx/include/sys/types.h +++ b/nuttx/include/sys/types.h @@ -123,6 +123,7 @@ typedef sint32 off_t; typedef uint16 blksize_t; typedef uint32 blkcnt_t; #endif +typedef off_t fpos_t; typedef sint16 uid_t; typedef sint16 gid_t; typedef uint16 dev_t; diff --git a/nuttx/lib/Makefile b/nuttx/lib/Makefile index a56cde66f..d35abb129 100644 --- a/nuttx/lib/Makefile +++ b/nuttx/lib/Makefile @@ -60,8 +60,9 @@ ifneq ($(CONFIG_NFILE_DESCRIPTORS),0) STDIO_SRCS += lib_rawstream.c ifneq ($(CONFIG_NFILE_STREAMS),0) STDIO_SRCS += lib_fopen.c lib_fclose.c lib_fread.c lib_libfread.c lib_fseek.c \ - lib_fgetc.c lib_fgets.c lib_gets.c lib_fwrite.c lib_libfwrite.c \ - lib_fflush.c lib_libflushall.c lib_libfflush.c lib_rdflush.c lib_wrflush.c \ + lib_ftell.c lib_fsetpos.c lib_fgetpos.c lib_fgetc.c lib_fgets.c \ + lib_gets.c lib_fwrite.c lib_libfwrite.c lib_fflush.c \ + lib_libflushall.c lib_libfflush.c lib_rdflush.c lib_wrflush.c \ lib_fputc.c lib_puts.c lib_fputs.c lib_ungetc.c lib_vprintf.c \ lib_fprintf.c lib_vfprintf.c lib_stdstream.c endif diff --git a/nuttx/lib/lib_fgetpos.c b/nuttx/lib/lib_fgetpos.c new file mode 100644 index 000000000..92044a54c --- /dev/null +++ b/nuttx/lib/lib_fgetpos.c @@ -0,0 +1,125 @@ +/**************************************************************************** + * lib/lib_fgetpos.c + * + * Copyright (C) 2008 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Compilation Switches + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include +#include + +#include "lib_internal.h" + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Type Declarations + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Global Constant Data + ****************************************************************************/ + +/**************************************************************************** + * Global Variables + ****************************************************************************/ + +/**************************************************************************** + * Private Constant Data + ****************************************************************************/ + +/**************************************************************************** + * Private Variables + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: fgetpos + * + * Description: + * fgetpos() function is an alternate interfaces equivalent to ftell(). + * It gets the current value of the file offset and store it in the location + * referenced by pos. On some non-UNIX systems an fpos_t object may be a + * complex object and fsetpos may be the only way to portably reposition a + * stream. + * + * Returned Value: + * Zero on succes; -1 on failure with errno set appropriately. + * + ****************************************************************************/ + +int fgetpos(FILE *stream, fpos_t *pos) +{ + long position; + +#if CONFIG_DEBUG + if (!stream || !pos) + { + errno = EINVAL; + return ERROR; + } +#endif + + position = ftell(stream, (off_t)*pos, SEEK_SET); + if (position == -1) + { + return ERROR; + } + + *pos = (fpos_t)pos; + return OK; +} diff --git a/nuttx/lib/lib_fseek.c b/nuttx/lib/lib_fseek.c index 18781b6e3..379e5c322 100644 --- a/nuttx/lib/lib_fseek.c +++ b/nuttx/lib/lib_fseek.c @@ -89,11 +89,25 @@ /**************************************************************************** * Name: fseek + * + * Description: + * The fseek() function sets the file position indicator for the stream + * pointed to by stream. The new position, measured in bytes, is obtained + * by adding offset bytes to the position specified by whence. If whence is + * set to SEEK_SET, SEEK_CUR, or SEEK_END, the offset is relative to the + * start of the file, the current position indicator, or end-of-file, + * respectively. A successful call to the fseek() function clears the + * end-of-file indicator for the stream and undoes any effects of the ungetc(3) + * function on the same stream. + * + * Returned Value: + * Zero on succes; -1 on failure with errno set appropriately. + * ****************************************************************************/ -int fseek(FILE *stream, long int offset, int whence) +int fseek(FAR FILE *stream, long int offset, int whence) { -#if CONFIG_STDIO_BUFFER_SIZE > 0 + #if CONFIG_STDIO_BUFFER_SIZE > 0 /* Flush any valid read/write data in the buffer (also verifies stream) */ if (lib_rdflush(stream) < 0 || lib_wrflush(stream) < 0) @@ -105,14 +119,20 @@ int fseek(FILE *stream, long int offset, int whence) if (!stream) { - *get_errno_ptr() = EBADF; + errno = EBADF; return ERROR; } #endif + /* On success or failure, discard any characters saved by ungetc() */ + +#if CONFIG_NUNGET_CHARS > 0 + stream->fs_nungotten = 0; +#endif + /* Perform the fseek on the underlying file descriptor */ - return lseek(stream->fs_filedes, offset, whence) >= 0 ? OK : ERROR; + return lseek(stream->fs_filedes, offset, whence) == (off_t)-1 ? ERROR : OK; } diff --git a/nuttx/lib/lib_fsetpos.c b/nuttx/lib/lib_fsetpos.c new file mode 100644 index 000000000..6a5fc7e9f --- /dev/null +++ b/nuttx/lib/lib_fsetpos.c @@ -0,0 +1,116 @@ +/**************************************************************************** + * lib/lib_fsetpos.c + * + * Copyright (C) 2008 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Compilation Switches + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include +#include + +#include "lib_internal.h" + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Type Declarations + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Global Constant Data + ****************************************************************************/ + +/**************************************************************************** + * Global Variables + ****************************************************************************/ + +/**************************************************************************** + * Private Constant Data + ****************************************************************************/ + +/**************************************************************************** + * Private Variables + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: fsetpos + * + * Description: + * fsetpos() function is an alternate interfaces equivalent to fseek() + * (with whence set to SEEK_SET). It sets the current value of the file + * offset to value in the location referenced by pos. On some non-UNIX + * systems an fpos_t object may be a complex object and fsetpos may be the + * only way to portably reposition a stream. + * + * Returned Value: + * Zero on succes; -1 on failure with errno set appropriately. + * + ****************************************************************************/ + +int fsetpos(FILE *stream, fpos_t *pos) +{ +#if CONFIG_DEBUG + if (!stream || !pos) + { + errno = EINVAL; + return ERROR; + } +#endif + + return fseek(stream, (off_t)*pos, SEEK_SET); +} diff --git a/nuttx/lib/lib_ftell.c b/nuttx/lib/lib_ftell.c new file mode 100644 index 000000000..3b3bee775 --- /dev/null +++ b/nuttx/lib/lib_ftell.c @@ -0,0 +1,129 @@ +/**************************************************************************** + * lib/lib_ftell.c + * + * Copyright (C) 2008 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in + * the documentation and/or other materials provided with the + * distribution. + * 3. Neither the name NuttX nor the names of its contributors may be + * used to endorse or promote products derived from this software + * without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS + * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE + * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, + * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, + * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS + * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED + * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN + * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + * POSSIBILITY OF SUCH DAMAGE. + * + ****************************************************************************/ + +/**************************************************************************** + * Compilation Switches + ****************************************************************************/ + +/**************************************************************************** + * Included Files + ****************************************************************************/ + +#include + +#include +#include +#include +#include +#include + +#include "lib_internal.h" + +/**************************************************************************** + * Definitions + ****************************************************************************/ + +/**************************************************************************** + * Private Type Declarations + ****************************************************************************/ + +/**************************************************************************** + * Private Function Prototypes + ****************************************************************************/ + +/**************************************************************************** + * Global Constant Data + ****************************************************************************/ + +/**************************************************************************** + * Global Variables + ****************************************************************************/ + +/**************************************************************************** + * Private Constant Data + ****************************************************************************/ + +/**************************************************************************** + * Private Variables + ****************************************************************************/ + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: ftell + * + * Description: + * ftell() returns the current value of the file position indicator for the + * stream pointed to by stream. + * + * Returned Value: + * Zero on succes; -1 on failure with errno set appropriately. + * + ****************************************************************************/ + +long ftell(FAR FILE *stream) +{ + off_t position; + + /* Verify that we were provided with a stream */ + + if (!stream) + { + errno = EBADF; + return ERROR; + } + + /* Perform the lseek to the current position. This will not move the + * file pointer, but will return its current setting + */ + + position = lseek(stream->fs_filedes, 0, SEEK_CUR); + if (position != (off_t)-1) + { + return (long)position; + } + else + { + return ERROR; + } +} + + diff --git a/nuttx/lib/lib_ungetc.c b/nuttx/lib/lib_ungetc.c index 34f760aa2..9831244d8 100644 --- a/nuttx/lib/lib_ungetc.c +++ b/nuttx/lib/lib_ungetc.c @@ -1,7 +1,7 @@ -/************************************************************ +/**************************************************************************** * lib_ungetc.c * - * Copyright (C) 2007 Gregory Nutt. All rights reserved. + * Copyright (C) 2007, 2008 Gregory Nutt. All rights reserved. * Author: Gregory Nutt * * Redistribution and use in source and binary forms, with or without @@ -14,7 +14,7 @@ * notice, this list of conditions and the following disclaimer in * the documentation and/or other materials provided with the * distribution. - * 3. Neither the name Gregory Nutt nor the names of its contributors may be + * 3. Neither the name NuttX nor the names of its contributors may be * used to endorse or promote products derived from this software * without specific prior written permission. * @@ -31,15 +31,15 @@ * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE * POSSIBILITY OF SUCH DAMAGE. * - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Compilation Switches - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Included Files - ************************************************************/ + ****************************************************************************/ #include #include @@ -48,45 +48,45 @@ #include #include "lib_internal.h" -/************************************************************ +/**************************************************************************** * Definitions - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Private Type Declarations - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Private Function Prototypes - ************************************************************/ + ****************************************************************************/ -/************************************************************ +/**************************************************************************** * Global Function Prototypes - ************************************************************/ + ****************************************************************************/ -/********************************************************** +/************************************************************************** * Global Constant Data - **********************************************************/ + **************************************************************************/ -/************************************************************ +/**************************************************************************** * Global Variables - ************************************************************/ + ****************************************************************************/ -/********************************************************** +/************************************************************************** * Private Constant Data - **********************************************************/ + **************************************************************************/ -/************************************************************ +/**************************************************************************** * Private Variables - **********************************************************/ + **************************************************************************/ -/************************************************************ +/**************************************************************************** * Global Functions - **********************************************************/ + **************************************************************************/ -/************************************************************ - * fgetc - **********************************************************/ +/**************************************************************************** + * Name: ungetc + **************************************************************************/ int ungetc(int c, FILE *stream) { @@ -99,7 +99,7 @@ int ungetc(int c, FILE *stream) if ((stream && stream->fs_filedes < 0) || ((stream->fs_oflags & O_RDOK) == 0)) { - *get_errno_ptr() = EBADF; + errno = EBADF; return EOF; } @@ -114,7 +114,7 @@ int ungetc(int c, FILE *stream) else #endif { - *get_errno_ptr() = ENOMEM; + errno = ENOMEM; return EOF; } }