APM:Libraries
posix.h
Go to the documentation of this file.
1 
24 #ifndef _POSIX_H_
25 #define _POSIX_H_
26 #include <board.h>
27 #ifdef USE_POSIX
28 #define POSIX
29 #pragma GCC diagnostic ignored "-Wshadow"
30 #undef EDOM
32 #undef ERANGE
33 #include <stdint.h>
34 #include <stddef.h>
35 #include <ff.h>
36 #include <stdarg.h>
37 #include <time.h>
38 
39 #define MAXLN 128
40 #define ISSPACE " \t\n\r\f\v"
41 
43 
44 #ifdef __cplusplus
45 extern "C" {
46 #endif
47 // =============================================
51 //typedef uint32_t blkcnt_t; /*< blkcnt_t for this architecture */
52 //typedef uint32_t blksize_t; /*< blksize_t for this architecture */
53 typedef int32_t off_t;
54 typedef off_t fpos_t;
55 extern int errno;
56 // =============================================
57 
58 // @brief posix errno values
59 enum POSIX_errno
60 {
61  EOK, /*< 0 NO ERROR */
62  EPERM, /*< 1 Operation not permitted */
63  ENOENT, /*< 2 No such file or directory */
64  ESRCH, /*< 3 No such process */
65  EINTR, /*< 4 Interrupted system call */
66  EIO, /*< 5 I/O error */
67  ENXIO, /*< 6 No such device or address */
68  E2BIG, /*< 7 Argument list too long */
69  ENOEXEC, /*< 8 Exec format error */
70  EBADF, /*< 9 Bad file number */
71  ECHILD, /*< 10 No child processes */
72  EAGAIN, /*< 11 Try again */
73  ENOMEM, /*< 12 Out of memory */
74  EACCES, /*< 13 Permission denied */
75  EFAULT, /*< 14 Bad address */
76  ENOTBLK, /*< 15 Block device required */
77  EBUSY, /*< 16 Device or resource busy */
78  EEXIST, /*< 17 File exists */
79  EXDEV, /*< 18 Cross-device link */
80  ENODEV, /*< 19 No such device */
81  ENOTDIR, /*< 20 Not a directory */
82  EISDIR, /*< 21 Is a directory */
83  EINVAL, /*< 22 Invalid argument */
84  ENFILE, /*< 23 File table overflow */
85  EMFILE, /*< 24 Too many open files */
86  ENOTTY, /*< 25 Not a typewriter */
87  ETXTBSY, /*< 26 Text file busy */
88  EFBIG, /*< 27 File too large */
89  ENOSPC, /*< 28 No space left on device */
90  ESPIPE, /*< 29 Illegal seek */
91  EROFS, /*< 30 Read-only file system */
92  EMLINK, /*< 31 Too many links */
93  EPIPE, /*< 32 Broken pipe */
94  EDOM, /*< 33 Math argument out of domain of func */
95  ERANGE, /*< 34 Math result not representable */
96  EBADMSG /*< 35 Bad Message */
97 };
98 // =============================================
99 
103 struct stat
104 {
105  dev_t st_dev; /*< ID of device containing file */
106  ino_t st_ino; /*< inode number */
107  mode_t st_mode; /*< protection */
108  nlink_t st_nlink; /*< number of hard links */
109  uid_t st_uid; /*< user ID of owner */
110  gid_t st_gid; /*< group ID of owner */
111  dev_t st_rdev; /*< device ID (if special file) */
112  off_t st_size; /*< total size, in bytes */
113  uint32_t st_blksize;/*< blocksize for filesystem I/O */
114  uint32_t st_blocks; /*< number of 512B blocks allocated */
115  time_t st_atime; /*< time of last access */
116  time_t st_mtime; /*< time of last modification */
117  time_t st_ctime; /*< time of last status change */
118 };
119 
122 typedef struct utimbuf
123 {
124  time_t actime; /* access time */
125  time_t modtime; /* modification time */
126 } utime_t;
127 
128 #if _USE_LFN != 0
129 #define MAX_NAME_LEN _MAX_LFN
130 #else
131 #define MAX_NAME_LEN 13
132 #endif
133 
134 struct dirent {
135 #if 0 // unsupported
136  ino_t d_ino; /* inode number */
137  off_t d_off; /* not an offset; see NOTES */
138  unsigned short d_reclen; /* length of this record */
139  unsigned char d_type; /* type of file; not supported
140  by all filesystem types */
141 #endif
142  char d_name[MAX_NAME_LEN]; /* filename */
143 };
144 
145 typedef struct dirent dirent_t;
146 
147 
150 #define lstat stat
151 // =============================================
153 struct __file {
154  char *buf; /* buffer pointer */
155  unsigned char unget; /* ungetc() buffer */
156  uint8_t flags; /* flags, see below */
157 #define __SRD 0x0001 /* OK to read */
158 #define __SWR 0x0002 /* OK to write */
159 #define __SSTR 0x0004 /* this is an sprintf/snprintf string */
160 #define __SPGM 0x0008 /* fmt string is in progmem */
161 #define __SERR 0x0010 /* found error */
162 #define __SEOF 0x0020 /* found EOF */
163 #define __SUNGET 0x040 /* ungetc() happened */
164 #define __SMALLOC 0x80 /* handle is malloc()ed */
165 #if 0
166  /* possible future extensions, will require uint16_t flags */
167  #define __SRW 0x0100 /* open for reading & writing */
168  #define __SLBF 0x0200 /* line buffered */
169  #define __SNBF 0x0400 /* unbuffered */
170  #define __SMBF 0x0800 /* buf is from malloc */
171 #endif
172  int size; /* size of buffer */
173  int len; /* characters read or written so far */
174  int (*put)(char, struct __file *); /* write one char to device */
175  int (*get)(struct __file *); /* read one char from device */
176 // FIXME add all low level functions here like _open, _close, ... like newlib does
177  void *udata; /* User defined and accessible data. */
178 };
179 // =============================================
183 #define O_ACCMODE 00000003 /*< read, write, read-write modes */
184 #define O_RDONLY 00000000 /*< Read only */
185 #define O_WRONLY 00000001 /*< Write only */
186 #define O_RDWR 00000002 /*< Read/Write */
187 #define O_CREAT 00000100 /*< Create file only if it does not exist */
188 #define O_EXCL 00000200 /*< O_CREAT option, Create fails if file exists
189 */
190 #define O_NOCTTY 00000400 /*< @todo */
191 #define O_TRUNC 00001000 /*< Truncate if exists */
192 #define O_APPEND 00002000 /*< All writes are to EOF */
193 #define O_NONBLOCK 00004000 /*< @todo */
194 #define O_BINARY 00000004 /*< Binary */
195 #define O_TEXT 00000004 /*< Text End Of Line translation */
196 #define O_CLOEXEC 00000000
197 #define S_IFMT 0170000 /*< These bits determine file type. */
199 #define S_IFDIR 0040000 /*< Directory. */
200 #define S_IFCHR 0020000 /*< Character device. */
201 #define S_IFBLK 0060000 /*< Block device. */
202 #define S_IFREG 0100000 /*< Regular file. */
203 #define S_IFIFO 0010000 /*< FIFO. */
204 #define S_IFLNK 0120000 /*< Symbolic link. */
205 #define S_IFSOCK 0140000 /*< Socket. */
206 #define S_IREAD 0400 /*< Read by owner. */
207 #define S_IWRITE 0200 /*< Write by owner. */
208 #define S_IEXEC 0100 /*< Execute by owner. */
209 
211 #define S_ISTYPE(mode, mask) (((mode) & S_IFMT) == (mask))
212 #define S_ISDIR(mode) S_ISTYPE((mode), S_IFDIR)
213 #define S_ISCHR(mode) S_ISTYPE((mode), S_IFCHR)
214 #define S_ISBLK(mode) S_ISTYPE((mode), S_IFBLK)
215 #define S_ISREG(mode) S_ISTYPE((mode), S_IFREG)
216 
217 //@brief POSIX File permissions, see fstat and stat
218 #define S_IRUSR S_IREAD /*< Read by owner. */
219 #define S_IWUSR S_IWRITE /*< Write by owner. */
220 #define S_IXUSR S_IEXEC /*< Execute by owner. */
221 #define S_IRWXU (S_IREAD|S_IWRITE|S_IEXEC) /*< Read,Write,Execute by owner */
222 
223 #define S_IRGRP (S_IRUSR >> 3) /*< Read by group. */
224 #define S_IWGRP (S_IWUSR >> 3) /*< Write by group. */
225 #define S_IXGRP (S_IXUSR >> 3) /*< Execute by group. */
226 #define S_IRWXG (S_IRWXU >> 3) /*< Read,Write,Execute by user */
227 
228 #define S_IROTH (S_IRGRP >> 3) /*< Read by others. */
229 #define S_IWOTH (S_IWGRP >> 3) /*< Write by others. */
230 #define S_IXOTH (S_IXGRP >> 3) /*< Execute by others. */
231 #define S_IRWXO (S_IRWXG >> 3) /*< Read,Write,Execute by other */
232 // =============================================
233 
235 #define modecmp(str, pat) (strcmp(str, pat) == 0 ? 1: 0)
236 
237 // =============================================
239 #define FATFS_R (S_IRUSR | S_IRGRP | S_IROTH) /*< FatFs Read perms */
240 #define FATFS_W (S_IWUSR | S_IWGRP | S_IWOTH) /*< FatFs Write perms */
241 #define FATFS_X (S_IXUSR | S_IXGRP | S_IXOTH) /*< FatFs Execute perms */
242 
243 // =============================================
245 #define EOF (-1)
246 
248 #define SEEK_SET 0
249 #define SEEK_CUR 1
250 #define SEEK_END 2
251 
252 // =============================================
254 typedef struct __file FILE;
255 
257 #define MAX_FILES 16
258 extern FILE *__iob[MAX_FILES];
259 
261 #undef stdin
262 #undef stdout
263 #undef stderr
264 
265 // Hard coded stdin,stdout and stderr locations
266 #define stdin (__iob[0])
267 #define stdout (__iob[1])
268 #define stderr (__iob[2])
269 
270 // =============================================
271 //#define IO_MACROS
272 #ifdef IO_MACROS
273 #define putc(__c, __stream) fputc(__c, __stream)
277 #define getc(__stream) fgetc(__stream)
278 
281 #define putchar(__c) fputc(__c,stdout)
282 
283 #define puts(__str) fputs(__str,stdout)
284 #endif
285 
286 // =============================================
288 #define fdev_set_udata(stream, u) do { (stream)->udata = u; } while(0)
289 #define fdev_get_udata(stream) ((stream)->udata)
290 
292 #define _FDEV_EOF (-1)
293 #define _FDEV_ERR (-2)
294 //@brief device read/write flags
295 #define _FDEV_SETUP_READ __SRD
296 #define _FDEV_SETUP_WRITE __SWR
297 #define _FDEV_SETUP_RW (__SRD|__SWR)
299 // =============================================
300 
301 
302 /* posix.c */
303 int isatty ( int fileno );
304 int fgetc ( FILE *stream );
305 int fputc ( int c , FILE *stream );
306 void clearerr(FILE *stream);
307 #ifndef IO_MACROS
308 int getchar ( void );
309 int putchar ( int c );
310 #endif
311 //int ungetc ( int c , FILE *stream );
312 #ifndef IO_MACROS
313 int putc ( int c , FILE *stream );
314 #endif
315 char *fgets ( char *str , int size , FILE *stream );
316 int fputs ( const char *str , FILE *stream );
317 #ifndef IO_MACROS
318 int puts ( const char *str );
319 #endif
320 int feof ( FILE *stream );
321 int fgetpos ( FILE *stream , size_t *pos );
322 int fseek ( FILE *stream , long offset , int whence );
323 int fsetpos ( FILE *stream , size_t *pos );
324 long ftell ( FILE *stream );
325 off_t lseek ( int fileno , off_t position , int whence );
326 void rewind ( FILE *stream );
327 int close ( int fileno );
328 int fileno ( FILE *stream );
329 FILE *fileno_to_stream ( int fileno );
330 FILE *fopen ( const char *path , const char *mode );
331 size_t __wrap_fread ( void *ptr , size_t size , size_t nmemb , FILE *stream );
332 int ftruncate ( int fd , off_t length );
333 size_t fwrite ( const void *ptr , size_t size , size_t nmemb , FILE *stream );
334 int open (const char *pathname, int flags);
335 ssize_t read ( int fd , void *buf , size_t count );
336 void sync ( void );
337 int syncfs(int fd);
338 int fsync ( int fd );
339 int truncate ( const char *path , off_t length );
340 ssize_t write ( int fd , const void *buf , size_t count );
341 int __wrap_fclose ( FILE *stream );
342 FILE * __wrap_freopen ( const char * filename, const char * mode, FILE * stream );
343 int getc(FILE *fp);
344 char *gets (char *p);
345 //void dump_stat ( struct stat *sp );
346 
347 #if 0
348 int fstat ( int fd , struct stat *buf );
349 #endif
350 int64_t fs_getfree(void);
351 int64_t fs_gettotal(void);
352 int stat ( const char *name , struct stat *buf );
353 char *basename (const char *str );
354 char *baseext ( char *str );
355 int chdir ( const char *pathname );
356 int chmod ( const char *pathname , mode_t mode );
357 int dirname ( char *str );
358 //int utime(const char *filename, const struct utimbuf *times);
359 
360 #if 0
361  int fchmod ( int fd , mode_t mode );
362 #endif
363 
364 char *getcwd ( char *pathname , int len );
365 int mkdir ( const char *pathname , mode_t mode );
366 int rename ( const char *oldpath , const char *newpath );
367 int rmdir ( const char *pathname );
368 int unlink ( const char *pathname );
369 int remove(const char *pathname);
370 int closedir ( DIR *dirp );
371 DIR *opendir ( const char *pathdir );
372 struct dirent *readdir ( DIR *dirp );
373 void clrerror ( FILE *stream );
374 int ferror ( FILE *stream );
375 void perror ( const char *s );
376 char *strerror ( int errnum );
377 char *__wrap_strerror_r ( int errnum , char *buf , size_t buflen );
378 FILE *fdevopen ( int (*put )(char ,FILE *), int (*get )(FILE *));
379 //int mkfs(char *name );
380 int fatfs_getc ( FILE *stream );
381 int fatfs_putc ( char c , FILE *stream );
382 int fatfs_to_errno ( FRESULT Result );
383 int fatfs_to_fileno ( FIL *fh );
384 time_t fat_time_to_unix ( uint16_t date , uint16_t time );
385 void unix_time_to_fat(time_t epoch, uint16_t *date, uint16_t *time);
386 FIL *fileno_to_fatfs ( int fileno );
387 int free_file_descriptor ( int fileno );
388 int new_file_descriptor ( void );
389 int posix_fopen_modes_to_open ( const char *mode );
390 
391 int fprintf(FILE *fp, const char *format, ...);
392 
393 #ifdef __cplusplus
394 }
395 #endif
396 
397 // =============================================
398 #endif //USE_POSIX
399 #endif //_POSIX_H_
int syncfs(int fd)
POSIX Sync pending file changes and metadata for specified fileno.
Definition: posix.c:1085
int fileno(FILE *stream)
Convert POSIX stream pointer to POSIX fileno (index of __iob[])
Definition: posix.c:714
char * baseext(char *str)
File extention of a file name. NOT POSIX.
Definition: posix.c:1467
int rename(const char *oldpath, const char *newpath)
POSIX rename a file by name.
Definition: posix.c:1648
int64_t fs_gettotal()
Definition: posix.c:1420
void rewind(FILE *stream)
POSIX rewind file to the beginning.
Definition: posix.c:656
dirent_t * readdir(DIR *dirp)
Definition: posix.c:1768
int ferror(FILE *stream)
ferror reports if the stream has an error flag set
Definition: posix.c:1811
int open(const char *pathname, int flags)
POSIX Open a file with integer mode flags.
Definition: posix.c:885
int fgetpos(FILE *stream, size_t *pos)
POSIX get position of file stream.
Definition: posix.c:517
int fatfs_putc(char c, FILE *stream)
Private FatFs function called by fputc() to put a byte from file stream NOT POSIX open() assigns stre...
Definition: posix.c:2087
char * __wrap_strerror_r(int errnum, char *buf, size_t buflen)
POSIX strerror_r() - convert POSIX errno to text with user message.
Definition: posix.c:1869
int fatfs_to_errno(FRESULT Result)
Convert FafFs error result to POSIX errno. NOT POSIX.
Definition: posix.c:2127
char * strerror(int errnum)
POSIX strerror() - convert POSIX errno to text with user message.
Definition: posix.c:1852
int dirname(char *str)
POSIX directory name of a filename. Return the index of the last &#39;/&#39; character.
Definition: posix.c:1552
ssize_t write(int fd, const void *buf, size_t count)
POSIX Write count bytes from *buf to fileno fd.
Definition: posix.c:1169
int posix_fopen_modes_to_open(const char *mode)
Convert POSIX fopen mode to POSIX open mode flags. NOT POSIX.
Definition: posix.c:2473
int rmdir(const char *pathname)
POSIX delete a directory.
Definition: posix.c:1671
int new_file_descriptor(void)
Allocate a POSIX FILE descriptor. NOT POSIX.
Definition: posix.c:2410
char * getcwd(char *pathname, int len)
POSIX get current working directory.
Definition: posix.c:1597
const char * name
Definition: BusTest.cpp:11
int fatfs_getc(FILE *stream)
Formt SD card.
Definition: posix.c:1997
int putchar(int c)
put a character to stdout See fdevopen() sets stream->put get for TTY devices
Definition: posix.c:329
int64_t fs_getfree()
Definition: posix.c:1405
time_t fat_time_to_unix(uint16_t date, uint16_t time)
Convert FatFs file date and time to POSIX epoch seconds. NOT POSIX.
Definition: posix.c:2282
FILE * fdevopen(int(*put)(char, FILE *), int(*get)(FILE *))
Device open functions.
Definition: posix.c:1888
Definition: ff.h:201
int fatfs_to_fileno(FIL *fh)
Convert FatFS file handle to POSIX fileno. NOT POSIX.
Definition: posix.c:2204
int fsetpos(FILE *stream, size_t *pos)
POSIX set position of file stream.
Definition: posix.c:566
void perror(const char *s)
POSIX perror() - convert POSIX errno to text with user message.
Definition: posix.c:1827
long ftell(FILE *stream)
POSIX file position of open stream.
Definition: posix.c:580
int free_file_descriptor(int fileno)
Free POSIX fileno FILE descriptor. NOT POSIX.
Definition: posix.c:2366
int feof(FILE *stream)
feof reports if the stream is at EOF
Definition: posix.c:500
ssize_t read(int fd, void *buf, size_t count)
POSIX read count bytes from *buf to fileno fd.
Definition: posix.c:995
int isatty(int fileno)
Test POSIX fileno if it is a Serial Console/TTY.
Definition: posix.c:185
void unix_time_to_fat(time_t epoch, uint16_t *date, uint16_t *time)
Convert Linux POSIX time_t to FAT32 date and time. NOT POSIX.
Definition: posix.c:2307
int puts(const char *str)
put a string to stdout See fdevopen() sets stream->put get for TTY devices
Definition: posix.c:475
int putc(int c, FILE *stream)
Un-Get byte from a TTY device or FatFs file stream.
Definition: posix.c:379
int __wrap_fclose(FILE *stream)
POSIX close a file stream.
Definition: posix.c:1236
int getchar()
functions normally defined as macros
Definition: posix.c:314
int close(int fileno)
POSIX Close a file with fileno handel.
Definition: posix.c:675
FRESULT
Definition: ff.h:243
void sync(void)
POSIX Sync all pending file changes and metadata on ALL files.
Definition: posix.c:1058
int unlink(const char *pathname)
POSIX delete a file.
Definition: posix.c:1693
int fputs(const char *str, FILE *stream)
put a string to stdout See fdevopen() sets stream->put get for TTY devices
Definition: posix.c:452
off_t lseek(int fileno, off_t position, int whence)
POSIX seek to file position.
Definition: posix.c:612
int mkdir(const char *pathname, mode_t mode)
POSIX make a directory.
Definition: posix.c:1620
char * gets(char *p)
Definition: posix.c:429
int fseek(FILE *stream, long offset, int whence)
POSIX seek to file possition.
Definition: posix.c:540
int ftruncate(int fd, off_t length)
POSIX truncate open file to length.
Definition: posix.c:817
static uint8_t buflen
Definition: srxl.cpp:57
FIL * fileno_to_fatfs(int fileno)
Convert POSIX fileno to FatFS handle NOT POSIX.
Definition: posix.c:2331
FILE * fileno_to_stream(int fileno)
Convert POSIX fileno to POSIX FILE stream pointer. NOT POSIX.
Definition: posix.c:744
int stat(const char *name, struct stat *buf)
Display struct stat, from POSIX stat(0 or fstat(), in ASCII. NOT POSIX.
Definition: posix.c:1319
FILE * fopen(const char *path, const char *mode)
POSIX Open a file with path name and ascii file mode string.
Definition: posix.c:772
int chdir(const char *pathname)
POSIX change directory.
Definition: posix.c:1489
int closedir(DIR *dirp)
POSIX closedir.
Definition: posix.c:1730
int fgetc(FILE *stream)
Get byte from a TTY device or FatFs file stream open() or fopen() sets stream->get = fatfs_getc() for...
Definition: posix.c:205
int chmod(const char *pathname, mode_t mode)
POSIX chmod function - change file access permission Unfortunately file f_open modes and f_chmod mode...
Definition: posix.c:1514
Definition: ff.h:173
int errno
Note: fdevopen assigns stdin,stdout,stderr.
Definition: posix.c:118
int fprintf(FILE *fp, const char *fmt,...)
fprintf character write function
Definition: posix.c:2539
int fsync(int fileno)
Definition: posix.c:2562
int getc(FILE *fp)
Definition: posix.c:250
int truncate(const char *path, off_t length)
POSIX truncate named file to length.
Definition: posix.c:1131
size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
POSIX write nmemb elements from buf, size bytes each, to the stream fd.
Definition: posix.c:858
int fputc(int c, FILE *stream)
Put a byte to TTY device or FatFs file stream open() or fopen() sets stream->put = fatfs_outc() for F...
Definition: posix.c:264
FILE * __iob[MAX_FILES]
POSIX fileno to POSIX FILE stream table.
Definition: posix.c:128
DIR * opendir(const char *pathdir)
Definition: posix.c:1749
FILE * __wrap_freopen(const char *filename, const char *mode, FILE *stream)
Definition: posix.c:1218
char * fgets(char *str, int size, FILE *stream)
get a string from stdin See fdevopen() sets stream->put get for TTY devices
Definition: posix.c:398
char * basename(const char *str)
POSIX Basename of filename.
Definition: posix.c:1446
void clearerr(FILE *stream)
Definition: posix.c:296
size_t __wrap_fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
POSIX read nmemb elements from buf, size bytes each, to the stream fd.
Definition: posix.c:793
void clrerror(FILE *stream)
clrerror resets stream EOF and error flags
Definition: posix.c:1799