H. Peter Anvin | 9222364 | 2017-02-23 18:39:00 -0800 | [diff] [blame] | 1 | /* ----------------------------------------------------------------------- * |
| 2 | * |
| 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. |
| 6 | * |
| 7 | * Redistribution and use in source and binary forms, with or without |
| 8 | * modification, are permitted provided that the following |
| 9 | * conditions are met: |
| 10 | * |
| 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. |
| 17 | * |
| 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. |
| 31 | * |
| 32 | * ----------------------------------------------------------------------- */ |
| 33 | |
| 34 | #ifndef NASMLIB_FILE_H |
| 35 | #define NASMLIB_FILE_H |
| 36 | |
| 37 | #include "compiler.h" |
| 38 | #include "nasmlib.h" |
H. Peter Anvin | b20bc73 | 2017-03-07 19:23:03 -0800 | [diff] [blame] | 39 | #include "error.h" |
H. Peter Anvin | 9222364 | 2017-02-23 18:39:00 -0800 | [diff] [blame] | 40 | |
| 41 | #include <errno.h> |
| 42 | |
| 43 | #ifdef HAVE_FCNTL_H |
| 44 | # include <fcntl.h> |
| 45 | #endif |
H. Peter Anvin | 675e7b7 | 2017-04-05 21:19:03 -0700 | [diff] [blame] | 46 | #ifdef HAVE_SYS_TYPES_H |
| 47 | # include <sys/types.h> |
| 48 | #endif |
H. Peter Anvin | 9222364 | 2017-02-23 18:39:00 -0800 | [diff] [blame] | 49 | #ifdef HAVE_SYS_STAT_H |
| 50 | # include <sys/stat.h> |
| 51 | #endif |
| 52 | #ifdef HAVE_IO_H |
| 53 | # include <io.h> |
| 54 | #endif |
| 55 | #ifdef HAVE_UNISTD_H |
| 56 | # include <unistd.h> |
| 57 | #endif |
| 58 | #ifdef HAVE_SYS_MMAN_H |
| 59 | # include <sys/mman.h> |
| 60 | #endif |
| 61 | |
H. Peter Anvin | 9222364 | 2017-02-23 18:39:00 -0800 | [diff] [blame] | 62 | #if !defined(HAVE_ACCESS) && defined(HAVE__ACCESS) |
| 63 | # define HAVE_ACCESS 1 |
| 64 | # define access _access |
| 65 | #endif |
| 66 | #ifndef R_OK |
| 67 | # define R_OK 4 /* Classic Unix constant, same on Windows */ |
| 68 | #endif |
| 69 | |
| 70 | /* Can we adjust the file size without actually writing all the bytes? */ |
H. Peter Anvin | 3118429 | 2017-04-06 15:30:56 -0700 | [diff] [blame^] | 71 | #ifdef HAVE__CHSIZE_S |
| 72 | # define nasm_ftruncate(fd,size) _chsize_s(fd,size) |
| 73 | #elif defined(HAVE__CHSIZE) |
| 74 | # define nasm_ftruncate(fd,size) _chsize(fd,size) |
| 75 | #elif defined(HAVE_FTRUNCATE) |
| 76 | # define nasm_ftruncate(fd,size) ftruncate(fd,size) |
H. Peter Anvin | 9222364 | 2017-02-23 18:39:00 -0800 | [diff] [blame] | 77 | #endif |
| 78 | |
| 79 | /* |
H. Peter Anvin | 3118429 | 2017-04-06 15:30:56 -0700 | [diff] [blame^] | 80 | * On Win32/64, stat has a 32-bit file size but _stati64 has a 64-bit file |
| 81 | * size. Things get complicated because some of these may be macros, |
| 82 | * which autoconf won't pick up on as the standard autoconf tests do |
| 83 | * #undef. |
H. Peter Anvin | 9222364 | 2017-02-23 18:39:00 -0800 | [diff] [blame] | 84 | */ |
H. Peter Anvin | 3118429 | 2017-04-06 15:30:56 -0700 | [diff] [blame^] | 85 | #ifdef _stati64 |
| 86 | # define HAVE_STRUCT__STATI64 1 |
| 87 | # define HAVE__STATI64 1 |
| 88 | #endif |
| 89 | #ifdef _fstati64 |
| 90 | # define HAVE__FSTATI64 1 |
| 91 | #endif |
| 92 | |
| 93 | #ifdef HAVE_STRUCT__STATI64 |
| 94 | typedef struct _stati64 nasm_struct_stat; |
| 95 | # ifdef HAVE__STATI64 |
| 96 | # define nasm_stat _stati64 |
| 97 | # endif |
| 98 | # ifdef HAVE__FSTATI64 |
H. Peter Anvin | 675e7b7 | 2017-04-05 21:19:03 -0700 | [diff] [blame] | 99 | # define nasm_fstat _fstati64 |
| 100 | # endif |
H. Peter Anvin | 3118429 | 2017-04-06 15:30:56 -0700 | [diff] [blame^] | 101 | #elif defined(HAVE_STRUCT_STAT) |
| 102 | typedef struct stat nasm_struct_stat; |
| 103 | # ifdef HAVE_STAT |
| 104 | # define nasm_stat stat |
| 105 | # endif |
| 106 | # ifdef HAVE_FSTAT |
H. Peter Anvin | 9222364 | 2017-02-23 18:39:00 -0800 | [diff] [blame] | 107 | # define nasm_fstat fstat |
| 108 | # endif |
| 109 | #endif |
| 110 | |
H. Peter Anvin | 3118429 | 2017-04-06 15:30:56 -0700 | [diff] [blame^] | 111 | #ifndef HAVE_FILENO |
| 112 | # ifdef fileno /* autoconf doesn't always pick up macros */ |
| 113 | # define HAVE_FILENO 1 |
| 114 | # elif defined(HAVE__FILENO) |
| 115 | # define HAVE_FILENO 1 |
| 116 | # define fileno _fileno |
| 117 | # endif |
| 118 | #endif |
| 119 | |
| 120 | /* These functions are utterly useless without fileno() */ |
| 121 | #ifndef HAVE_FILENO |
| 122 | # undef nasm_fstat |
| 123 | # undef nasm_ftruncate |
| 124 | # undef HAVE_MMAP |
| 125 | # undef HAVE__FILELENGTHI64 |
| 126 | #endif |
| 127 | |
H. Peter Anvin | 9222364 | 2017-02-23 18:39:00 -0800 | [diff] [blame] | 128 | #endif /* NASMLIB_FILE_H */ |