Disable line buffering if the file is opened in binary mode; Also fix a couple of fopen/fdopen bugs
git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@4630 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
parent
675c2e46b2
commit
44d31f7a80
@ -2663,4 +2663,11 @@
|
||||
initial check-in is just a stm3210e-eval driver with renaming).
|
||||
* sched/sched_setscheduler.c: Correct successful return value (Contributed
|
||||
by Richard Cochran).
|
||||
* include/fcntl.h and lib/stdio: Ignore CONFIG_STDIO_LINEBUFFER is the
|
||||
file was opened in binary mode.
|
||||
* lib/stdio/lib_fopen.c: Correct an error in parsing open mode string. The
|
||||
plus sign may not appear right after the basic mode. For example, "r+", "rb+",
|
||||
and "r+b" are all value open strings and mean the same thing.
|
||||
* lib/stdio/lib_fopen.c: Correct return errno value from f_open() and
|
||||
f_fdopen() if the open mode string is invalid.
|
||||
|
||||
|
@ -237,7 +237,7 @@ FAR struct file_struct *fs_fdopen(int fd, int oflags, FAR _TCB *tcb)
|
||||
*/
|
||||
|
||||
stream->fs_filedes = fd;
|
||||
stream->fs_oflags = oflags;
|
||||
stream->fs_oflags = (uint16_t)oflags;
|
||||
|
||||
sem_post(&slist->sl_sem);
|
||||
return stream;
|
||||
|
@ -1,8 +1,8 @@
|
||||
/********************************************************************************
|
||||
* include/fcntl.h
|
||||
*
|
||||
* Copyright (C) 2007-2009 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
||||
* Copyright (C) 2007-2009, 2012 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
@ -51,24 +51,33 @@
|
||||
|
||||
/* open flag settings for open() (and related APIs) */
|
||||
|
||||
#define O_RDONLY 0x01 /* Open for read access */
|
||||
#define O_WRONLY 0x02 /* Open for write access */
|
||||
#define O_RDWR 0x03 /* Open for both read & write access */
|
||||
#define O_CREAT 0x04 /* Create file/sem/mq object */
|
||||
#define O_EXCL 0x08 /* Name must not exist when opened */
|
||||
#define O_APPEND 0x10 /* Keep contents, append to end */
|
||||
#define O_TRUNC 0x20 /* Delete contents */
|
||||
#define O_NONBLOCK 0x40 /* Don't wait for data */
|
||||
#define O_NDELAY O_NONBLOCK
|
||||
#define O_SYNC 0x80 /* Synchronize output on write */
|
||||
#define O_DSYNC O_SYNC
|
||||
#define O_RDONLY (1 << 0) /* Open for read access (only) */
|
||||
#define O_RDOK O_RDONLY /* Read access is permitted (non-standard) */
|
||||
#define O_WRONLY (1 << 1) /* Open for write access (only) */
|
||||
#define O_WROK O_WRONLY /* Write access is permitted (non-standard) */
|
||||
#define O_RDWR (O_RDOK|O_WROK) /* Open for both read & write access */
|
||||
#define O_CREAT (1 << 2) /* Create file/sem/mq object */
|
||||
#define O_EXCL (1 << 3) /* Name must not exist when opened */
|
||||
#define O_APPEND (1 << 4) /* Keep contents, append to end */
|
||||
#define O_TRUNC (1 << 5) /* Delete contents */
|
||||
#define O_NONBLOCK (1 << 6) /* Don't wait for data */
|
||||
#define O_NDELAY O_NONBLOCK /* Synonym for O_NONBLOCK */
|
||||
#define O_SYNC (1 << 7) /* Synchronize output on write */
|
||||
#define O_DSYNC O_SYNC /* Equivalent to OSYNC in NuttX */
|
||||
#define O_BINARY (1 << 8) /* Open the file in binary (untranslated) mode. */
|
||||
|
||||
#define O_RSYNC 0x00 /* Sychronize input on read */
|
||||
#define O_ACCMODE 0x00 /* Required by POSIX */
|
||||
#define O_NOCTTY 0x00 /* Reqired by POSIX */
|
||||
/* Unsupported, but required open flags */
|
||||
|
||||
#define O_RDOK O_RDONLY /* Not POSIX */
|
||||
#define O_WROK O_WRONLY /* Not POSIX */
|
||||
#define O_RSYNC 0 /* Synchronize input on read */
|
||||
#define O_ACCMODE 0 /* Required by POSIX */
|
||||
#define O_NOCTTY 0 /* Required by POSIX */
|
||||
#defone O_TEXT 0 /* Open the file in text (translated) mode. */
|
||||
|
||||
/* This is the highest bit number used in the open flags bitset. Bits above
|
||||
* this bit number may be used within NuttX for other, internal purposes.
|
||||
*/
|
||||
|
||||
#define _O_MAXBIT 8
|
||||
|
||||
/* fcntl() commands */
|
||||
|
||||
|
@ -270,7 +270,7 @@ struct filelist
|
||||
struct file_struct
|
||||
{
|
||||
int fs_filedes; /* File descriptor associated with stream */
|
||||
mode_t fs_oflags; /* Open mode flags */
|
||||
uint16_t fs_oflags; /* Open mode flags */
|
||||
#if CONFIG_NUNGET_CHARS > 0
|
||||
uint8_t fs_nungotten; /* The number of characters buffered for ungetc */
|
||||
unsigned char fs_ungotten[CONFIG_NUNGET_CHARS];
|
||||
|
@ -1,8 +1,8 @@
|
||||
/****************************************************************************
|
||||
* include/sys/stat.h
|
||||
*
|
||||
* Copyright (C) 2007-2009 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
||||
* Copyright (C) 2007-2009, 2012 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
@ -47,10 +47,9 @@
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/* mode_t bit settings (most of these do not apply to Nuttx).
|
||||
* This assumes that the full size of a mode_t is 16-bits.
|
||||
* (However, mode_t must be size 'int' because it is promoted
|
||||
* to size int when passed in varargs).
|
||||
/* mode_t bit settings (most of these do not apply to Nuttx). This assumes
|
||||
* that the full size of a mode_t is 16-bits. (However, mode_t must be size
|
||||
* 'int' because it is promoted to size int when passed in varargs).
|
||||
*/
|
||||
|
||||
#define S_IXOTH 0000001 /* Permissions for others: RWX */
|
||||
|
@ -53,6 +53,11 @@
|
||||
/****************************************************************************
|
||||
* Definitions
|
||||
****************************************************************************/
|
||||
/* This configuration directory is used in environment variable processing
|
||||
* when we need to reference the user's home directory. There are no user
|
||||
* directories in NuttX so, by default, this always refers to the root
|
||||
* directory.
|
||||
*/
|
||||
|
||||
#ifndef CONFIG_LIB_HOMEDIR
|
||||
# define CONFIG_LIB_HOMEDIR "/"
|
||||
|
@ -1,8 +1,8 @@
|
||||
/****************************************************************************
|
||||
* lib/stdio/lib_fopen.c
|
||||
*
|
||||
* Copyright (C) 2007-2011 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
||||
* Copyright (C) 2007-2012 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
@ -48,6 +48,25 @@
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
* Pre-processor Definitions
|
||||
****************************************************************************/
|
||||
|
||||
/****************************************************************************
|
||||
* Private Types
|
||||
****************************************************************************/
|
||||
|
||||
enum open_mode_e
|
||||
{
|
||||
MODE_NONE = 0, /* No access mode determined */
|
||||
MODE_R, /* "r" or "rb" open for reading */
|
||||
MODE_W, /* "w" or "wb" open for writing, truncating or creating file */
|
||||
MODE_A, /* "a" or "ab" open for writing, appending to file */
|
||||
MODE_RPLUS, /* "r+", "rb+", or "r+b" open for update (reading and writing) */
|
||||
MODE_WPLUS, /* "w+", "wb+", or "w+b" open for update, truncating or creating file */
|
||||
MODE_APLUS, /* "a+", "ab+", or "a+b" open for update, appending to file */
|
||||
};
|
||||
|
||||
/****************************************************************************
|
||||
* Private Functions
|
||||
****************************************************************************/
|
||||
@ -58,77 +77,159 @@
|
||||
|
||||
static int lib_mode2oflags(FAR const char *mode)
|
||||
{
|
||||
int oflags = 0;
|
||||
if (mode)
|
||||
enum open_mode_e state;
|
||||
int oflags;
|
||||
|
||||
/* Verify that a mode string was provided. No error is */
|
||||
|
||||
if (!mode)
|
||||
{
|
||||
while(*mode)
|
||||
goto errout;
|
||||
}
|
||||
|
||||
/* Parse the mode string to determine the corresponding open flags */
|
||||
|
||||
state = MODE_NONE;
|
||||
oflags = 0;
|
||||
|
||||
for (; *mode; mode++)
|
||||
{
|
||||
switch (*mode)
|
||||
{
|
||||
switch (*mode)
|
||||
{
|
||||
/* Open for read access */
|
||||
/* Open for read access ("r", "r[+]", "r[b]", "r[b+]", or "r[+b]") */
|
||||
|
||||
case 'r' :
|
||||
if (*(mode + 1) == '+')
|
||||
{
|
||||
/* Open for read/write access */
|
||||
case 'r' :
|
||||
if (state == MODE_NONE)
|
||||
{
|
||||
/* Open for read access */
|
||||
|
||||
oflags |= O_RDWR;
|
||||
mode++;
|
||||
oflags = O_RDOK;
|
||||
state = MODE_R;
|
||||
}
|
||||
else
|
||||
{
|
||||
goto errout;
|
||||
}
|
||||
break;
|
||||
|
||||
/* Open for write access ("w", "w[+]", "w[b]", "w[b+]", or "w[+b]") */
|
||||
|
||||
case 'w' :
|
||||
if (state == MODE_NONE)
|
||||
{
|
||||
/* Open for write access, truncating any existing file */
|
||||
|
||||
oflags = O_WROK|O_CREAT|O_TRUNC;
|
||||
state = MODE_W;
|
||||
}
|
||||
else
|
||||
{
|
||||
goto errout;
|
||||
}
|
||||
break;
|
||||
|
||||
/* Open for write/append access ("a", "a[+]", "a[b]", "a[b+]", or "a[+b]") */
|
||||
|
||||
case 'a' :
|
||||
if (state == MODE_NONE)
|
||||
{
|
||||
/* Write to the end of the file */
|
||||
|
||||
oflags = O_WROK|O_CREAT|O_APPEND;
|
||||
state = MODE_A;
|
||||
}
|
||||
else
|
||||
{
|
||||
goto errout;
|
||||
}
|
||||
break;
|
||||
|
||||
/* Open for update access ("[r]+", "[rb]+]", "[r]+[b]", "[w]+",
|
||||
* "[wb]+]", "[w]+[b]", "[a]+", "[ab]+]", "[a]+[b]")
|
||||
*/
|
||||
|
||||
case '+' :
|
||||
switch (state)
|
||||
{
|
||||
case MODE_R:
|
||||
{
|
||||
/* Retain any binary mode selection */
|
||||
|
||||
oflags &= O_BINARY;
|
||||
|
||||
/* Open for read/write access */
|
||||
|
||||
oflags |= O_RDWR;
|
||||
state = MODE_RPLUS;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Open for read access */
|
||||
break;
|
||||
|
||||
oflags |= O_RDOK;
|
||||
}
|
||||
break;
|
||||
case MODE_W:
|
||||
{
|
||||
/* Retain any binary mode selection */
|
||||
|
||||
/* Open for write access? */
|
||||
oflags &= O_BINARY;
|
||||
|
||||
case 'w' :
|
||||
if (*(mode + 1) == '+')
|
||||
{
|
||||
/* Open for write read/access, truncating any existing file */
|
||||
/* Open for write read/access, truncating any existing file */
|
||||
|
||||
oflags |= O_RDWR|O_CREAT|O_TRUNC;
|
||||
mode++;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Open for write access, truncating any existing file */
|
||||
oflags |= O_RDWR|O_CREAT|O_TRUNC;
|
||||
state = MODE_WPLUS;
|
||||
}
|
||||
break;
|
||||
|
||||
oflags |= O_WROK|O_CREAT|O_TRUNC;
|
||||
}
|
||||
break;
|
||||
case MODE_A:
|
||||
{
|
||||
/* Retain any binary mode selection */
|
||||
|
||||
/* Open for write/append access? */
|
||||
oflags &= O_BINARY;
|
||||
|
||||
case 'a' :
|
||||
if (*(mode + 1) == '+')
|
||||
{
|
||||
/* Read from the beginning of the file; write to the end */
|
||||
/* Read from the beginning of the file; write to the end */
|
||||
|
||||
oflags |= O_RDWR|O_CREAT|O_APPEND;
|
||||
mode++;
|
||||
}
|
||||
else
|
||||
{
|
||||
/* Write to the end of the file */
|
||||
oflags |= O_RDWR|O_CREAT|O_APPEND;
|
||||
state = MODE_APLUS;
|
||||
}
|
||||
break;
|
||||
|
||||
oflags |= O_WROK|O_CREAT|O_APPEND;
|
||||
}
|
||||
break;
|
||||
default:
|
||||
goto errout;
|
||||
break;
|
||||
}
|
||||
break;
|
||||
|
||||
/* Open for binary access? */
|
||||
/* Open for binary access ("[r]b", "[r]b[+]", "[r+]b", "[w]b",
|
||||
* "[w]b[+]", "[w+]b", "[a]b", "[a]b[+]", "[a+]b")
|
||||
*/
|
||||
|
||||
case 'b' :
|
||||
default:
|
||||
break;
|
||||
}
|
||||
mode++;
|
||||
case 'b' :
|
||||
if (state != MODE_NONE)
|
||||
{
|
||||
/* The file is opened in binary mode */
|
||||
|
||||
oflags |= O_BINARY;
|
||||
}
|
||||
else
|
||||
{
|
||||
goto errout;
|
||||
}
|
||||
break;
|
||||
|
||||
/* Unrecognized or unsupported mode */
|
||||
|
||||
default:
|
||||
goto errout;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return oflags;
|
||||
|
||||
/* Both fopen and fdopen should fail with errno == EINVAL if the mode
|
||||
* string is invalid.
|
||||
*/
|
||||
|
||||
errout:
|
||||
set_errno(EINVAL);
|
||||
return ERROR;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
@ -141,7 +242,18 @@ static int lib_mode2oflags(FAR const char *mode)
|
||||
|
||||
FAR FILE *fdopen(int fd, FAR const char *mode)
|
||||
{
|
||||
return fs_fdopen(fd, lib_mode2oflags(mode), NULL);
|
||||
FAR FILE *ret = NULL;
|
||||
int oflags;
|
||||
|
||||
/* Map the open mode string to open flags */
|
||||
|
||||
oflags = lib_mode2oflags(mode);
|
||||
if (oflags >= 0)
|
||||
{
|
||||
ret = fs_fdopen(fd, oflags, NULL);
|
||||
}
|
||||
|
||||
return ret;
|
||||
}
|
||||
|
||||
/****************************************************************************
|
||||
@ -154,9 +266,16 @@ FAR FILE *fopen(FAR const char *path, FAR const char *mode)
|
||||
int oflags;
|
||||
int fd;
|
||||
|
||||
/* Open the file */
|
||||
/* Map the open mode string to open flags */
|
||||
|
||||
oflags = lib_mode2oflags(mode);
|
||||
if (oflags < 0)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
/* Open the file */
|
||||
|
||||
fd = open(path, oflags, 0666);
|
||||
|
||||
/* If the open was successful, then fdopen() the fil using the file
|
||||
|
@ -1,8 +1,8 @@
|
||||
/****************************************************************************
|
||||
* lib/stdio/lib_fputc.c
|
||||
*
|
||||
* Copyright (C) 2007, 2008, 2011 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
||||
* Copyright (C) 2007, 2008, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
@ -1,8 +1,8 @@
|
||||
/****************************************************************************
|
||||
* lib/stdio/lib_fputs.c
|
||||
*
|
||||
* Copyright (C) 2007, 2008, 2011 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
||||
* Copyright (C) 2007, 2008, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
@ -1,8 +1,8 @@
|
||||
/****************************************************************************
|
||||
* lib/stdio/lib_libnoflush.c
|
||||
*
|
||||
* Copyright (C) 2011 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
||||
* Copyright (C) 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
@ -1,8 +1,8 @@
|
||||
/****************************************************************************
|
||||
* lib/stdio/lib_lowoutstream.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
||||
* Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
@ -1,8 +1,8 @@
|
||||
/****************************************************************************
|
||||
* lib/stdio/lib_memoutstream.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
||||
* Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
@ -1,8 +1,8 @@
|
||||
/****************************************************************************
|
||||
* lib/stdio/lib_nulloutstream.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
||||
* Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
@ -1,8 +1,8 @@
|
||||
/****************************************************************************
|
||||
* lib/stdio/lib_puts.c
|
||||
*
|
||||
* Copyright (C) 2007, 2008, 2011 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
||||
* Copyright (C) 2007, 2008, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
@ -112,7 +112,7 @@ int puts(FAR const char *s)
|
||||
{
|
||||
nput = nwritten + 1;
|
||||
|
||||
/* Flush the buffer after the newline is output */
|
||||
/* Flush the buffer after the newline is output. */
|
||||
|
||||
#ifdef CONFIG_STDIO_LINEBUFFER
|
||||
ret = lib_fflush(stream, true);
|
||||
|
@ -1,8 +1,8 @@
|
||||
/****************************************************************************
|
||||
* lib/stdio/lib_rawoutstream.c
|
||||
*
|
||||
* Copyright (C) 2007-2009, 2011 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <spudmonkey@racsa.co.cr>
|
||||
* Copyright (C) 2007-2009, 2011-2012 Gregory Nutt. All rights reserved.
|
||||
* Author: Gregory Nutt <gnutt@nuttx.org>
|
||||
*
|
||||
* Redistribution and use in source and binary forms, with or without
|
||||
* modification, are permitted provided that the following conditions
|
||||
|
@ -37,6 +37,8 @@
|
||||
* Included Files
|
||||
****************************************************************************/
|
||||
|
||||
#include <fcntl.h>
|
||||
|
||||
#include "lib_internal.h"
|
||||
|
||||
/****************************************************************************
|
||||
@ -95,14 +97,32 @@ int stdoutstream_flush(FAR struct lib_outstream_s *this)
|
||||
void lib_stdoutstream(FAR struct lib_stdoutstream_s *stdoutstream,
|
||||
FAR FILE *stream)
|
||||
{
|
||||
/* Select the put operation */
|
||||
|
||||
stdoutstream->public.put = stdoutstream_putc;
|
||||
|
||||
/* Select the correct flush operation. This flush is only called when
|
||||
* a newline is encountered in the output stream. However, we do not
|
||||
* want to support this line buffering behavior if the stream was
|
||||
* opened in binary mode. In binary mode, the newline has no special
|
||||
* meaning.
|
||||
*/
|
||||
|
||||
#ifdef CONFIG_STDIO_LINEBUFFER
|
||||
#if CONFIG_STDIO_BUFFER_SIZE > 0
|
||||
stdoutstream->public.flush = stdoutstream_flush;
|
||||
#else
|
||||
stdoutstream->public.flush = lib_noflush;
|
||||
if ((stream->fs_oflags & O_BINARY) == 0)
|
||||
{
|
||||
stdoutstream->public.flush = stdoutstream_flush;
|
||||
}
|
||||
else
|
||||
#endif
|
||||
{
|
||||
stdoutstream->public.flush = lib_noflush;
|
||||
}
|
||||
#endif
|
||||
|
||||
/* Set the number of bytes put to zero and remember the stream */
|
||||
|
||||
stdoutstream->public.nput = 0;
|
||||
stdoutstream->stream = stream;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user