POSIX API

The best-known API for accessing and managing files and directories is specified within the POSIX standard (IEEE Std 1003.1). The basis of some of this functionality, in particular buffered input/output, lies in the ISO C standard (ISO/IEC 9899), though many extensions provide new features and clarify existing behaviors. Functions and macros prototyped in four header files are of particular importance:

  • stdio.h. Standard buffered input/output (fopen(), fread(), etc), operating on FILE objects.
  • dirent.h. Directory accesses (opendir(), readdir(), etc), operating on DIR objects.
  • unistd.h. Miscellaneous functions, including working directory management (chdir(), getcwd()), ftruncate() and rmdir().
  • sys/stat.h. File statistics functions and mkdir().

µC/FS provides a POSIX-like API based on a subset of the functions in these four header files. To avoid conflicts with the user compilation environment, files, functions and objects are renamed:

  • All functions begin with ‘fs_’. For example, fopen() is renamed fs_fopen(), opendir() is renamed fs_opendir(), getcwd() is renamed fs_getcwd(), etc.
  • All objects begin with ‘FS_’. So fs_fopen() returns a pointer to a FS_FILE and fs_opendir() returns a pointer to a FS_DIR.
  • Some argument types are renamed. For example, the second and third parameters of fs_fread() are typed fs_size_t to avoid conflicting with other size_t definitions.

Important warning about the POSIX API

The µC/FS implementation of the POSIX API is not 100% compliant. Most notably, the errno error flag isn’t set when an error occurs and thus it is recommended to use the µC/FS proprietary API (FSFile_####()FSDir_####(), FSEntry_####(), etc.).