blob: 5905aa22eebe9ce3f5f2f1af41f58581ccd6645d [file] [log] [blame]
Jonathan Yong4ba0f9e2010-05-02 11:57:31 +00001/**
2 * This file has no copyright assigned and is placed in the Public Domain.
Rafaël Carré8a67ab42012-06-28 15:40:59 +00003 * This file is part of the mingw-w64 runtime package.
Jonathan Yong4ba0f9e2010-05-02 11:57:31 +00004 * No warranty is given; refer to the file DISCLAIMER.PD within this package.
5 */
6#include <stdio.h>
7#include <io.h>
8#include <errno.h>
9#include <windows.h>
10#include <internal.h>
11
12struct oserr_map {
13 unsigned long oscode; /* OS values */
14 int errnocode; /* System V codes */
15};
16
17typedef union doubleint {
18 __int64 bigint;
19 struct {
20 unsigned long lowerhalf;
21 long upperhalf;
22 } twoints;
23} DINT;
24
25#define _IOYOURBUF 0x0100
26#define _IOSETVBUF 0x0400
27#define _IOFEOF 0x0800
28#define _IOFLRTN 0x1000
29#define _IOCTRLZ 0x2000
30#define _IOCOMMIT 0x4000
31
32/* General use macros */
33
34#define inuse(s) ((s)->_flag & (_IOREAD|_IOWRT|_IORW))
35#define mbuf(s) ((s)->_flag & _IOMYBUF)
36#define nbuf(s) ((s)->_flag & _IONBF)
37#define ybuf(s) ((s)->_flag & _IOYOURBUF)
38#define bigbuf(s) ((s)->_flag & (_IOMYBUF|_IOYOURBUF))
39#define anybuf(s) ((s)->_flag & (_IOMYBUF|_IONBF|_IOYOURBUF))
40
41#define _INTERNAL_BUFSIZ 4096
42#define _SMALL_BUFSIZ 512
43
44#define FOPEN 0x01 /* file handle open */
45#define FEOFLAG 0x02 /* end of file has been encountered */
46#define FCRLF 0x04 /* CR-LF across read buffer (in text mode) */
47#define FPIPE 0x08 /* file handle refers to a pipe */
48#define FNOINHERIT 0x10 /* file handle opened _O_NOINHERIT */
49#define FAPPEND 0x20 /* file handle opened O_APPEND */
50#define FDEV 0x40 /* file handle refers to device */
51#define FTEXT 0x80 /* file handle is in text mode */
52
53static struct oserr_map local_errtab[] = {
54 { ERROR_INVALID_FUNCTION, EINVAL }, { ERROR_FILE_NOT_FOUND, ENOENT },
55 { ERROR_PATH_NOT_FOUND, ENOENT }, { ERROR_TOO_MANY_OPEN_FILES, EMFILE },
56 { ERROR_ACCESS_DENIED, EACCES }, { ERROR_INVALID_HANDLE, EBADF },
57 { ERROR_ARENA_TRASHED, ENOMEM }, { ERROR_NOT_ENOUGH_MEMORY, ENOMEM },
58 { ERROR_INVALID_BLOCK, ENOMEM }, { ERROR_BAD_ENVIRONMENT, E2BIG },
59 { ERROR_BAD_FORMAT, ENOEXEC }, { ERROR_INVALID_ACCESS, EINVAL },
60 { ERROR_INVALID_DATA, EINVAL }, { ERROR_INVALID_DRIVE, ENOENT },
61 { ERROR_CURRENT_DIRECTORY, EACCES }, { ERROR_NOT_SAME_DEVICE, EXDEV },
62 { ERROR_NO_MORE_FILES, ENOENT }, { ERROR_LOCK_VIOLATION, EACCES },
63 { ERROR_BAD_NETPATH, ENOENT }, { ERROR_NETWORK_ACCESS_DENIED, EACCES },
64 { ERROR_BAD_NET_NAME, ENOENT }, { ERROR_FILE_EXISTS, EEXIST },
65 { ERROR_CANNOT_MAKE, EACCES }, { ERROR_FAIL_I24, EACCES },
66 { ERROR_INVALID_PARAMETER, EINVAL }, { ERROR_NO_PROC_SLOTS, EAGAIN },
67 { ERROR_DRIVE_LOCKED, EACCES }, { ERROR_BROKEN_PIPE, EPIPE },
68 { ERROR_DISK_FULL, ENOSPC }, { ERROR_INVALID_TARGET_HANDLE, EBADF },
69 { ERROR_INVALID_HANDLE, EINVAL }, { ERROR_WAIT_NO_CHILDREN, ECHILD },
70 { ERROR_CHILD_NOT_COMPLETE, ECHILD }, { ERROR_DIRECT_ACCESS_HANDLE, EBADF },
71 { ERROR_NEGATIVE_SEEK, EINVAL }, { ERROR_SEEK_ON_DEVICE, EACCES },
72 { ERROR_DIR_NOT_EMPTY, ENOTEMPTY }, { ERROR_NOT_LOCKED, EACCES },
73 { ERROR_BAD_PATHNAME, ENOENT }, { ERROR_MAX_THRDS_REACHED, EAGAIN },
74 { ERROR_LOCK_FAILED, EACCES }, { ERROR_ALREADY_EXISTS, EEXIST },
75 { ERROR_FILENAME_EXCED_RANGE, ENOENT }, { ERROR_NESTING_NOT_ALLOWED, EAGAIN },
76 { ERROR_NOT_ENOUGH_QUOTA, ENOMEM }, { 0, -1 }
77};
78
79_CRTIMP __int64 __cdecl _lseeki64(int fh,__int64 pos,int mthd);
80__int64 __cdecl _ftelli64(FILE *str);
81void mingw_dosmaperr (unsigned long oserrno);
82int __cdecl _flush (FILE *str);
83
84int __cdecl _flush (FILE *str)
85{
86 FILE *stream;
87 int rc = 0; /* assume good return */
88 __int64 nchar;
89
90 stream = str;
91 if ((stream->_flag & (_IOREAD | _IOWRT)) == _IOWRT && bigbuf(stream)
92 && (nchar = (__int64) (stream->_ptr - stream->_base)) > 0ll)
93 {
94 if ( _write(_fileno(stream), stream->_base, nchar) == nchar) {
95 if (_IORW & stream->_flag)
96 stream->_flag &= ~_IOWRT;
97 } else {
98 stream->_flag |= _IOERR;
99 rc = EOF;
100 }
101 }
102 stream->_ptr = stream->_base;
103 stream->_cnt = 0ll;
104 return rc;
105}
106
107int fseeko64 (FILE* stream, _off64_t offset, int whence)
108{
109 fpos_t pos;
110 if (whence == SEEK_CUR)
111 {
112 /* If stream is invalid, fgetpos sets errno. */
113 if (fgetpos (stream, &pos))
114 return (-1);
115 pos += (fpos_t) offset;
116 }
117 else if (whence == SEEK_END)
118 {
119 /* If writing, we need to flush before getting file length. */
120 fflush (stream);
121 pos = (fpos_t) (_filelengthi64 (_fileno (stream)) + offset);
122 }
123 else if (whence == SEEK_SET)
124 pos = (fpos_t) offset;
125 else
126 {
127 errno = EINVAL;
128 return (-1);
129 }
130 return fsetpos (stream, &pos);
131}
132
133int __cdecl _fseeki64(FILE *str,__int64 offset,int whence)
134{
135 FILE *stream;
136 /* Init stream pointer */
137 stream = str;
138 errno=0;
139 if(!stream || ((whence != SEEK_SET) && (whence != SEEK_CUR) && (whence != SEEK_END)))
140 {
141 errno=EINVAL;
142 return -1;
143 }
144 /* Clear EOF flag */
145 stream->_flag &= ~_IOEOF;
146
147 if (whence == SEEK_CUR) {
148 offset += _ftelli64(stream);
149 whence = SEEK_SET;
150 }
151 /* Flush buffer as necessary */
152 _flush(stream);
153
154 /* If file opened for read/write, clear flags since we don't know
155 what the user is going to do next. If the file was opened for
156 read access only, decrease _bufsiz so that the next _filbuf
157 won't cost quite so much */
158
159 if (stream->_flag & _IORW)
160 stream->_flag &= ~(_IOWRT|_IOREAD);
161 else if ( (stream->_flag & _IOREAD) && (stream->_flag & _IOMYBUF) &&
162 !(stream->_flag & _IOSETVBUF) )
163 stream->_bufsiz = _SMALL_BUFSIZ;
164
165 /* Seek to the desired locale and return. */
166
167 return (_lseeki64(_fileno(stream), offset, whence) == -1ll ? -1 : 0);
168}
169
170__int64 __cdecl _ftelli64(FILE *str)
171{
172 FILE *stream;
173 size_t offset;
174 __int64 filepos;
175 register char *p;
176 char *max;
177 int fd;
178 size_t rdcnt = 0;
179
180 errno=0;
181 stream = str;
182 fd = _fileno(stream);
183 if (stream->_cnt < 0ll) stream->_cnt = 0ll;
184 if ((filepos = _lseeki64(fd, 0ll, SEEK_CUR)) < 0L)
185 return -1ll;
186
187 if (!bigbuf(stream)) /* _IONBF or no buffering designated */
188 return (filepos - (__int64) stream->_cnt);
189
190 offset = (size_t)(stream->_ptr - stream->_base);
191
192 if (stream->_flag & (_IOWRT|_IOREAD))
193 {
194 if (_osfile(fd) & FTEXT)
195 for (p = stream->_base; p < stream->_ptr; p++)
196 if (*p == '\n') /* adjust for '\r' */
197 offset++;
198 }
199 else if (!(stream->_flag & _IORW)) {
200 errno=EINVAL;
201 return -1ll;
202 }
203 if (filepos == 0ll)
204 return ((__int64)offset);
205
206 if (stream->_flag & _IOREAD) /* go to preceding sector */
207 {
208 if (stream->_cnt == 0ll) /* filepos holds correct location */
209 offset = 0ll;
210 else
211 {
212 rdcnt = ((size_t) stream->_cnt) + ((size_t) (size_t)(stream->_ptr - stream->_base));
213 if (_osfile(fd) & FTEXT) {
214 if (_lseeki64(fd, 0ll, SEEK_END) == filepos) {
215 max = stream->_base + rdcnt;
216 for (p = stream->_base; p < max; p++)
217 if (*p == '\n') /* adjust for '\r' */
218 rdcnt++;
219 if (stream->_flag & _IOCTRLZ)
220 ++rdcnt;
221 } else {
222 _lseeki64(fd, filepos, SEEK_SET);
223 if ( (rdcnt <= _SMALL_BUFSIZ) && (stream->_flag & _IOMYBUF) &&
224 !(stream->_flag & _IOSETVBUF))
225 rdcnt = _SMALL_BUFSIZ;
226 else
227 rdcnt = stream->_bufsiz;
228 if (_osfile(fd) & FCRLF)
229 ++rdcnt;
230 }
231 } /* end if FTEXT */
232 }
233 filepos -= (__int64)rdcnt;
234 } /* end else stream->_cnt != 0 */
235 return (filepos + (__int64)offset);
236}
237
238void mingw_dosmaperr (unsigned long oserrno)
239{
240 size_t i;
241
242 _doserrno = oserrno; /* set _doserrno */
243 /* check the table for the OS error code */
244 i = 0;
245 do {
246 if (oserrno == local_errtab[i].oscode)
247 {
248 errno = local_errtab[i].errnocode;
249 return;
250 }
251 } while (local_errtab[++i].errnocode != -1);
252 if (oserrno >= ERROR_WRITE_PROTECT && oserrno <= ERROR_SHARING_BUFFER_EXCEEDED)
253 errno = EACCES;
254 else if (oserrno >= ERROR_INVALID_STARTING_CODESEG && oserrno <= ERROR_INFLOOP_IN_RELOC_CHAIN)
255 errno = ENOEXEC;
256 else
257 errno = EINVAL;
258}