1 /* ----------------------------------------------------------------------- *
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.
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 * ----------------------------------------------------------------------- */
46 /* Missing fseeko/ftello */
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
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)
71 void nasm_write(const void *ptr
, size_t size
, FILE *f
)
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
));
78 #ifdef WORDS_LITTLEENDIAN
80 void fwriteint16_t(uint16_t data
, FILE * fp
)
82 nasm_write(&data
, 2, fp
);
85 void fwriteint32_t(uint32_t data
, FILE * fp
)
87 nasm_write(&data
, 4, fp
);
90 void fwriteint64_t(uint64_t data
, FILE * fp
)
92 nasm_write(&data
, 8, fp
);
95 void fwriteaddr(uint64_t data
, int size
, FILE * fp
)
97 nasm_write(&data
, size
, fp
);
100 #else /* not WORDS_LITTLEENDIAN */
102 void fwriteint16_t(uint16_t data
, FILE * fp
)
104 char buffer
[2], *p
= buffer
;
106 nasm_write(buffer
, 2, fp
);
109 void fwriteint32_t(uint32_t data
, FILE * fp
)
111 char buffer
[4], *p
= buffer
;
113 nasm_write(buffer
, 4, fp
);
116 void fwriteint64_t(uint64_t data
, FILE * fp
)
118 char buffer
[8], *p
= buffer
;
120 nasm_write(buffer
, 8, fp
);
123 void fwriteaddr(uint64_t data
, int size
, FILE * fp
)
125 char buffer
[8], *p
= buffer
;
126 WRITEADDR(p
, data
, size
);
127 nasm_write(buffer
, size
, fp
);
133 void fwritezero(size_t bytes
, FILE *fp
)
137 #ifdef nasm_ftruncate
138 if (bytes
>= BUFSIZ
&& !ferror(fp
) && !feof(fp
)) {
139 off_t pos
= ftello(fp
);
142 !nasm_ftruncate(fileno(fp
), pos
+ bytes
) &&
143 !fseeko(fp
, pos
+bytes
, SEEK_SET
))
150 blksize
= (bytes
< ZERO_BUF_SIZE
) ? bytes
: ZERO_BUF_SIZE
;
152 nasm_write(zero_buffer
, blksize
, fp
);
158 /* If we are using glibc, attempt to mmap the files for speed */
159 # define READ_TEXT_FILE "rtm"
160 # define READ_BIN_FILE "rbm"
162 # define READ_TEXT_FILE "rt"
163 # define READ_BIN_FILE "rb"
165 #define WRITE_TEXT_FILE "wt"
166 #define WRITE_BIN_FILE "wb"
168 FILE *nasm_open_read(const char *filename
, enum file_flags flags
)
172 f
= fopen(filename
, (flags
& NF_TEXT
) ? READ_TEXT_FILE
: READ_BIN_FILE
);
173 if (!f
&& (flags
& NF_FATAL
))
174 nasm_fatal(ERR_NOFILE
, "unable to open input file: `%s': %s",
175 filename
, strerror(errno
));
180 FILE *nasm_open_write(const char *filename
, enum file_flags flags
)
184 f
= fopen(filename
, (flags
& NF_TEXT
) ? WRITE_TEXT_FILE
: WRITE_BIN_FILE
);
185 if (!f
&& (flags
& NF_FATAL
))
186 nasm_fatal(ERR_NOFILE
, "unable to open output file: `%s': %s",
187 filename
, strerror(errno
));