rbtree: add rb_search_exact()
[nasm.git] / nasmlib / file.c
blob62b854ded2ff0c7c3b62815742320c28088d8662
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
9 * conditions are met:
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 #include "file.h"
36 void nasm_read(void *ptr, size_t size, FILE *f)
38 size_t n = fread(ptr, 1, size, f);
39 if (ferror(f)) {
40 nasm_fatal("unable to read input: %s", strerror(errno));
41 } else if (n != size || feof(f)) {
42 nasm_fatal("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("unable to write output: %s", strerror(errno));
53 void fwriteint16_t(uint16_t data, FILE * fp)
55 data = cpu_to_le16(data);
56 nasm_write(&data, 2, fp);
59 void fwriteint32_t(uint32_t data, FILE * fp)
61 data = cpu_to_le32(data);
62 nasm_write(&data, 4, fp);
65 void fwriteint64_t(uint64_t data, FILE * fp)
67 data = cpu_to_le64(data);
68 nasm_write(&data, 8, fp);
71 void fwriteaddr(uint64_t data, int size, FILE * fp)
73 data = cpu_to_le64(data);
74 nasm_write(&data, size, fp);
77 void fwritezero(off_t bytes, FILE *fp)
79 size_t blksize;
81 #ifdef os_ftruncate
82 if (bytes >= BUFSIZ && !ferror(fp) && !feof(fp)) {
83 off_t pos = ftello(fp);
84 if (pos != (off_t)-1) {
85 off_t end = pos + bytes;
86 if (!fflush(fp) && !os_ftruncate(fileno(fp), end)) {
87 fseeko(fp, 0, SEEK_END);
88 pos = ftello(fp);
89 if (pos != (off_t)-1)
90 bytes = end - pos; /* This SHOULD be zero */
94 #endif
96 while (bytes > 0) {
97 blksize = (bytes < ZERO_BUF_SIZE) ? bytes : ZERO_BUF_SIZE;
99 nasm_write(zero_buffer, blksize, fp);
100 bytes -= blksize;
104 #ifdef _WIN32
107 * On Windows, we want to use _wfopen(), as fopen() has a much smaller limit
108 * on the path length that it supports.
110 * Previously we tried to prefix the path name with \\?\ in order to
111 * let the Windows kernel know that we are not limited to PATH_MAX
112 * characters, but it breaks relative paths among other things, and
113 * apparently Windows 10 contains a registry option to override this
114 * limit anyway. One day maybe they will even implement UTF-8 as byte
115 * characters so we can use the standard file API even on this OS.
118 os_filename os_mangle_filename(const char *filename)
120 mbstate_t ps;
121 size_t wclen;
122 wchar_t *buf;
123 const char *p;
126 * Note: mbsrtowcs() return (size_t)-1 on error, otherwise
127 * the length of the string *without* final NUL in wchar_t
128 * units. Thus we add 1 for the final NUL; the error value
129 * now becomes 0.
131 memset(&ps, 0, sizeof ps); /* Begin in the initial state */
132 p = filename;
133 wclen = mbsrtowcs(NULL, &p, 0, &ps) + 1;
134 if (!wclen)
135 return NULL;
137 buf = nasm_malloc(wclen * sizeof(wchar_t));
139 memset(&ps, 0, sizeof ps); /* Begin in the initial state */
140 p = filename;
141 if (mbsrtowcs(buf, &p, wclen, &ps) + 1 != wclen || p) {
142 nasm_free(buf);
143 return NULL;
146 return buf;
149 #endif
151 void nasm_set_binary_mode(FILE *f)
153 os_set_binary_mode(f);
156 FILE *nasm_open_read(const char *filename, enum file_flags flags)
158 FILE *f = NULL;
159 os_filename osfname;
161 osfname = os_mangle_filename(filename);
162 if (osfname) {
163 os_fopenflag fopen_flags[4];
164 memset(fopen_flags, 0, sizeof fopen_flags);
166 fopen_flags[0] = 'r';
167 fopen_flags[1] = (flags & NF_TEXT) ? 't' : 'b';
169 #if defined(__GLIBC__) || defined(__linux__)
171 * Try to open this file with memory mapping for speed, unless we are
172 * going to do it "manually" with nasm_map_file()
174 if (!(flags & NF_FORMAP))
175 fopen_flags[2] = 'm';
176 #endif
178 while (true) {
179 f = os_fopen(osfname, fopen_flags);
180 if (f || errno != EINVAL || !fopen_flags[2])
181 break;
183 /* We got EINVAL but with 'm'; try again without 'm' */
184 fopen_flags[2] = '\0';
187 os_free_filename(osfname);
190 if (!f && (flags & NF_FATAL))
191 nasm_fatalf(ERR_NOFILE, "unable to open input file: `%s': %s",
192 filename, strerror(errno));
194 return f;
197 FILE *nasm_open_write(const char *filename, enum file_flags flags)
199 FILE *f = NULL;
200 os_filename osfname;
202 osfname = os_mangle_filename(filename);
203 if (osfname) {
204 os_fopenflag fopen_flags[3];
206 fopen_flags[0] = 'w';
207 fopen_flags[1] = (flags & NF_TEXT) ? 't' : 'b';
208 fopen_flags[2] = '\0';
210 f = os_fopen(osfname, fopen_flags);
211 os_free_filename(osfname);
214 if (!f && (flags & NF_FATAL))
215 nasm_fatalf(ERR_NOFILE, "unable to open output file: `%s': %s",
216 filename, strerror(errno));
218 switch (flags & NF_BUF_MASK) {
219 case NF_IONBF:
220 setvbuf(f, NULL, _IONBF, 0);
221 break;
222 case NF_IOLBF:
223 setvbuf(f, NULL, _IOLBF, 0);
224 break;
225 case NF_IOFBF:
226 setvbuf(f, NULL, _IOFBF, 0);
227 break;
228 default:
229 break;
232 return f;
235 /* The appropriate "rb" strings for os_fopen() */
236 static const os_fopenflag fopenflags_rb[3] = { 'r', 'b', 0 };
239 * Report the existence of a file
241 bool nasm_file_exists(const char *filename)
243 #ifndef os_access
244 FILE *f;
245 #endif
246 os_filename osfname;
247 bool exists;
249 osfname = os_mangle_filename(filename);
250 if (!osfname)
251 return false;
253 #ifdef os_access
254 exists = os_access(osfname, R_OK) == 0;
255 #else
256 f = os_fopen(osfname, fopenflags_rb);
257 exists = f != NULL;
258 if (f)
259 fclose(f);
260 #endif
262 os_free_filename(osfname);
263 return exists;
267 * Report the file size of an open file. This MAY move the file pointer.
269 off_t nasm_file_size(FILE *f)
271 off_t where, end;
272 os_struct_stat st;
274 if (!os_fstat(fileno(f), &st) && S_ISREG(st.st_mode))
275 return st.st_size;
277 /* Do it the hard way... this tests for seekability */
279 if (fseeko(f, 0, SEEK_CUR))
280 goto fail; /* Not seekable, don't even try */
282 where = ftello(f);
283 if (where == (off_t)-1)
284 goto fail;
286 if (fseeko(f, 0, SEEK_END))
287 goto fail;
289 end = ftello(f);
290 if (end == (off_t)-1)
291 goto fail;
294 * Move the file pointer back. If this fails, this is probably
295 * not a plain file.
297 if (fseeko(f, where, SEEK_SET))
298 goto fail;
300 return end;
302 fail:
303 return -1;
307 * Report file size given pathname
309 off_t nasm_file_size_by_path(const char *pathname)
311 os_filename osfname;
312 off_t len = -1;
313 os_struct_stat st;
314 FILE *fp;
316 osfname = os_mangle_filename(pathname);
318 if (!os_stat(osfname, &st) && S_ISREG(st.st_mode))
319 len = st.st_size;
321 fp = os_fopen(osfname, fopenflags_rb);
322 if (fp) {
323 len = nasm_file_size(fp);
324 fclose(fp);
327 return len;
331 * Report the timestamp on a file, returns true if successful
333 bool nasm_file_time(time_t *t, const char *pathname)
335 #ifdef os_stat
336 os_filename osfname;
337 os_struct_stat st;
338 bool rv = false;
340 osfname = os_mangle_filename(pathname);
341 if (!osfname)
342 return false;
344 rv = !os_stat(osfname, &st);
345 *t = st.st_mtime;
346 os_free_filename(osfname);
348 return rv;
349 #else
350 return false; /* No idea how to do this on this OS */
351 #endif