1 /* ----------------------------------------------------------------------- *
3 * Copyright 1996-2017 The NASM Authors - All Rights Reserved
4 * See the file AUTHORS included with the NASM distribution for
5 * the specific copyright holders.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * * Redistributions in binary form must reproduce the above
14 * copyright notice, this list of conditions and the following
15 * disclaimer in the documentation and/or other materials provided
16 * with the distribution.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
19 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
20 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
23 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
26 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 * ----------------------------------------------------------------------- */
34 #ifndef NASMLIB_FILE_H
35 #define NASMLIB_FILE_H
46 #ifdef HAVE_SYS_TYPES_H
48 #ifdef HAVE_SYS_STAT_H
49 # include <sys/stat.h>
57 #ifdef HAVE_SYS_MMAN_H
58 # include <sys/mman.h>
62 # define R_OK 4 /* Classic Unix constant, same on Windows */
65 /* Can we adjust the file size without actually writing all the bytes? */
67 # define os_ftruncate(fd,size) _chsize_s(fd,size)
68 #elif defined(HAVE__CHSIZE)
69 # define os_ftruncate(fd,size) _chsize(fd,size)
70 #elif defined(HAVE_FTRUNCATE)
71 # define os_ftruncate(fd,size) ftruncate(fd,size)
75 * On Windows, we want to use _wfopen(), as fopen() has a much smaller limit
76 * on the path length that it supports. Furthermore, we want to prefix the
77 * path name with \\?\ in order to let the Windows kernel know that
78 * we are not limited to PATH_MAX characters. Thus, we wrap all the functions
79 * which take filenames...
83 typedef wchar_t *os_filename
;
84 typedef wchar_t os_fopenflag
;
86 os_filename
os_mangle_filename(const char *filename
);
87 static inline void os_free_filename(os_filename filename
)
92 # define os_fopen _wfopen
93 # define os_access _waccess
96 * On Win32/64, we have to use the _wstati64() function. Note that
97 * we can't use _wstat64() without depending on a needlessly new
101 typedef struct _stati64 os_struct_stat
;
103 # define os_stat _wstati64
104 # define os_fstat _fstati64
107 * On Win32/64, freopen() and _wfreopen() fails when the mode string
108 * is with the letter 'b' that represents to set binary mode. On
109 * POSIX operating systems, the 'b' is ignored, without failure.
115 static inline void os_set_binary_mode(FILE *f
) {
116 int ret
= _setmode(_fileno(f
), _O_BINARY
);
119 nasm_fatalf(ERR_NOFILE
, "unable to open file: %s",
124 #else /* not _WIN32 */
126 typedef const char *os_filename
;
127 typedef char os_fopenflag
;
129 static inline os_filename
os_mangle_filename(const char *filename
)
133 static inline void os_free_filename(os_filename filename
)
135 (void)filename
; /* Nothing to do */
138 static inline void os_set_binary_mode(FILE *f
) {
142 # define os_fopen fopen
144 #if defined(HAVE_FACCESSAT) && defined(AT_EACCESS)
145 static inline int os_access(os_filename pathname
, int mode
)
147 return faccessat(AT_FDCWD
, pathname
, mode
, AT_EACCESS
);
149 # define os_access os_access
150 #elif defined(HAVE_ACCESS)
151 # define os_access access
154 #ifdef HAVE_STRUCT_STAT
155 typedef struct stat os_struct_stat
;
157 # define os_stat stat
160 # define os_fstat fstat
163 struct dummy_struct_stat
{
167 typedef struct dummy_struct_stat os_struct_stat
;
170 #endif /* Not _WIN32 */
174 #elif defined(HAVE_S_ISREG)
175 /* exists, but not a macro */
176 # define S_ISREG S_ISREG
177 #elif defined(S_IFMT) && defined(S_IFREG)
178 # define S_ISREG(m) (((m) & S_IFMT) == S_IFREG)
179 #elif defined(_S_IFMT) && defined(_S_IFREG)
180 # define S_ISREG(m) (((m) & _S_IFMT) == _S_IFREG)
185 #elif defined(HAVE_FILENO)
186 /* exists, but not a macro */
187 # define fileno fileno
188 #elif defined(_fileno) || defined(HAVE__FILENO)
189 # define fileno _fileno
197 /* Disable these functions if they don't support something we need */
205 * If we don't have functional versions of these functions,
206 * stub them out so we don't need so many #ifndefs
209 static inline int os_stat(os_filename osfname
, os_struct_stat
*st
)
218 static inline int os_fstat(int fd
, os_struct_stat
*st
)
227 static inline bool S_ISREG(int m
)
234 #endif /* NASMLIB_FILE_H */