H. Peter Anvin | bdae10b | 2016-03-08 01:22:13 -0800 | [diff] [blame] | 1 | /* ----------------------------------------------------------------------- * |
| 2 | * |
| 3 | * Copyright 1996-2016 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 | #include "compiler.h" |
| 35 | #include "nasmlib.h" |
| 36 | |
| 37 | #include <errno.h> |
| 38 | |
| 39 | #ifdef HAVE_IO_H |
| 40 | # include <io.h> |
| 41 | #endif |
| 42 | #ifdef HAVE_UNISTD_H |
| 43 | # include <unistd.h> |
| 44 | #endif |
| 45 | |
H. Peter Anvin | 7ab5595 | 2016-03-08 02:23:23 -0800 | [diff] [blame^] | 46 | /* Missing fseeko/ftello */ |
| 47 | #ifndef HAVE_FSEEKO |
| 48 | # undef off_t /* Just in case it is a macro */ |
| 49 | # ifdef HAVE__FSEEKI64 |
| 50 | # define fseeko _fseeki64 |
| 51 | # define ftello _ftelli64 |
| 52 | # define off_t int64_t |
| 53 | # else |
| 54 | # define fseeko fseek |
| 55 | # define ftello ftell |
| 56 | # define off_t long |
| 57 | # endif |
| 58 | #endif |
| 59 | |
| 60 | /* Can we adjust the file size without actually writing all the bytes? */ |
| 61 | #ifdef HAVE_FILENO /* Useless without fileno() */ |
| 62 | # ifdef HAVE__CHSIZE_S |
| 63 | # define nasm_ftruncate(fd,size) _chsize_s(fd,size) |
| 64 | # elif defined(HAVE__CHSIZE) |
| 65 | # define nasm_ftruncate(fd,size) _chsize(fd,size) |
| 66 | # elif defined(HAVE_FTRUNCATE) |
| 67 | # define nasm_ftruncate(fd,size) ftruncate(fd,size) |
| 68 | # endif |
| 69 | #endif |
| 70 | |
H. Peter Anvin | bdae10b | 2016-03-08 01:22:13 -0800 | [diff] [blame] | 71 | void nasm_write(const void *ptr, size_t size, FILE *f) |
| 72 | { |
| 73 | size_t n = fwrite(ptr, 1, size, f); |
| 74 | if (n != size || ferror(f) || feof(f)) |
| 75 | nasm_fatal(0, "unable to write output: %s", strerror(errno)); |
| 76 | } |
| 77 | |
| 78 | #ifdef WORDS_LITTLEENDIAN |
| 79 | |
| 80 | void fwriteint16_t(uint16_t data, FILE * fp) |
| 81 | { |
| 82 | nasm_write(&data, 2, fp); |
| 83 | } |
| 84 | |
| 85 | void fwriteint32_t(uint32_t data, FILE * fp) |
| 86 | { |
| 87 | nasm_write(&data, 4, fp); |
| 88 | } |
| 89 | |
| 90 | void fwriteint64_t(uint64_t data, FILE * fp) |
| 91 | { |
| 92 | nasm_write(&data, 8, fp); |
| 93 | } |
| 94 | |
| 95 | void fwriteaddr(uint64_t data, int size, FILE * fp) |
| 96 | { |
| 97 | nasm_write(&data, size, fp); |
| 98 | } |
| 99 | |
| 100 | #else /* not WORDS_LITTLEENDIAN */ |
| 101 | |
| 102 | void fwriteint16_t(uint16_t data, FILE * fp) |
| 103 | { |
| 104 | char buffer[2], *p = buffer; |
| 105 | WRITESHORT(p, data); |
| 106 | nasm_write(buffer, 2, fp); |
| 107 | } |
| 108 | |
| 109 | void fwriteint32_t(uint32_t data, FILE * fp) |
| 110 | { |
| 111 | char buffer[4], *p = buffer; |
| 112 | WRITELONG(p, data); |
| 113 | nasm_write(buffer, 4, fp); |
| 114 | } |
| 115 | |
| 116 | void fwriteint64_t(uint64_t data, FILE * fp) |
| 117 | { |
| 118 | char buffer[8], *p = buffer; |
| 119 | WRITEDLONG(p, data); |
| 120 | nasm_write(buffer, 8, fp); |
| 121 | } |
| 122 | |
| 123 | void fwriteaddr(uint64_t data, int size, FILE * fp) |
| 124 | { |
| 125 | char buffer[8], *p = buffer; |
| 126 | WRITEADDR(p, data, size); |
| 127 | nasm_write(buffer, size, fp); |
| 128 | } |
| 129 | |
| 130 | #endif |
| 131 | |
| 132 | |
H. Peter Anvin | bdae10b | 2016-03-08 01:22:13 -0800 | [diff] [blame] | 133 | void fwritezero(size_t bytes, FILE *fp) |
| 134 | { |
| 135 | size_t blksize; |
| 136 | |
| 137 | #ifdef nasm_ftruncate |
| 138 | if (bytes >= BUFSIZ && !ferror(fp) && !feof(fp)) { |
| 139 | off_t pos = ftello(fp); |
| 140 | if (pos >= 0) { |
| 141 | if (!fflush(fp) && |
| 142 | !nasm_ftruncate(fileno(fp), pos + bytes) && |
| 143 | !fseeko(fp, pos+bytes, SEEK_SET)) |
| 144 | return; |
| 145 | } |
| 146 | } |
| 147 | #endif |
| 148 | |
| 149 | while (bytes) { |
| 150 | blksize = (bytes < ZERO_BUF_SIZE) ? bytes : ZERO_BUF_SIZE; |
| 151 | |
| 152 | nasm_write(zero_buffer, blksize, fp); |
| 153 | bytes -= blksize; |
| 154 | } |
| 155 | } |