47#include "wine/unicode.h"
59#define _IOCOMMIT 0x4000
94#define WX_READEOF 0x04
97#define WX_DONTINHERIT 0x10
105#define EF_CRIT_INIT 0x04
106#define EF_UNK_UNICODE 0x08
112#define MSVCRT_MAX_FILES 2048
113#define MSVCRT_FD_BLOCK_SIZE 32
115#define MSVCRT_INTERNAL_BUFSIZ 4096
152 0, 0, { (
DWORD_PTR)(__FILE__
": MSVCRT_file_cs") }
155#define LOCK_FILES() do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
156#define UNLOCK_FILES() do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
205 WARN(
":out of memory!\n");
258 WARN(
":files exhausted!\n");
283 ERR(
"out of memory\n");
348 TRACE(
":handle (%p) allocating fd (%d)\n", hand,
fd);
371 if (
file->_flag == 0)
384 TRACE(
":fd (%d) allocating FILE*\n",
fd);
387 WARN(
":invalid fd %d\n",
fd);
395 file->_flag = stream_flags;
430 wxflag_ptr = (
char*)*
block +
sizeof(
unsigned);
431 handle_ptr = (
HANDLE*)(wxflag_ptr + last_fd);
433 *(
unsigned*)*
block = last_fd;
434 for (
fd = 0;
fd < last_fd;
fd++)
440 *wxflag_ptr = fdinfo->
wxflag;
441 *handle_ptr = fdinfo->
handle;
448 wxflag_ptr++; handle_ptr++;
485 wxflag_ptr++; handle_ptr++;
524 for (
i = 0;
i < 3;
i++)
546 file->_flag &= ~_IOWRT;
583 file->_base = (
char*)(&
file->_charbuf);
615 file->_flag &= ~_USERBUF;
634 *
p = (
num & 31) +
'0';
636 *
p += (
'a' -
'0' - 10);
659 *
p = (
num & 31) +
'0';
661 *
p += (
'a' -
'0' - 10);
672 wchar_t *wstr =
malloc(
len*
sizeof (
wchar_t));
869 int i, num_flushed = 0;
886 TRACE(
":flushed (%d) handles\n",num_flushed);
969 ioinfo *info_od, *info_nd;
972 TRACE(
"(od=%d, nd=%d)\n", od, nd);
996 int wxflag = info_od->
wxflag & ~WX_DONTINHERIT;
1044 DWORD curpos,endpos;
1045 LONG hcurpos,hendpos;
1047 TRACE(
":fd (%d) handle (%p)\n",
fd,
info->handle);
1062 hcurpos = hendpos = 0;
1066 if (curpos == endpos && hcurpos == hendpos)
1083 int num_closed = 0,
i;
1095 TRACE(
":closed (%d) handles\n",num_closed);
1143 TRACE(
":fd (%d) handle (%p)\n",
fd,
info->handle);
1151 if (whence < 0 || whence > 2)
1158 TRACE(
":fd (%d) to %s pos %s\n",
1162 (whence==
SEEK_END)?
"SEEK_END":
"UNKNOWN");
1226 TRACE(
":fd (%d) handle (%p)\n",
fd,
info->handle);
1233 if (mode < 0 || mode > 4)
1240 TRACE(
":fd (%d) by 0x%08x mode %s\n",
1251 FIXME (
"Seek failed\n");
1272 return ret ? 0 : -1;
1300 file->_flag &= ~_IOEOF;
1419 *open_flags &= ~_O_TEXT;
1423 *open_flags &= ~_O_BINARY;
1435 *stream_flags &= ~_IOCOMMIT;
1447 FIXME(
"ignoring cache optimization flag: %c\n",
mode[-1]);
1450 ERR(
"incorrect mode flag: %c\n",
mode[-1]);
1456 static const WCHAR ccs[] = {
'c',
'c',
's'};
1457 static const WCHAR utf8[] = {
'u',
't',
'f',
'-',
'8'};
1458 static const WCHAR utf16le[] = {
'u',
't',
'f',
'-',
'1',
'6',
'l',
'e'};
1459 static const WCHAR unicode[] = {
'u',
'n',
'i',
'c',
'o',
'd',
'e'};
1465 mode +=
sizeof(ccs)/
sizeof(ccs[0]);
1475 mode +=
sizeof(utf8)/
sizeof(utf8[0]);
1477 else if(!
strncmpiW(utf16le,
mode,
sizeof(utf16le)/
sizeof(utf16le[0])))
1480 mode +=
sizeof(utf16le)/
sizeof(utf16le[0]);
1482 else if(!
strncmpiW(unicode,
mode,
sizeof(unicode)/
sizeof(unicode[0])))
1485 mode +=
sizeof(unicode)/
sizeof(unicode[0]);
1507 wchar_t *modeW =
NULL;
1522 int open_flags, stream_flags;
1552 if (endPos != curPos)
1571 if (endPos != curPos)
1594 TRACE(
":fd (%d) handle (%p)\n",
fd,hand);
1615 numX = (*
pattern++ ==
'X')? numX + 1 : 0;
1623 int tempNum =
id / 10;
1624 *
pattern-- =
id - (tempNum * 10) +
'0';
1633 }
while(letter <=
'z');
1645 wchar_t letter =
'a';
1648 numX = (*
pattern++ ==
'X')? numX + 1 : 0;
1656 int tempNum =
id / 10;
1657 *
pattern-- =
id - (tempNum * 10) +
'0';
1667 }
while(letter !=
'|');
1686 if ((unsupp = oflags & ~(
1694 ERR(
":unsupported oflags 0x%04x\n",unsupp);
1706 HANDLE readHandle, writeHandle;
1716 sa.lpSecurityDescriptor =
NULL;
1786 TRACE(
"fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1803 FIXME(
": pmode 0x%04x ignored\n", pmode);
1805 WARN(
": pmode 0x%04x ignored\n", pmode);
1837 ERR(
"Unhandled shflags 0x%x\n", shflags );
1850 sa.lpSecurityDescriptor =
NULL;
1883 DWORD written = 0, tmp;
1889 WARN(
"error writing BOM\n");
1897 DWORD written = 0, tmp;
1904 WARN(
"error writing BOM\n");
1932 TRACE(
":fd (%d) handle (%p)\n", *
fd, hand);
2096 int num_removed = 0,
i;
2103 if (
file->_tmpfname)
2112 TRACE(
":removed (%d) temp files\n",num_removed);
2118 if((ch&0xf8) == 0xf0)
2120 else if((ch&0xf0) == 0xe0)
2122 else if((ch&0xe0) == 0xc0)
2133 char min_buf[4], *readbuf, lookahead;
2134 DWORD readbuf_size,
pos=0, num_read=1, char_len,
i,
j;
2140 if(readbuf_size<=4 || !(readbuf =
malloc(readbuf_size))) {
2172 }
else if(!num_read) {
2185 if(readbuf[0] ==
'\n')
2188 fdinfo->
wxflag &= ~WX_READNL;
2190 if(readbuf[0] == 0x1a) {
2195 if(readbuf[0] ==
'\r') {
2196 if(!
ReadFile(hand, &lookahead, 1, &num_read,
NULL) || num_read!=1)
2198 else if(lookahead ==
'\n')
2223 if (readbuf != min_buf)
free(readbuf);
2227 if (readbuf != min_buf)
free(readbuf);
2230 }
else if(!
pos && !num_read) {
2232 if (readbuf != min_buf)
free(readbuf);
2237 if(readbuf[0] ==
'\n')
2240 fdinfo->
wxflag &= ~WX_READNL;
2244 if((readbuf[
i]&0xc0) != 0x80)
2247 if(char_len+
i <=
pos)
2263 if(readbuf[
i] == 0x1a) {
2269 if(readbuf[
i] ==
'\r' &&
i+1==
pos) {
2271 readbuf[
j++] =
'\r';
2272 }
else if(lookahead ==
'\n' &&
j==0) {
2273 readbuf[
j++] =
'\n';
2275 if(lookahead !=
'\n')
2276 readbuf[
j++] =
'\r';
2283 }
else if(readbuf[
i]!=
'\r' || readbuf[
i+1]!=
'\n') {
2284 readbuf[
j++] = readbuf[
i];
2291 if (readbuf != min_buf)
free(readbuf);
2295 if (readbuf != min_buf)
free(readbuf);
2308 DWORD num_read, utf16;
2309 char *bufstart =
buf;
2315 TRACE(
"already at EOF, returning 0\n");
2351 num_read += 1+utf16;
2356 if(utf16 && (num_read&1))
2360 ERR(
"got odd number of bytes in UTF16 mode\n");
2364 if (
count != 0 && num_read == 0)
2373 if (bufstart[0]==
'\n' && (!utf16 || bufstart[1]==0))
2376 fdinfo->
wxflag &= ~WX_READNL;
2378 for (
i=0,
j=0;
i<num_read;
i+=1+utf16)
2381 if (bufstart[
i]==0x1a && (!utf16 || bufstart[
i+1]==0))
2389 if (bufstart[
i]==
'\r' && (!utf16 || bufstart[
i+1]==0) &&
i+1+utf16==num_read)
2394 lookahead[1] =
'\n';
2397 if(lookahead[0]==
'\n' && (!utf16 || lookahead[1]==0) &&
j==0)
2399 bufstart[
j++] =
'\n';
2400 if(utf16) bufstart[
j++] = 0;
2404 if(lookahead[0]!=
'\n' || (utf16 && lookahead[1]!=0))
2406 bufstart[
j++] =
'\r';
2407 if(utf16) bufstart[
j++] = 0;
2412 if (lookahead[0]==
'\n' && (!utf16 || !lookahead[1]))
2414 bufstart[
j++] =
'\n';
2415 if (utf16) bufstart[
j++] = 0;
2429 bufstart[
j++] =
'\r';
2430 if(utf16) bufstart[
j++] = 0;
2433 else if((bufstart[
i]!=
'\r' || (utf16 && bufstart[
i+1]!=0))
2434 || (bufstart[
i+1+utf16]!=
'\n' || (utf16 && bufstart[
i+3]!=0)))
2436 bufstart[
j++] = bufstart[
i];
2437 if(utf16) bufstart[
j++] = bufstart[
i+1];
2447 TRACE(
":end-of-pipe\n");
2497 info->wxflag &= ~WX_TEXT;
2538 const char *tmp_dir =
getenv(
"TMP");
2540 if (tmp_dir)
dir = tmp_dir;
2542 TRACE(
"dir (%s) prefix (%s)\n",
dir,prefix);
2545 TRACE(
"got name (%s)\n",tmpbuf);
2617 && (num_written ==
count))
2622 TRACE(
"WriteFile (fd %d, hand %p) failed-last error (%d)\n",
fd,
2628 unsigned int i,
j, nr_lf,
size;
2631 const char *
s =
buf, *buf_start =
buf;
2653 FIXME(
"Malloc failed\n");
2668 if (
s[
i]==
'\n' &&
s[
i+1]==0)
2677 if (
s[
i]==
'\n' &&
s[
i+1]==0)
2688 FIXME(
"Malloc failed\n");
2705 if (
s[
i]==
'\n' &&
s[
i+1]==0)
2716 size = conv_len+nr_lf;
2721 if (
s[
i]==
'\n' &&
s[
i+1]==0)
2735 FIXME(
"Malloc failed\n");
2747 if (num_written !=
size)
2749 TRACE(
"WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2752 return s - buf_start;
2770 if (
len ==
sizeof(
val)) {
2879 c = *(
unsigned char *)
file->_base;
2896 i = (
unsigned char *)
file->_ptr++;
2919 char * buf_start =
s;
2921 TRACE(
":file(%p) fd (%d) str (%p) len (%d)\n",
2931 if ((
cc ==
EOF) && (
s == buf_start))
2933 TRACE(
":nothing read\n");
3004 for (
j=0;
j<
sizeof(
int);
j++) {
3048 wchar_t * buf_start =
s;
3050 TRACE(
":file(%p) fd (%d) str (%p) len (%d)\n",
3060 if ((
cc ==
WEOF) && (
s == buf_start))
3062 TRACE(
":nothing read\n");
3079 size_t wrcnt=
size * nmemb;
3088 if(
file->_cnt < 0) {
3089 WARN(
"negative file->_cnt value in %p\n",
file);
3095 int pcnt=(
file->_cnt>wrcnt)? wrcnt:
file->_cnt;
3101 ptr = (
const char*)
ptr + pcnt;
3113 bufsiz =
file->_bufsiz;
3115 pcnt = (wrcnt / bufsiz) * bufsiz;
3126 ptr = (
const char*)
ptr + pcnt;
3132 ptr = (
const char*)
ptr + 1;
3137 return written /
size;
3190 int open_flags, stream_flags,
fd;
3224 wchar_t *pathW =
NULL, *modeW =
NULL;
3283 const wchar_t *
mode)
3342 size_t rcnt=
size * nmemb;
3353 int pcnt= (rcnt>
file->_cnt)?
file->_cnt:rcnt;
3378 i = (
file->_cnt<rcnt) ?
file->_cnt : rcnt;
3380 if (
i > 0 && i < file->_cnt) {
3382 file->_flag &= ~_IOEOF;
3391 }
else if (rcnt <
BUFSIZ) {
3423 int open_flags, stream_flags,
fd;
3461 wchar_t *pathW =
NULL, *modeW =
NULL;
3527 }
else if(!
file->_cnt) {
3534 if(
file->_ptr[
i] ==
'\n')
3648 char * buf_start =
buf;
3657 TRACE(
"got '%s'\n", buf_start);
3732 static const wchar_t nl =
'\n';
3778 TRACE(
":from %s to %s\n",oldpath,newpath);
3859 if(!
data->tmpnam_buffer)
3862 s =
data->tmpnam_buffer;
3884 static const wchar_t format[] = {
'\\',
's',
'%',
's',
'.',0};
3891 if(!
data->wtmpnam_buffer)
3894 s =
data->wtmpnam_buffer;
3959 if(*
file->_ptr !=
c) {
3992 unsigned char * pp = (
unsigned char *)&mwc;
3995 for(
i=
sizeof(
wchar_t)-1;
i>=0;
i--) {
4038 TRACE(
"%d\n", newmax);
int memcmp(void *Buffer1, void *Buffer2, ACPI_SIZE Count)
ACPI_SIZE strlen(const char *String)
static struct sockaddr_in sa
HANDLE WINAPI GetStdHandle(IN DWORD nStdHandle)
#define _setmode(fd, mode)
#define INVALID_SET_FILE_POINTER
struct _SECURITY_ATTRIBUTES SECURITY_ATTRIBUTES
#define ReadFile(a, b, c, d, e)
#define INVALID_HANDLE_VALUE
static __inline const char * debugstr_an(const char *s, int n)
static __inline const char * wine_dbgstr_longlong(ULONGLONG ll)
#define GetCurrentProcess()
#define ERROR_INVALID_HANDLE
#define FILE_ATTRIBUTE_NORMAL
#define WideCharToMultiByte
#define MultiByteToWideChar
#define ERROR_ACCESS_DENIED
BOOL WINAPI DECLSPEC_HOTPATCH SetStdHandle(DWORD nStdHandle, HANDLE hHandle)
BOOL WINAPI DeleteFileA(IN LPCSTR lpFileName)