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 * ----------------------------------------------------------------------- */
36 void nasm_read(void *ptr
, size_t size
, FILE *f
)
38 size_t n
= fread(ptr
, 1, size
, f
);
40 nasm_fatal(0, "unable to read input: %s", strerror(errno
));
41 } else if (n
!= size
|| feof(f
)) {
42 nasm_fatal(0, "fatal short read on input");
46 void nasm_write(const void *ptr
, size_t size
, FILE *f
)
48 size_t n
= fwrite(ptr
, 1, size
, f
);
49 if (n
!= size
|| ferror(f
) || feof(f
))
50 nasm_fatal(0, "unable to write output: %s", strerror(errno
));
53 #ifdef WORDS_LITTLEENDIAN
55 void fwriteint16_t(uint16_t data
, FILE * fp
)
57 nasm_write(&data
, 2, fp
);
60 void fwriteint32_t(uint32_t data
, FILE * fp
)
62 nasm_write(&data
, 4, fp
);
65 void fwriteint64_t(uint64_t data
, FILE * fp
)
67 nasm_write(&data
, 8, fp
);
70 void fwriteaddr(uint64_t data
, int size
, FILE * fp
)
72 nasm_write(&data
, size
, fp
);
75 #else /* not WORDS_LITTLEENDIAN */
77 void fwriteint16_t(uint16_t data
, FILE * fp
)
79 char buffer
[2], *p
= buffer
;
81 nasm_write(buffer
, 2, fp
);
84 void fwriteint32_t(uint32_t data
, FILE * fp
)
86 char buffer
[4], *p
= buffer
;
88 nasm_write(buffer
, 4, fp
);
91 void fwriteint64_t(uint64_t data
, FILE * fp
)
93 char buffer
[8], *p
= buffer
;
95 nasm_write(buffer
, 8, fp
);
98 void fwriteaddr(uint64_t data
, int size
, FILE * fp
)
100 char buffer
[8], *p
= buffer
;
101 WRITEADDR(p
, data
, size
);
102 nasm_write(buffer
, size
, fp
);
108 void fwritezero(off_t bytes
, FILE *fp
)
112 #ifdef nasm_ftruncate
113 if (bytes
>= BUFSIZ
&& !ferror(fp
) && !feof(fp
)) {
114 off_t pos
= ftello(fp
);
118 !nasm_ftruncate(fileno(fp
), pos
) &&
119 !fseeko(fp
, pos
, SEEK_SET
))
126 blksize
= (bytes
< ZERO_BUF_SIZE
) ? bytes
: ZERO_BUF_SIZE
;
128 nasm_write(zero_buffer
, blksize
, fp
);
133 FILE *nasm_open_read(const char *filename
, enum file_flags flags
)
140 * Try to open this file with memory mapping for speed, unless we are
141 * going to do it "manually" with nasm_map_file()
143 if (!(flags
& NF_FORMAP
)) {
144 f
= fopen(filename
, (flags
& NF_TEXT
) ? "rtm" : "rbm");
145 again
= (!f
) && (errno
== EINVAL
); /* Not supported, try without m */
150 f
= fopen(filename
, (flags
& NF_TEXT
) ? "rt" : "rb");
152 if (!f
&& (flags
& NF_FATAL
))
153 nasm_fatal(ERR_NOFILE
, "unable to open input file: `%s': %s",
154 filename
, strerror(errno
));
159 FILE *nasm_open_write(const char *filename
, enum file_flags flags
)
163 f
= fopen(filename
, (flags
& NF_TEXT
) ? "wt" : "wb");
165 if (!f
&& (flags
& NF_FATAL
))
166 nasm_fatal(ERR_NOFILE
, "unable to open output file: `%s': %s",
167 filename
, strerror(errno
));
173 * Report the existence of a file
175 bool nasm_file_exists(const char *filename
)
177 #if defined(HAVE_FACCESSAT) && defined(AT_EACCESS)
178 return faccessat(AT_FDCWD
, filename
, R_OK
, AT_EACCESS
) == 0;
179 #elif defined(HAVE_ACCESS)
180 return access(filename
, R_OK
) == 0;
184 f
= fopen(filename
, "rb");
195 * Report file size. This MAY move the file pointer.
197 off_t
nasm_file_size(FILE *f
)
199 #ifdef HAVE__FILELENGTHI64
200 return _filelengthi64(fileno(f
));
201 #elif defined(nasm_fstat)
204 if (nasm_fstat(fileno(f
), &st
))
209 if (fseeko(f
, 0, SEEK_END
))
217 * Report file size given pathname
219 off_t
nasm_file_size_by_path(const char *pathname
)
224 if (nasm_stat(pathname
, &st
))
232 fp
= nasm_open_read(pathname
, NF_BINARY
);
236 len
= nasm_file_size(fp
);
244 * Report the timestamp on a file, returns true if successful
246 bool nasm_file_time(time_t *t
, const char *pathname
)
251 if (nasm_stat(pathname
, &st
))
257 return false; /* No idea how to do this on this OS */