blob: c0b4e781e12ca1d52d742b2f0369d0511af5d5c4 [file] [log] [blame]
H. Peter Anvinbdae10b2016-03-08 01:22:13 -08001/* ----------------------------------------------------------------------- *
2 *
H. Peter Anvincbd72b62017-02-23 18:19:25 -08003 * Copyright 1996-2017 The NASM Authors - All Rights Reserved
H. Peter Anvinbdae10b2016-03-08 01:22:13 -08004 * 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
H. Peter Anvin92223642017-02-23 18:39:00 -080034#include "file.h"
H. Peter Anvin397c1692016-10-04 17:01:59 -070035
H. Peter Anvin8dc96532017-04-17 13:52:19 -070036void nasm_read(void *ptr, size_t size, FILE *f)
37{
38 size_t n = fread(ptr, 1, size, f);
39 if (ferror(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");
43 }
44}
45
H. Peter Anvinbdae10b2016-03-08 01:22:13 -080046void nasm_write(const void *ptr, size_t size, FILE *f)
47{
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));
51}
52
H. Peter Anvinbdae10b2016-03-08 01:22:13 -080053void fwriteint16_t(uint16_t data, FILE * fp)
54{
H. Peter Anvin82025ea2017-11-29 16:53:52 -080055 data = cpu_to_le16(data);
H. Peter Anvinbdae10b2016-03-08 01:22:13 -080056 nasm_write(&data, 2, fp);
57}
58
59void fwriteint32_t(uint32_t data, FILE * fp)
60{
H. Peter Anvin82025ea2017-11-29 16:53:52 -080061 data = cpu_to_le32(data);
H. Peter Anvinbdae10b2016-03-08 01:22:13 -080062 nasm_write(&data, 4, fp);
63}
64
65void fwriteint64_t(uint64_t data, FILE * fp)
66{
H. Peter Anvin82025ea2017-11-29 16:53:52 -080067 data = cpu_to_le64(data);
H. Peter Anvinbdae10b2016-03-08 01:22:13 -080068 nasm_write(&data, 8, fp);
69}
70
71void fwriteaddr(uint64_t data, int size, FILE * fp)
72{
H. Peter Anvin82025ea2017-11-29 16:53:52 -080073 data = cpu_to_le64(data);
H. Peter Anvinbdae10b2016-03-08 01:22:13 -080074 nasm_write(&data, size, fp);
75}
76
H. Peter Anvind81a2352016-09-21 14:03:18 -070077void fwritezero(off_t bytes, FILE *fp)
H. Peter Anvinbdae10b2016-03-08 01:22:13 -080078{
79 size_t blksize;
80
81#ifdef nasm_ftruncate
82 if (bytes >= BUFSIZ && !ferror(fp) && !feof(fp)) {
83 off_t pos = ftello(fp);
84 if (pos >= 0) {
H. Peter Anvind81a2352016-09-21 14:03:18 -070085 pos += bytes;
H. Peter Anvinbdae10b2016-03-08 01:22:13 -080086 if (!fflush(fp) &&
H. Peter Anvind81a2352016-09-21 14:03:18 -070087 !nasm_ftruncate(fileno(fp), pos) &&
88 !fseeko(fp, pos, SEEK_SET))
H. Peter Anvinbdae10b2016-03-08 01:22:13 -080089 return;
90 }
91 }
92#endif
93
H. Peter Anvind81a2352016-09-21 14:03:18 -070094 while (bytes > 0) {
H. Peter Anvinbdae10b2016-03-08 01:22:13 -080095 blksize = (bytes < ZERO_BUF_SIZE) ? bytes : ZERO_BUF_SIZE;
96
97 nasm_write(zero_buffer, blksize, fp);
98 bytes -= blksize;
99 }
100}
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700101
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700102FILE *nasm_open_read(const char *filename, enum file_flags flags)
103{
Chang S. Bae54317322017-04-16 20:24:39 -0700104 FILE *f = NULL;
H. Peter Anvind81a2352016-09-21 14:03:18 -0700105 bool again = true;
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700106
H. Peter Anvind81a2352016-09-21 14:03:18 -0700107#ifdef __GLIBC__
108 /*
109 * Try to open this file with memory mapping for speed, unless we are
110 * going to do it "manually" with nasm_map_file()
111 */
112 if (!(flags & NF_FORMAP)) {
113 f = fopen(filename, (flags & NF_TEXT) ? "rtm" : "rbm");
114 again = (!f) && (errno == EINVAL); /* Not supported, try without m */
115 }
116#endif
117
118 if (again)
119 f = fopen(filename, (flags & NF_TEXT) ? "rt" : "rb");
120
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700121 if (!f && (flags & NF_FATAL))
122 nasm_fatal(ERR_NOFILE, "unable to open input file: `%s': %s",
123 filename, strerror(errno));
124
125 return f;
126}
127
128FILE *nasm_open_write(const char *filename, enum file_flags flags)
129{
130 FILE *f;
131
H. Peter Anvind81a2352016-09-21 14:03:18 -0700132 f = fopen(filename, (flags & NF_TEXT) ? "wt" : "wb");
133
H. Peter Anvin3e83cec2016-05-25 04:28:46 -0700134 if (!f && (flags & NF_FATAL))
135 nasm_fatal(ERR_NOFILE, "unable to open output file: `%s': %s",
136 filename, strerror(errno));
137
138 return f;
139}
H. Peter Anvinc1700892016-09-20 18:26:42 -0700140
H. Peter Anvind81a2352016-09-21 14:03:18 -0700141/*
142 * Report the existence of a file
143 */
144bool nasm_file_exists(const char *filename)
145{
146#if defined(HAVE_FACCESSAT) && defined(AT_EACCESS)
147 return faccessat(AT_FDCWD, filename, R_OK, AT_EACCESS) == 0;
148#elif defined(HAVE_ACCESS)
149 return access(filename, R_OK) == 0;
150#else
151 FILE *f;
152
153 f = fopen(filename, "rb");
154 if (f) {
155 fclose(f);
156 return true;
157 } else {
158 return false;
159 }
160#endif
161}
162
163/*
164 * Report file size. This MAY move the file pointer.
165 */
166off_t nasm_file_size(FILE *f)
167{
H. Peter Anvin31184292017-04-06 15:30:56 -0700168#ifdef HAVE__FILELENGTHI64
H. Peter Anvin397c1692016-10-04 17:01:59 -0700169 return _filelengthi64(fileno(f));
H. Peter Anvincbd72b62017-02-23 18:19:25 -0800170#elif defined(nasm_fstat)
H. Peter Anvin31184292017-04-06 15:30:56 -0700171 nasm_struct_stat st;
H. Peter Anvind81a2352016-09-21 14:03:18 -0700172
H. Peter Anvincbd72b62017-02-23 18:19:25 -0800173 if (nasm_fstat(fileno(f), &st))
H. Peter Anvind81a2352016-09-21 14:03:18 -0700174 return (off_t)-1;
175
176 return st.st_size;
177#else
178 if (fseeko(f, 0, SEEK_END))
179 return (off_t)-1;
180
181 return ftello(f);
182#endif
183}
184
185/*
186 * Report file size given pathname
187 */
188off_t nasm_file_size_by_path(const char *pathname)
189{
H. Peter Anvin675e7b72017-04-05 21:19:03 -0700190#ifdef nasm_stat
H. Peter Anvin31184292017-04-06 15:30:56 -0700191 nasm_struct_stat st;
H. Peter Anvind81a2352016-09-21 14:03:18 -0700192
H. Peter Anvin675e7b72017-04-05 21:19:03 -0700193 if (nasm_stat(pathname, &st))
H. Peter Anvind81a2352016-09-21 14:03:18 -0700194 return (off_t)-1;
195
196 return st.st_size;
197#else
198 FILE *fp;
199 off_t len;
200
201 fp = nasm_open_read(pathname, NF_BINARY);
202 if (!fp)
203 return (off_t)-1;
204
205 len = nasm_file_size(fp);
206 fclose(fp);
207
208 return len;
209#endif
210}
H. Peter Anvina771be82017-08-16 14:53:18 -0700211
212/*
213 * Report the timestamp on a file, returns true if successful
214 */
215bool nasm_file_time(time_t *t, const char *pathname)
216{
217#ifdef nasm_stat
218 nasm_struct_stat st;
219
220 if (nasm_stat(pathname, &st))
221 return false;
222
223 *t = st.st_mtime;
224 return true;
225#else
226 return false; /* No idea how to do this on this OS */
227#endif
228}