123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234 |
- #ifndef NASMLIB_FILE_H
- #define NASMLIB_FILE_H
- #include "compiler.h"
- #include "nasmlib.h"
- #include "error.h"
- #include <errno.h>
- #ifdef HAVE_FCNTL_H
- # include <fcntl.h>
- #endif
- #ifdef HAVE_SYS_TYPES_H
- #endif
- #ifdef HAVE_SYS_STAT_H
- # include <sys/stat.h>
- #endif
- #ifdef HAVE_IO_H
- # include <io.h>
- #endif
- #ifdef HAVE_UNISTD_H
- # include <unistd.h>
- #endif
- #ifdef HAVE_SYS_MMAN_H
- # include <sys/mman.h>
- #endif
- #ifndef R_OK
- # define R_OK 4
- #endif
- #ifdef HAVE__CHSIZE_S
- # define os_ftruncate(fd,size) _chsize_s(fd,size)
- #elif defined(HAVE__CHSIZE)
- # define os_ftruncate(fd,size) _chsize(fd,size)
- #elif defined(HAVE_FTRUNCATE)
- # define os_ftruncate(fd,size) ftruncate(fd,size)
- #endif
- #ifdef _WIN32
- # include <wchar.h>
- typedef wchar_t *os_filename;
- typedef wchar_t os_fopenflag;
- os_filename os_mangle_filename(const char *filename);
- static inline void os_free_filename(os_filename filename)
- {
- nasm_free(filename);
- }
- # define os_fopen _wfopen
- # define os_access _waccess
- typedef struct _stati64 os_struct_stat;
- # define os_stat _wstati64
- # define os_fstat _fstati64
- #include <io.h>
- #include <fcntl.h>
- static inline void os_set_binary_mode(FILE *f) {
- int ret = _setmode(_fileno(f), _O_BINARY);
- if (ret == -1) {
- nasm_fatalf(ERR_NOFILE, "unable to open file: %s",
- strerror(errno));
- }
- }
- #else
- typedef const char *os_filename;
- typedef char os_fopenflag;
- static inline os_filename os_mangle_filename(const char *filename)
- {
- return filename;
- }
- static inline void os_free_filename(os_filename filename)
- {
- (void)filename;
- }
- static inline void os_set_binary_mode(FILE *f) {
- (void)f;
- }
- # define os_fopen fopen
- #if defined(HAVE_FACCESSAT) && defined(AT_EACCESS)
- static inline int os_access(os_filename pathname, int mode)
- {
- return faccessat(AT_FDCWD, pathname, mode, AT_EACCESS);
- }
- # define os_access os_access
- #elif defined(HAVE_ACCESS)
- # define os_access access
- #endif
- #ifdef HAVE_STRUCT_STAT
- typedef struct stat os_struct_stat;
- # ifdef HAVE_STAT
- # define os_stat stat
- # endif
- # ifdef HAVE_FSTAT
- # define os_fstat fstat
- # endif
- #else
- struct dummy_struct_stat {
- int st_mode;
- int st_size;
- };
- typedef struct dummy_struct_stat os_struct_stat;
- #endif
- #endif
- #ifdef S_ISREG
- #elif defined(HAVE_S_ISREG)
- # define S_ISREG S_ISREG
- #elif defined(S_IFMT) && defined(S_IFREG)
- # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
- #elif defined(_S_IFMT) && defined(_S_IFREG)
- # define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
- #endif
- #ifdef fileno
- #elif defined(HAVE_FILENO)
- # define fileno fileno
- #elif defined(_fileno) || defined(HAVE__FILENO)
- # define fileno _fileno
- #endif
- #ifndef S_ISREG
- # undef os_stat
- # undef os_fstat
- #endif
- #ifndef fileno
- # undef os_fstat
- # undef os_ftruncate
- # undef HAVE_MMAP
- #endif
- #ifndef os_stat
- static inline int os_stat(os_filename osfname, os_struct_stat *st)
- {
- (void)osfname;
- (void)st;
- return -1;
- }
- #endif
- #ifndef os_fstat
- static inline int os_fstat(int fd, os_struct_stat *st)
- {
- (void)osfname;
- (void)st;
- return -1;
- }
- #endif
- #ifndef S_ISREG
- static inline bool S_ISREG(int m)
- {
- (void)m;
- return false;
- }
- #endif
- #endif
|