ESP8266 ILI9341 display support code with printf sources, wire-frame viewer and custom fonts  1.0
ESP8266ILI9341DisplayProject
posix.c
Go to the documentation of this file.
1 
114 #include "user_config.h"
115 
116 #include "stringsup.h"
117 #include "fatfs.h"
118 
119 #include "posix.h"
120 
121 #ifdef ESP8266
122 // FIXME ESP8266 library conflict
123 #undef strerror_r
124 #endif
125 
127 
129 int errno;
130 
131 
140 
144 const char *sys_errlist[] =
145 {
146  "OK",
147  "Operation not permitted",
148  "No such file or directory",
149  "No such process",
150  "Interrupted system call",
151  "I/O error",
152  "No such device or address",
153  "Argument list too long",
154  "Exec format error",
155  "Bad file number",
156  "No child processes",
157  "Try again",
158  "Out of memory",
159  "Permission denied",
160  "Bad address",
161  "Block device required",
162  "Device or resource busy",
163  "File exists",
164  "Cross-device link",
165  "No such device",
166  "Not a directory",
167  "Is a directory",
168  "Invalid argument",
169  "File table overflow",
170  "Too many open files",
171  "Not a typewriter",
172  "Text file busy",
173  "File too large",
174  "No space left on device",
175  "Illegal seek",
176  "Read-only file system",
177  "Too many links",
178  "Broken pipe",
179  "Math argument out of domain of func",
180  "Math result not representable",
181  "Bad Message",
182  NULL
183 };
184 
185 // =============================================
195 MEMSPACE
196 int isatty(int fileno)
197 {
199  if(fileno >= 0 && fileno <= 2)
200  return(1);
201  return 0;
202 }
203 
214 MEMSPACE
215 int
216 fgetc(FILE *stream)
217 {
218  int c;
219 
220  if(stream == NULL)
221  {
222  errno = EBADF; // Bad File Number
223  return(EOF);
224  }
225 
226 #ifdef ESP8266
227  optimistic_yield(1000);
228  wdt_reset();
229 #endif
230 
231  if ((stream->flags & __SRD) == 0)
232  return EOF;
233 
234  if ((stream->flags & __SUNGET) != 0) {
235  stream->flags &= ~__SUNGET;
236  stream->len++;
237  return stream->unget;
238  }
239 
240  if (stream->flags & __SSTR) {
241  c = *stream->buf;
242  if (c == '\0') {
243  stream->flags |= __SEOF;
244  return EOF;
245  } else {
246  stream->buf++;
247  }
248  } else {
249  if(!stream->get)
250  {
251  printf("fgetc stream->get NULL\n");
252  return(EOF);
253  }
254  // get character from device or file
255  c = stream->get(stream);
256  if (c < 0) {
257  /* if != _FDEV_ERR, assume its _FDEV_EOF */
258  stream->flags |= (c == _FDEV_ERR)? __SERR: __SEOF;
259  return EOF;
260  }
261  }
262 
263  stream->len++;
264  return (c);
265 }
266 
276 MEMSPACE
277 int
278 fputc(int c, FILE *stream)
279 {
280  errno = 0;
281  int ret;
282 
283  if(stream == NULL)
284  {
285  errno = EBADF; // Bad File Number
286  return(EOF);
287  }
288 
289 #ifdef ESP8266
290  optimistic_yield(1000);
291  wdt_reset();
292 #endif
293 
294  if(stream != stdout && stream != stderr)
295  {
296  return(fatfs_putc(c,stream));
297  }
298 
299  // TTY outputs
300 
301  if ((stream->flags & __SWR) == 0)
302  return EOF;
303 
304  if (stream->flags & __SSTR) {
305  if (stream->len < stream->size)
306  *stream->buf++ = c;
307  stream->len++;
308  return c;
309  } else {
310  if(!stream->put)
311  {
312  printf("fputc stream->put NULL\n");
313  return(EOF);
314  }
315  ret = stream->put(c, stream);
316  if(ret != EOF)
317  stream->len++;
318  return(ret);
319  }
320 }
321 
322 
323 
325 #ifndef IO_MACROS
326 MEMSPACE
335 int
337 {
338  return(fgetc(stdin));
339 }
340 
349 MEMSPACE
350 int
351 putchar(int c)
352 {
353  return(fputc(c,stdout));
354 }
355 #endif
356 
366 MEMSPACE
367 int
368 ungetc(int c, FILE *stream)
369 {
370  int fd = fileno(stream);
371  if(!isatty(fd))
372  return(EOF);
373 
374  if(c == EOF)
375  return EOF;
376  if((stream->flags & __SUNGET) != 0 )
377  return EOF;
378  if ((stream->flags & __SRD) == 0 )
379  return EOF;
380 
381  stream->flags |= __SUNGET;
382  stream->flags &= ~__SEOF;
383 
384  stream->unget = c;
385  stream->len--;
386 
387  return (c);
388 }
389 
390 #ifndef IO_MACROS
391 // =============================================
399 MEMSPACE
400 int
401 putc(int c, FILE *stream)
402 {
403  return(fputc(c, stream));
404 }
405 
406 #endif
407 
408 // =============================================
418 MEMSPACE
419 char *
420 fgets(char *str, int size, FILE *stream)
421 {
422  int c;
423  int ind = 0;
424  while(size--)
425  {
426  c = fgetc(stream);
427  if(c == EOF)
428  {
429  if( ind == 0)
430  return(NULL);
431  break;
432  }
433  if(c == '\n')
434  break;
435  if(c == 0x08)
436  {
437  if(ind > 0)
438  --ind;
439  continue;
440  }
441  str[ind++] = c;
442  }
443  str[ind] = 0;
444  return(str);
445 }
446 
455 MEMSPACE
456 int
457 fputs(const char *str, FILE *stream)
458 {
459  while(*str)
460  {
461  if(fputc(*str, stream) == EOF)
462  return(EOF);
463  ++str;
464  }
465  return(0);
466 }
467 
468 
469 #ifndef IO_MACROS
470 MEMSPACE
479 int
480 puts(const char *str)
481 {
482  while(*str)
483  {
484  if(fputc(*str, stdout) == EOF)
485  return(EOF);
486  ++str;
487  }
488  return ( fputc('\n',stdout) );
489 }
490 
491 #endif
492 
493 
494 // =============================================
495 // =============================================
497 // =============================================
498 // =============================================
504 MEMSPACE
505 int feof(FILE *stream)
506 {
507  if(stream->flags & __SEOF)
508  return(1);
509  return(0);
510 }
511 
521 MEMSPACE
522 int fgetpos(FILE *stream, size_t *pos)
523 {
524  long offset = ftell(stream);
525  *pos = offset;
526  if(offset == -1)
527  return(-1);
528  return( 0 );
529 }
530 
544 MEMSPACE
545 int fseek(FILE *stream, long offset, int whence)
546 {
547  long ret;
548 
549  int fn = fileno(stream);
550  if(fn < 0)
551  return(-1);
552 
553  ret = lseek(fn, offset, whence);
554 
555  if(ret == -1)
556  return(-1);
557 
558  return(0);
559 }
560 
570 MEMSPACE
571 int fsetpos(FILE *stream, size_t *pos)
572 {
573  return (fseek(stream, (size_t) *pos, SEEK_SET) );
574 }
575 
584 MEMSPACE
585 long ftell(FILE *stream)
586 {
587  errno = 0;
588 
589  int fn = fileno(stream);
590  if(isatty(fn))
591  return(-1);
592  // fileno_to_fatfs checks for fd out of bounds
593  FIL *fh = fileno_to_fatfs(fn);
594  if ( fh == NULL )
595  {
596  errno = EBADF;
597  return(-1);
598  }
599 
600  return( fh->fptr );
601 }
602 
616 MEMSPACE
617 off_t lseek(int fileno, off_t position, int whence)
618 {
619  FRESULT res;
620  FIL *fh;
621  errno = 0;
622  FILE *stream;
623 
624 
625  // fileno_to_fatfs checks for fd out of bounds
626  fh = fileno_to_fatfs(fileno);
627  if(fh == NULL)
628  {
629  errno = EMFILE;
630  return(-1);
631  }
632  if(isatty(fileno))
633  return(-1);
634 
635 
636  stream = fileno_to_stream(fileno);
637  stream->flags |= __SUNGET;
638 
639  if(whence == SEEK_END)
640  position += f_size(fh);
641  else if(whence==SEEK_CUR)
642  position += fh->fptr;
643 
644  res = f_lseek(fh, position);
645  if(res)
646  {
647  errno = fatfs_to_errno(res);
648  return -1;
649  }
650  if(position != f_tell(fh) )
651  {
652  printf("Seek failed");
653  return(-1L);
654  }
655  return (fh->fptr);
656 }
657 
665 MEMSPACE
666 void rewind( FILE *stream)
667 {
668  fseek(stream, 0L, SEEK_SET);
669 }
670 
671 // =============================================
672 // =============================================
674 // =============================================
675 // =============================================
684 MEMSPACE
685 int close(int fileno)
686 {
687  FILE *stream;
688  FIL *fh;
689  int res;
690 
691  errno = 0;
692 
693  // checks if fileno out of bounds
694  stream = fileno_to_stream(fileno);
695  if(stream == NULL)
696  {
697  return(-1);
698  }
699 
700  // fileno_to_fatfs checks for fileno out of bounds
701  fh = fileno_to_fatfs(fileno);
702  if(fh == NULL)
703  {
704  return(-1);
705  }
706  res = f_close(fh);
707  free_file_descriptor(fileno);
708  if (res != FR_OK)
709  {
710  errno = fatfs_to_errno(res);
711  return(-1);
712  }
713  return(0);
714 }
715 
723 MEMSPACE
724 int fileno(FILE *stream)
725 {
726  int fileno;
727 
728  if(stream == NULL)
729  {
730  errno = EBADF;
731  return(-1);
732  }
733 
734  for(fileno=0; fileno<MAX_FILES; ++fileno)
735  {
736  if ( __iob[fileno] == stream)
737  return(fileno);
738  }
739  return(-1);
740 }
741 
753 MEMSPACE
755 {
756  FILE *stream;
757  if(fileno < 0 || fileno >= MAX_FILES)
758  {
759  errno = EBADF;
760  return(NULL);
761  }
762 
763  stream = __iob[fileno];
764  if(stream == NULL)
765  {
766  errno = EBADF;
767  return(NULL);
768  }
769  return(stream);
770 }
771 
781 MEMSPACE
782 FILE *fopen(const char *path, const char *mode)
783 {
784  int flags = posix_fopen_modes_to_open(mode);
785  int fileno = open(path, flags);
786 
787  // checks if fileno out of bounds
788  return( fileno_to_stream(fileno) );
789 }
790 
802 MEMSPACE
803 size_t fread(void *ptr, size_t size, size_t nmemb, FILE *stream)
804 {
805  size_t count = size * nmemb;
806  int fn = fileno(stream);
807  ssize_t ret;
808 
809  // read() checks for fn out of bounds
810  ret = read(fn, ptr, count);
811  if(ret < 0)
812  return(0);
813 
814  return((size_t) ret);
815 }
816 
826 MEMSPACE
827 int ftruncate(int fd, off_t length)
828 {
829  errno = 0;
830  FIL *fh;
831  FRESULT rc;
832 
833  if(isatty(fd))
834  return(-1);
835  // fileno_to_fatfs checks for fd out of bounds
836  fh = fileno_to_fatfs(fd);
837  if(fh == NULL)
838  {
839  return(-1);
840  }
841  rc = f_lseek(fh, length);
842  if (rc != FR_OK)
843  {
844  errno = fatfs_to_errno(rc);
845  return(-1);
846  }
847  rc = f_truncate(fh);
848  if (rc != FR_OK)
849  {
850  errno = fatfs_to_errno(rc);
851  return(-1);
852  }
853  return(0);
854 }
855 
867 MEMSPACE
868 size_t fwrite(const void *ptr, size_t size, size_t nmemb, FILE *stream)
869 {
870  size_t count = size * nmemb;
871  int fn = fileno(stream);
872  ssize_t ret;
873 
874  // write () checks for fn out of bounds
875  ret = write(fn, ptr, count);
876 
877  if(ret < 0)
878  return(0);
879 
880  return((size_t) ret);
881 }
882 
883 
884 
894 MEMSPACE
895 int open(const char *pathname, int flags)
896 {
897  int fileno;
898  int fatfs_modes;
899  FILE *stream;
900  FIL *fh;
901  int res;
902 
903  errno = 0;
904 
905 // FIXME Assume here that the disk interface mmc_init was already called
906 #if 0
907 // Checks Disk status
908  res = mmc_init(0);
909 
910  if(res != RES_OK)
911  {
912  errno = fatfs_to_errno(res);
913  return(-1);
914  }
915 #endif
916 
917  if((flags & O_ACCMODE) == O_RDWR)
918  fatfs_modes = FA_READ | FA_WRITE;
919  else if((flags & O_ACCMODE) == O_RDONLY)
920  fatfs_modes = FA_READ;
921  else
922  fatfs_modes = FA_WRITE;
923 
924  if(flags & O_CREAT)
925  {
926  if(flags & O_TRUNC)
927  fatfs_modes |= FA_CREATE_ALWAYS;
928  else
929  fatfs_modes |= FA_OPEN_ALWAYS;
930  }
931 
932  fileno = new_file_descriptor();
933 
934  // checks if fileno out of bounds
935  stream = fileno_to_stream(fileno);
936  if(stream == NULL)
937  {
938  free_file_descriptor(fileno);
939  return(-1);
940  }
941 
942  // fileno_to_fatfs checks for fileno out of bounds
943  fh = fileno_to_fatfs(fileno);
944  if(fh == NULL)
945  {
946  free_file_descriptor(fileno);
947  errno = EBADF;
948  return(-1);
949  }
950  res = f_open(fh, pathname, (BYTE) (fatfs_modes & 0xff));
951  if(res != FR_OK)
952  {
953  errno = fatfs_to_errno(res);
954  free_file_descriptor(fileno);
955  return(-1);
956  }
957  if(flags & O_APPEND)
958  {
960  res = f_lseek(fh, f_size(fh));
961  if (res != FR_OK)
962  {
963  errno = fatfs_to_errno(res);
964  f_close(fh);
965  free_file_descriptor(fileno);
966  return(-1);
967  }
968  }
969 
970  if((flags & O_ACCMODE) == O_RDWR)
971  {
972  // FIXME fdevopen should do this
973  stream->put = fatfs_putc;
974  stream->get = fatfs_getc;
975  stream->flags = _FDEV_SETUP_RW;
976  }
977  else if((flags & O_ACCMODE) == O_RDONLY)
978  {
979  // FIXME fdevopen should do this
980  stream->put = NULL;
981  stream->get = fatfs_getc;
982  stream->flags = _FDEV_SETUP_READ;
983  }
984  else
985  {
986  // FIXME fdevopen should do this
987  stream->put = fatfs_putc;
988  stream->get = NULL;
989  stream->flags = _FDEV_SETUP_WRITE;
990  }
991 
992  return(fileno);
993 }
994 
1005 MEMSPACE
1006 ssize_t read(int fd, const void *buf, size_t count)
1007 {
1008  UINT size;
1009  UINT bytes = count;
1010  int res;
1011  int ret;
1012  FIL *fh;
1013  FILE *stream;
1014 
1015  //FIXME
1016  *(char *) buf = 0;
1017 
1018  errno = 0;
1019 
1020  // TTY read function
1021  // FIXME should we really be blocking ???
1022  stream = fileno_to_stream(fd);
1023  if(stream == stdin)
1024  {
1025  char *ptr = (char *) buf;
1026  // ungetc is undefined for read
1027  stream->flags |= __SUNGET;
1028  size = 0;
1029  while(count--)
1030  {
1031  ret = fgetc(stream);
1032  if(ret < 0)
1033  break;
1034 
1035  *ptr++ = ret;
1036  ++size;
1037  }
1038  return(size);
1039  }
1040  if(stream == stdout || stream == stderr)
1041  {
1042  return(-1);
1043  }
1044 
1045  // fileno_to_fatfs checks for fd out of bounds
1046  fh = fileno_to_fatfs(fd);
1047  if ( fh == NULL )
1048  {
1049  errno = EBADF;
1050  return(-1);
1051  }
1052 
1053  res = f_read(fh, (void *) buf, bytes, &size);
1054  if(res != FR_OK)
1055  {
1056  errno = fatfs_to_errno(res);
1057  return(-1);
1058  }
1059  return ((ssize_t) size);
1060 }
1061 
1062 
1068 MEMSPACE
1069 void sync(void)
1070 {
1071  FIL *fh;
1072  int i;
1073 
1074  for(i=0;i<MAX_FILES;++i)
1075  {
1076  if(isatty(i))
1077  continue;
1078 
1079  // fileno_to_fatfs checks for i out of bounds
1080  fh = fileno_to_fatfs(i);
1081  if(fh == NULL)
1082  continue;
1083 
1084  (void ) syncfs(i);
1085  }
1086 }
1087 
1095 MEMSPACE
1096 int syncfs(int fd)
1097 {
1098  FIL *fh;
1099  FRESULT res;
1100  FILE *stream;
1101 
1102  errno = 0;
1103 
1104  if(isatty(fd))
1105  {
1106  errno = EBADF;
1107  return(-1);
1108  }
1109  stream = fileno_to_stream(fd);
1110  // reset unget on sync
1111  stream->flags |= __SUNGET;
1112 
1113  // fileno_to_fatfs checks for fd out of bounds
1114  fh = fileno_to_fatfs(fd);
1115  if(fh == NULL)
1116  {
1117  errno = EBADF;
1118  return(-1);
1119  }
1120 
1121  res = f_sync ( fh );
1122  if (res != FR_OK)
1123  {
1124  errno = fatfs_to_errno(res);
1125  return(-1);
1126  }
1127  return(0);
1128 }
1129 
1130 
1131 
1141 MEMSPACE
1142 int truncate(const char *path, off_t length)
1143 {
1144  errno = 0;
1145  FIL fh;
1146  FRESULT rc;
1147 
1148  rc = f_open(&fh , path, FA_OPEN_EXISTING | FA_READ | FA_WRITE);
1149  if (rc != FR_OK)
1150  {
1151  errno = fatfs_to_errno(rc);
1152  return(-1);
1153  }
1154  rc = f_lseek(&fh, length);
1155  if (rc != FR_OK)
1156  {
1157  errno = fatfs_to_errno(rc);
1158  return(-1);
1159  }
1160  rc = f_truncate(&fh);
1161  if (rc != FR_OK)
1162  {
1163  errno = fatfs_to_errno(rc);
1164  return(-1);
1165  }
1166  return(0);
1167 }
1168 
1169 
1179 MEMSPACE
1180 ssize_t write(int fd, const void *buf, size_t count)
1181 {
1182  UINT size;
1183  UINT bytes = count;
1184  FRESULT res;
1185  FIL *fh;
1186  FILE *stream;
1187  errno = 0;
1188 
1189  // TTY read function
1190  stream = fileno_to_stream(fd);
1191  if(stream == stdout || stream == stderr)
1192  {
1193  char *ptr = (char *) buf;
1194  size = 0;
1195  while(count--)
1196  {
1197  int c,ret;
1198  c = *ptr++;
1199  ret = fputc(c, stream);
1200  if(c != ret)
1201  break;
1202 
1203  ++size;
1204  }
1205  return(size);
1206  }
1207  if(stream == stdin)
1208  {
1209  return(-1);
1210  }
1211 
1212  // fileno_to_fatfs checks for fd out of bounds
1213  fh = fileno_to_fatfs(fd);
1214  if ( fh == NULL )
1215  {
1216  errno = EBADF;
1217  return(-1);
1218  }
1219 
1220  res = f_write(fh, buf, bytes, &size);
1221  if(res != FR_OK)
1222  {
1223  errno = fatfs_to_errno(res);
1224  return(-1);
1225  }
1226  return ((ssize_t) size);
1227 }
1228 
1229 
1235 
1238 MEMSPACE
1239 int fclose(FILE *stream)
1240 {
1241  int fn = fileno(stream);
1242  if(fn < 0)
1243  return(EOF);
1244 
1245  return( close(fn) );
1246 }
1247 
1248 // =============================================
1249 // =============================================
1251 // =============================================
1252 // =============================================
1253 
1259 MEMSPACE
1260 void dump_stat(struct stat *sp)
1261 {
1262  mode_t mode = sp->st_mode;
1263 
1264  printf("\tSize: %lu\n", (uint32_t)sp->st_size);
1265 
1266  printf("\tType: ");
1267  if(S_ISDIR(mode))
1268  printf("DIR\n");
1269  else if(S_ISREG(mode))
1270  printf("File\n");
1271  else
1272  printf("Unknown\n");
1273 
1274 
1275  printf("\tMode: %lo\n", (uint32_t)sp->st_mode);
1276  printf("\tUID: %lu\n", (uint32_t)sp->st_uid);
1277  printf("\tGID: %lu\n", (uint32_t)sp->st_gid);
1278  printf("\tatime: %s\n",mctime((time_t)sp->st_atime));
1279  printf("\tmtime: %s\n",mctime((time_t)sp->st_mtime));
1280  printf("\tctime: %s\n",mctime((time_t)sp->st_ctime));
1281 }
1282 
1283 #if 0
1284 MEMSPACE
1299 int fstat(int fd, struct stat *buf)
1300 {
1301  FIL *fh;
1302  FRESULT rc;
1303 
1304  if(isatty(fd))
1305  return(-1);
1306 
1307  //FIXME TODO
1308  return(-1);
1309 
1310 }
1311 #endif
1312 
1322 MEMSPACE
1323 char *mctime(time_t timev)
1324 {
1325  errno = 0;
1326  char *ptr = (char *)ctime_gm(&timev);
1327  int len;
1328  len = strlen(ptr);
1329  if(len && ptr[len-1] == '\n')
1330  ptr[len-1] = 0;
1331  return(ptr);
1332 }
1333 
1343 MEMSPACE
1344 int stat(char *name, struct stat *buf)
1345 {
1346  FILINFO info;
1347  int res;
1348  time_t epoch;
1349  uint16_t mode;
1350  errno = 0;
1351 
1352  // f_stat does not handle / or . as root directory
1353  if (strcmp(name,"/") == 0 || strcmp(name,".") == 0)
1354  {
1355  buf->st_atime = 0;
1356  buf->st_mtime = 0;
1357  buf->st_ctime = 0;
1358  buf->st_uid= 0;
1359  buf->st_gid= 0;
1360  buf->st_size = 0;
1361  buf->st_mode = S_IFDIR;
1362  return(0);
1363  }
1364 
1365  res = f_stat(name, &info);
1366  if(res != FR_OK)
1367  {
1368  errno = fatfs_to_errno(res);
1369  return(-1);
1370  }
1371 
1372  buf->st_size = info.fsize;
1373  epoch = fat_time_to_unix(info.fdate, info.ftime);
1374  buf->st_atime = epoch; // Access time
1375  buf->st_mtime = epoch; // Modification time
1376  buf->st_ctime = epoch; // Creation time
1377 
1378  // We only handle read only case
1379  mode = (FATFS_R | FATFS_X);
1380  if( !(info.fattrib & AM_RDO))
1381  mode |= (FATFS_W); // enable write if NOT read only
1382 
1383  if(info.fattrib & AM_SYS)
1384  {
1385  buf->st_uid= 0;
1386  buf->st_gid= 0;
1387  }
1388  {
1389  buf->st_uid=1000;
1390  buf->st_gid=1000;
1391  }
1392 
1393  if(info.fattrib & AM_DIR)
1394  mode |= S_IFDIR;
1395  else
1396  mode |= S_IFREG;
1397  buf->st_mode = mode;
1398 
1399  return(0);
1400 }
1401 
1406 MEMSPACE
1407 int utime(const char *filename, const struct utimbuf *times)
1408 {
1409 
1410  FILINFO fno;
1411  uint16_t fdate,ftime;
1412  time_t ut;
1413  int res;
1414 
1415  if(times)
1416  ut = times->modtime;
1417  else
1418  ut = time(0);
1419 
1420  unix_time_to_fat(ut, (uint16_t *) &fdate, (uint16_t *) &ftime);
1421 
1422 
1423  fno.fdate = fdate;
1424  fno.ftime = ftime;
1425 
1426  res = f_utime(filename, (FILINFO *) &fno);
1427 
1428  return( fatfs_to_errno(res) );
1429 }
1430 
1431 
1432 
1433 // =============================================
1434 // =============================================
1436 // =============================================
1437 // =============================================
1445 MEMSPACE
1446 char *basename(char *str)
1447 {
1448  char *base = str;
1449  if(!str)
1450  return("");
1451  while(*str)
1452  {
1453  if(*str++ == '/')
1454  base = str;
1455  }
1456  return(base);
1457 }
1458 
1465 MEMSPACE
1466 char *baseext(char *str)
1467 {
1468  char *ext = "";
1469 
1470  while(*str)
1471  {
1472  if(*str++ == '.')
1473  ext = str;
1474  }
1475  return(ext);
1476 }
1477 
1478 
1487 MEMSPACE
1488 int chdir(const char *pathname)
1489 {
1490  errno = 0;
1491 
1492  int res = f_chdir(pathname);
1493  if(res != FR_OK)
1494  {
1495  errno = fatfs_to_errno(res);
1496  return(-1);
1497  }
1498  return(0);
1499 }
1500 
1512 MEMSPACE
1513 int chmod(const char *pathname, mode_t mode)
1514 {
1515  int rc;
1516  errno = 0;
1517 
1518  // FIXME for now we combine user,group and other perms and ask if anyone has write perms ?
1519 
1520  // Read only ???
1521  if ( !( mode & ( S_IWUSR | S_IWGRP | S_IWOTH)))
1522  {
1523  // file is read only
1524  rc = f_chmod(pathname, AM_RDO, AM_RDO);
1525  if (rc != FR_OK)
1526  {
1527  errno = fatfs_to_errno(rc);
1528  return(-1);
1529  }
1530  }
1531 
1532  return(0);
1533 }
1534 
1550 MEMSPACE
1551 int dirname(char *str)
1552 {
1553  int end = 0;
1554  int ind = 0;
1555 
1556  if(!str)
1557  return(0);
1558 
1559  while(*str)
1560  {
1561  if(*str == '/')
1562  end = ind;
1563  ++str;
1564  ++ind;
1565  }
1566  return(end);
1567 }
1568 
1569 #if 0
1570 MEMSPACE
1580 int fchmod(int fd, mode_t mode)
1581 {
1582  //FIXME TODO
1583  return (-1);
1584 }
1585 #endif
1586 
1595 MEMSPACE
1596 char *getcwd(char *pathname, int len)
1597 {
1598  int res;
1599  errno = 0;
1600 
1601  res = f_getcwd(pathname, len);
1602  if(res != FR_OK)
1603  {
1604  errno = fatfs_to_errno(res);
1605  return(NULL);
1606  }
1607  return(pathname);
1608 }
1609 
1618 MEMSPACE
1619 int mkdir(const char *pathname, mode_t mode)
1620 {
1621  errno = 0;
1622 
1623  if(mode)
1624  {
1625  if(chmod(pathname, mode))
1626  return(-1);
1627  }
1628 
1629  int res = f_mkdir(pathname);
1630  if(res != FR_OK)
1631  {
1632  errno = fatfs_to_errno(res);
1633  return(-1);
1634  }
1635  return(0);
1636 }
1637 
1647 MEMSPACE
1648 int rename(const char *oldpath, const char *newpath)
1649 {
1650 /* Rename an object */
1651  int rc;
1652  errno = 0;
1653  rc = f_rename(oldpath, newpath);
1654  if(rc)
1655  {
1656  errno = fatfs_to_errno(rc);
1657  return(-1);
1658  }
1659  return(0);
1660 }
1661 
1670 MEMSPACE
1671 int rmdir(const char *pathname)
1672 {
1673  errno = 0;
1674  int res = f_unlink(pathname);
1675  if(res != FR_OK)
1676  {
1677  errno = fatfs_to_errno(res);
1678  return(-1);
1679  }
1680  return(0);
1681 }
1682 
1683 
1692 MEMSPACE
1693 int unlink(const char *pathname)
1694 {
1695  errno = 0;
1696  int res = f_unlink(pathname);
1697  if(res != FR_OK)
1698  {
1699  errno = fatfs_to_errno(res);
1700  return(-1);
1701  }
1702  return(0);
1703 }
1704 
1705 // =============================================
1706 // =============================================
1708 // =============================================
1709 // =============================================
1717 int closedir(DIR *dirp)
1718 {
1719  int res = f_closedir (dirp);
1720  if(res != FR_OK)
1721  {
1722  errno = fatfs_to_errno(res);
1723  return(-1);
1724  }
1725  return(0);
1726 }
1727 
1735 static DIR _dp;
1736 DIR *opendir(const char *pathdir)
1737 {
1738  int res = f_opendir((DIR *) &_dp, pathdir);
1739  if(res != FR_OK)
1740  {
1741  errno = fatfs_to_errno(res);
1742  return(NULL);
1743  }
1744  return ((DIR *) &_dp);
1745 }
1746 
1754 static dirent_t _de;
1756 {
1757  FILINFO fno;
1758  int len;
1759  int res;
1760 
1761  _de.d_name[0] = 0;
1762  res = f_readdir ( dirp, &fno );
1763  if(res != FR_OK)
1764  {
1765  errno = fatfs_to_errno(res);
1766  return(NULL);
1767  }
1768  len = strlen(fno.fname);
1769  strncpy(_de.d_name,fno.fname,len);
1770  _de.d_name[len] = 0;
1771  return( (dirent_t *) &_de);
1772 }
1773 
1774 // =============================================
1775 // =============================================
1777 // =============================================
1778 // =============================================
1779 
1785 MEMSPACE
1786 void clrerror(FILE *stream)
1787 {
1788  stream->flags &= ~__SEOF;
1789  stream->flags &= ~__SERR;
1790 }
1791 
1797 MEMSPACE
1798 int ferror(FILE *stream)
1799 {
1800  if(stream->flags & __SERR)
1801  return(1);
1802  return(0);
1803 }
1804 
1813 MEMSPACE
1814 void perror(const char *s)
1815 {
1816  const char *ptr = NULL;
1817 
1818 
1819  if(errno >=0 && errno < EBADMSG)
1820  ptr = sys_errlist[errno];
1821  else
1822  ptr = sys_errlist[EBADMSG];
1823 
1824  if(s && *s)
1825  printf("%s: %s\n", s, ptr);
1826  else
1827  printf("%s\n", ptr);
1828 }
1829 
1838 MEMSPACE
1839 char
1840 WEAK_ATR *strerror(int errnum)
1841 {
1842  return( (char *)sys_errlist[errnum] );
1843 }
1844 
1845 
1856 MEMSPACE
1857 char *strerror_r(int errnum, char *buf, size_t buflen)
1858 {
1859  strncpy(buf, sys_errlist[errnum], buflen);
1860  return(buf);
1861 }
1862 
1863 
1864 // =============================================
1865 // =============================================
1867 // =============================================
1868 // =============================================
1874 MEMSPACE
1875 FILE *
1876 fdevopen(int (*put)(char, FILE *), int (*get)(FILE *))
1877 {
1878  FILE *s;
1879 
1880  if (put == 0 && get == 0)
1881  return 0;
1882 
1883  if ((s = safecalloc(1, sizeof(FILE))) == 0)
1884  return 0;
1885 
1886  s->flags = __SMALLOC;
1887 
1888  if (get != 0) {
1889  s->get = get;
1890  s->flags |= __SRD;
1891  // We assign the first device with a read discriptor to stdin
1892  // Only assign once
1893  if (stdin == 0)
1894  stdin = s;
1895  }
1896 
1897  if (put != 0) {
1898  s->put = put;
1899  s->flags |= __SWR;
1900  // NOTE: We assign the first device with a write to both STDOUT andd STDERR
1901 
1902  // Only assign in unassigned
1903  if (stdout == 0)
1904  stdout = s;
1905  if (stderr == 0)
1906  stderr = s;
1907  }
1908 
1909  return s;
1910 }
1911 
1912 
1913 // =============================================
1914 // =============================================
1916 // =============================================
1917 // =============================================
1918 
1922 int mkfs(char *name)
1923 {
1924  FATFS fs;
1925  uint8_t *mem;
1926  int res;
1927  int len;
1928  int c;
1929  char dev[4];
1930 
1931  len = MATCH(name,"/dev/sd");
1932  if(!len)
1933  {
1934  printf("Expected /dev/sda .. /dev/sdj\n");
1935  return(0);
1936  }
1937  // Convert /dev/sd[a-j] to 0: .. 9:
1938  dev[1] = ':';
1939  dev[2] = 0;
1940  c = tolower( name[len-1] );
1941  if(c >= 'a' && c <= ('a' + 9))
1942  dev[0] = (c - 'a');
1943  dev[3] = 0;
1944 
1945  // Register work area to the logical drive 0:
1946  res = f_mount(&fs, dev, 0);
1947  if(!res)
1948  {
1949  put_rc(res);
1950  return(0);
1951  }
1952 
1953  // Allocate memory for mkfs function
1954  mem = safemalloc(1024);
1955  if(!mem)
1956  return(0);
1957 
1958  // Create FAT volume on the logical drive 0
1959  // 2nd argument is ignored. */
1960  res = f_mkfs(dev, FM_FAT32, 0, mem, 1024);
1961  if(res)
1962  {
1963  put_rc(res);
1964  safefree(mem);
1965  return(0);
1966  }
1967  safefree(mem);
1968  return(1);
1969 }
1970 
1983 MEMSPACE
1984 int fatfs_getc(FILE *stream)
1985 {
1986  FIL *fh;
1987  UINT size;
1988  int res;
1989  uint8_t c;
1990  long pos;
1991 
1992  errno = 0;
1993 
1994  if(stream == NULL)
1995  {
1996  errno = EBADF; // Bad File Number
1997  return(EOF);
1998  }
1999 
2000  fh = (FIL *) fdev_get_udata(stream);
2001  if(fh == NULL)
2002  {
2003  errno = EBADF; // Bad File Number
2004  return(EOF);
2005  }
2006 
2007  res = f_read(fh, &c, 1, (UINT *) &size);
2008  if( res != FR_OK || size != 1)
2009  {
2010  errno = fatfs_to_errno(res);
2011  stream->flags |= __SEOF;
2012  return(EOF);
2013  }
2014 
2015  // AUTOMATIC end of line METHOD detection
2016  // ALWAYS return '\n' for ALL methods
2017  // History: End of line (EOL) characters sometimes differ, mostly legacy systems, and modern UNIX (which uses just '\n')
2018  // '\r' ONLY
2019  // '\r\n'
2020  // '\n'
2021  // The difference was mostly from the way old mechanical printers were controlled.
2022  // '\n' (New Line = NL) advanced the line
2023  // '\r' (Charage Return = CR) moved the print head to start of line
2024  // '\t' (Tabstop = TAB)
2025  // '\f' (Form feed = FF)
2026  // The problem with mechanical devices is that each had differing control and time delays to deal with.
2027  // (TAB, CR, NL and FF) did different things and took differing times depending on the device.
2028  //
2029  // Long before DOS UNIX took the position that controlling physical devices must be a device drivers problem only.
2030  // They reasoned if users had to worry about all the ugly controll and timing issues no code would be portable.
2031  // Therefore they made NL just a SYMBOL for the driver to determine what to do.
2032  // This design philosophy argued if you needed better control its better to use a real designed purposed tool for it.
2033  // (ie. like curses or termcap).
2034 
2035  // Here to deal with those other old ugly stupid pointless EOL methods we convert to just a symbol '\n'
2036  // FROM '\n' OR '\r'char OR '\r\n' TO '\n'
2037  // Note: char != '\n'
2038  if(c == '\r')
2039  {
2040  // PEEK forward 1 character
2041  pos = f_tell(fh);
2042  // Check for trailing '\n' or EOF
2043  res = f_read(fh, &c, 1, (UINT *) &size);
2044  if(res != FR_OK || size != 1)
2045  {
2046  // '\r' with EOF impiles '\n'
2047  return('\n');
2048  }
2049  // This file must be '\r' ONLY for end of line
2050  if(c != '\n')
2051  {
2052  // Not '\n' or EOF o move file pointer back to just after the '\r'
2053  f_lseek(fh, pos);
2054  return('\n');
2055  }
2056  c = '\n';
2057  }
2058  return(c & 0xff);
2059 }
2060 
2073 MEMSPACE
2074 int fatfs_putc(char c, FILE *stream)
2075 {
2076  int res;
2077  FIL *fh;
2078  UINT size;
2079 
2080  errno = 0;
2081  if(stream == NULL)
2082  {
2083  errno = EBADF; // Bad File Number
2084  return(EOF);
2085  }
2086 
2087  fh = (FIL *) fdev_get_udata(stream);
2088  if(fh == NULL)
2089  {
2090  errno = EBADF; // Bad File Number
2091  return(EOF);
2092  }
2093 
2094  res = f_write(fh, &c, 1, (UINT *) &size);
2095  if( res != FR_OK || size != 1)
2096  {
2097  errno = fatfs_to_errno(res);
2098  stream->flags |= __SEOF;
2099  return(EOF);
2100  }
2101  return(c);
2102 }
2103 
2113 MEMSPACE
2115 {
2116  switch( Result )
2117  {
2118  case FR_OK: /* FatFS (0) Succeeded */
2119  return (0); /* POSIX OK */
2120  case FR_DISK_ERR: /* FatFS (1) A hard error occurred in the low level disk I/O layer */
2121  return (EIO); /* POSIX Input/output error (POSIX.1) */
2122 
2123  case FR_INT_ERR: /* FatFS (2) Assertion failed */
2124  return (EPERM); /* POSIX Operation not permitted (POSIX.1) */
2125 
2126  case FR_NOT_READY: /* FatFS (3) The physical drive cannot work */
2127  return (EBUSY); /* POSIX Device or resource busy (POSIX.1) */
2128 
2129  case FR_NO_FILE: /* FatFS (4) Could not find the file */
2130  return (ENOENT); /* POSIX No such file or directory (POSIX.1) */
2131 
2132  case FR_NO_PATH: /* FatFS (5) Could not find the path */
2133  return (ENOENT); /* POSIX No such file or directory (POSIX.1) */
2134 
2135  case FR_INVALID_NAME: /* FatFS (6) The path name format is invalid */
2136  return (EINVAL); /* POSIX Invalid argument (POSIX.1) */
2137 
2138  case FR_DENIED: /* FatFS (7) Access denied due to prohibited access or directory full */
2139  return (EACCES); /* POSIX Permission denied (POSIX.1) */
2140  case FR_EXIST: /* FatFS (8) Access denied due to prohibited access */
2141  return (EACCES); /* POSIX Permission denied (POSIX.1) */
2142 
2143  case FR_INVALID_OBJECT: /* FatFS (9) The file/directory object is invalid */
2144  return (EINVAL); /* POSIX Invalid argument (POSIX.1) */
2145 
2146  case FR_WRITE_PROTECTED: /* FatFS (10) The physical drive is write protected */
2147  return(EROFS); /* POSIX Read-only filesystem (POSIX.1) */
2148 
2149  case FR_INVALID_DRIVE: /* FatFS (11) The logical drive number is invalid */
2150  return(ENXIO); /* POSIX No such device or address (POSIX.1) */
2151 
2152  case FR_NOT_ENABLED: /* FatFS (12) The volume has no work area */
2153  return (ENOSPC); /* POSIX No space left on device (POSIX.1) */
2154 
2155  case FR_NO_FILESYSTEM: /* FatFS (13) There is no valid FAT volume */
2156  return(ENXIO); /* POSIX No such device or address (POSIX.1) */
2157 
2158  case FR_MKFS_ABORTED: /* FatFS (14) The f_mkfs() aborted due to any parameter error */
2159  return (EINVAL); /* POSIX Invalid argument (POSIX.1) */
2160 
2161  case FR_TIMEOUT: /* FatFS (15) Could not get a grant to access the volume within defined period */
2162  return (EBUSY); /* POSIX Device or resource busy (POSIX.1) */
2163 
2164  case FR_LOCKED: /* FatFS (16) The operation is rejected according to the file sharing policy */
2165  return (EBUSY); /* POSIX Device or resource busy (POSIX.1) */
2166 
2167 
2168  case FR_NOT_ENOUGH_CORE: /* FatFS (17) LFN working buffer could not be allocated */
2169  return (ENOMEM); /* POSIX Not enough space (POSIX.1) */
2170 
2171  case FR_TOO_MANY_OPEN_FILES:/* FatFS (18) Number of open files > _FS_SHARE */
2172  return (EMFILE); /* POSIX Too many open files (POSIX.1) */
2173 
2174  case FR_INVALID_PARAMETER:/* FatFS (19) Given parameter is invalid */
2175  return (EINVAL); /* POSIX Invalid argument (POSIX.1) */
2176 
2177  }
2178  return (EBADMSG); /* POSIX Bad message (POSIX.1) */
2179 }
2180 
2181 
2189 MEMSPACE
2191 {
2192  int i;
2193 
2194  FILE *stream;
2195 
2196  if(fh == NULL)
2197  {
2198  errno = EBADF;
2199  return(-1);
2200  }
2201 
2202  for(i=0;i<MAX_FILES;++i)
2203  {
2204  stream = __iob[i];
2205  if(stream)
2206  {
2207  if( fh == (FIL *) fdev_get_udata(stream) )
2208  return(i);
2209  }
2210  }
2211  errno = EBADF;
2212  return(-1);
2213 }
2214 
2226 MEMSPACE
2228 {
2229  struct tm tp;
2230  time_t unix;
2231 
2232  memset(&tp, 0, sizeof(struct tm));
2233 
2234  tp.tm_sec = (time << 1) & 0x3e; // 2 second resolution
2235  tp.tm_min = ((time >> 5) & 0x3f);
2236  tp.tm_hour = ((time >> 11) & 0x1f);
2237  tp.tm_mday = (date & 0x1f);
2238  tp.tm_mon = ((date >> 5) & 0x0f) - 1;
2239  tp.tm_year = ((date >> 9) & 0x7f) + 80;
2240  unix = timegm( &tp );
2241  return( unix );
2242 }
2243 
2251 MEMSPACE
2253 {
2254  tm_t *t = gmtime((time_t *) &epoch);
2255 
2256 /* Pack date and time into a uint32_t variable */
2257  *date = ((uint16_t)(t->tm_year - 80) << 9)
2258  | (((uint16_t)t->tm_mon+1) << 5)
2259  | (((uint16_t)t->tm_mday));
2260 
2261  *time = ((uint16_t)t->tm_hour << 11)
2262  | ((uint16_t)t->tm_min << 5)
2263  | ((uint16_t)t->tm_sec >> 1);
2264 }
2265 
2275 MEMSPACE
2277 {
2278  FILE *stream;
2279  FIL *fh;
2280 
2281  if(isatty( fileno ))
2282  {
2283  errno = EBADF;
2284  return(NULL);
2285  }
2286 
2287  // checks if fileno out of bounds
2288  stream = fileno_to_stream(fileno);
2289  if( stream == NULL )
2290  return(NULL);
2291 
2292  fh = fdev_get_udata(stream);
2293  if(fh == NULL)
2294  {
2295  errno = EBADF;
2296  return(NULL);
2297  }
2298  return(fh);
2299 }
2300 
2301 
2302 
2310 MEMSPACE
2312 {
2313  FILE *stream;
2314  FIL *fh;
2315 
2316  if(isatty( fileno ))
2317  {
2318  errno = EBADF;
2319  return(-1);
2320  }
2321 
2322  // checks if fileno out of bounds
2323  stream = fileno_to_stream(fileno);
2324  if(stream == NULL)
2325  {
2326  return(-1);
2327  }
2328 
2329  fh = fdev_get_udata(stream);
2330 
2331  if(fh != NULL)
2332  {
2333  safefree(fh);
2334  }
2335 
2336  if(stream->buf != NULL && stream->flags & __SMALLOC)
2337  {
2338  safefree(stream->buf);
2339  }
2340 
2341  __iob[fileno] = NULL;
2342  safefree(stream);
2343  return(fileno);
2344 }
2345 
2346 
2347 
2348 // =============================================
2354 MEMSPACE
2356 {
2357  int i;
2358  FILE *stream;
2359  FIL *fh;
2360 
2361  for(i=0;i<MAX_FILES;++i)
2362  {
2363  if(isatty(i))
2364  continue;
2365  if( __iob[i] == NULL)
2366  {
2367  stream = (FILE *) safecalloc(sizeof(FILE),1);
2368  if(stream == NULL)
2369  {
2370  errno = ENOMEM;
2371  return(-1);
2372  }
2373  fh = (FIL *) safecalloc(sizeof(FIL),1);
2374  if(fh == NULL)
2375  {
2376  safefree(stream);
2377  errno = ENOMEM;
2378  return(-1);
2379  }
2380 
2381  __iob[i] = stream;
2382  fdev_set_udata(stream, (void *) fh);
2383  return(i);
2384  }
2385  }
2386  errno = ENFILE;
2387  return(-1);
2388 }
2389 
2417 MEMSPACE
2418 int posix_fopen_modes_to_open(const char *mode)
2419 {
2420  int flag = 0;
2421 
2422  if(modecmp(mode,"r") || modecmp(mode,"rb"))
2423  {
2424  flag = O_RDONLY;
2425  return(flag);
2426  }
2427  if(modecmp(mode,"r+") || modecmp(mode, "r+b" ) || modecmp(mode, "rb+" ))
2428  {
2429  flag = O_RDWR | O_TRUNC;
2430  return(flag);
2431  }
2432  if(modecmp(mode,"w") || modecmp(mode,"wb"))
2433  {
2434  flag = O_WRONLY | O_CREAT | O_TRUNC;
2435  return(flag);
2436  }
2437  if(modecmp(mode,"w+") || modecmp(mode, "w+b" ) || modecmp(mode, "wb+" ))
2438  {
2439  flag = O_RDWR | O_CREAT | O_TRUNC;
2440  return(flag);
2441  }
2442  if(modecmp(mode,"a") || modecmp(mode,"ab"))
2443  {
2444  flag = O_WRONLY | O_CREAT | O_APPEND;
2445  return(flag);
2446  }
2447  if(modecmp(mode,"a+") || modecmp(mode, "a+b" ) || modecmp(mode, "ab+" ))
2448  {
2449  flag = O_RDWR | O_CREAT | O_APPEND;
2450  return(-1);
2451  }
2452  return(-1); // nvalid mode
2453 }
2454 
2455 // =============================================
2456 // =============================================
2458 // =============================================
2459 // =============================================
2460 
2461 
2462 
2467 MEMSPACE
2468 static void _fprintf_putc(struct _printf_t *p, char ch)
2469 {
2470  p->sent++;
2471  fputc(ch, (FILE *) p->buffer);
2472 }
2473 
2474 
2482 MEMSPACE
2483 int
2484 fprintf(FILE *fp, const char *format, ...)
2485 {
2486  printf_t fn;
2487  va_list va;
2488 
2489  fn.put = _fprintf_putc;
2490  fn.sent = 0;
2491  fn.buffer = (void *) fp;
2492 
2493  va_start(va, format);
2494  _printf_fn(&fn, format, va);
2495  va_end(va);
2496 
2497  return ((int)fn.sent);
2498 }
2499 
Definition: posix.h:87
#define fdev_set_udata(stream, u)
device IO udata
Definition: posix.h:291
MEMSPACE int new_file_descriptor(void)
Allocate a POSIX FILE descriptor. NOT POSIX.
Definition: posix.c:2355
MEMSPACE FRESULT f_closedir(DIR *dp)
Definition: ff.c:4226
MEMSPACE int putc(int c, FILE *stream)
Put a character to a stream See fdevopen() sets stream->put get for TTY devices.
Definition: posix.c:401
char d_name[MAX_NAME_LEN]
Definition: posix.h:144
int tm_min
Definition: time.h:44
#define S_IWGRP
Definition: posix.h:227
MEMSPACE int isatty(int fileno)
Test POSIX fileno if it is a Serial Console/TTY.
Definition: posix.c:196
MEMSPACE int WEAK_ATR strcmp(const char *str, const char *pat)
Compare two strings.
Definition: stringsup.c:362
MEMSPACE FIL * fileno_to_fatfs(int fileno)
Convert POSIX fileno to FatFS handle NOT POSIX.
Definition: posix.c:2276
MEMSPACE off_t lseek(int fileno, off_t position, int whence)
POSIX seek to file position.
Definition: posix.c:617
unsigned short uint16_t
Definition: send.c:18
MEMSPACE size_t WEAK_ATR strlen(const char *str)
String Length.
Definition: stringsup.c:146
#define FATFS_X
Definition: posix.h:244
MEMSPACE char * baseext(char *str)
File extention of a file name. NOT POSIX.
Definition: posix.c:1466
#define O_RDONLY
Definition: posix.h:187
MEMSPACE void dump_stat(struct stat *sp)
Display struct stat, from POSIX stat(0 or fstat(), in ASCII. NOT POSIX.
Definition: posix.c:1260
int(* get)(struct __file *)
Definition: posix.h:178
MEMSPACE int open(const char *pathname, int flags)
POSIX Open a file with integer mode flags.
Definition: posix.c:895
time_t st_ctime
Definition: posix.h:119
Definition: posix.h:85
MEMSPACE void clrerror(FILE *stream)
clrerror resets stream EOF and error flags
Definition: posix.c:1786
MEMSPACE FRESULT f_write(FIL *fp, const void *buff, UINT btw, UINT *bw)
Definition: ff.c:3607
MEMSPACE void sync(void)
POSIX Sync all pending file changes and metadata on ALL files.
Definition: posix.c:1069
dirent_t * readdir(DIR *dirp)
Definition: posix.c:1755
Master include file for project Includes all project includes and defines here.
MEMSPACE 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:2074
WORD fdate
Definition: ff.h:205
Definition: ff.h:203
Definition: posix.h:79
MEMSPACE int free_file_descriptor(int fileno)
Free POSIX fileno FILE descriptor. NOT POSIX.
Definition: posix.c:2311
MEMSPACE int close(int fileno)
POSIX Close a file with fileno handel.
Definition: posix.c:685
MEMSPACE FRESULT f_chmod(const TCHAR *path, BYTE attr, BYTE mask)
Definition: ff.c:4823
#define MATCH(a, b)
Definition: bdffontutil.h:39
MEMSPACE int fseek(FILE *stream, long offset, int whence)
POSIX seek to file possition.
Definition: posix.c:545
int tm_mday
Definition: time.h:46
uint32_t off_t
Definition: posix.h:51
#define FATFS_R
FATFS open modes.
Definition: posix.h:242
uint32_t mode_t
Definition: posix.h:47
#define O_APPEND
Definition: posix.h:195
Definition: ff.h:92
MEMSPACE int putchar(int c)
put a character to stdout See fdevopen() sets stream->put get for TTY devices
Definition: posix.c:351
MEMSPACE 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:420
MEMSPACE FILE * fopen(const char *path, const char *mode)
POSIX Open a file with path name and ascii file mode string.
Definition: posix.c:782
#define O_ACCMODE
POSIX open modes - no other combination are allowed.
Definition: posix.h:186
MEMSPACE FRESULT f_readdir(DIR *dp, FILINFO *fno)
Definition: ff.c:4260
MEMSPACE int rmdir(const char *pathname)
POSIX delete a directory.
Definition: posix.c:1671
static struct ip_info info
Definition: network.c:55
MEMSPACE FRESULT f_rename(const TCHAR *path_old, const TCHAR *path_new)
Definition: ff.c:4716
int tm_year
Definition: time.h:48
WORD ftime
Definition: ff.h:206
#define S_IWOTH
Definition: posix.h:232
unsigned int uint32_t
Definition: send.c:19
#define FA_OPEN_ALWAYS
Definition: ff.h:335
Definition: posix.h:76
int32_t ssize_t
Definition: posix.h:55
MEMSPACE void perror(const char *s)
POSIX perror() - convert POSIX errno to text with user message.
Definition: posix.c:1814
MEMSPACE int fgetpos(FILE *stream, size_t *pos)
POSIX get position of file stream.
Definition: posix.c:522
MEMSPACE int fatfs_to_errno(FRESULT Result)
Convert FafFs error result to POSIX errno. NOT POSIX.
Definition: posix.c:2114
FILE type structure.
Definition: posix.h:156
#define O_CREAT
Definition: posix.h:190
MEMSPACE WEAK_ATR char * strncpy(char *dest, const char *src, size_t size)
copy a string of at most N characters
Definition: stringsup.c:179
MEMSPACE void _printf_fn(printf_t *fn, __memx const char *fmt, va_list va)
vsnprintf function
Definition: printf.c:741
MEMSPACE FILE * fdevopen(int(*put)(char, FILE *), int(*get)(FILE *))
Device open functions.
Definition: posix.c:1876
#define S_ISREG(mode)
Definition: posix.h:218
Definition: ff.h:228
#define f_tell(fp)
Definition: ff.h:284
MEMSPACE FRESULT f_read(FIL *fp, void *buff, UINT btr, UINT *br)
Definition: ff.c:3506
#define MAX_FILES
Maximum number of POSIX file handles.
Definition: posix.h:260
MEMSPACE FRESULT f_close(FIL *fp)
Definition: ff.c:3808
Definition: posix.h:136
MEMSPACE 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:278
#define FA_WRITE
Definition: ff.h:331
MEMSPACE int fprintf(FILE *fp, const char *format,...)
fprintf function Example user defined printf function using fputc for I/O This method allows I/O to d...
Definition: posix.c:2484
MEMSPACE FRESULT f_open(FIL *fp, const TCHAR *path, BYTE mode)
Definition: ff.c:3304
int mkfs(char *name)
Formt SD card.
Definition: posix.c:1922
MEMSPACE time_t timegm(tm_t *t)
Convert tm_t structure as GMT time into GMT seconds since 1900. All calculactions are in GMT regardle...
Definition: time.c:347
MEMSPACE ssize_t write(int fd, const void *buf, size_t count)
POSIX Write count bytes from *buf to fileno fd.
Definition: posix.c:1180
#define FA_READ
Definition: ff.h:330
MEMSPACE int utime(const char *filename, const struct utimbuf *times)
Set Modification and Access time of a file.
Definition: posix.c:1407
MEMSPACE size_t 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:803
#define __SSTR
Definition: posix.h:162
char * buf
Definition: posix.h:157
MEMSPACE int feof(FILE *stream)
feof reports if the stream is at EOF
Definition: posix.c:505
MEMSPACE time_t time(time_t *t)
Return second from epoch - POSIX function.
Definition: time.c:843
POSIX utimbuf structure.
Definition: posix.h:124
#define FA_CREATE_ALWAYS
Definition: ff.h:334
MEMSPACE FRESULT f_opendir(DIR *dp, const TCHAR *path)
Definition: ff.c:4155
MEMSPACE FRESULT f_mkdir(const TCHAR *path)
Definition: ff.c:4622
void optimistic_yield(uint32_t interval_us)
Definition: user_task.c:102
MEMSPACE 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:2227
Definition: ff.h:223
Definition: ff.h:184
Definition: posix.h:72
#define EOF
End of file or device read.
Definition: posix.h:248
MEMSPACE FRESULT f_lseek(FIL *fp, FSIZE_t ofs)
Definition: ff.c:4000
Definition: ff.h:229
#define __SWR
Definition: posix.h:161
MEMSPACE int fatfs_to_fileno(FIL *fh)
Convert FatFS file handle to POSIX fileno. NOT POSIX.
Definition: posix.c:2190
uint32_t time_t
type of EPOCH result.
Definition: time.h:35
MEMSPACE int dirname(char *str)
POSIX directory name of a filename. Return the index of the last &#39;/&#39; character.
Definition: posix.c:1551
int tm_mon
Definition: time.h:47
MEMSPACE void put_rc(int rc)
display FatFs return code as ascii string
Definition: fatfs_sup.c:145
#define AM_SYS
Definition: ff.h:357
Definition: ff.h:225
unsigned char BYTE
Definition: integer.h:22
#define NULL
Definition: cpu.h:55
int len
Definition: posix.h:176
MEMSPACE char * ctime_gm(time_t *tp)
GMT version of POSIX ctime().
Definition: time.c:441
Definition: posix.h:69
unsigned char unget
Definition: posix.h:158
#define SEEK_END
Definition: posix.h:253
MEMSPACE 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:2252
MEMSPACE int fputs(const char *str, FILE *stream)
put a string to stdout See fdevopen() sets stream->put get for TTY devices
Definition: posix.c:457
Definition: ff.h:226
Definition: posix.h:75
MEMSPACE int syncfs(int fd)
POSIX Sync pending file changes and metadata for specified fileno.
Definition: posix.c:1096
#define FA_OPEN_EXISTING
Definition: ff.h:332
MEMSPACE int truncate(const char *path, off_t length)
POSIX truncate named file to length.
Definition: posix.c:1142
MEMSPACE ssize_t read(int fd, const void *buf, size_t count)
POSIX read count bytes from *buf to fileno fd.
Definition: posix.c:1006
Definition: diskio.h:24
MEMSPACE int WEAK_ATR tolower(int c)
Convert character to lower case, only if it is upper case.
Definition: stringsup.c:96
MEMSPACE void * safemalloc(size_t size)
Safe Malloc - Display Error message if Malloc fails.
Definition: system.c:146
MEMSPACE 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:868
MEMSPACE int ftruncate(int fd, off_t length)
POSIX truncate open file to length.
Definition: posix.c:827
MEMSPACE int unlink(const char *pathname)
POSIX delete a file.
Definition: posix.c:1693
FRESULT
Definition: ff.h:220
#define O_TRUNC
Definition: posix.h:194
mode_t st_mode
Definition: posix.h:109
Definition: posix.h:91
#define __SRD
Definition: posix.h:160
MEMSPACE int posix_fopen_modes_to_open(const char *mode)
Convert POSIX fopen mode to POSIX open mode flags. NOT POSIX.
Definition: posix.c:2418
#define _FDEV_SETUP_RW
Definition: posix.h:300
MEMSPACE int mmc_init(int verbose)
Initialize MMC and FatFs interface, display diagnostics.
Definition: mmc_hal.c:208
#define _FDEV_SETUP_WRITE
Definition: posix.h:299
FSIZE_t fsize
Definition: ff.h:204
int ind
Definition: ili9341.c:373
MEMSPACE int fclose(FILE *stream)
POSIX close a file stream.
Definition: posix.c:1239
MEMSPACE tm_t * gmtime(time_t *tp)
Convert epoch GMT time_t *tp into POSIX static tm_t *t.
Definition: time.c:471
Definition: posix.h:64
void * buffer
Definition: mathio.h:84
#define __SEOF
Definition: posix.h:165
#define stdin
define stdin, stdout and stderr
Definition: posix.h:269
#define modecmp(str, pat)
used in posix.c to compare to ascii file modes
Definition: posix.h:238
TCHAR fname[_MAX_LFN+1]
Definition: ff.h:210
Various string and character functions.
Definition: ff.h:236
int(* put)(char, struct __file *)
Definition: posix.h:177
#define __SUNGET
Definition: posix.h:166
POSIX stat structure.
Definition: posix.h:105
MEMSPACE int fileno(FILE *stream)
Convert POSIX stream pointer to POSIX fileno (index of __iob[])
Definition: posix.c:724
time_t modtime
Definition: posix.h:127
MEMSPACE FRESULT f_unlink(const TCHAR *path)
Definition: ff.c:4526
#define SEEK_SET
Seek offset macros.
Definition: posix.h:251
POSIX struct tm.
Definition: time.h:41
#define stdout
Definition: posix.h:270
static dirent_t _de
POSIX opendir.
Definition: posix.c:1754
MEMSPACE 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:216
#define S_IFDIR
Definition: posix.h:202
MEMSPACE int puts(const char *str)
put a string to stdout See fdevopen() sets stream->put get for TTY devices
Definition: posix.c:480
FSIZE_t fptr
Definition: ff.h:165
BYTE fattrib
Definition: ff.h:207
#define MEMSPACE
Definition: cpu.h:25
unsigned int UINT
Definition: integer.h:19
Definition: posix.h:86
#define FM_FAT32
Definition: ff.h:343
MEMSPACE void wdt_reset(void)
reset watchdog
Definition: system.c:190
#define FATFS_W
Definition: posix.h:243
MEMSPACE int fatfs_getc(FILE *stream)
Private FatFs function called by fgetc() to get a byte from file stream FIXME buffer this function ca...
Definition: posix.c:1984
MEMSPACE int mkdir(const char *pathname, mode_t mode)
POSIX make a directory.
Definition: posix.c:1619
MEMSPACE int ungetc(int c, FILE *stream)
Un-Get byte from a TTY device or FatFs file stream.
Definition: posix.c:368
MEMSPACE char * strerror_r(int errnum, char *buf, size_t buflen)
POSIX strerror_r() - convert POSIX errno to text with user message.
Definition: posix.c:1857
#define AM_RDO
Definition: ff.h:355
#define f_size(fp)
Definition: ff.h:285
int closedir(DIR *dirp)
POSIX closedir.
Definition: posix.c:1717
MEMSPACE 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:1513
MEMSPACE int printf(const char *format,...)
#define WEAK_ATR
Definition: stringsup.h:33
Definition: ff.h:237
MEMSPACE long ftell(FILE *stream)
POSIX file position of open stream.
Definition: posix.c:585
#define S_IWUSR
Definition: posix.h:222
time_t st_atime
Definition: posix.h:117
#define _FDEV_ERR
Definition: posix.h:296
Definition: posix.h:93
MEMSPACE FRESULT f_stat(const TCHAR *path, FILINFO *fno)
Definition: ff.c:4351
MEMSPACE int chdir(const char *pathname)
POSIX change directory.
Definition: posix.c:1488
#define stderr
Definition: posix.h:271
Definition: ff.h:161
MEMSPACE char * basename(char *str)
POSIX Basename of filename.
Definition: posix.c:1446
int errno
Note: fdevopen assigns stdin,stdout,stderr.
Definition: posix.c:129
MEMSPACE FRESULT f_getcwd(TCHAR *buff, UINT len)
Definition: ff.c:3919
MEMSPACE int fsetpos(FILE *stream, size_t *pos)
POSIX set position of file stream.
Definition: posix.c:571
MEMSPACE char * getcwd(char *pathname, int len)
POSIX get current working directory.
Definition: posix.c:1596
#define __SERR
Definition: posix.h:164
Definition: posix.h:68
#define L(x)
Definition: cal_dex.c:54
int sent
Definition: mathio.h:86
#define _FDEV_SETUP_READ
Definition: posix.h:298
int tm_sec
Definition: time.h:43
MEMSPACE int stat(char *name, struct stat *buf)
POSIX stat - get file status of named file.
Definition: posix.c:1344
Definition: posix.h:65
MEMSPACE void safefree(void *p)
Safe free - Only free a pointer if it is in malloc memory range. We want to try to catch frees of sta...
Definition: system.c:165
MEMSPACE char * mctime(time_t timev)
Display Ascii formatted time from timev seconds NOT POSIX.
Definition: posix.c:1323
const char * sys_errlist[]
POSIX error messages for each errno value.
Definition: posix.c:144
#define fdev_get_udata(stream)
Definition: posix.h:292
MEMSPACE int getchar()
functions normally defined as macros
Definition: posix.c:336
MEMSPACE FRESULT f_sync(FIL *fp)
Definition: ff.c:3729
FILE * __iob[MAX_FILES]
POSIX fileno to POSIX FILE stream table.
Definition: posix.c:139
off_t st_size
Definition: posix.h:114
Definition: ff.h:221
MEMSPACE void rewind(FILE *stream)
POSIX rewind file to the beginning.
Definition: posix.c:666
POSIX wrapper for FatFS.
DIR * opendir(const char *pathdir)
Definition: posix.c:1736
int tm_hour
Definition: time.h:45
#define S_IFREG
Definition: posix.h:205
unsigned char uint8_t
Definition: send.c:17
MEMSPACE int ferror(FILE *stream)
ferror reports if the stream has an error flag set
Definition: posix.c:1798
MEMSPACE FRESULT f_chdir(const TCHAR *path)
Definition: ff.c:3866
uint8_t flags
Definition: posix.h:159
#define AM_DIR
Definition: ff.h:358
void(* put)(struct _printf_t *, char)
Definition: mathio.h:83
time_t st_mtime
Definition: posix.h:118
#define O_WRONLY
Definition: posix.h:188
#define O_RDWR
Definition: posix.h:189
MEMSPACE FRESULT f_truncate(FIL *fp)
Definition: ff.c:4475
MEMSPACE FILE * fileno_to_stream(int fileno)
Convert POSIX fileno to POSIX FILE stream pointer. NOT POSIX.
Definition: posix.c:754
static DIR _dp
POSIX opendir.
Definition: posix.c:1735
uid_t st_uid
Definition: posix.h:111
static MEMSPACE void _fprintf_putc(struct _printf_t *p, char ch)
fprintf character write function
Definition: posix.c:2468
MEMSPACE FRESULT f_mount(FATFS *fs, const TCHAR *path, BYTE opt)
Definition: ff.c:3255
MEMSPACE void * safecalloc(size_t nmemb, size_t size)
Safe Calloc - Display Error message if Calloc fails.
Definition: system.c:128
#define S_ISDIR(mode)
Definition: posix.h:215
MEMSPACE FRESULT f_utime(const TCHAR *path, const FILINFO *fno)
Definition: ff.c:4869
#define __SMALLOC
Definition: posix.h:167
#define SEEK_CUR
Definition: posix.h:252
MEMSPACE int rename(const char *oldpath, const char *newpath)
POSIX rename a file by name.
Definition: posix.c:1648
gid_t st_gid
Definition: posix.h:112
int size
Definition: posix.h:175
undefine any potential macro version of these functions
Definition: mathio.h:81
MEMSPACE FRESULT f_mkfs(const TCHAR *path, BYTE opt, DWORD au, void *work, UINT len)
Definition: ff.c:5296
MEMSPACE char WEAK_ATR * strerror(int errnum)
POSIX strerror() - convert POSIX errno to text with user message.
Definition: posix.c:1840
Definition: posix.h:98