hwdef: add more posix methods

This commit is contained in:
Siddharth Purohit 2018-02-02 01:01:44 +05:30 committed by Andrew Tridgell
parent 288f6b1fa0
commit 539d5adc13
4 changed files with 98 additions and 11 deletions

View File

@ -247,6 +247,9 @@ fgetc(FILE *stream)
return (c); return (c);
} }
int getc(FILE *fp) {
return (fgetc (fp));
}
/// @brief Put a byte to TTY device or FatFs file stream /// @brief Put a byte to TTY device or FatFs file stream
/// open() or fopen() sets stream->put = fatfs_outc() for FatFs functions /// open() or fopen() sets stream->put = fatfs_outc() for FatFs functions
/// See fdevopen() sets stream->put get for TTY devices /// See fdevopen() sets stream->put get for TTY devices
@ -298,6 +301,10 @@ fputc(int c, FILE *stream)
} }
} }
void clearerr(FILE *stream)
{
stream->flags = 0;
}
///@brief functions normally defined as macros ///@brief functions normally defined as macros
@ -423,6 +430,23 @@ fgets(char *str, int size, FILE *stream)
return(str); return(str);
} }
/** char *gets(p) -- get line from stdin */
char *
gets (char *p)
{
char *s;
int n;
s = fgets (p, MAXLN, stdin);
if (s == 0)
return (0);
n = strlen (p);
if (n && p[n - 1] == '\n')
p[n - 1] = 0;
return (s);
}
/// @brief put a string to stdout /// @brief put a string to stdout
/// See fdevopen() sets stream->put get for TTY devices /// See fdevopen() sets stream->put get for TTY devices
/// ///
@ -774,7 +798,7 @@ FILE *fopen(const char *path, const char *mode)
/// @return count on sucess. /// @return count on sucess.
/// @return 0 or < size on error with errno set. /// @return 0 or < size on error with errno set.
size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream) size_t __wrap_fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
{ {
size_t count = size * nmemb; size_t count = size * nmemb;
int fn = fileno(stream); int fn = fileno(stream);
@ -1199,7 +1223,14 @@ ssize_t write(int fd, const void *buf, size_t count)
return ((ssize_t) size); return ((ssize_t) size);
} }
FILE * __wrap_freopen ( const char * filename, const char * mode, FILE * stream )
{
int ret = close(stream);
if (ret < 0) {
return NULL;
}
return fopen(filename, mode);
}
/// @brief POSIX close a file stream. /// @brief POSIX close a file stream.
/// ///
/// - man page flose (3). /// - man page flose (3).
@ -1208,8 +1239,8 @@ ssize_t write(int fd, const void *buf, size_t count)
/// @return 0 on sucess. /// @return 0 on sucess.
/// @return -1 on error witrh errno set. /// @return -1 on error witrh errno set.
/*
int fclose(FILE *stream) int __wrap_fclose(FILE *stream)
{ {
int fn = fileno(stream); int fn = fileno(stream);
if(fn < 0) if(fn < 0)
@ -1217,7 +1248,6 @@ int fclose(FILE *stream)
return( close(fn) ); return( close(fn) );
} }
*/
// ============================================= // =============================================
// ============================================= // =============================================
/// - POSIX file information functions /// - POSIX file information functions
@ -1679,6 +1709,19 @@ int unlink(const char *pathname)
return(0); return(0);
} }
int remove(const char *pathname)
{
errno = 0;
int res = f_unlink(pathname);
if(res != FR_OK)
{
errno = fatfs_to_errno(res);
return(-1);
}
return(0);
}
// ============================================= // =============================================
// ============================================= // =============================================
/// - POSIX - directory scanning functions /// - POSIX - directory scanning functions

View File

@ -35,6 +35,10 @@ along with this program. If not, see <http://www.gnu.org/licenses/>.
#include <ff.h> #include <ff.h>
#include <stdarg.h> #include <stdarg.h>
#include <time.h> #include <time.h>
#define MAXLN 128
#define ISSPACE " \t\n\r\f\v"
///@brief make sure we use our strerror_r function ///@brief make sure we use our strerror_r function
#ifdef __cplusplus #ifdef __cplusplus
@ -46,6 +50,8 @@ extern "C" {
/// - Using these makes code portable accross many acrchitectures /// - Using these makes code portable accross many acrchitectures
//typedef uint32_t blkcnt_t; /*< blkcnt_t for this architecture */ //typedef uint32_t blkcnt_t; /*< blkcnt_t for this architecture */
//typedef uint32_t blksize_t; /*< blksize_t for this architecture */ //typedef uint32_t blksize_t; /*< blksize_t for this architecture */
typedef int32_t off_t;
typedef off_t fpos_t;
extern int errno; extern int errno;
// ============================================= // =============================================
@ -297,6 +303,7 @@ extern FILE *__iob[MAX_FILES];
int isatty ( int fileno ); int isatty ( int fileno );
int fgetc ( FILE *stream ); int fgetc ( FILE *stream );
int fputc ( int c , FILE *stream ); int fputc ( int c , FILE *stream );
void clearerr(FILE *stream);
#ifndef IO_MACROS #ifndef IO_MACROS
int getchar ( void ); int getchar ( void );
int putchar ( int c ); int putchar ( int c );
@ -321,7 +328,7 @@ int close ( int fileno );
int fileno ( FILE *stream ); int fileno ( FILE *stream );
FILE *fileno_to_stream ( int fileno ); FILE *fileno_to_stream ( int fileno );
FILE *fopen ( const char *path , const char *mode ); FILE *fopen ( const char *path , const char *mode );
size_t fread ( void *ptr , size_t size , size_t nmemb , FILE *stream ); size_t __wrap_fread ( void *ptr , size_t size , size_t nmemb , FILE *stream );
int ftruncate ( int fd , off_t length ); int ftruncate ( int fd , off_t length );
size_t fwrite ( const void *ptr , size_t size , size_t nmemb , FILE *stream ); size_t fwrite ( const void *ptr , size_t size , size_t nmemb , FILE *stream );
int open (const char *pathname, int flags); int open (const char *pathname, int flags);
@ -331,7 +338,10 @@ int syncfs(int fd);
int fsync ( int fd ); int fsync ( int fd );
int truncate ( const char *path , off_t length ); int truncate ( const char *path , off_t length );
ssize_t write ( int fd , const void *buf , size_t count ); ssize_t write ( int fd , const void *buf , size_t count );
//int fclose ( FILE *stream ); int __wrap_fclose ( FILE *stream );
FILE * __wrap_freopen ( const char * filename, const char * mode, FILE * stream );
int getc(FILE *fp);
char *gets (char *p);
//void dump_stat ( struct stat *sp ); //void dump_stat ( struct stat *sp );
#if 0 #if 0
@ -356,6 +366,7 @@ int mkdir ( const char *pathname , mode_t mode );
int rename ( const char *oldpath , const char *newpath ); int rename ( const char *oldpath , const char *newpath );
int rmdir ( const char *pathname ); int rmdir ( const char *pathname );
int unlink ( const char *pathname ); int unlink ( const char *pathname );
int remove(const char *pathname);
int closedir ( DIR *dirp ); int closedir ( DIR *dirp );
DIR *opendir ( const char *pathdir ); DIR *opendir ( const char *pathdir );
struct dirent *readdir ( DIR *dirp ); struct dirent *readdir ( DIR *dirp );

View File

@ -112,9 +112,12 @@ int printf(const char *fmt, ...)
return done; return done;
} }
#define MAXLN 128 //just a stub
#define ISSPACE " \t\n\r\f\v" int
scanf (const char *fmt, ...)
{
return 0;
}
/* /*
* sscanf(buf,fmt,va_alist) * sscanf(buf,fmt,va_alist)
*/ */
@ -311,3 +314,32 @@ vsscanf (const char *buf, const char *s, va_list ap)
} }
return (count); return (count);
} }
/*
* fscanf(stream,fmt,va_alist)
*/
int fscanf (FILE *stream, const char *fmt, ...)
{
int count;
va_list ap;
va_start (ap, fmt);
count = vfscanf (stream, fmt, ap);
va_end (ap);
return (count);
}
/*
* vfscanf(stream,fmt,ap)
*/
static int vfscanf (FILE *stream, const char *fmt, va_list ap)
{
int count;
char buf[MAXLN + 1];
if (fgets (buf, MAXLN, stream) == 0) {
return (-1);
}
count = vsscanf (buf, fmt, ap);
return (count);
}

View File

@ -27,8 +27,9 @@ int vasprintf(char **strp, const char *fmt, va_list ap);
int asprintf(char **strp, const char *fmt, ...); int asprintf(char **strp, const char *fmt, ...);
int vprintf(const char *fmt, va_list arg); int vprintf(const char *fmt, va_list arg);
int printf(const char *fmt, ...); int printf(const char *fmt, ...);
int fscanf ( FILE * stream, const char * format, ... );
int scanf (const char *fmt, ...);
int sscanf (const char *buf, const char *fmt, ...); int sscanf (const char *buf, const char *fmt, ...);
int vsscanf (const char *buf, const char *s, va_list ap); int vsscanf (const char *buf, const char *s, va_list ap);
void *malloc(size_t size); void *malloc(size_t size);