ReactOS 0.4.15-dev-7958-gcd0bb1a
file.c File Reference
#include <precomp.h>
#include "wine/unicode.h"
#include "internal/wine/msvcrt.h"
#include <sys/utime.h>
#include <direct.h>
Include dependency graph for file.c:

Go to the source code of this file.

Classes

struct  file_crit
 

Macros

#define _IOCOMMIT   0x4000
 
#define WX_OPEN   0x01
 
#define WX_ATEOF   0x02
 
#define WX_READNL   0x04 /* read started with \n */
 
#define WX_READEOF   0x04 /* like ATEOF, but for underlying file rather than buffer */
 
#define WX_PIPE   0x08
 
#define WX_READCR   0x08 /* underlying file is at \r */
 
#define WX_DONTINHERIT   0x10
 
#define WX_APPEND   0x20
 
#define WX_TTY   0x40
 
#define WX_TEXT   0x80
 
#define EF_UTF8   0x01
 
#define EF_UTF16   0x02
 
#define EF_CRIT_INIT   0x04
 
#define EF_UNK_UNICODE   0x08
 
#define MSVCRT_MAX_FILES   2048
 
#define MSVCRT_FD_BLOCK_SIZE   32
 
#define MSVCRT_INTERNAL_BUFSIZ   4096
 
#define LOCK_FILES()   do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)
 
#define UNLOCK_FILES()   do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)
 

Functions

int__p__fmode (void)
 
int__p___mb_cur_max (void)
 
static ioinfoget_ioinfo_nolock (int fd)
 
static void init_ioinfo_cs (ioinfo *info)
 
ioinfoget_ioinfo (int fd)
 
static BOOL alloc_pioinfo_block (int fd)
 
static ioinfoget_ioinfo_alloc_fd (int fd)
 
static ioinfoget_ioinfo_alloc (int *fd)
 
void release_ioinfo (ioinfo *info)
 
static FILEmsvcrt_get_file (int i)
 
static void msvcrt_free_fd (int fd)
 
static void msvcrt_set_fd (ioinfo *fdinfo, HANDLE hand, int flag)
 
int msvcrt_alloc_fd (HANDLE hand, int flag)
 
static FILEmsvcrt_alloc_fp (void)
 
static int msvcrt_init_fp (FILE *file, int fd, unsigned stream_flags)
 
unsigned create_io_inherit_block (WORD *size, BYTE **block)
 
void msvcrt_init_io (void)
 
static int msvcrt_flush_buffer (FILE *file)
 
int CDECL _isatty (int fd)
 
BOOL msvcrt_alloc_buffer (FILE *file)
 
static BOOL add_std_buffer (FILE *file)
 
static void remove_std_buffer (FILE *file)
 
static int msvcrt_int_to_base32 (int num, char *str)
 
static int msvcrt_int_to_base32_w (int num, wchar_t *str)
 
wchar_tmsvcrt_wstrdupa (const char *str)
 
FILE *CDECL __iob_func (void)
 
int CDECL _access (const char *filename, int mode)
 
int CDECL _access_s (const char *filename, int mode)
 
int CDECL _waccess (const wchar_t *filename, int mode)
 
int CDECL _waccess_s (const wchar_t *filename, int mode)
 
int CDECL _chmod (const char *path, int flags)
 
int CDECL _wchmod (const wchar_t *path, int flags)
 
int CDECL _unlink (const char *path)
 
int CDECL _wunlink (const wchar_t *path)
 
int CDECL _commit (int fd)
 
int CDECL fflush (FILE *file)
 
static int msvcrt_flush_all_buffers (int mask)
 
int CDECL _flushall (void)
 
int CDECL _close (int fd)
 
int CDECL _dup2 (int od, int nd)
 
int CDECL _dup (int od)
 
int CDECL _eof (int fd)
 
int CDECL _fcloseall (void)
 
void msvcrt_free_io (void)
 
__int64 CDECL _lseeki64 (int fd, __int64 offset, int whence)
 
LONG CDECL _lseek (int fd, LONG offset, int whence)
 
void CDECL _lock_file (FILE *file)
 
void CDECL _unlock_file (FILE *file)
 
int CDECL _locking (int fd, int mode, LONG nbytes)
 
int CDECL _fseeki64 (FILE *file, __int64 offset, int whence)
 
int CDECL fseek (FILE *file, long offset, int whence)
 
int CDECL _chsize_s (int fd, __int64 size)
 
int CDECL _chsize (int fd, long size)
 
void CDECL clearerr (FILE *file)
 
void CDECL rewind (FILE *file)
 
static int msvcrt_get_flags (const wchar_t *mode, int *open_flags, int *stream_flags)
 
FILE *CDECL _fdopen (int fd, const char *mode)
 
FILE *CDECL _wfdopen (int fd, const wchar_t *mode)
 
LONG CDECL _filelength (int fd)
 
__int64 CDECL _filelengthi64 (int fd)
 
int CDECL _fileno (FILE *file)
 
intptr_t CDECL _get_osfhandle (int fd)
 
char *CDECL _mktemp (char *pattern)
 
wchar_t *CDECL _wmktemp (wchar_t *pattern)
 
unsigned split_oflags (unsigned oflags)
 
int CDECL _pipe (int *pfds, unsigned int psize, int textmode)
 
static int check_bom (HANDLE h, int oflags, BOOL seek)
 
int CDECL _wsopen_s (int *fd, const wchar_t *path, int oflags, int shflags, int pmode)
 
int CDECL _wsopen (const wchar_t *path, int oflags, int shflags,...)
 
int CDECL _sopen_s (int *fd, const char *path, int oflags, int shflags, int pmode)
 
int CDECL _sopen (const char *path, int oflags, int shflags,...)
 
int CDECL _open (const char *path, int flags,...)
 
int CDECL _wopen (const wchar_t *path, int flags,...)
 
int CDECL _creat (const char *path, int flags)
 
int CDECL _wcreat (const wchar_t *path, int flags)
 
int CDECL _open_osfhandle (intptr_t handle, int oflags)
 
int CDECL _rmtmp (void)
 
static int get_utf8_char_len (char ch)
 
static int read_utf8 (ioinfo *fdinfo, wchar_t *buf, unsigned int count)
 
static int read_i (int fd, ioinfo *fdinfo, void *buf, unsigned int count)
 
int CDECL _read (int fd, void *buf, unsigned int count)
 
int CDECL _setmode (int fd, int mode)
 
long CDECL _tell (int fd)
 
__int64 CDECL _telli64 (int fd)
 
char *CDECL _tempnam (const char *dir, const char *prefix)
 
wchar_t *CDECL _wtempnam (const wchar_t *dir, const wchar_t *prefix)
 
int CDECL _umask (int umask)
 
int CDECL _write (int fd, const void *buf, unsigned int count)
 
int CDECL _putw (int val, FILE *file)
 
int CDECL fclose (FILE *file)
 
int CDECL feof (FILE *file)
 
int CDECL ferror (FILE *file)
 
int CDECL _filbuf (FILE *file)
 
int CDECL fgetc (FILE *file)
 
int CDECL _fgetchar (void)
 
char *CDECL fgets (char *s, int size, FILE *file)
 
wint_t CDECL fgetwc (FILE *file)
 
int CDECL _getw (FILE *file)
 
wint_t CDECL getwc (FILE *file)
 
wint_t CDECL _fgetwchar (void)
 
wint_t CDECL getwchar (void)
 
wchar_t *CDECL fgetws (wchar_t *s, int size, FILE *file)
 
size_t CDECL fwrite (const void *ptr, size_t size, size_t nmemb, FILE *file)
 
wint_t CDECL fputwc (wchar_t c, FILE *stream)
 
wint_t CDECL _fputwchar (wint_t wc)
 
FILE *CDECL _wfsopen (const wchar_t *path, const wchar_t *mode, int share)
 
FILE *CDECL _fsopen (const char *path, const char *mode, int share)
 
FILE *CDECL fopen (const char *path, const char *mode)
 
int CDECL fopen_s (FILE **pFile, const char *filename, const char *mode)
 
FILE *CDECL _wfopen (const wchar_t *path, const wchar_t *mode)
 
int CDECL _wfopen_s (FILE **pFile, const wchar_t *filename, const wchar_t *mode)
 
int CDECL _flsbuf (int c, FILE *file)
 
int CDECL fputc (int c, FILE *file)
 
int CDECL _fputchar (int c)
 
size_t CDECL fread (void *ptr, size_t size, size_t nmemb, FILE *file)
 
FILE *CDECL _wfreopen (const wchar_t *path, const wchar_t *mode, FILE *file)
 
FILE *CDECL freopen (const char *path, const char *mode, FILE *file)
 
int CDECL fsetpos (FILE *file, const fpos_t *pos)
 
__int64 CDECL _ftelli64 (FILE *file)
 
LONG CDECL ftell (FILE *file)
 
int CDECL fgetpos (FILE *file, fpos_t *pos)
 
int CDECL fputs (const char *s, FILE *file)
 
int CDECL fputws (const wchar_t *s, FILE *file)
 
int CDECL getchar (void)
 
int CDECL getc (FILE *file)
 
char *CDECL gets (char *buf)
 
wchar_t *CDECL _getws (wchar_t *buf)
 
int CDECL putc (int c, FILE *file)
 
int CDECL putchar (int c)
 
wint_t CDECL _putwch (wchar_t c)
 
int CDECL puts (const char *s)
 
int CDECL _putws (const wchar_t *s)
 
int CDECL remove (const char *path)
 
int CDECL _wremove (const wchar_t *path)
 
int CDECL rename (const char *oldpath, const char *newpath)
 
int CDECL _wrename (const wchar_t *oldpath, const wchar_t *newpath)
 
int CDECL setvbuf (FILE *file, char *buf, int mode, size_t size)
 
void CDECL setbuf (FILE *file, char *buf)
 
char *CDECL tmpnam (char *s)
 
wchar_t *CDECL _wtmpnam (wchar_t *s)
 
FILE *CDECL tmpfile (void)
 
int CDECL ungetc (int c, FILE *file)
 
wint_t CDECL ungetwc (wint_t wc, FILE *file)
 
int CDECL _getmaxstdio (void)
 
int CDECL _setmaxstdio (int newmax)
 

Variables

int _commode
 
static char utf8_bom [3] = { 0xef, 0xbb, 0xbf }
 
static char utf16_bom [2] = { 0xff, 0xfe }
 
ioinfo__pioinfo [MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE] = { 0 }
 
ioinfo __badioinfo = { INVALID_HANDLE_VALUE, WX_TEXT }
 
FILE _iob [_IOB_ENTRIES] = { { 0 } }
 
static file_critMSVCRT_fstream [MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE] = { NULL }
 
static int MSVCRT_max_streams = 512
 
static int MSVCRT_stream_idx
 
static int MSVCRT_umask = 0
 
static int tmpnam_unique
 
static CRITICAL_SECTION MSVCRT_file_cs = { &MSVCRT_file_cs_debug, -1, 0, 0, 0, 0 }
 
static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug
 

Macro Definition Documentation

◆ _IOCOMMIT

#define _IOCOMMIT   0x4000

Definition at line 59 of file file.c.

◆ EF_CRIT_INIT

#define EF_CRIT_INIT   0x04

Definition at line 105 of file file.c.

◆ EF_UNK_UNICODE

#define EF_UNK_UNICODE   0x08

Definition at line 106 of file file.c.

◆ EF_UTF16

#define EF_UTF16   0x02

Definition at line 104 of file file.c.

◆ EF_UTF8

#define EF_UTF8   0x01

Definition at line 103 of file file.c.

◆ LOCK_FILES

#define LOCK_FILES ( )    do { EnterCriticalSection(&MSVCRT_file_cs); } while (0)

Definition at line 155 of file file.c.

◆ MSVCRT_FD_BLOCK_SIZE

#define MSVCRT_FD_BLOCK_SIZE   32

Definition at line 113 of file file.c.

◆ MSVCRT_INTERNAL_BUFSIZ

#define MSVCRT_INTERNAL_BUFSIZ   4096

Definition at line 115 of file file.c.

◆ MSVCRT_MAX_FILES

#define MSVCRT_MAX_FILES   2048

Definition at line 112 of file file.c.

◆ UNLOCK_FILES

#define UNLOCK_FILES ( )    do { LeaveCriticalSection(&MSVCRT_file_cs); } while (0)

Definition at line 156 of file file.c.

◆ WX_APPEND

#define WX_APPEND   0x20

Definition at line 98 of file file.c.

◆ WX_ATEOF

#define WX_ATEOF   0x02

Definition at line 92 of file file.c.

◆ WX_DONTINHERIT

#define WX_DONTINHERIT   0x10

Definition at line 97 of file file.c.

◆ WX_OPEN

#define WX_OPEN   0x01

Definition at line 91 of file file.c.

◆ WX_PIPE

#define WX_PIPE   0x08

Definition at line 95 of file file.c.

◆ WX_READCR

#define WX_READCR   0x08 /* underlying file is at \r */

Definition at line 96 of file file.c.

◆ WX_READEOF

#define WX_READEOF   0x04 /* like ATEOF, but for underlying file rather than buffer */

Definition at line 94 of file file.c.

◆ WX_READNL

#define WX_READNL   0x04 /* read started with \n */

Definition at line 93 of file file.c.

◆ WX_TEXT

#define WX_TEXT   0x80

Definition at line 100 of file file.c.

◆ WX_TTY

#define WX_TTY   0x40

Definition at line 99 of file file.c.

Function Documentation

◆ __iob_func()

FILE *CDECL __iob_func ( void  )

Definition at line 682 of file file.c.

683{
684 return &_iob[0];
685}
FILE _iob[_IOB_ENTRIES]
Definition: file.c:133

Referenced by Test__iob().

◆ __p___mb_cur_max()

int * __p___mb_cur_max ( void  )

Definition at line 430 of file environ.c.

431{
432 return &get_locinfo()->mb_cur_max;
433}
#define get_locinfo()
Definition: winesup.h:25

◆ __p__fmode()

int * __p__fmode ( void  )

Definition at line 9 of file fmode.c.

10{
11 return &_fmode;
12}
int _fmode
Definition: fmode.c:4

Referenced by split_oflags().

◆ _access()

int CDECL _access ( const char filename,
int  mode 
)

Definition at line 690 of file file.c.

691{
693
694 TRACE("(%s,%d) %d\n",filename,mode,attr);
695
697 {
699 return -1;
700 }
701 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & W_OK))
702 {
704 return -1;
705 }
706 return 0;
707}
#define ERROR_ACCESS_DENIED
Definition: compat.h:97
DWORD WINAPI GetFileAttributesA(LPCSTR lpFileName)
Definition: fileinfo.c:636
unsigned long DWORD
Definition: ntddk_ex.h:95
GLenum mode
Definition: glext.h:6217
const char * filename
Definition: ioapi.h:137
#define FILE_ATTRIBUTE_READONLY
Definition: nt_native.h:702
void _dosmaperr(unsigned long oserrcode)
Definition: errno.c:79
errno_t __cdecl _set_errno(_In_ int _Value)
#define W_OK
Definition: io.h:170
#define TRACE(s)
Definition: solgame.cpp:4
Definition: cookie.c:202
#define INVALID_FILE_ATTRIBUTES
Definition: vfdcmd.c:23
DWORD WINAPI GetLastError(void)
Definition: except.c:1042

Referenced by _access_s().

◆ _access_s()

int CDECL _access_s ( const char filename,
int  mode 
)

Definition at line 712 of file file.c.

713{
714 if (!MSVCRT_CHECK_PMT(filename != NULL) ||
715 !MSVCRT_CHECK_PMT((mode & ~(R_OK | W_OK)) == 0))
716 {
718 return -1;
719 }
720
721 return _access(filename, mode);
722}
#define EINVAL
Definition: acclib.h:90
#define NULL
Definition: types.h:112
#define MSVCRT_CHECK_PMT(x)
Definition: mbstowcs_s.c:26
#define R_OK
Definition: io.h:171
int CDECL _access(const char *filename, int mode)
Definition: file.c:690

◆ _chmod()

int CDECL _chmod ( const char path,
int  flags 
)

Definition at line 764 of file file.c.

765{
766 DWORD oldFlags = GetFileAttributesA(path);
767
768 if (oldFlags != INVALID_FILE_ATTRIBUTES)
769 {
770 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
771 oldFlags | FILE_ATTRIBUTE_READONLY;
772
773 if (newFlags == oldFlags || SetFileAttributesA(path, newFlags))
774 return 0;
775 }
777 return -1;
778}
#define _S_IWRITE
Definition: cabinet.h:33
BOOL WINAPI SetFileAttributesA(LPCSTR lpFileName, DWORD dwFileAttributes)
Definition: fileinfo.c:776
GLbitfield flags
Definition: glext.h:7161

◆ _chsize()

int CDECL _chsize ( int  fd,
long  size 
)

Definition at line 1354 of file file.c.

1355{
1356 /* _chsize_s returns errno on failure but _chsize should return -1 */
1357 return _chsize_s( fd, size ) == 0 ? 0 : -1;
1358}
GLsizeiptr size
Definition: glext.h:5919
int CDECL _chsize_s(int fd, __int64 size)
Definition: file.c:1318
static int fd
Definition: io.c:51

◆ _chsize_s()

int CDECL _chsize_s ( int  fd,
__int64  size 
)

Definition at line 1318 of file file.c.

1319{
1320 ioinfo *info;
1321 __int64 cur, pos;
1322 BOOL ret = FALSE;
1323
1324 TRACE("(fd=%d, size=%s)\n", fd, wine_dbgstr_longlong(size));
1325
1326 if (!MSVCRT_CHECK_PMT(size >= 0)) return EINVAL;
1327
1328 info = get_ioinfo(fd);
1329 if (info->handle != INVALID_HANDLE_VALUE)
1330 {
1331 /* save the current file pointer */
1332 cur = _lseeki64(fd, 0, SEEK_CUR);
1333 if (cur >= 0)
1334 {
1336 if (pos >= 0)
1337 {
1338 ret = SetEndOfFile(info->handle);
1339 if (!ret) _dosmaperr(GetLastError());
1340 }
1341
1342 /* restore the file pointer */
1344 }
1345 }
1346
1348 return ret ? 0 : *_errno();
1349}
#define __int64
Definition: basetyps.h:16
#define FALSE
Definition: types.h:117
#define INVALID_HANDLE_VALUE
Definition: compat.h:731
static __inline const char * wine_dbgstr_longlong(ULONGLONG ll)
Definition: compat.h:49
BOOL WINAPI SetEndOfFile(HANDLE hFile)
Definition: fileinfo.c:1004
unsigned int BOOL
Definition: ntddk_ex.h:94
FxCollectionEntry * cur
#define SEEK_SET
Definition: jmemansi.c:26
#define SEEK_CUR
Definition: util.h:63
_CRTIMP int *__cdecl _errno(void)
Definition: errno.c:17
ioinfo * get_ioinfo(int fd)
Definition: file.c:181
void release_ioinfo(ioinfo *info)
Definition: file.c:263
__int64 CDECL _lseeki64(int fd, __int64 offset, int whence)
Definition: file.c:1138
Definition: file.c:40
int ret

Referenced by _chsize().

◆ _close()

int CDECL _close ( int  fd)

Definition at line 931 of file file.c.

932{
934 int ret;
935
936 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
937 if (!(info->wxflag & WX_OPEN)) {
938 ret = -1;
939 } else if (fd == STDOUT_FILENO &&
942 ret = 0;
943 } else if (fd == STDERR_FILENO &&
946 ret = 0;
947 } else {
948 ret = CloseHandle(info->handle) ? 0 : -1;
950 if (ret) {
951 WARN(":failed-last error (%d)\n",GetLastError());
953 ret = -1;
954 }
955 }
957 return ret;
958}
#define WARN(fmt,...)
Definition: debug.h:112
#define CloseHandle
Definition: compat.h:739
#define STDOUT_FILENO
Definition: syshdrs.h:89
#define STDERR_FILENO
Definition: syshdrs.h:90
#define WX_OPEN
Definition: file.c:91
static void msvcrt_free_fd(int fd)
Definition: file.c:296
static ioinfo * get_ioinfo_nolock(int fd)
Definition: file.c:158

◆ _commit()

int CDECL _commit ( int  fd)

Definition at line 828 of file file.c.

829{
831 int ret;
832
833 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
834
835 if (info->handle == INVALID_HANDLE_VALUE)
836 ret = -1;
837 else if (!FlushFileBuffers(info->handle))
838 {
840 {
841 /* FlushFileBuffers fails for console handles
842 * so we ignore this error.
843 */
844 ret = 0;
845 }
846 else
847 {
848 TRACE(":failed-last error (%d)\n",GetLastError());
850 ret = -1;
851 }
852 }
853 else
854 {
855 TRACE(":ok\n");
856 ret = 0;
857 }
858
860 return ret;
861}
#define ERROR_INVALID_HANDLE
Definition: compat.h:98
BOOL WINAPI FlushFileBuffers(IN HANDLE hFile)
Definition: fileinfo.c:25

◆ _creat()

int CDECL _creat ( const char path,
int  flags 
)

Definition at line 2039 of file file.c.

2040{
2041 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
2042 return _open(path, usedFlags);
2043}
#define _O_TEXT
Definition: cabinet.h:50
#define _O_TRUNC
Definition: cabinet.h:47
#define _O_CREAT
Definition: cabinet.h:46
#define _O_WRONLY
Definition: cabinet.h:38
_CRTIMP int __cdecl _open(const char *_Filename, int _OpenFlag,...)
Definition: file.c:2001

◆ _dup()

int CDECL _dup ( int  od)

Definition at line 1025 of file file.c.

1026{
1027 int fd, ret;
1029
1030 if (_dup2(od, fd) == 0)
1031 ret = fd;
1032 else
1033 ret = -1;
1035 return ret;
1036}
_Check_return_ _CRTIMP int __cdecl _dup2(_In_ int _FileHandleSrc, _In_ int _FileHandleDst)
static ioinfo * get_ioinfo_alloc(int *fd)
Definition: file.c:230

◆ _dup2()

int CDECL _dup2 ( int  od,
int  nd 
)

Definition at line 967 of file file.c.

968{
969 ioinfo *info_od, *info_nd;
970 int ret;
971
972 TRACE("(od=%d, nd=%d)\n", od, nd);
973
974 if (od < nd)
975 {
976 info_od = get_ioinfo(od);
977 info_nd = get_ioinfo_alloc_fd(nd);
978 }
979 else
980 {
981 info_nd = get_ioinfo_alloc_fd(nd);
982 info_od = get_ioinfo(od);
983 }
984
985 if (info_nd == &__badioinfo)
986 {
987 ret = -1;
988 }
989 else if (info_od->wxflag & WX_OPEN)
990 {
992
995 {
996 int wxflag = info_od->wxflag & ~WX_DONTINHERIT;
997
998 if (info_nd->wxflag & WX_OPEN)
999 _close(nd);
1000
1001 msvcrt_set_fd(info_nd, handle, wxflag);
1002 /* _dup2 returns 0, not nd, on success */
1003 ret = 0;
1004 }
1005 else
1006 {
1007 ret = -1;
1009 }
1010 }
1011 else
1012 {
1013 *_errno() = EBADF;
1014 ret = -1;
1015 }
1016
1017 release_ioinfo(info_od);
1018 release_ioinfo(info_nd);
1019 return ret;
1020}
#define EBADF
Definition: acclib.h:82
#define TRUE
Definition: types.h:120
#define GetCurrentProcess()
Definition: compat.h:759
BOOL WINAPI DuplicateHandle(IN HANDLE hSourceProcessHandle, IN HANDLE hSourceHandle, IN HANDLE hTargetProcessHandle, OUT LPHANDLE lpTargetHandle, IN DWORD dwDesiredAccess, IN BOOL bInheritHandle, IN DWORD dwOptions)
Definition: handle.c:149
_Check_return_opt_ _CRTIMP int __cdecl _close(_In_ int _FileHandle)
static ioinfo * get_ioinfo_alloc_fd(int fd)
Definition: file.c:216
static void msvcrt_set_fd(ioinfo *fdinfo, HANDLE hand, int flag)
Definition: file.c:325
ioinfo __badioinfo
Definition: file.c:126
unsigned char wxflag
Definition: file.c:42
HANDLE handle
Definition: file.c:41
#define DUPLICATE_SAME_ACCESS

◆ _eof()

int CDECL _eof ( int  fd)

Definition at line 1041 of file file.c.

1042{
1044 DWORD curpos,endpos;
1045 LONG hcurpos,hendpos;
1046
1047 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1048
1049 if (info->handle == INVALID_HANDLE_VALUE)
1050 {
1052 return -1;
1053 }
1054
1055 if (info->wxflag & WX_ATEOF)
1056 {
1058 return TRUE;
1059 }
1060
1061 /* Otherwise we do it the hard way */
1062 hcurpos = hendpos = 0;
1063 curpos = SetFilePointer(info->handle, 0, &hcurpos, FILE_CURRENT);
1064 endpos = SetFilePointer(info->handle, 0, &hendpos, FILE_END);
1065
1066 if (curpos == endpos && hcurpos == hendpos)
1067 {
1068 /* FIXME: shouldn't WX_ATEOF be set here? */
1070 return TRUE;
1071 }
1072
1073 SetFilePointer(info->handle, curpos, &hcurpos, FILE_BEGIN);
1075 return FALSE;
1076}
#define FILE_BEGIN
Definition: compat.h:761
#define SetFilePointer
Definition: compat.h:743
long LONG
Definition: pedump.c:60
#define WX_ATEOF
Definition: file.c:92
#define FILE_END
Definition: winbase.h:114
#define FILE_CURRENT
Definition: winbase.h:113

◆ _fcloseall()

int CDECL _fcloseall ( void  )

Definition at line 1081 of file file.c.

1082{
1083 int num_closed = 0, i;
1084 FILE *file;
1085
1086 LOCK_FILES();
1087 for (i = 3; i < MSVCRT_stream_idx; i++) {
1089
1090 if (file->_flag && !fclose(file))
1091 num_closed++;
1092 }
1093 UNLOCK_FILES();
1094
1095 TRACE(":closed (%d) handles\n",num_closed);
1096 return num_closed;
1097}
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
Definition: glfuncs.h:248
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
#define LOCK_FILES()
Definition: file.c:155
#define UNLOCK_FILES()
Definition: file.c:156
static FILE * msvcrt_get_file(int i)
Definition: file.c:269
static int MSVCRT_stream_idx
Definition: file.c:135
Definition: fci.c:127

Referenced by msvcrt_free_io(), and test_fopen_fclose_fcloseall().

◆ _fdopen()

FILE *CDECL _fdopen ( int  fd,
const char mode 
)

Definition at line 1504 of file file.c.

1505{
1506 FILE *ret;
1507 wchar_t *modeW = NULL;
1508
1509 if (mode && !(modeW = msvcrt_wstrdupa(mode))) return NULL;
1510
1511 ret = _wfdopen(fd, modeW);
1512
1513 free(modeW);
1514 return ret;
1515}
#define free
Definition: debug_ros.c:5
wchar_t * msvcrt_wstrdupa(const char *str)
Definition: file.c:669
FILE *CDECL _wfdopen(int fd, const wchar_t *mode)
Definition: file.c:1520

◆ _fgetchar()

int CDECL _fgetchar ( void  )

Definition at line 2908 of file file.c.

2909{
2910 return fgetc(stdin);
2911}
_Check_return_opt_ _CRTIMP int __cdecl fgetc(_Inout_ FILE *_File)
#define stdin
Definition: stdio.h:98

◆ _fgetwchar()

wint_t CDECL _fgetwchar ( void  )

Definition at line 3029 of file file.c.

3030{
3031 return fgetwc(stdin);
3032}
_Check_return_opt_ _CRTIMP wint_t __cdecl fgetwc(_Inout_ FILE *_File)

Referenced by getwchar().

◆ _filbuf()

int CDECL _filbuf ( FILE file)

Definition at line 2835 of file file.c.

2836{
2837 unsigned char c;
2839
2840 if(file->_flag & _IOSTRG) {
2842 return EOF;
2843 }
2844
2845 /* Allocate buffer if needed */
2846 if(!(file->_flag & (_IONBF | _IOMYBUF | _USERBUF)))
2848
2849 if(!(file->_flag & _IOREAD)) {
2850 if(file->_flag & _IORW)
2851 file->_flag |= _IOREAD;
2852 else {
2854 return EOF;
2855 }
2856 }
2857
2858 if(!(file->_flag & (_IOMYBUF | _USERBUF))) {
2859 int r;
2860 if ((r = _read(file->_file,&c,1)) != 1) {
2861 file->_flag |= (r == 0) ? _IOEOF : _IOERR;
2863 return EOF;
2864 }
2865
2867 return c;
2868 } else {
2869 file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
2870 if(file->_cnt<=0) {
2871 file->_flag |= (file->_cnt == 0) ? _IOEOF : _IOERR;
2872 file->_cnt = 0;
2874 return EOF;
2875 }
2876
2877 file->_cnt--;
2878 file->_ptr = file->_base+1;
2879 c = *(unsigned char *)file->_base;
2881 return c;
2882 }
2883}
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
const GLubyte * c
Definition: glext.h:8905
#define _IOMYBUF
Definition: stdio.h:131
#define EOF
Definition: stdio.h:24
#define _IOEOF
Definition: stdio.h:132
#define _IOREAD
Definition: stdio.h:124
#define _IORW
Definition: stdio.h:135
#define _USERBUF
Definition: stdio.h:136
#define _IOSTRG
Definition: stdio.h:134
#define _IONBF
Definition: stdio.h:129
#define _IOERR
Definition: stdio.h:133
#define c
Definition: ke_i.h:80
_Check_return_ _CRTIMP int __cdecl _read(_In_ int _FileHandle, _Out_writes_bytes_(_MaxCharCount) void *_DstBuf, _In_ unsigned int _MaxCharCount)
BOOL msvcrt_alloc_buffer(FILE *file)
Definition: file.c:572
void CDECL _lock_file(FILE *file)
Definition: file.c:1193
void CDECL _unlock_file(FILE *file)
Definition: file.c:1205

◆ _filelength()

LONG CDECL _filelength ( int  fd)

Definition at line 1544 of file file.c.

1545{
1546 LONG curPos = _lseek(fd, 0, SEEK_CUR);
1547 if (curPos != -1)
1548 {
1549 LONG endPos = _lseek(fd, 0, SEEK_END);
1550 if (endPos != -1)
1551 {
1552 if (endPos != curPos)
1553 _lseek(fd, curPos, SEEK_SET);
1554 return endPos;
1555 }
1556 }
1557 return -1;
1558}
#define SEEK_END
Definition: cabinet.c:29
_Check_return_opt_ _CRTIMP long __cdecl _lseek(_In_ int _FileHandle, _In_ long _Offset, _In_ int _Origin)

◆ _filelengthi64()

__int64 CDECL _filelengthi64 ( int  fd)

Definition at line 1563 of file file.c.

1564{
1565 __int64 curPos = _lseeki64(fd, 0, SEEK_CUR);
1566 if (curPos != -1)
1567 {
1568 __int64 endPos = _lseeki64(fd, 0, SEEK_END);
1569 if (endPos != -1)
1570 {
1571 if (endPos != curPos)
1572 _lseeki64(fd, curPos, SEEK_SET);
1573 return endPos;
1574 }
1575 }
1576 return -1;
1577}

◆ _fileno()

int CDECL _fileno ( FILE file)

Definition at line 1582 of file file.c.

1583{
1584 TRACE(":FILE* (%p) fd (%d)\n",file,file->_file);
1585 return file->_file;
1586}

◆ _flsbuf()

int CDECL _flsbuf ( int  c,
FILE file 
)

Definition at line 14 of file _flsbuf.c.

15{
16 int count, written;
17
18 /* Check if the stream supports flushing */
19 if ((stream->_flag & _IOSTRG) || !(stream->_flag & (_IORW|_IOWRT)))
20 {
21 stream->_flag |= _IOERR;
22 return EOF;
23 }
24
25 /* Always reset _cnt */
26 stream->_cnt = 0;
27
28 /* Check if this was a read buffer */
29 if (stream->_flag & _IOREAD)
30 {
31 /* Must be at the end of the file */
32 if (!(stream->_flag & _IOEOF))
33 {
34 stream->_flag |= _IOERR;
35 return EOF;
36 }
37
38 /* Reset buffer */
39 stream->_ptr = stream->_base;
40 }
41
42 /* Fixup flags */
43 stream->_flag &= ~(_IOREAD|_IOEOF);
44 stream->_flag |= _IOWRT;
45
46 /* Check if should get a buffer */
47 if (!(stream->_flag & _IONBF) && stream != stdout && stream != stderr)
48 {
49 /* If we have no buffer, try to allocate one */
50 if (!stream->_base) msvcrt_alloc_buffer(stream);
51 }
52
53 /* Check if we can use a buffer now */
54 if (stream->_base && !(stream->_flag & _IONBF))
55 {
56 /* We can, check if there is something to write */
57 count = (int)(stream->_ptr - stream->_base);
58 if (count > 0)
59 written = _write(stream->_file, stream->_base, count);
60 else
61 written = 0;
62
63 /* Reset buffer and put the char into it */
64 stream->_ptr = stream->_base + sizeof(TCHAR);
65 stream->_cnt = stream->_bufsiz - sizeof(TCHAR);
66 *(TCHAR*)stream->_base = ch;
67 }
68 else
69 {
70 /* There is no buffer, write the char directly */
71 count = sizeof(TCHAR);
72 written = _write(stream->_file, &ch, sizeof(TCHAR));
73 }
74
75 /* Check for failure */
76 if (written != count)
77 {
78 stream->_flag |= _IOERR;
79 return EOF;
80 }
81
82 return ch & (sizeof(TCHAR) > sizeof(char) ? 0xffff : 0xff);
83}
BOOL __cdecl msvcrt_alloc_buffer(FILE *stream)
Definition: file.c:572
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
GLuint GLuint GLsizei count
Definition: gl.h:1545
#define stdout
Definition: stdio.h:99
#define _IOWRT
Definition: stdio.h:125
#define stderr
Definition: stdio.h:100
_CRTIMP int __cdecl _write(_In_ int _FileHandle, _In_reads_bytes_(_MaxCharCount) const void *_Buf, _In_ unsigned int _MaxCharCount)
Definition: parse.h:23
char TCHAR
Definition: xmlstorage.h:189

◆ _flushall()

int CDECL _flushall ( void  )

Definition at line 893 of file file.c.

894{
896}
static int msvcrt_flush_all_buffers(int mask)
Definition: file.c:867

Referenced by msvcrt_free_io(), test_fflush(), and wmain().

◆ _fputchar()

int CDECL _fputchar ( int  c)

Definition at line 3332 of file file.c.

3333{
3334 return fputc(c, stdout);
3335}
_Check_return_opt_ _CRTIMP int __cdecl fputc(_In_ int _Ch, _Inout_ FILE *_File)

◆ _fputwchar()

wint_t CDECL _fputwchar ( wint_t  wc)

Definition at line 3179 of file file.c.

3180{
3181 return fputwc(wc, stdout);
3182}
_Check_return_opt_ _CRTIMP wint_t __cdecl fputwc(_In_ wchar_t _Ch, _Inout_ FILE *_File)

◆ _fseeki64()

int CDECL _fseeki64 ( FILE file,
__int64  offset,
int  whence 
)

Definition at line 1278 of file file.c.

1279{
1280 int ret;
1281
1283 /* Flush output if needed */
1284 if(file->_flag & _IOWRT)
1286
1287 if(whence == SEEK_CUR && file->_flag & _IOREAD ) {
1288 whence = SEEK_SET;
1289 offset += _ftelli64(file);
1290 }
1291
1292 /* Discard buffered input */
1293 file->_cnt = 0;
1294 file->_ptr = file->_base;
1295 /* Reset direction of i/o */
1296 if(file->_flag & _IORW) {
1297 file->_flag &= ~(_IOREAD|_IOWRT);
1298 }
1299 /* Clear end of file flag */
1300 file->_flag &= ~_IOEOF;
1301 ret = (_lseeki64(file->_file,offset,whence) == -1)?-1:0;
1302
1304 return ret;
1305}
GLintptr offset
Definition: glext.h:5920
__int64 CDECL _ftelli64(FILE *file)
Definition: file.c:3506
static int msvcrt_flush_buffer(FILE *file)
Definition: file.c:535

Referenced by fseek().

◆ _fsopen()

FILE *CDECL _fsopen ( const char path,
const char mode,
int  share 
)

Definition at line 3221 of file file.c.

3222{
3223 FILE *ret;
3224 wchar_t *pathW = NULL, *modeW = NULL;
3225
3226 if (path && !(pathW = msvcrt_wstrdupa(path))) {
3228 *_errno() = EINVAL;
3229 return NULL;
3230 }
3231 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
3232 {
3233 free(pathW);
3235 *_errno() = EINVAL;
3236 return NULL;
3237 }
3238
3239 ret = _wfsopen(pathW, modeW, share);
3240
3241 free(pathW);
3242 free(modeW);
3243 return ret;
3244}
void _invalid_parameter(const wchar_t *expression, const wchar_t *function, const wchar_t *file, unsigned int line, uintptr_t pReserved)
FILE *CDECL _wfsopen(const wchar_t *path, const wchar_t *mode, int share)
Definition: file.c:3187

Referenced by fopen().

◆ _ftelli64()

__int64 CDECL _ftelli64 ( FILE file)

Definition at line 3506 of file file.c.

3507{
3508 __int64 pos;
3509
3511 pos = _telli64(file->_file);
3512 if(pos == -1) {
3514 return -1;
3515 }
3516 if(file->_flag & (_IOMYBUF | _USERBUF)) {
3517 if(file->_flag & _IOWRT) {
3518 pos += file->_ptr - file->_base;
3519
3520 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
3521 char *p;
3522
3523 for(p=file->_base; p<file->_ptr; p++)
3524 if(*p == '\n')
3525 pos++;
3526 }
3527 } else if(!file->_cnt) { /* nothing to do */
3528 } else if(_lseeki64(file->_file, 0, SEEK_END)==pos) {
3529 int i;
3530
3531 pos -= file->_cnt;
3532 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
3533 for(i=0; i<file->_cnt; i++)
3534 if(file->_ptr[i] == '\n')
3535 pos--;
3536 }
3537 } else {
3538 char *p;
3539
3540 if(_lseeki64(file->_file, pos, SEEK_SET) != pos) {
3542 return -1;
3543 }
3544
3545 pos -= file->_bufsiz;
3546 pos += file->_ptr - file->_base;
3547
3548 if(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT) {
3549 if(get_ioinfo_nolock(file->_file)->wxflag & WX_READNL)
3550 pos--;
3551
3552 for(p=file->_base; p<file->_ptr; p++)
3553 if(*p == '\n')
3554 pos++;
3555 }
3556 }
3557 }
3558
3560 return pos;
3561}
GLfloat GLfloat p
Definition: glext.h:8902
#define WX_READNL
Definition: file.c:93
__int64 CDECL _telli64(int fd)
Definition: file.c:2527
#define WX_TEXT
Definition: file.c:100

Referenced by _fseeki64(), fgetpos(), and ftell().

◆ _get_osfhandle()

intptr_t CDECL _get_osfhandle ( int  fd)

Definition at line 1591 of file file.c.

1592{
1594 TRACE(":fd (%d) handle (%p)\n",fd,hand);
1595
1596 if(hand == INVALID_HANDLE_VALUE)
1597 *_errno() = EBADF;
1598 return (intptr_t)hand;
1599}
int intptr_t
Definition: crtdefs.h:304

◆ _getmaxstdio()

int CDECL _getmaxstdio ( void  )

Definition at line 4028 of file file.c.

4029{
4030 return MSVCRT_max_streams;
4031}
static int MSVCRT_max_streams
Definition: file.c:135

◆ _getw()

int CDECL _getw ( FILE file)

Definition at line 2996 of file file.c.

2997{
2998 char *ch;
2999 int i, k;
3000 unsigned int j;
3001 ch = (char *)&i;
3002
3004 for (j=0; j<sizeof(int); j++) {
3005 k = fgetc(file);
3006 if (k == EOF) {
3007 file->_flag |= _IOEOF;
3009 return EOF;
3010 }
3011 ch[j] = k;
3012 }
3013
3015 return i;
3016}
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint GLint GLint j
Definition: glfuncs.h:250
int k
Definition: mpi.c:3369

◆ _getws()

wchar_t *CDECL _getws ( wchar_t buf)

Definition at line 3665 of file file.c.

3666{
3667 wint_t cc;
3668 wchar_t* ws = buf;
3669
3671 for (cc = fgetwc(stdin); cc != WEOF && cc != '\n';
3672 cc = fgetwc(stdin))
3673 {
3674 if (cc != '\r')
3675 *buf++ = (wchar_t)cc;
3676 }
3677 *buf = '\0';
3678
3679 TRACE("got %s\n", debugstr_w(ws));
3681 return ws;
3682}
int wint_t
Definition: _apple.h:38
GLenum GLuint GLenum GLsizei const GLchar * buf
Definition: glext.h:7751
uint32_t cc
Definition: isohybrid.c:75
#define debugstr_w
Definition: kernel32.h:32
#define WEOF
Definition: conio.h:185
const char * ws
Definition: skip_ws.cpp:7
#define wchar_t
Definition: wchar.h:102

◆ _isatty()

int CDECL _isatty ( int  fd)

Definition at line 564 of file file.c.

565{
566 TRACE(":fd (%d)\n",fd);
567
569}
#define WX_TTY
Definition: file.c:99

Referenced by add_std_buffer(), and msvcrt_alloc_buffer().

◆ _lock_file()

void CDECL _lock_file ( FILE file)

Definition at line 1193 of file file.c.

1194{
1197 /* ReactOS: string streams dont need to be locked */
1198 else if(!(file->_flag & _IOSTRG))
1200}
#define _IOB_ENTRIES
Definition: stdio.h:23
#define _STREAM_LOCKS
Definition: mtdll.h:65
void _lock(int locknum)
Definition: lock.c:97
void WINAPI EnterCriticalSection(LPCRITICAL_SECTION)

Referenced by _filbuf(), _fseeki64(), _ftelli64(), _getw(), _getws(), _putw(), _putws(), clearerr(), fclose(), fflush(), fgetc(), fgets(), fgetwc(), fgetws(), fputc(), fputs(), fputws(), fread(), fsetpos(), fwrite(), gets(), puts(), rewind(), setvbuf(), ungetc(), and ungetwc().

◆ _locking()

int CDECL _locking ( int  fd,
int  mode,
LONG  nbytes 
)

Definition at line 1220 of file file.c.

1221{
1223 BOOL ret;
1224 DWORD cur_locn;
1225
1226 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1227 if (info->handle == INVALID_HANDLE_VALUE)
1228 {
1230 return -1;
1231 }
1232
1233 if (mode < 0 || mode > 4)
1234 {
1236 *_errno() = EINVAL;
1237 return -1;
1238 }
1239
1240 TRACE(":fd (%d) by 0x%08x mode %s\n",
1241 fd,nbytes,(mode==_LK_UNLCK)?"_LK_UNLCK":
1242 (mode==_LK_LOCK)?"_LK_LOCK":
1243 (mode==_LK_NBLCK)?"_LK_NBLCK":
1244 (mode==_LK_RLCK)?"_LK_RLCK":
1245 (mode==_LK_NBRLCK)?"_LK_NBRLCK":
1246 "UNKNOWN");
1247
1248 if ((cur_locn = SetFilePointer(info->handle, 0L, NULL, FILE_CURRENT)) == INVALID_SET_FILE_POINTER)
1249 {
1251 FIXME ("Seek failed\n");
1252 *_errno() = EINVAL; /* FIXME */
1253 return -1;
1254 }
1255 if (mode == _LK_LOCK || mode == _LK_RLCK)
1256 {
1257 int nretry = 10;
1258 ret = 1; /* just to satisfy gcc */
1259 while (nretry--)
1260 {
1261 ret = LockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1262 if (ret) break;
1263 Sleep(1);
1264 }
1265 }
1266 else if (mode == _LK_UNLCK)
1267 ret = UnlockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1268 else
1269 ret = LockFile(info->handle, cur_locn, 0L, nbytes, 0L);
1270 /* FIXME - what about error settings? */
1272 return ret ? 0 : -1;
1273}
#define FIXME(fmt,...)
Definition: debug.h:111
#define INVALID_SET_FILE_POINTER
Definition: compat.h:732
BOOL WINAPI UnlockFile(IN HANDLE hFile, IN DWORD dwFileOffsetLow, IN DWORD dwFileOffsetHigh, IN DWORD nNumberOfBytesToUnlockLow, IN DWORD nNumberOfBytesToUnlockHigh)
Definition: lock.c:142
BOOL WINAPI LockFile(IN HANDLE hFile, IN DWORD dwFileOffsetLow, IN DWORD dwFileOffsetHigh, IN DWORD nNumberOfBytesToLockLow, IN DWORD nNumberOfBytesToLockHigh)
Definition: lock.c:25
#define _LK_LOCK
Definition: locking.h:16
#define _LK_UNLCK
Definition: locking.h:15
#define _LK_RLCK
Definition: locking.h:18
#define _LK_NBRLCK
Definition: locking.h:19
#define _LK_NBLCK
Definition: locking.h:17
#define L(x)
Definition: ntvdm.h:50
VOID WINAPI DECLSPEC_HOTPATCH Sleep(IN DWORD dwMilliseconds)
Definition: synch.c:790

◆ _lseek()

LONG CDECL _lseek ( int  fd,
LONG  offset,
int  whence 
)

Definition at line 1185 of file file.c.

1186{
1187 return (LONG)_lseeki64(fd, offset, whence);
1188}

◆ _lseeki64()

__int64 CDECL _lseeki64 ( int  fd,
__int64  offset,
int  whence 
)

Definition at line 1138 of file file.c.

1139{
1141 LARGE_INTEGER ofs;
1142
1143 TRACE(":fd (%d) handle (%p)\n", fd, info->handle);
1144
1145 if (info->handle == INVALID_HANDLE_VALUE)
1146 {
1148 return -1;
1149 }
1150
1151 if (whence < 0 || whence > 2)
1152 {
1154 *_errno() = EINVAL;
1155 return -1;
1156 }
1157
1158 TRACE(":fd (%d) to %s pos %s\n",
1160 (whence==SEEK_SET)?"SEEK_SET":
1161 (whence==SEEK_CUR)?"SEEK_CUR":
1162 (whence==SEEK_END)?"SEEK_END":"UNKNOWN");
1163
1164 /* The MoleBox protection scheme expects msvcrt to use SetFilePointer only,
1165 * so a LARGE_INTEGER offset cannot be passed directly via SetFilePointerEx. */
1166 ofs.QuadPart = offset;
1167 if ((ofs.u.LowPart = SetFilePointer(info->handle, ofs.u.LowPart, &ofs.u.HighPart, whence)) != INVALID_SET_FILE_POINTER ||
1169 {
1170 info->wxflag &= ~(WX_ATEOF|WX_READEOF);
1171 /* FIXME: What if we seek _to_ EOF - is EOF set? */
1172
1174 return ofs.QuadPart;
1175 }
1177 TRACE(":error-last error (%d)\n",GetLastError());
1179 return -1;
1180}
#define ERROR_SUCCESS
Definition: deptool.c:10
#define WX_READEOF
Definition: file.c:94
LONGLONG QuadPart
Definition: typedefs.h:114
struct _LARGE_INTEGER::@2295 u

Referenced by _chsize_s(), _filelengthi64(), _fseeki64(), _ftelli64(), _lseek(), _telli64(), and fsetpos().

◆ _mktemp()

char *CDECL _mktemp ( char pattern)

Definition at line 1604 of file file.c.

1605{
1606 int numX = 0;
1607 char *retVal = pattern;
1608 int id;
1609 char letter = 'a';
1610
1611 if(!pattern)
1612 return NULL;
1613
1614 while(*pattern)
1615 numX = (*pattern++ == 'X')? numX + 1 : 0;
1616 if (numX < 6)
1617 return NULL;
1618 pattern--;
1619 id = GetCurrentProcessId();
1620 numX = 6;
1621 while(numX--)
1622 {
1623 int tempNum = id / 10;
1624 *pattern-- = id - (tempNum * 10) + '0';
1625 id = tempNum;
1626 }
1627 pattern++;
1628 do
1629 {
1630 *pattern = letter++;
1632 return retVal;
1633 } while(letter <= 'z');
1634 return NULL;
1635}
GLubyte * pattern
Definition: glext.h:7787
GLuint id
Definition: glext.h:5910
DWORD WINAPI GetCurrentProcessId(void)
Definition: proc.c:1158

◆ _open()

int CDECL _open ( const char path,
int  flags,
  ... 
)

Definition at line 2001 of file file.c.

2002{
2003 va_list ap;
2004
2005 if (flags & _O_CREAT)
2006 {
2007 int pmode;
2008 va_start(ap, flags);
2009 pmode = va_arg(ap, int);
2010 va_end(ap);
2011 return _sopen( path, flags, _SH_DENYNO, pmode );
2012 }
2013 else
2014 return _sopen( path, flags, _SH_DENYNO);
2015}
char * va_list
Definition: acmsvcex.h:78
#define va_end(ap)
Definition: acmsvcex.h:90
#define va_start(ap, A)
Definition: acmsvcex.h:91
#define va_arg(ap, T)
Definition: acmsvcex.h:89
_CRTIMP int __cdecl _sopen(const char *_Filename, int _OpenFlag, int _ShareFlag,...)
Definition: file.c:1978
#define _SH_DENYNO
Definition: share.h:17
void int int ULONGLONG int va_list * ap
Definition: winesup.h:36

Referenced by _creat(), compat_open(), install_from_unix_file(), main(), rd_open_file(), rw(), test_chsize(), test_file_write_read(), test_get_osfhandle(), test_setmode(), and tmpfile().

◆ _open_osfhandle()

int CDECL _open_osfhandle ( intptr_t  handle,
int  oflags 
)

Definition at line 2057 of file file.c.

2058{
2059 DWORD flags;
2060 int fd;
2061
2062 /* _O_RDONLY (0) always matches, so set the read flag
2063 * MFC's CStdioFile clears O_RDONLY (0)! if it wants to write to the
2064 * file, so set the write flag. It also only sets _O_TEXT if it wants
2065 * text - it never sets _O_BINARY.
2066 */
2067 /* don't let split_oflags() decide the mode if no mode is passed */
2068 if (!(oflags & (_O_BINARY | _O_TEXT)))
2069 oflags |= _O_BINARY;
2070
2073 {
2075 return -1;
2076 }
2077
2078 if (flags == FILE_TYPE_CHAR)
2079 flags = WX_TTY;
2080 else if (flags == FILE_TYPE_PIPE)
2081 flags = WX_PIPE;
2082 else
2083 flags = 0;
2084 flags |= split_oflags(oflags);
2085
2087 TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, flags);
2088 return fd;
2089}
#define NO_ERROR
Definition: dderror.h:5
#define _O_BINARY
Definition: cabinet.h:51
DWORD WINAPI GetFileType(HANDLE hFile)
Definition: fileinfo.c:269
#define WX_PIPE
Definition: file.c:95
unsigned split_oflags(unsigned oflags)
Definition: file.c:1671
int msvcrt_alloc_fd(HANDLE hand, int flag)
Definition: file.c:343
#define FILE_TYPE_UNKNOWN
Definition: winbase.h:258
#define FILE_TYPE_CHAR
Definition: winbase.h:260
#define FILE_TYPE_PIPE
Definition: winbase.h:261

◆ _pipe()

int CDECL _pipe ( int pfds,
unsigned int  psize,
int  textmode 
)

Definition at line 1702 of file file.c.

1703{
1704 int ret = -1;
1706 HANDLE readHandle, writeHandle;
1707
1708 if (!pfds)
1709 {
1710 *_errno() = EINVAL;
1711 return -1;
1712 }
1713
1714 sa.nLength = sizeof(SECURITY_ATTRIBUTES);
1715 sa.bInheritHandle = !(textmode & _O_NOINHERIT);
1716 sa.lpSecurityDescriptor = NULL;
1717 if (CreatePipe(&readHandle, &writeHandle, &sa, psize))
1718 {
1719 unsigned int wxflags = split_oflags(textmode);
1720 int fd;
1721
1722 fd = msvcrt_alloc_fd(readHandle, wxflags);
1723 if (fd != -1)
1724 {
1725 pfds[0] = fd;
1726 fd = msvcrt_alloc_fd(writeHandle, wxflags);
1727 if (fd != -1)
1728 {
1729 pfds[1] = fd;
1730 ret = 0;
1731 }
1732 else
1733 {
1734 _close(pfds[0]);
1735 CloseHandle(writeHandle);
1736 *_errno() = EMFILE;
1737 }
1738 }
1739 else
1740 {
1741 CloseHandle(readHandle);
1742 CloseHandle(writeHandle);
1743 *_errno() = EMFILE;
1744 }
1745 }
1746 else
1748
1749 return ret;
1750}
static struct sockaddr_in sa
Definition: adnsresfilter.c:69
#define _O_NOINHERIT
Definition: cabinet.h:45
struct _SECURITY_ATTRIBUTES SECURITY_ATTRIBUTES
#define EMFILE
Definition: errno.h:30
BOOL WINAPI CreatePipe(PHANDLE hReadPipe, PHANDLE hWritePipe, LPSECURITY_ATTRIBUTES lpPipeAttributes, DWORD nSize)
Definition: npipe.c:117
_Must_inspect_result_ _Out_ LPSIZE psize
Definition: ntgdi.h:1569

◆ _putw()

int CDECL _putw ( int  val,
FILE file 
)

Definition at line 2764 of file file.c.

2765{
2766 int len;
2767
2769 len = _write(file->_file, &val, sizeof(val));
2770 if (len == sizeof(val)) {
2772 return val;
2773 }
2774
2775 file->_flag |= _IOERR;
2777 return EOF;
2778}
GLuint GLfloat * val
Definition: glext.h:7180
GLenum GLsizei len
Definition: glext.h:6722

◆ _putwch()

wint_t CDECL _putwch ( wchar_t  c)

Definition at line 3703 of file file.c.

3704{
3705 return fputwc(c, stdout);
3706}

◆ _putws()

int CDECL _putws ( const wchar_t s)

Definition at line 3730 of file file.c.

3731{
3732 static const wchar_t nl = '\n';
3733 size_t len = strlenW(s);
3734 int ret;
3735
3737 if(fwrite(s, sizeof(*s), len, stdout) != len) {
3739 return EOF;
3740 }
3741
3742 ret = fwrite(&nl,sizeof(nl),1,stdout) == 1 ? 0 : EOF;
3744 return ret;
3745}
GLdouble s
Definition: gl.h:2039
_Check_return_opt_ _CRTIMP size_t __cdecl fwrite(_In_reads_bytes_(_Size *_Count) const void *_Str, _In_ size_t _Size, _In_ size_t _Count, _Inout_ FILE *_File)
#define strlenW(s)
Definition: unicode.h:28

◆ _read()

int CDECL _read ( int  fd,
void buf,
unsigned int  count 
)

Definition at line 2466 of file file.c.

2467{
2469 int num_read = read_i(fd, info, buf, count);
2471 return num_read;
2472}
static int read_i(int fd, ioinfo *fdinfo, void *buf, unsigned int count)
Definition: file.c:2306

◆ _rmtmp()

int CDECL _rmtmp ( void  )

Definition at line 2094 of file file.c.

2095{
2096 int num_removed = 0, i;
2097 FILE *file;
2098
2099 LOCK_FILES();
2100 for (i = 3; i < MSVCRT_stream_idx; i++) {
2102
2103 if (file->_tmpfname)
2104 {
2105 fclose(file);
2106 num_removed++;
2107 }
2108 }
2109 UNLOCK_FILES();
2110
2111 if (num_removed)
2112 TRACE(":removed (%d) temp files\n",num_removed);
2113 return num_removed;
2114}

◆ _setmaxstdio()

int CDECL _setmaxstdio ( int  newmax)

Definition at line 4036 of file file.c.

4037{
4038 TRACE("%d\n", newmax);
4039
4040 if(newmax<_IOB_ENTRIES || newmax>MSVCRT_MAX_FILES || newmax<MSVCRT_stream_idx)
4041 return -1;
4042
4043 MSVCRT_max_streams = newmax;
4044 return MSVCRT_max_streams;
4045}
#define MSVCRT_MAX_FILES
Definition: file.c:112

◆ _setmode()

int CDECL _setmode ( int  fd,
int  mode 
)

Definition at line 2477 of file file.c.

2478{
2480 int ret = info->wxflag & WX_TEXT ? _O_TEXT : _O_BINARY;
2481 if(ret==_O_TEXT && (info->exflag & (EF_UTF8|EF_UTF16)))
2482 ret = _O_WTEXT;
2483
2485 && mode!=_O_U16TEXT && mode!=_O_U8TEXT) {
2486 *_errno() = EINVAL;
2488 return -1;
2489 }
2490
2491 if(info == &__badioinfo) {
2492 *_errno() = EBADF;
2493 return EOF;
2494 }
2495
2496 if(mode == _O_BINARY) {
2497 info->wxflag &= ~WX_TEXT;
2498 info->exflag &= ~(EF_UTF8|EF_UTF16);
2500 return ret;
2501 }
2502
2503 info->wxflag |= WX_TEXT;
2504 if(mode == _O_TEXT)
2505 info->exflag &= ~(EF_UTF8|EF_UTF16);
2506 else if(mode == _O_U8TEXT)
2507 info->exflag = (info->exflag & ~EF_UTF16) | EF_UTF8;
2508 else
2509 info->exflag = (info->exflag & ~EF_UTF8) | EF_UTF16;
2510
2512 return ret;
2513
2514}
#define _O_WTEXT
Definition: fcntl.h:20
#define _O_U8TEXT
Definition: fcntl.h:22
#define _O_U16TEXT
Definition: fcntl.h:21
#define EF_UTF8
Definition: file.c:103
#define EF_UTF16
Definition: file.c:104

◆ _sopen()

int CDECL _sopen ( const char path,
int  oflags,
int  shflags,
  ... 
)

Definition at line 1978 of file file.c.

1979{
1980 int pmode;
1981 int fd;
1982
1983 if (oflags & _O_CREAT)
1984 {
1985 va_list ap;
1986
1987 va_start(ap, shflags);
1988 pmode = va_arg(ap, int);
1989 va_end(ap);
1990 }
1991 else
1992 pmode = 0;
1993
1994 _sopen_s(&fd, path, oflags, shflags, pmode);
1995 return fd;
1996}
int CDECL _sopen_s(int *fd, const char *path, int oflags, int shflags, int pmode)
Definition: file.c:1962

Referenced by _open(), and test_get_osfhandle().

◆ _sopen_s()

int CDECL _sopen_s ( int fd,
const char path,
int  oflags,
int  shflags,
int  pmode 
)

Definition at line 1962 of file file.c.

1963{
1964 wchar_t *pathW;
1965 int ret;
1966
1967 if(!MSVCRT_CHECK_PMT(path && (pathW = msvcrt_wstrdupa(path))))
1968 return EINVAL;
1969
1970 ret = _wsopen_s(fd, pathW, oflags, shflags, pmode);
1971 free(pathW);
1972 return ret;
1973}
int CDECL _wsopen_s(int *fd, const wchar_t *path, int oflags, int shflags, int pmode)
Definition: file.c:1778

Referenced by _sopen().

◆ _tell()

long CDECL _tell ( int  fd)

Definition at line 2519 of file file.c.

2520{
2521 return _lseek(fd, 0, SEEK_CUR);
2522}

◆ _telli64()

__int64 CDECL _telli64 ( int  fd)

Definition at line 2527 of file file.c.

2528{
2529 return _lseeki64(fd, 0, SEEK_CUR);
2530}

Referenced by _ftelli64().

◆ _tempnam()

char *CDECL _tempnam ( const char dir,
const char prefix 
)

Definition at line 2535 of file file.c.

2536{
2537 char tmpbuf[MAX_PATH];
2538 const char *tmp_dir = getenv("TMP");
2539
2540 if (tmp_dir) dir = tmp_dir;
2541
2542 TRACE("dir (%s) prefix (%s)\n",dir,prefix);
2543 if (GetTempFileNameA(dir,prefix,0,tmpbuf))
2544 {
2545 TRACE("got name (%s)\n",tmpbuf);
2546 DeleteFileA(tmpbuf);
2547 return _strdup(tmpbuf);
2548 }
2549 TRACE("failed (%d)\n",GetLastError());
2550 return NULL;
2551}
unsigned int dir
Definition: maze.c:112
#define _strdup
Definition: debug_ros.c:7
#define MAX_PATH
Definition: compat.h:34
BOOL WINAPI DeleteFileA(IN LPCSTR lpFileName)
Definition: delete.c:24
UINT WINAPI GetTempFileNameA(IN LPCSTR lpPathName, IN LPCSTR lpPrefixString, IN UINT uUnique, OUT LPSTR lpTempFileName)
Definition: filename.c:26
_Check_return_ char *__cdecl getenv(_In_z_ const char *_VarName)

◆ _umask()

int CDECL _umask ( int  umask)

Definition at line 2574 of file file.c.

2575{
2576 int old_umask = MSVCRT_umask;
2577 TRACE("(%d)\n",umask);
2579 return old_umask;
2580}
_CRTIMP int __cdecl umask(_In_ int _Mode)
static int MSVCRT_umask
Definition: file.c:138

◆ _unlink()

int CDECL _unlink ( const char path)

Definition at line 802 of file file.c.

803{
804 TRACE("%s\n",debugstr_a(path));
805 if(DeleteFileA(path))
806 return 0;
807 TRACE("failed (%d)\n",GetLastError());
809 return -1;
810}
#define debugstr_a
Definition: kernel32.h:31

◆ _unlock_file()

void CDECL _unlock_file ( FILE file)

Definition at line 1205 of file file.c.

1206{
1209 /* ReactOS: string streams dont need to be locked */
1210 else if(!(file->_flag & _IOSTRG))
1212
1213}
void _unlock(int locknum)
Definition: lock.c:126
void WINAPI LeaveCriticalSection(LPCRITICAL_SECTION)

Referenced by _filbuf(), _fseeki64(), _ftelli64(), _getw(), _getws(), _putw(), _putws(), clearerr(), fclose(), fflush(), fgetc(), fgets(), fgetwc(), fgetws(), fputc(), fputs(), fputws(), fread(), fsetpos(), fwrite(), gets(), puts(), rewind(), setvbuf(), ungetc(), and ungetwc().

◆ _waccess()

int CDECL _waccess ( const wchar_t filename,
int  mode 
)

Definition at line 727 of file file.c.

728{
730
731 TRACE("(%s,%d) %d\n",debugstr_w(filename),mode,attr);
732
734 {
736 return -1;
737 }
738 if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & W_OK))
739 {
741 return -1;
742 }
743 return 0;
744}
DWORD WINAPI GetFileAttributesW(LPCWSTR lpFileName)
Definition: fileinfo.c:652

Referenced by _waccess_s().

◆ _waccess_s()

int CDECL _waccess_s ( const wchar_t filename,
int  mode 
)

Definition at line 749 of file file.c.

750{
751 if (!MSVCRT_CHECK_PMT(filename != NULL) ||
752 !MSVCRT_CHECK_PMT((mode & ~(R_OK | W_OK)) == 0))
753 {
754 *_errno() = EINVAL;
755 return -1;
756 }
757
758 return _waccess(filename, mode);
759}
int CDECL _waccess(const wchar_t *filename, int mode)
Definition: file.c:727

◆ _wchmod()

int CDECL _wchmod ( const wchar_t path,
int  flags 
)

Definition at line 783 of file file.c.

784{
785 DWORD oldFlags = GetFileAttributesW(path);
786
787 if (oldFlags != INVALID_FILE_ATTRIBUTES)
788 {
789 DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY:
790 oldFlags | FILE_ATTRIBUTE_READONLY;
791
792 if (newFlags == oldFlags || SetFileAttributesW(path, newFlags))
793 return 0;
794 }
796 return -1;
797}
BOOL WINAPI SetFileAttributesW(LPCWSTR lpFileName, DWORD dwFileAttributes)
Definition: fileinfo.c:794

◆ _wcreat()

int CDECL _wcreat ( const wchar_t path,
int  flags 
)

Definition at line 2048 of file file.c.

2049{
2050 int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC;
2051 return _wopen(path, usedFlags);
2052}
int CDECL _wopen(const wchar_t *path, int flags,...)
Definition: file.c:2020

◆ _wfdopen()

FILE *CDECL _wfdopen ( int  fd,
const wchar_t mode 
)

Definition at line 1520 of file file.c.

1521{
1522 int open_flags, stream_flags;
1523 FILE* file;
1524
1525 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1) return NULL;
1526
1527 LOCK_FILES();
1528 if (!(file = msvcrt_alloc_fp()))
1529 file = NULL;
1530 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
1531 {
1532 file->_flag = 0;
1533 file = NULL;
1534 }
1535 else TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
1536 UNLOCK_FILES();
1537
1538 return file;
1539}
static FILE * msvcrt_alloc_fp(void)
Definition: file.c:360
static int msvcrt_get_flags(const wchar_t *mode, int *open_flags, int *stream_flags)
Definition: file.c:1385
static int msvcrt_init_fp(FILE *file, int fd, unsigned stream_flags)
Definition: file.c:382

Referenced by _fdopen().

◆ _wfopen()

FILE *CDECL _wfopen ( const wchar_t path,
const wchar_t mode 
)

Definition at line 3274 of file file.c.

3275{
3276 return _wfsopen( path, mode, _SH_DENYNO );
3277}

Referenced by _wfopen_s().

◆ _wfopen_s()

int CDECL _wfopen_s ( FILE **  pFile,
const wchar_t filename,
const wchar_t mode 
)

Definition at line 3282 of file file.c.

3284{
3286 !MSVCRT_CHECK_PMT(mode != NULL)) {
3287 *_errno() = EINVAL;
3288 return EINVAL;
3289 }
3290
3292
3293 if(!*pFile)
3294 return *_errno();
3295 return 0;
3296}
MdFileObject pFile
FILE *CDECL _wfopen(const wchar_t *path, const wchar_t *mode)
Definition: file.c:3274

◆ _wfreopen()

FILE *CDECL _wfreopen ( const wchar_t path,
const wchar_t mode,
FILE file 
)

Definition at line 3421 of file file.c.

3422{
3423 int open_flags, stream_flags, fd;
3424
3425 TRACE(":path (%s) mode (%s) file (%p) fd (%d)\n", debugstr_w(path), debugstr_w(mode), file, file ? file->_file : -1);
3426
3427 LOCK_FILES();
3428 if (!file || ((fd = file->_file) < 0))
3429 file = NULL;
3430 else
3431 {
3432 fclose(file);
3433 /* map mode string to open() flags. "man fopen" for possibilities. */
3434 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
3435 file = NULL;
3436 else
3437 {
3438 fd = _wopen(path, open_flags, _S_IREAD | _S_IWRITE);
3439 if (fd < 0)
3440 file = NULL;
3441 else if (msvcrt_init_fp(file, fd, stream_flags) == -1)
3442 {
3443 file->_flag = 0;
3444 WARN(":failed-last error (%d)\n",GetLastError());
3446 file = NULL;
3447 }
3448 }
3449 }
3450 UNLOCK_FILES();
3451 return file;
3452}
#define _S_IREAD
Definition: cabinet.h:34

Referenced by freopen().

◆ _wfsopen()

FILE *CDECL _wfsopen ( const wchar_t path,
const wchar_t mode,
int  share 
)

Definition at line 3187 of file file.c.

3188{
3189 FILE* file;
3190 int open_flags, stream_flags, fd;
3191
3192 TRACE("(%s,%s)\n", debugstr_w(path), debugstr_w(mode));
3193
3194 /* map mode string to open() flags. "man fopen" for possibilities. */
3195 if (msvcrt_get_flags(mode, &open_flags, &stream_flags) == -1)
3196 return NULL;
3197
3198 LOCK_FILES();
3199 fd = _wsopen(path, open_flags, share, _S_IREAD | _S_IWRITE);
3200 if (fd < 0)
3201 file = NULL;
3202 else if ((file = msvcrt_alloc_fp()) && msvcrt_init_fp(file, fd, stream_flags)
3203 != -1)
3204 TRACE(":fd (%d) mode (%s) FILE* (%p)\n", fd, debugstr_w(mode), file);
3205 else if (file)
3206 {
3207 file->_flag = 0;
3208 file = NULL;
3209 }
3210
3211 TRACE(":got (%p)\n",file);
3212 if (fd >= 0 && !file)
3213 _close(fd);
3214 UNLOCK_FILES();
3215 return file;
3216}
int CDECL _wsopen(const wchar_t *path, int oflags, int shflags,...)
Definition: file.c:1939

Referenced by _fsopen(), and _wfopen().

◆ _wmktemp()

wchar_t *CDECL _wmktemp ( wchar_t pattern)

Definition at line 1640 of file file.c.

1641{
1642 int numX = 0;
1643 wchar_t *retVal = pattern;
1644 int id;
1645 wchar_t letter = 'a';
1646
1647 while(*pattern)
1648 numX = (*pattern++ == 'X')? numX + 1 : 0;
1649 if (numX < 5)
1650 return NULL;
1651 pattern--;
1652 id = GetCurrentProcessId();
1653 numX = 6;
1654 while(numX--)
1655 {
1656 int tempNum = id / 10;
1657 *pattern-- = id - (tempNum * 10) + '0';
1658 id = tempNum;
1659 }
1660 pattern++;
1661 do
1662 {
1665 return retVal;
1666 *pattern = letter++;
1667 } while(letter != '|');
1668 return NULL;
1669}
#define ERROR_FILE_NOT_FOUND
Definition: disk.h:79

◆ _wopen()

int CDECL _wopen ( const wchar_t path,
int  flags,
  ... 
)

Definition at line 2020 of file file.c.

2021{
2022 va_list ap;
2023
2024 if (flags & _O_CREAT)
2025 {
2026 int pmode;
2027 va_start(ap, flags);
2028 pmode = va_arg(ap, int);
2029 va_end(ap);
2030 return _wsopen( path, flags, _SH_DENYNO, pmode );
2031 }
2032 else
2033 return _wsopen( path, flags, _SH_DENYNO);
2034}

Referenced by _wcreat(), _wfreopen(), compat_open(), and gz_open().

◆ _wremove()

int CDECL _wremove ( const wchar_t path)

Definition at line 3763 of file file.c.

3764{
3765 TRACE("(%s)\n",debugstr_w(path));
3766 if (DeleteFileW(path))
3767 return 0;
3768 TRACE(":failed (%d)\n",GetLastError());
3770 return -1;
3771}
BOOL WINAPI DeleteFileW(IN LPCWSTR lpFileName)
Definition: delete.c:39

◆ _wrename()

int CDECL _wrename ( const wchar_t oldpath,
const wchar_t newpath 
)

Definition at line 3789 of file file.c.

3790{
3791 TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath));
3792 if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3793 return 0;
3794 TRACE(":failed (%d)\n",GetLastError());
3796 return -1;
3797}
BOOL WINAPI MoveFileExW(IN LPCWSTR lpExistingFileName, IN LPCWSTR lpNewFileName OPTIONAL, IN DWORD dwFlags)
Definition: move.c:1120
#define MOVEFILE_COPY_ALLOWED
Definition: filesup.h:29

◆ _write()

int CDECL _write ( int  fd,
const void buf,
unsigned int  count 
)

Definition at line 2585 of file file.c.

2586{
2587 DWORD num_written;
2589 HANDLE hand = info->handle;
2590
2591 /* Don't trace small writes, it gets *very* annoying */
2592#if 0
2593 if (count > 32)
2594 TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
2595#endif
2596 if (hand == INVALID_HANDLE_VALUE)
2597 {
2598 *_errno() = EBADF;
2600 return -1;
2601 }
2602
2603 if (((info->exflag&EF_UTF8) || (info->exflag&EF_UTF16)) && count&1)
2604 {
2605 *_errno() = EINVAL;
2607 return -1;
2608 }
2609
2610 /* If appending, go to EOF */
2611 if (info->wxflag & WX_APPEND)
2612 _lseek(fd, 0, FILE_END);
2613
2614 if (!(info->wxflag & WX_TEXT))
2615 {
2616 if (WriteFile(hand, buf, count, &num_written, NULL)
2617 && (num_written == count))
2618 {
2620 return num_written;
2621 }
2622 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d)\n", fd,
2623 hand, GetLastError());
2624 *_errno() = ENOSPC;
2625 }
2626 else
2627 {
2628 unsigned int i, j, nr_lf, size;
2629 char *p = NULL;
2630 const char *q;
2631 const char *s = buf, *buf_start = buf;
2632
2633 if (!(info->exflag & (EF_UTF8|EF_UTF16)))
2634 {
2635 /* find number of \n */
2636 for (nr_lf=0, i=0; i<count; i++)
2637 if (s[i] == '\n')
2638 nr_lf++;
2639 if (nr_lf)
2640 {
2641 size = count+nr_lf;
2642 if ((q = p = malloc(size)))
2643 {
2644 for (s = buf, i = 0, j = 0; i < count; i++)
2645 {
2646 if (s[i] == '\n')
2647 p[j++] = '\r';
2648 p[j++] = s[i];
2649 }
2650 }
2651 else
2652 {
2653 FIXME("Malloc failed\n");
2654 nr_lf = 0;
2655 size = count;
2656 q = buf;
2657 }
2658 }
2659 else
2660 {
2661 size = count;
2662 q = buf;
2663 }
2664 }
2665 else if (info->exflag & EF_UTF16)
2666 {
2667 for (nr_lf=0, i=0; i<count; i+=2)
2668 if (s[i]=='\n' && s[i+1]==0)
2669 nr_lf += 2;
2670 if (nr_lf)
2671 {
2672 size = count+nr_lf;
2673 if ((q = p = malloc(size)))
2674 {
2675 for (s=buf, i=0, j=0; i<count; i++)
2676 {
2677 if (s[i]=='\n' && s[i+1]==0)
2678 {
2679 p[j++] = '\r';
2680 p[j++] = 0;
2681 }
2682 p[j++] = s[i++];
2683 p[j++] = s[i];
2684 }
2685 }
2686 else
2687 {
2688 FIXME("Malloc failed\n");
2689 nr_lf = 0;
2690 size = count;
2691 q = buf;
2692 }
2693 }
2694 else
2695 {
2696 size = count;
2697 q = buf;
2698 }
2699 }
2700 else
2701 {
2702 DWORD conv_len;
2703
2704 for(nr_lf=0, i=0; i<count; i+=2)
2705 if (s[i]=='\n' && s[i+1]==0)
2706 nr_lf++;
2707
2708 conv_len = WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)buf, count/2, NULL, 0, NULL, NULL);
2709 if(!conv_len) {
2711 free(p);
2713 return -1;
2714 }
2715
2716 size = conv_len+nr_lf;
2717 if((p = malloc(count+nr_lf*2+size)))
2718 {
2719 for (s=buf, i=0, j=0; i<count; i++)
2720 {
2721 if (s[i]=='\n' && s[i+1]==0)
2722 {
2723 p[j++] = '\r';
2724 p[j++] = 0;
2725 }
2726 p[j++] = s[i++];
2727 p[j++] = s[i];
2728 }
2729 q = p+count+nr_lf*2;
2730 WideCharToMultiByte(CP_UTF8, 0, (WCHAR*)p, count/2+nr_lf,
2731 p+count+nr_lf*2, conv_len+nr_lf, NULL, NULL);
2732 }
2733 else
2734 {
2735 FIXME("Malloc failed\n");
2736 nr_lf = 0;
2737 size = count;
2738 q = buf;
2739 }
2740 }
2741
2742 if (!WriteFile(hand, q, size, &num_written, NULL))
2743 num_written = -1;
2745 if(p)
2746 free(p);
2747 if (num_written != size)
2748 {
2749 TRACE("WriteFile (fd %d, hand %p) failed-last error (%d), num_written %d\n",
2750 fd, hand, GetLastError(), num_written);
2751 *_errno() = ENOSPC;
2752 return s - buf_start;
2753 }
2754 return count;
2755 }
2756
2758 return -1;
2759}
#define malloc
Definition: debug_ros.c:4
#define WideCharToMultiByte
Definition: compat.h:111
BOOL WINAPI WriteFile(IN HANDLE hFile, IN LPCVOID lpBuffer, IN DWORD nNumberOfBytesToWrite OPTIONAL, OUT LPDWORD lpNumberOfBytesWritten, IN LPOVERLAPPED lpOverlapped OPTIONAL)
Definition: rw.c:24
#define ENOSPC
Definition: errno.h:34
GLdouble GLdouble GLdouble GLdouble q
Definition: gl.h:2063
#define CP_UTF8
Definition: nls.h:20
#define WX_APPEND
Definition: file.c:98
__wchar_t WCHAR
Definition: xmlstorage.h:180

◆ _wsopen()

int CDECL _wsopen ( const wchar_t path,
int  oflags,
int  shflags,
  ... 
)

Definition at line 1939 of file file.c.

1940{
1941 int pmode;
1942 int fd;
1943
1944 if (oflags & _O_CREAT)
1945 {
1947
1948 __ms_va_start(ap, shflags);
1949 pmode = va_arg(ap, int);
1950 __ms_va_end(ap);
1951 }
1952 else
1953 pmode = 0;
1954
1955 _wsopen_s(&fd, path, oflags, shflags, pmode);
1956 return fd;
1957}
#define __ms_va_list
Definition: windef.h:456
#define __ms_va_end(list)
Definition: windef.h:458
#define __ms_va_start(list, arg)
Definition: windef.h:457

Referenced by _wfsopen(), and _wopen().

◆ _wsopen_s()

int CDECL _wsopen_s ( int fd,
const wchar_t path,
int  oflags,
int  shflags,
int  pmode 
)

Definition at line 1778 of file file.c.

1779{
1780 DWORD access = 0, creation = 0, attrib;
1783 int wxflag;
1784 HANDLE hand;
1785
1786 TRACE("fd*: %p :file (%s) oflags: 0x%04x shflags: 0x%04x pmode: 0x%04x\n",
1787 fd, debugstr_w(path), oflags, shflags, pmode);
1788
1789 if (!MSVCRT_CHECK_PMT( fd != NULL )) return EINVAL;
1790
1791 *fd = -1;
1792 wxflag = split_oflags(oflags);
1793 switch (oflags & (_O_RDONLY | _O_WRONLY | _O_RDWR))
1794 {
1795 case _O_RDONLY: access |= GENERIC_READ; break;
1796 case _O_WRONLY: access |= GENERIC_WRITE; break;
1797 case _O_RDWR: access |= GENERIC_WRITE | GENERIC_READ; break;
1798 }
1799
1800 if (oflags & _O_CREAT)
1801 {
1802 if(pmode & ~(_S_IREAD | _S_IWRITE))
1803 FIXME(": pmode 0x%04x ignored\n", pmode);
1804 else
1805 WARN(": pmode 0x%04x ignored\n", pmode);
1806
1807 if (oflags & _O_EXCL)
1808 creation = CREATE_NEW;
1809 else if (oflags & _O_TRUNC)
1810 creation = CREATE_ALWAYS;
1811 else
1812 creation = OPEN_ALWAYS;
1813 }
1814 else /* no _O_CREAT */
1815 {
1816 if (oflags & _O_TRUNC)
1817 creation = TRUNCATE_EXISTING;
1818 else
1819 creation = OPEN_EXISTING;
1820 }
1821
1822 switch( shflags )
1823 {
1824 case _SH_DENYRW:
1825 sharing = 0L;
1826 break;
1827 case _SH_DENYWR:
1829 break;
1830 case _SH_DENYRD:
1832 break;
1833 case _SH_DENYNO:
1835 break;
1836 default:
1837 ERR( "Unhandled shflags 0x%x\n", shflags );
1838 return EINVAL;
1839 }
1840 attrib = FILE_ATTRIBUTE_NORMAL;
1841
1842 if (oflags & _O_TEMPORARY)
1843 {
1844 attrib |= FILE_FLAG_DELETE_ON_CLOSE;
1845 access |= DELETE;
1847 }
1848
1849 sa.nLength = sizeof( SECURITY_ATTRIBUTES );
1850 sa.lpSecurityDescriptor = NULL;
1851 sa.bInheritHandle = !(oflags & _O_NOINHERIT);
1852
1853 if ((oflags&(_O_WTEXT|_O_U16TEXT|_O_U8TEXT))
1854 && (creation==OPEN_ALWAYS || creation==OPEN_EXISTING)
1855 && !(access&GENERIC_READ))
1856 {
1859 if (hand != INVALID_HANDLE_VALUE)
1860 {
1861 oflags = check_bom(hand, oflags, FALSE);
1862 CloseHandle(hand);
1863 }
1864 else
1865 oflags &= ~(_O_WTEXT|_O_U16TEXT|_O_U8TEXT);
1866 }
1867
1868 hand = CreateFileW(path, access, sharing, &sa, creation, attrib, 0);
1869 if (hand == INVALID_HANDLE_VALUE) {
1870 WARN(":failed-last error (%d)\n",GetLastError());
1872 return *_errno();
1873 }
1874
1875 if (oflags & (_O_WTEXT | _O_U16TEXT | _O_U8TEXT))
1876 {
1877 if ((access & GENERIC_WRITE) && (creation==CREATE_NEW
1878 || creation==CREATE_ALWAYS || creation==TRUNCATE_EXISTING
1879 || (creation==OPEN_ALWAYS && GetLastError()==ERROR_ALREADY_EXISTS)))
1880 {
1881 if (oflags & _O_U8TEXT)
1882 {
1883 DWORD written = 0, tmp;
1884
1885 while(written!=sizeof(utf8_bom) && WriteFile(hand, (char*)utf8_bom+written,
1886 sizeof(utf8_bom)-written, &tmp, NULL))
1887 written += tmp;
1888 if (written != sizeof(utf8_bom)) {
1889 WARN("error writing BOM\n");
1890 CloseHandle(hand);
1892 return *_errno();
1893 }
1894 }
1895 else
1896 {
1897 DWORD written = 0, tmp;
1898
1899 while(written!=sizeof(utf16_bom) && WriteFile(hand, (char*)utf16_bom+written,
1900 sizeof(utf16_bom)-written, &tmp, NULL))
1901 written += tmp;
1902 if (written != sizeof(utf16_bom))
1903 {
1904 WARN("error writing BOM\n");
1905 CloseHandle(hand);
1907 return *_errno();
1908 }
1909 }
1910 }
1911 else if (access & GENERIC_READ)
1912 oflags = check_bom(hand, oflags, TRUE);
1913 }
1914
1915 type = GetFileType(hand);
1916 if (type == FILE_TYPE_CHAR)
1917 wxflag |= WX_TTY;
1918 else if (type == FILE_TYPE_PIPE)
1919 wxflag |= WX_PIPE;
1920
1921 *fd = msvcrt_alloc_fd(hand, wxflag);
1922 if (*fd == -1)
1923 return *_errno();
1924
1925 if (oflags & _O_WTEXT)
1927 else if (oflags & _O_U16TEXT)
1929 else if (oflags & _O_U8TEXT)
1931
1932 TRACE(":fd (%d) handle (%p)\n", *fd, hand);
1933 return 0;
1934}
#define ERR(fmt,...)
Definition: debug.h:110
#define _O_RDWR
Definition: cabinet.h:39
#define _O_TEMPORARY
Definition: cabinet.h:44
#define _O_RDONLY
Definition: cabinet.h:37
#define _O_EXCL
Definition: cabinet.h:48
#define OPEN_EXISTING
Definition: compat.h:775
#define GENERIC_READ
Definition: compat.h:135
#define CreateFileW
Definition: compat.h:741
#define FILE_ATTRIBUTE_NORMAL
Definition: compat.h:137
#define FILE_SHARE_READ
Definition: compat.h:136
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
GLuint GLint GLboolean GLint GLenum access
Definition: glext.h:7866
if(dx< 0)
Definition: linetemp.h:194
#define CREATE_ALWAYS
Definition: disk.h:72
#define ERROR_ALREADY_EXISTS
Definition: disk.h:80
#define TRUNCATE_EXISTING
Definition: disk.h:71
#define FILE_FLAG_DELETE_ON_CLOSE
Definition: disk.h:42
#define CREATE_NEW
Definition: disk.h:69
#define OPEN_ALWAYS
Definition: disk.h:70
static ULONG POBJECT_ATTRIBUTES PIO_STATUS_BLOCK ULONG sharing
Definition: pipe.c:70
#define FILE_SHARE_WRITE
Definition: nt_native.h:681
#define FILE_SHARE_DELETE
Definition: nt_native.h:682
#define DELETE
Definition: nt_native.h:57
#define GENERIC_WRITE
Definition: nt_native.h:90
static int check_bom(HANDLE h, int oflags, BOOL seek)
Definition: file.c:1752
static char utf8_bom[3]
Definition: file.c:108
static char utf16_bom[2]
Definition: file.c:109
#define EF_UNK_UNICODE
Definition: file.c:106
#define _SH_DENYRW
Definition: share.h:14
#define _SH_DENYRD
Definition: share.h:16
#define _SH_DENYWR
Definition: share.h:15
int exflag
Definition: file.c:44

Referenced by _sopen_s(), and _wsopen().

◆ _wtempnam()

wchar_t *CDECL _wtempnam ( const wchar_t dir,
const wchar_t prefix 
)

Definition at line 2556 of file file.c.

2557{
2558 wchar_t tmpbuf[MAX_PATH];
2559
2560 TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix));
2561 if (GetTempFileNameW(dir,prefix,0,tmpbuf))
2562 {
2563 TRACE("got name (%s)\n",debugstr_w(tmpbuf));
2564 DeleteFileW(tmpbuf);
2565 return _wcsdup(tmpbuf);
2566 }
2567 TRACE("failed (%d)\n",GetLastError());
2568 return NULL;
2569}
UINT WINAPI GetTempFileNameW(IN LPCWSTR lpPathName, IN LPCWSTR lpPrefixString, IN UINT uUnique, OUT LPWSTR lpTempFileName)
Definition: filename.c:84
_Check_return_ _CRTIMP wchar_t *__cdecl _wcsdup(_In_z_ const wchar_t *_Str)

◆ _wtmpnam()

wchar_t *CDECL _wtmpnam ( wchar_t s)

Definition at line 3882 of file file.c.

3883{
3884 static const wchar_t format[] = {'\\','s','%','s','.',0};
3885 wchar_t tmpstr[16];
3886 wchar_t *p;
3887 int count, size;
3888 if (!s) {
3890
3891 if(!data->wtmpnam_buffer)
3892 data->wtmpnam_buffer = malloc(sizeof(wchar_t[MAX_PATH]));
3893
3894 s = data->wtmpnam_buffer;
3895 }
3896
3898 p = s + _snwprintf(s, MAX_PATH, format, tmpstr);
3899 for (count = 0; count < TMP_MAX; count++)
3900 {
3902 memcpy(p, tmpstr, size*sizeof(wchar_t));
3903 p[size] = '\0';
3906 break;
3907 }
3908 return s;
3909}
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
Definition: gl.h:1950
GLint GLint GLsizei GLsizei GLsizei GLint GLenum format
Definition: gl.h:1546
#define TMP_MAX
Definition: stdio.h:67
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
int _snwprintf(wchar_t *buffer, size_t count, const wchar_t *format,...)
thread_data_t * msvcrt_get_thread_data(void)
Definition: tls.c:31
static int tmpnam_unique
Definition: file.c:141
static int msvcrt_int_to_base32_w(int num, wchar_t *str)
Definition: file.c:644

◆ _wunlink()

int CDECL _wunlink ( const wchar_t path)

Definition at line 815 of file file.c.

816{
817 TRACE("(%s)\n",debugstr_w(path));
818 if(DeleteFileW(path))
819 return 0;
820 TRACE("failed (%d)\n",GetLastError());
822 return -1;
823}

◆ add_std_buffer()

static BOOL add_std_buffer ( FILE file)
static

Definition at line 593 of file file.c.

594{
595 static char buffers[2][BUFSIZ];
596
597 if((file->_file!=STDOUT_FILENO && file->_file!=STDERR_FILENO)
598 || (file->_flag & (_IONBF | _IOMYBUF | _USERBUF))
599 || !_isatty(file->_file))
600 return FALSE;
601
602 file->_ptr = file->_base = buffers[file->_file == STDOUT_FILENO ? 0 : 1];
603 file->_bufsiz = file->_cnt = BUFSIZ;
604 file->_flag |= _USERBUF;
605 return TRUE;
606}
const GLuint * buffers
Definition: glext.h:5916
#define BUFSIZ
Definition: nsplookup.c:25
int CDECL _isatty(int fd)
Definition: file.c:564

Referenced by fputws().

◆ alloc_pioinfo_block()

static BOOL alloc_pioinfo_block ( int  fd)
inlinestatic

Definition at line 191 of file file.c.

192{
193 ioinfo *block;
194 int i;
195
196 if(fd<0 || fd>=MSVCRT_MAX_FILES)
197 {
198 *_errno() = ENFILE;
199 return FALSE;
200 }
201
203 if(!block)
204 {
205 WARN(":out of memory!\n");
206 *_errno() = ENOMEM;
207 return FALSE;
208 }
209 for(i=0; i<MSVCRT_FD_BLOCK_SIZE; i++)
212 free(block);
213 return TRUE;
214}
#define ENOMEM
Definition: acclib.h:84
#define ENFILE
Definition: errno.h:29
#define InterlockedCompareExchangePointer
Definition: interlocked.h:129
static ioinfo ** __pioinfo
Definition: file.c:47
#define MSVCRT_FD_BLOCK_SIZE
Definition: file.c:39
#define calloc
Definition: rosglue.h:14
static unsigned int block
Definition: xmlmemory.c:101

Referenced by get_ioinfo_alloc(), and get_ioinfo_alloc_fd().

◆ check_bom()

static int check_bom ( HANDLE  h,
int  oflags,
BOOL  seek 
)
static

Definition at line 1752 of file file.c.

1753{
1754 char bom[sizeof(utf8_bom)];
1755 DWORD r;
1756
1757 oflags &= ~(_O_WTEXT|_O_U16TEXT|_O_U8TEXT);
1758
1759 if (!ReadFile(h, bom, sizeof(utf8_bom), &r, NULL))
1760 return oflags;
1761
1762 if (r==sizeof(utf8_bom) && !memcmp(bom, utf8_bom, sizeof(utf8_bom))) {
1763 oflags |= _O_U8TEXT;
1764 }else if (r>=sizeof(utf16_bom) && !memcmp(bom, utf16_bom, sizeof(utf16_bom))) {
1765 if (seek && r>2)
1767 oflags |= _O_U16TEXT;
1768 }else if (seek) {
1770 }
1771
1772 return oflags;
1773}
int memcmp(void *Buffer1, void *Buffer2, ACPI_SIZE Count)
Definition: utclib.c:112
#define ReadFile(a, b, c, d, e)
Definition: compat.h:742
GLfloat GLfloat GLfloat GLfloat h
Definition: glext.h:7723
int seek(void *fd, ulong off, int mode)
Definition: pe.c:51

Referenced by _wsopen_s().

◆ clearerr()

void CDECL clearerr ( FILE file)

Definition at line 1363 of file file.c.

1364{
1365 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1366
1368 file->_flag &= ~(_IOERR | _IOEOF);
1370}

Referenced by rewind(), and ungetc().

◆ create_io_inherit_block()

unsigned create_io_inherit_block ( WORD size,
BYTE **  block 
)

Definition at line 411 of file file.c.

412{
413 int fd, last_fd;
414 char* wxflag_ptr;
415 HANDLE* handle_ptr;
416 ioinfo* fdinfo;
417
418 for (last_fd=MSVCRT_MAX_FILES-1; last_fd>=0; last_fd--)
420 break;
421 last_fd++;
422
423 *size = sizeof(unsigned) + (sizeof(char) + sizeof(HANDLE)) * last_fd;
424 *block = calloc(1, *size);
425 if (!*block)
426 {
427 *size = 0;
428 return FALSE;
429 }
430 wxflag_ptr = (char*)*block + sizeof(unsigned);
431 handle_ptr = (HANDLE*)(wxflag_ptr + last_fd);
432
433 *(unsigned*)*block = last_fd;
434 for (fd = 0; fd < last_fd; fd++)
435 {
436 /* to be inherited, we need it to be open, and that DONTINHERIT isn't set */
437 fdinfo = get_ioinfo_nolock(fd);
438 if ((fdinfo->wxflag & (WX_OPEN | WX_DONTINHERIT)) == WX_OPEN)
439 {
440 *wxflag_ptr = fdinfo->wxflag;
441 *handle_ptr = fdinfo->handle;
442 }
443 else
444 {
445 *wxflag_ptr = 0;
446 *handle_ptr = INVALID_HANDLE_VALUE;
447 }
448 wxflag_ptr++; handle_ptr++;
449 }
450 return TRUE;
451}
static unsigned(__cdecl *hash_bstr)(bstr_t s)
#define WX_DONTINHERIT
Definition: file.c:97
PVOID HANDLE
Definition: typedefs.h:73

◆ fclose()

int CDECL fclose ( FILE file)

Definition at line 2783 of file file.c.

2784{
2785 int r, flag;
2786
2787 if (!MSVCRT_CHECK_PMT(file != NULL)) return EOF;
2788
2790 flag = file->_flag;
2791 free(file->_tmpfname);
2792 file->_tmpfname = NULL;
2793 /* flush stdio buffers */
2794 if(file->_flag & _IOWRT)
2795 fflush(file);
2796 if(file->_flag & _IOMYBUF)
2797 free(file->_base);
2798
2799 r=_close(file->_file);
2800
2801 file->_flag = 0;
2805
2807 while(MSVCRT_stream_idx>3 && !file->_flag) {
2810 }
2811 }
2812
2813 return ((r == -1) || (flag & _IOERR) ? EOF : 0);
2814}
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean flag
Definition: glfuncs.h:52
_Check_return_opt_ _CRTIMP int __cdecl fflush(_Inout_opt_ FILE *_File)
void WINAPI DeleteCriticalSection(PCRITICAL_SECTION)

◆ feof()

int CDECL feof ( FILE file)

Definition at line 2819 of file file.c.

2820{
2821 return file->_flag & _IOEOF;
2822}

◆ ferror()

int CDECL ferror ( FILE file)

Definition at line 2827 of file file.c.

2828{
2829 return file->_flag & _IOERR;
2830}

◆ fflush()

int CDECL fflush ( FILE file)

Definition at line 901 of file file.c.

902{
903 if(!file) {
905 } else if(file->_flag & _IOWRT) {
906 int res;
907
910 /* FIXME
911 if(!res && (file->_flag & _IOCOMMIT))
912 res = _commit(file->_file) ? EOF : 0;
913 */
915
916 return res;
917 } else if(file->_flag & _IOREAD) {
919 file->_cnt = 0;
920 file->_ptr = file->_base;
922
923 return 0;
924 }
925 return 0;
926}
GLuint res
Definition: glext.h:9613

◆ fgetc()

int CDECL fgetc ( FILE file)

Definition at line 2888 of file file.c.

2889{
2890 unsigned char *i;
2891 unsigned int j;
2892
2894 if (file->_cnt>0) {
2895 file->_cnt--;
2896 i = (unsigned char *)file->_ptr++;
2897 j = *i;
2898 } else
2899 j = _filbuf(file);
2900
2902 return j;
2903}
_Check_return_ _CRTIMP int __cdecl _filbuf(_Inout_ FILE *_File)

◆ fgetpos()

int CDECL fgetpos ( FILE file,
fpos_t pos 
)

Definition at line 3574 of file file.c.

3575{
3576 *pos = _ftelli64(file);
3577 if(*pos == -1)
3578 return -1;
3579 return 0;
3580}

◆ fgets()

char *CDECL fgets ( char s,
int  size,
FILE file 
)

Definition at line 2916 of file file.c.

2917{
2918 int cc = EOF;
2919 char * buf_start = s;
2920
2921 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
2922 file,file->_file,s,size);
2923
2925
2926 while ((size >1) && (cc = fgetc(file)) != EOF && cc != '\n')
2927 {
2928 *s++ = (char)cc;
2929 size --;
2930 }
2931 if ((cc == EOF) && (s == buf_start)) /* If nothing read, return 0*/
2932 {
2933 TRACE(":nothing read\n");
2935 return NULL;
2936 }
2937 if ((cc != EOF) && (size > 1))
2938 *s++ = cc;
2939 *s = '\0';
2940 TRACE(":got %s\n", debugstr_a(buf_start));
2942 return buf_start;
2943}
unsigned char
Definition: typeof.h:29

◆ fgetwc()

wint_t CDECL fgetwc ( FILE file)

Definition at line 2948 of file file.c.

2949{
2950 wint_t ret;
2951 int ch;
2952
2954
2955 if((get_ioinfo_nolock(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
2956 || !(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
2957 char *p;
2958
2959 for(p=(char*)&ret; (wint_t*)p<&ret+1; p++) {
2960 ch = fgetc(file);
2961 if(ch == EOF) {
2962 ret = WEOF;
2963 break;
2964 }
2965 *p = (char)ch;
2966 }
2967 }else {
2968 char mbs[MB_LEN_MAX];
2969 int len = 0;
2970
2971 ch = fgetc(file);
2972 if(ch != EOF) {
2973 mbs[0] = (char)ch;
2974 if(isleadbyte((unsigned char)mbs[0])) {
2975 ch = fgetc(file);
2976 if(ch != EOF) {
2977 mbs[1] = (char)ch;
2978 len = 2;
2979 }
2980 }else {
2981 len = 1;
2982 }
2983 }
2984
2985 if(!len || mbtowc(&ret, mbs, len)==-1)
2986 ret = WEOF;
2987 }
2988
2990 return ret;
2991}
#define MB_LEN_MAX
Definition: limits.h:35
#define isleadbyte(_c)
Definition: wchar.h:596
#define mbtowc(wp, cp, len)
Definition: wchar.h:155
char mbs[5]

◆ fgetws()

wchar_t *CDECL fgetws ( wchar_t s,
int  size,
FILE file 
)

Definition at line 3045 of file file.c.

3046{
3047 int cc = WEOF;
3048 wchar_t * buf_start = s;
3049
3050 TRACE(":file(%p) fd (%d) str (%p) len (%d)\n",
3051 file,file->_file,s,size);
3052
3054
3055 while ((size >1) && (cc = fgetwc(file)) != WEOF && cc != '\n')
3056 {
3057 *s++ = (char)cc;
3058 size --;
3059 }
3060 if ((cc == WEOF) && (s == buf_start)) /* If nothing read, return 0*/
3061 {
3062 TRACE(":nothing read\n");
3064 return NULL;
3065 }
3066 if ((cc != WEOF) && (size > 1))
3067 *s++ = cc;
3068 *s = 0;
3069 TRACE(":got %s\n", debugstr_w(buf_start));
3071 return buf_start;
3072}

◆ fopen()

FILE *CDECL fopen ( const char path,
const char mode 
)

Definition at line 3249 of file file.c.

3250{
3251 return _fsopen( path, mode, _SH_DENYNO );
3252}
FILE *CDECL _fsopen(const char *path, const char *mode, int share)
Definition: file.c:3221

◆ fopen_s()

int CDECL fopen_s ( FILE **  pFile,
const char filename,
const char mode 
)

Definition at line 3257 of file file.c.

3259{
3260 if (!MSVCRT_CHECK_PMT(pFile != NULL)) return EINVAL;
3261 if (!MSVCRT_CHECK_PMT(filename != NULL)) return EINVAL;
3262 if (!MSVCRT_CHECK_PMT(mode != NULL)) return EINVAL;
3263
3264 *pFile = fopen(filename, mode);
3265
3266 if(!*pFile)
3267 return *_errno();
3268 return 0;
3269}
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)

Referenced by tinyxml2::callfopen(), CreateSpecFile(), and FileReporter::FileReporter().

◆ fputc()

int CDECL fputc ( int  c,
FILE file 
)

Definition at line 3304 of file file.c.

3305{
3306 int res;
3307
3309 if(file->_cnt>0) {
3310 *file->_ptr++=c;
3311 file->_cnt--;
3312 if (c == '\n')
3313 {
3316 return res ? res : c;
3317 }
3318 else {
3320 return c & 0xff;
3321 }
3322 } else {
3323 res = _flsbuf(c, file);
3325 return res;
3326 }
3327}
#define _flsbuf
Definition: _flswbuf.c:10

◆ fputs()

int CDECL fputs ( const char s,
FILE file 
)

Definition at line 3585 of file file.c.

3586{
3587 size_t len = strlen(s);
3588 int ret;
3589
3591 ret = fwrite(s, sizeof(*s), len, file) == len ? 0 : EOF;
3593 return ret;
3594}
ACPI_SIZE strlen(const char *String)
Definition: utclib.c:269

◆ fputwc()

wint_t CDECL fputwc ( wchar_t  c,
FILE stream 
)

Definition at line 3147 of file file.c.

3148{
3149 /* If this is a real file stream (and not some temporary one for
3150 sprintf-like functions), check whether it is opened in text mode.
3151 In this case, we have to perform an implicit conversion to ANSI. */
3152 if (!(stream->_flag & _IOSTRG) && get_ioinfo_nolock(stream->_file)->wxflag & WX_TEXT)
3153 {
3154 /* Convert to multibyte in text mode */
3155 char mbc[MB_LEN_MAX];
3156 int mb_return;
3157
3158 mb_return = wctomb(mbc, c);
3159
3160 if(mb_return == -1)
3161 return WEOF;
3162
3163 /* Output all characters */
3164 if (fwrite(mbc, mb_return, 1, stream) != 1)
3165 return WEOF;
3166 }
3167 else
3168 {
3169 if (fwrite(&c, sizeof(c), 1, stream) != 1)
3170 return WEOF;
3171 }
3172
3173 return c;
3174}
#define wctomb(cp, wc)
Definition: wchar.h:161
char mbc

◆ fputws()

int CDECL fputws ( const wchar_t s,
FILE file 
)

Definition at line 3599 of file file.c.

3600{
3601 size_t i, len = strlenW(s);
3602 BOOL tmp_buf;
3603 int ret;
3604
3606 if (!(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
3607 ret = fwrite(s,sizeof(*s),len,file) == len ? 0 : EOF;
3609 return ret;
3610 }
3611
3612 tmp_buf = add_std_buffer(file);
3613 for (i=0; i<len; i++) {
3614 if(fputwc(s[i], file) == WEOF) {
3615 if(tmp_buf) remove_std_buffer(file);
3617 return WEOF;
3618 }
3619 }
3620
3621 if(tmp_buf) remove_std_buffer(file);
3623 return 0;
3624}
static void remove_std_buffer(FILE *file)
Definition: file.c:610
static BOOL add_std_buffer(FILE *file)
Definition: file.c:593

◆ fread()

size_t CDECL fread ( void ptr,
size_t  size,
size_t  nmemb,
FILE file 
)

Definition at line 3340 of file file.c.

3341{
3342 size_t rcnt=size * nmemb;
3343 size_t read=0;
3344 size_t pread=0;
3345
3346 if(!rcnt)
3347 return 0;
3348
3350
3351 /* first buffered data */
3352 if(file->_cnt>0) {
3353 int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt;
3354 memcpy(ptr, file->_ptr, pcnt);
3355 file->_cnt -= pcnt;
3356 file->_ptr += pcnt;
3357 read += pcnt ;
3358 rcnt -= pcnt ;
3359 ptr = (char*)ptr + pcnt;
3360 } else if(!(file->_flag & _IOREAD )) {
3361 if(file->_flag & _IORW) {
3362 file->_flag |= _IOREAD;
3363 } else {
3365 return 0;
3366 }
3367 }
3368
3369 if(rcnt>0 && !(file->_flag & (_IONBF | _IOMYBUF | _USERBUF)))
3371
3372 while(rcnt>0)
3373 {
3374 int i;
3375 if (!file->_cnt && rcnt<BUFSIZ && (file->_flag & (_IOMYBUF | _USERBUF))) {
3376 file->_cnt = _read(file->_file, file->_base, file->_bufsiz);
3377 file->_ptr = file->_base;
3378 i = (file->_cnt<rcnt) ? file->_cnt : rcnt;
3379 /* If the buffer fill reaches eof but fread wouldn't, clear eof. */
3380 if (i > 0 && i < file->_cnt) {
3381 get_ioinfo_nolock(file->_file)->wxflag &= ~WX_ATEOF;
3382 file->_flag &= ~_IOEOF;
3383 }
3384 if (i > 0) {
3385 memcpy(ptr, file->_ptr, i);
3386 file->_cnt -= i;
3387 file->_ptr += i;
3388 }
3389 } else if (rcnt > INT_MAX) {
3390 i = _read(file->_file, ptr, INT_MAX);
3391 } else if (rcnt < BUFSIZ) {
3392 i = _read(file->_file, ptr, rcnt);
3393 } else {
3394 i = _read(file->_file, ptr, rcnt - BUFSIZ/2);
3395 }
3396 pread += i;
3397 rcnt -= i;
3398 ptr = (char *)ptr+i;
3399 /* expose feof condition in the flags
3400 * MFC tests file->_flag for feof, and doesn't call feof())
3401 */
3402 if (get_ioinfo_nolock(file->_file)->wxflag & WX_ATEOF)
3403 file->_flag |= _IOEOF;
3404 else if (i == -1)
3405 {
3406 file->_flag |= _IOERR;
3407 pread = 0;
3408 rcnt = 0;
3409 }
3410 if (i < 1) break;
3411 }
3412 read+=pread;
3414 return read / size;
3415}
#define read
Definition: acwin.h:96
#define INT_MAX
Definition: limits.h:40
static PVOID ptr
Definition: dispmode.c:27
ssize_t pread(int fd, void *buf, size_t count, off_t offset)

◆ freopen()

FILE *CDECL freopen ( const char path,
const char mode,
FILE file 
)

Definition at line 3458 of file file.c.

3459{
3460 FILE *ret;
3461 wchar_t *pathW = NULL, *modeW = NULL;
3462
3463 if (path && !(pathW = msvcrt_wstrdupa(path))) return NULL;
3464 if (mode && !(modeW = msvcrt_wstrdupa(mode)))
3465 {
3466 free(pathW);
3467 return NULL;
3468 }
3469
3470 ret = _wfreopen(pathW, modeW, file);
3471
3472 free(pathW);
3473 free(modeW);
3474 return ret;
3475}
FILE *CDECL _wfreopen(const wchar_t *path, const wchar_t *mode, FILE *file)
Definition: file.c:3421

◆ fseek()

int CDECL fseek ( FILE file,
long  offset,
int  whence 
)

Definition at line 1310 of file file.c.

1311{
1312 return _fseeki64( file, offset, whence );
1313}
int CDECL _fseeki64(FILE *file, __int64 offset, int whence)
Definition: file.c:1278

◆ fsetpos()

int CDECL fsetpos ( FILE file,
const fpos_t pos 
)

Definition at line 3480 of file file.c.

3481{
3482 int ret;
3483
3485 /* Note that all this has been lifted 'as is' from fseek */
3486 if(file->_flag & _IOWRT)
3488
3489 /* Discard buffered input */
3490 file->_cnt = 0;
3491 file->_ptr = file->_base;
3492
3493 /* Reset direction of i/o */
3494 if(file->_flag & _IORW) {
3495 file->_flag &= ~(_IOREAD|_IOWRT);
3496 }
3497
3498 ret = (_lseeki64(file->_file,*pos,SEEK_SET) == -1) ? -1 : 0;
3500 return ret;
3501}

◆ ftell()

LONG CDECL ftell ( FILE file)

Definition at line 3566 of file file.c.

3567{
3568 return (LONG)_ftelli64(file);
3569}

◆ fwrite()

size_t CDECL fwrite ( const void ptr,
size_t  size,
size_t  nmemb,
FILE file 
)

Definition at line 3077 of file file.c.

3078{
3079 size_t wrcnt=size * nmemb;
3080 int written = 0;
3081 if (size == 0)
3082 return 0;
3083
3085
3086 while(wrcnt) {
3087#ifndef __REACTOS__
3088 if(file->_cnt < 0) {
3089 WARN("negative file->_cnt value in %p\n", file);
3090 file->_flag |= MSVCRT__IOERR;
3091 break;
3092 } else
3093#endif
3094 if(file->_cnt) {
3095 int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt;
3096 memcpy(file->_ptr, ptr, pcnt);
3097 file->_cnt -= pcnt;
3098 file->_ptr += pcnt;
3099 written += pcnt;
3100 wrcnt -= pcnt;
3101 ptr = (const char*)ptr + pcnt;
3102 } else if((file->_flag & _IONBF)
3103 || ((file->_flag & (_IOMYBUF | _USERBUF)) && wrcnt >= file->_bufsiz)
3104 || (!(file->_flag & (_IOMYBUF | _USERBUF)) && wrcnt >= MSVCRT_INTERNAL_BUFSIZ)) {
3105 size_t pcnt;
3106 int bufsiz;
3107
3108 if(file->_flag & _IONBF)
3109 bufsiz = 1;
3110 else if(!(file->_flag & (_IOMYBUF | _USERBUF)))
3111 bufsiz = MSVCRT_INTERNAL_BUFSIZ;
3112 else
3113 bufsiz = file->_bufsiz;
3114
3115 pcnt = (wrcnt / bufsiz) * bufsiz;
3116
3118 break;
3119
3120 if(_write(file->_file, ptr, pcnt) <= 0) {
3121 file->_flag |= _IOERR;
3122 break;
3123 }
3124 written += pcnt;
3125 wrcnt -= pcnt;
3126 ptr = (const char*)ptr + pcnt;
3127 } else {
3128 if(_flsbuf(*(const char*)ptr, file) == EOF)
3129 break;
3130 written++;
3131 wrcnt--;
3132 ptr = (const char*)ptr + 1;
3133 }
3134 }
3135
3137 return written / size;
3138}
#define MSVCRT__IOERR
Definition: msvcrt.h:690
#define MSVCRT_INTERNAL_BUFSIZ
Definition: file.c:115

◆ get_ioinfo()

ioinfo * get_ioinfo ( int  fd)

Definition at line 181 of file file.c.

182{
184 if(ret == &__badioinfo)
185 return ret;
188 return ret;
189}
static void init_ioinfo_cs(ioinfo *info)
Definition: file.c:169

Referenced by _chsize_s(), _close(), _commit(), _dup2(), _eof(), _fstat64(), _futime(), _locking(), _lseeki64(), _read(), _setmode(), _write(), get_ioinfo_alloc_fd(), and msvcrt_free_fd().

◆ get_ioinfo_alloc()

static ioinfo * get_ioinfo_alloc ( int fd)
inlinestatic

Definition at line 230 of file file.c.

231{
232 int i;
233
234 *fd = -1;
235 for(i=0; i<MSVCRT_MAX_FILES; i++)
236 {
238
239 if(info == &__badioinfo)
240 {
242 return &__badioinfo;
244 }
245
247 if(TryEnterCriticalSection(&info->crit))
248 {
249 if(info->handle == INVALID_HANDLE_VALUE)
250 {
251 *fd = i;
252 return info;
253 }
255 }
256 }
257
258 WARN(":files exhausted!\n");
259 *_errno() = ENFILE;
260 return &__badioinfo;
261}
static BOOL alloc_pioinfo_block(int fd)
Definition: file.c:191
BOOL WINAPI TryEnterCriticalSection(LPCRITICAL_SECTION)

Referenced by _dup(), and msvcrt_alloc_fd().

◆ get_ioinfo_alloc_fd()

static ioinfo * get_ioinfo_alloc_fd ( int  fd)
inlinestatic

Definition at line 216 of file file.c.

217{
218 ioinfo *ret;
219
220 ret = get_ioinfo(fd);
221 if(ret != &__badioinfo)
222 return ret;
223
225 return &__badioinfo;
226
227 return get_ioinfo(fd);
228}

Referenced by _dup2(), and msvcrt_init_io().

◆ get_ioinfo_nolock()

static ioinfo * get_ioinfo_nolock ( int  fd)
inlinestatic

◆ get_utf8_char_len()

static int get_utf8_char_len ( char  ch)
inlinestatic

Definition at line 2116 of file file.c.

2117{
2118 if((ch&0xf8) == 0xf0)
2119 return 4;
2120 else if((ch&0xf0) == 0xe0)
2121 return 3;
2122 else if((ch&0xe0) == 0xc0)
2123 return 2;
2124 return 1;
2125}

Referenced by read_utf8().

◆ getc()

int CDECL getc ( FILE file)

Definition at line 3637 of file file.c.

3638{
3639 return fgetc(file);
3640}

◆ getchar()

int CDECL getchar ( void  )

Definition at line 3629 of file file.c.

3630{
3631 return fgetc(stdin);
3632}

Referenced by FSE_normalizeCount(), get_key(), macdef(), main(), Main(), and printWindowsError().

◆ gets()

char *CDECL gets ( char buf)

Definition at line 3645 of file file.c.

3646{
3647 int cc;
3648 char * buf_start = buf;
3649
3651 for(cc = fgetc(stdin); cc != EOF && cc != '\n';
3652 cc = fgetc(stdin))
3653 if(cc != '\r') *buf++ = (char)cc;
3654
3655 *buf = '\0';
3656
3657 TRACE("got '%s'\n", buf_start);
3659 return buf_start;
3660}

Referenced by cd(), cmdscanner(), confirm(), delete(), do_chmod(), domacro(), doproxy(), getit(), macdef(), main(), makedir(), mdelete(), mget(), mls(), modtime(), mput(), put(), quote(), removedir(), renamefile(), setnmap(), setpeer(), site(), sizecmd(), and user().

◆ getwc()

wint_t CDECL getwc ( FILE file)

Definition at line 3021 of file file.c.

3022{
3023 return fgetwc(file);
3024}

◆ getwchar()

wint_t CDECL getwchar ( void  )

Definition at line 3037 of file file.c.

3038{
3039 return _fgetwchar();
3040}
wint_t CDECL _fgetwchar(void)
Definition: file.c:3029

◆ init_ioinfo_cs()

static void init_ioinfo_cs ( ioinfo info)
inlinestatic

Definition at line 169 of file file.c.

170{
171 if(!(info->exflag & EF_CRIT_INIT)) {
172 LOCK_FILES();
173 if(!(info->exflag & EF_CRIT_INIT)) {
175 info->exflag |= EF_CRIT_INIT;
176 }
177 UNLOCK_FILES();
178 }
179}
#define EF_CRIT_INIT
Definition: file.c:105
VOID WINAPI InitializeCriticalSection(OUT LPCRITICAL_SECTION lpCriticalSection)
Definition: synch.c:751

Referenced by get_ioinfo(), and get_ioinfo_alloc().

◆ msvcrt_alloc_buffer()

BOOL msvcrt_alloc_buffer ( FILE file)

Definition at line 572 of file file.c.

573{
574 if((file->_file==STDOUT_FILENO || file->_file==STDERR_FILENO)
575 && _isatty(file->_file))
576 return FALSE;
577
579 if(file->_base) {
580 file->_bufsiz = MSVCRT_INTERNAL_BUFSIZ;
581 file->_flag |= _IOMYBUF;
582 } else {
583 file->_base = (char*)(&file->_charbuf);
584 file->_bufsiz = 2;
585 file->_flag |= _IONBF;
586 }
587 file->_ptr = file->_base;
588 file->_cnt = 0;
589 return TRUE;
590}

Referenced by _filbuf(), _flsbuf(), fread(), and ungetc().

◆ msvcrt_alloc_fd()

int msvcrt_alloc_fd ( HANDLE  hand,
int  flag 
)

Definition at line 343 of file file.c.

344{
345 int fd;
347
348 TRACE(":handle (%p) allocating fd (%d)\n", hand, fd);
349
350 if(info == &__badioinfo)
351 return -1;
352
353 msvcrt_set_fd(info, hand, flag);
355 return fd;
356}

Referenced by _open_osfhandle(), _pipe(), _tpopen(), and _wsopen_s().

◆ msvcrt_alloc_fp()

static FILE * msvcrt_alloc_fp ( void  )
static

Definition at line 360 of file file.c.

361{
362 int i;
363 FILE *file;
364
365 for (i = 3; i < MSVCRT_max_streams; i++)
366 {
368 if (!file)
369 return NULL;
370
371 if (file->_flag == 0)
372 {
374 return file;
375 }
376 }
377
378 return NULL;
379}

Referenced by _wfdopen(), _wfsopen(), and tmpfile().

◆ msvcrt_flush_all_buffers()

static int msvcrt_flush_all_buffers ( int  mask)
static

Definition at line 867 of file file.c.

868{
869 int i, num_flushed = 0;
870 FILE *file;
871
872 LOCK_FILES();
873 for (i = 0; i < MSVCRT_stream_idx; i++) {
875
876 if (file->_flag)
877 {
878 if(file->_flag & mask) {
879 fflush(file);
880 num_flushed++;
881 }
882 }
883 }
884 UNLOCK_FILES();
885
886 TRACE(":flushed (%d) handles\n",num_flushed);
887 return num_flushed;
888}
GLenum GLint GLuint mask
Definition: glext.h:6028

Referenced by _flushall(), and fflush().

◆ msvcrt_flush_buffer()

static int msvcrt_flush_buffer ( FILE file)
static

Definition at line 535 of file file.c.

536{
537 if((file->_flag & (_IOREAD|_IOWRT)) == _IOWRT &&
538 file->_flag & (_IOMYBUF|_USERBUF)) {
539 int cnt=file->_ptr-file->_base;
540 if(cnt>0 && _write(file->_file, file->_base, cnt) != cnt) {
541 file->_flag |= _IOERR;
542 return EOF;
543 }
544
545 if(file->_flag & _IORW)
546 file->_flag &= ~_IOWRT;
547
548#ifdef __REACTOS__ /* CORE-11949 */
549 file->_ptr=file->_base;
550 file->_cnt=0;
551#endif
552 }
553
554#ifndef __REACTOS__ /* CORE-11949 */
555 file->_ptr=file->_base;
556 file->_cnt=0;
557#endif
558 return 0;
559}

Referenced by _fseeki64(), fflush(), fputc(), fsetpos(), fwrite(), and remove_std_buffer().

◆ msvcrt_free_fd()

static void msvcrt_free_fd ( int  fd)
static

Definition at line 296 of file file.c.

297{
298 ioinfo *fdinfo = get_ioinfo(fd);
299
300 if(fdinfo != &__badioinfo)
301 {
303 fdinfo->wxflag = 0;
304 }
305 TRACE(":fd (%d) freed\n",fd);
306
307 if (fd < 3)
308 {
309 switch (fd)
310 {
311 case 0:
313 break;
314 case 1:
316 break;
317 case 2:
319 break;
320 }
321 }
322 release_ioinfo(fdinfo);
323}
BOOL WINAPI DECLSPEC_HOTPATCH SetStdHandle(DWORD nStdHandle, HANDLE hHandle)
Definition: console.c:1213
#define STD_OUTPUT_HANDLE
Definition: winbase.h:268
#define STD_INPUT_HANDLE
Definition: winbase.h:267
#define STD_ERROR_HANDLE
Definition: winbase.h:269

Referenced by _close().

◆ msvcrt_free_io()

void msvcrt_free_io ( void  )

Definition at line 1100 of file file.c.

1101{
1102 unsigned int i;
1103 int j;
1104
1105 _flushall();
1106 _fcloseall();
1107
1108 for(i=0; i<sizeof(__pioinfo)/sizeof(__pioinfo[0]); i++)
1109 {
1110 if(!__pioinfo[i])
1111 continue;
1112
1113 for(j=0; j<MSVCRT_FD_BLOCK_SIZE; j++)
1114 {
1115 if(__pioinfo[i][j].exflag & EF_CRIT_INIT)
1117 }
1118 free(__pioinfo[i]);
1119 }
1120
1121 for(j=0; j<MSVCRT_stream_idx; j++)
1122 {
1125 {
1126 ((file_crit*)file)->crit.DebugInfo->Spare[0] = 0;
1128 }
1129 }
1130
1131 for(i=0; i<sizeof(MSVCRT_fstream)/sizeof(MSVCRT_fstream[0]); i++)
1133}
_Check_return_opt_ _CRTIMP int __cdecl _fcloseall(void)
Definition: file.c:1081
_Check_return_opt_ _CRTIMP int __cdecl _flushall(void)
Definition: file.c:893
static file_crit * MSVCRT_fstream[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE]
Definition: file.c:134

Referenced by DllMain(), and msvcrt_init_exception().

◆ msvcrt_get_file()

static FILE * msvcrt_get_file ( int  i)
inlinestatic

Definition at line 269 of file file.c.

270{
271 file_crit *ret;
272
273 if(i >= MSVCRT_max_streams)
274 return NULL;
275
276 if(i < _IOB_ENTRIES)
277 return &_iob[i];
278
280 if(!ret) {
283 ERR("out of memory\n");
284 *_errno() = ENOMEM;
285 return NULL;
286 }
287
289 } else
291
292 return &ret->file;
293}

Referenced by _fcloseall(), _rmtmp(), fclose(), msvcrt_alloc_fp(), msvcrt_flush_all_buffers(), and msvcrt_free_io().

◆ msvcrt_get_flags()

static int msvcrt_get_flags ( const wchar_t mode,
int open_flags,
int stream_flags 
)
static

Definition at line 1385 of file file.c.

1386{
1387 int plus = strchrW(mode, '+') != NULL;
1388
1389 TRACE("%s\n", debugstr_w(mode));
1390
1391 while(*mode == ' ') mode++;
1392
1393 switch(*mode++)
1394 {
1395 case 'R': case 'r':
1396 *open_flags = plus ? _O_RDWR : _O_RDONLY;
1397 *stream_flags = plus ? _IORW : _IOREAD;
1398 break;
1399 case 'W': case 'w':
1400 *open_flags = _O_CREAT | _O_TRUNC | (plus ? _O_RDWR : _O_WRONLY);
1401 *stream_flags = plus ? _IORW : _IOWRT;
1402 break;
1403 case 'A': case 'a':
1404 *open_flags = _O_CREAT | _O_APPEND | (plus ? _O_RDWR : _O_WRONLY);
1405 *stream_flags = plus ? _IORW : _IOWRT;
1406 break;
1407 default:
1409 return -1;
1410 }
1411
1412 *stream_flags |= _commode;
1413
1414 while (*mode && *mode!=',')
1415 switch (*mode++)
1416 {
1417 case 'B': case 'b':
1418 *open_flags |= _O_BINARY;
1419 *open_flags &= ~_O_TEXT;
1420 break;
1421 case 't':
1422 *open_flags |= _O_TEXT;
1423 *open_flags &= ~_O_BINARY;
1424 break;
1425 case 'D':
1426 *open_flags |= _O_TEMPORARY;
1427 break;
1428 case 'T':
1429 *open_flags |= _O_SHORT_LIVED;
1430 break;
1431 case 'c':
1432 *stream_flags |= _IOCOMMIT;
1433 break;
1434 case 'n':
1435 *stream_flags &= ~_IOCOMMIT;
1436 break;
1437 case 'N':
1438 *open_flags |= _O_NOINHERIT;
1439 break;
1440 case '+':
1441 case ' ':
1442 case 'a':
1443 case 'w':
1444 break;
1445 case 'S':
1446 case 'R':
1447 FIXME("ignoring cache optimization flag: %c\n", mode[-1]);
1448 break;
1449 default:
1450 ERR("incorrect mode flag: %c\n", mode[-1]);
1451 break;
1452 }
1453
1454 if(*mode == ',')
1455 {
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'};
1460
1461 mode++;
1462 while(*mode == ' ') mode++;
1463 if(!MSVCRT_CHECK_PMT(!strncmpW(ccs, mode, sizeof(ccs)/sizeof(ccs[0]))))
1464 return -1;
1465 mode += sizeof(ccs)/sizeof(ccs[0]);
1466 while(*mode == ' ') mode++;
1467 if(!MSVCRT_CHECK_PMT(*mode == '='))
1468 return -1;
1469 mode++;
1470 while(*mode == ' ') mode++;
1471
1472 if(!strncmpiW(utf8, mode, sizeof(utf8)/sizeof(utf8[0])))
1473 {
1474 *open_flags |= _O_U8TEXT;
1475 mode += sizeof(utf8)/sizeof(utf8[0]);
1476 }
1477 else if(!strncmpiW(utf16le, mode, sizeof(utf16le)/sizeof(utf16le[0])))
1478 {
1479 *open_flags |= _O_U16TEXT;
1480 mode += sizeof(utf16le)/sizeof(utf16le[0]);
1481 }
1482 else if(!strncmpiW(unicode, mode, sizeof(unicode)/sizeof(unicode[0])))
1483 {
1484 *open_flags |= _O_WTEXT;
1485 mode += sizeof(unicode)/sizeof(unicode[0]);
1486 }
1487 else
1488 {
1490 return -1;
1491 }
1492
1493 while(*mode == ' ') mode++;
1494 }
1495
1496 if(!MSVCRT_CHECK_PMT(*mode == 0))
1497 return -1;
1498 return 0;
1499}
#define _O_SHORT_LIVED
Definition: cabinet.h:49
#define _O_APPEND
Definition: cabinet.h:41
#define MSVCRT_INVALID_PMT(x)
Definition: mbstowcs_s.c:25
#define strncmpiW(s1, s2, n)
Definition: unicode.h:40
#define strchrW(s, c)
Definition: unicode.h:34
#define strncmpW(s1, s2, n)
Definition: unicode.h:36
int _commode
Definition: environ.c:31
#define _IOCOMMIT
Definition: file.c:59

Referenced by _wfdopen(), _wfreopen(), and _wfsopen().

◆ msvcrt_init_fp()

static int msvcrt_init_fp ( FILE file,
int  fd,
unsigned  stream_flags 
)
static

Definition at line 382 of file file.c.

383{
384 TRACE(":fd (%d) allocating FILE*\n",fd);
385 if (!(get_ioinfo_nolock(fd)->wxflag & WX_OPEN))
386 {
387 WARN(":invalid fd %d\n",fd);
388 *__doserrno() = 0;
389 *_errno() = EBADF;
390 return -1;
391 }
392 file->_ptr = file->_base = NULL;
393 file->_cnt = 0;
394 file->_file = fd;
395 file->_flag = stream_flags;
396 file->_tmpfname = NULL;
397
400
401 TRACE(":got FILE* (%p)\n",file);
402 return 0;
403}
_CRTIMP unsigned long *__cdecl __doserrno(void)
Definition: errno.c:25

Referenced by _wfdopen(), _wfreopen(), _wfsopen(), and tmpfile().

◆ msvcrt_init_io()

void msvcrt_init_io ( void  )

Definition at line 456 of file file.c.

457{
458 STARTUPINFOA si;
459 unsigned int i;
460 ioinfo *fdinfo;
461
462 GetStartupInfoA(&si);
463 if (si.cbReserved2 >= sizeof(unsigned int) && si.lpReserved2 != NULL)
464 {
465 BYTE* wxflag_ptr;
466 HANDLE* handle_ptr;
467 unsigned int count;
468
469 count = *(unsigned*)si.lpReserved2;
470 wxflag_ptr = si.lpReserved2 + sizeof(unsigned);
471 handle_ptr = (HANDLE*)(wxflag_ptr + count);
472
473 count = min(count, (si.cbReserved2 - sizeof(unsigned)) / (sizeof(HANDLE) + 1));
475 for (i = 0; i < count; i++)
476 {
477 if ((*wxflag_ptr & WX_OPEN) && GetFileType(*handle_ptr) != FILE_TYPE_UNKNOWN)
478 {
479 fdinfo = get_ioinfo_alloc_fd(i);
480 if(fdinfo != &__badioinfo)
481 msvcrt_set_fd(fdinfo, *handle_ptr, *wxflag_ptr);
482 release_ioinfo(fdinfo);
483 }
484
485 wxflag_ptr++; handle_ptr++;
486 }
487 }
488
490 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
493
494 msvcrt_set_fd(fdinfo, h, WX_OPEN|WX_TEXT|((type&0xf)==FILE_TYPE_CHAR ? WX_TTY : 0)
495 |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0));
496 }
497 release_ioinfo(fdinfo);
498
500 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
503
504 msvcrt_set_fd(fdinfo, h, WX_OPEN|WX_TEXT|((type&0xf)==FILE_TYPE_CHAR ? WX_TTY : 0)
505 |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0));
506 }
507 release_ioinfo(fdinfo);
508
510 if (!(fdinfo->wxflag & WX_OPEN) || fdinfo->handle == INVALID_HANDLE_VALUE) {
513
514 msvcrt_set_fd(fdinfo, h, WX_OPEN|WX_TEXT|((type&0xf)==FILE_TYPE_CHAR ? WX_TTY : 0)
515 |((type&0xf)==FILE_TYPE_PIPE ? WX_PIPE : 0));
516 }
517 release_ioinfo(fdinfo);
518
519 TRACE(":handles (%p)(%p)(%p)\n", get_ioinfo_nolock(STDIN_FILENO)->handle,
522
523 memset(_iob,0,3*sizeof(FILE));
524 for (i = 0; i < 3; i++)
525 {
526 /* FILE structs for stdin/out/err are static and never deleted */
527 _iob[i]._file = i;
528 _iob[i]._tmpfname = NULL;
529 _iob[i]._flag = (i == 0) ? _IOREAD : _IOWRT;
530 }
532}
HANDLE WINAPI GetStdHandle(IN DWORD nStdHandle)
Definition: console.c:203
VOID WINAPI GetStartupInfoA(IN LPSTARTUPINFOA lpStartupInfo)
Definition: proc.c:1320
#define min(a, b)
Definition: monoChain.cc:55
#define STDIN_FILENO
Definition: syshdrs.h:88
#define memset(x, y, z)
Definition: compat.h:39
WORD cbReserved2
Definition: winbase.h:844
PBYTE lpReserved2
Definition: winbase.h:845
int _file
Definition: mbstring.h:24
int _flag
Definition: mbstring.h:23
char * _tmpfname
Definition: mbstring.h:27
unsigned char BYTE
Definition: xxhash.c:193

Referenced by crt_process_init(), and DllMain().

◆ msvcrt_int_to_base32()

static int msvcrt_int_to_base32 ( int  num,
char str 
)
static

Definition at line 619 of file file.c.

620{
621 char *p;
622 int n = num;
623 int digits = 0;
624
625 while (n != 0)
626 {
627 n >>= 5;
628 digits++;
629 }
630 p = str + digits;
631 *p = 0;
632 while (--p >= str)
633 {
634 *p = (num & 31) + '0';
635 if (*p > '9')
636 *p += ('a' - '0' - 10);
637 num >>= 5;
638 }
639
640 return digits;
641}
GLdouble n
Definition: glext.h:7729
GLuint GLuint num
Definition: glext.h:9618
static const int digits[]
Definition: decode.c:71
const WCHAR * str

Referenced by tmpnam().

◆ msvcrt_int_to_base32_w()

static int msvcrt_int_to_base32_w ( int  num,
wchar_t str 
)
static

Definition at line 644 of file file.c.

645{
646 wchar_t *p;
647 int n = num;
648 int digits = 0;
649
650 while (n != 0)
651 {
652 n >>= 5;
653 digits++;
654 }
655 p = str + digits;
656 *p = 0;
657 while (--p >= str)
658 {
659 *p = (num & 31) + '0';
660 if (*p > '9')
661 *p += ('a' - '0' - 10);
662 num >>= 5;
663 }
664
665 return digits;
666}

Referenced by _wtmpnam().

◆ msvcrt_set_fd()

static void msvcrt_set_fd ( ioinfo fdinfo,
HANDLE  hand,
int  flag 
)
static

Definition at line 325 of file file.c.

326{
327 fdinfo->handle = hand;
329 fdinfo->lookahead[0] = '\n';
330 fdinfo->lookahead[1] = '\n';
331 fdinfo->lookahead[2] = '\n';
332 fdinfo->exflag &= EF_CRIT_INIT;
333
334 switch (fdinfo-__pioinfo[0])
335 {
336 case 0: SetStdHandle(STD_INPUT_HANDLE, hand); break;
337 case 1: SetStdHandle(STD_OUTPUT_HANDLE, hand); break;
338 case 2: SetStdHandle(STD_ERROR_HANDLE, hand); break;
339 }
340}
char lookahead[3]
Definition: file.c:43

Referenced by _dup2(), msvcrt_alloc_fd(), and msvcrt_init_io().

◆ msvcrt_wstrdupa()

wchar_t * msvcrt_wstrdupa ( const char str)

Definition at line 669 of file file.c.

670{
671 const unsigned int len = strlen(str) + 1 ;
672 wchar_t *wstr = malloc(len* sizeof (wchar_t));
673 if (!wstr)
674 return NULL;
676 return wstr;
677}
#define CP_ACP
Definition: compat.h:109
#define MultiByteToWideChar
Definition: compat.h:110
#define MB_PRECOMPOSED
Definition: winnls.h:281

Referenced by _fdopen(), _fsopen(), _sopen_s(), and freopen().

◆ putc()

int CDECL putc ( int  c,
FILE file 
)

Definition at line 3687 of file file.c.

3688{
3689 return fputc(c, file);
3690}

◆ putchar()

int CDECL putchar ( int  c)

Definition at line 3695 of file file.c.

3696{
3697 return fputc(c, stdout);
3698}

◆ puts()

int CDECL puts ( const char s)

Definition at line 3711 of file file.c.

3712{
3713 size_t len = strlen(s);
3714 int ret;
3715
3717 if(fwrite(s, sizeof(*s), len, stdout) != len) {
3719 return EOF;
3720 }
3721
3722 ret = fwrite("\n",1,1,stdout) == 1 ? 0 : EOF;
3724 return ret;
3725}

◆ read_i()

static int read_i ( int  fd,
ioinfo fdinfo,
void buf,
unsigned int  count 
)
static

Definition at line 2306 of file file.c.

2307{
2308 DWORD num_read, utf16;
2309 char *bufstart = buf;
2310
2311 if (count == 0)
2312 return 0;
2313
2314 if (fdinfo->wxflag & WX_ATEOF) {
2315 TRACE("already at EOF, returning 0\n");
2316 return 0;
2317 }
2318 /* Don't trace small reads, it gets *very* annoying */
2319 if (count > 4)
2320 TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n", fd, fdinfo->handle, buf, count);
2321 if (fdinfo->handle == INVALID_HANDLE_VALUE)
2322 {
2323 *_errno() = EBADF;
2324 return -1;
2325 }
2326
2327 utf16 = (fdinfo->exflag & EF_UTF16) != 0;
2328 if (((fdinfo->exflag&EF_UTF8) || utf16) && count&1)
2329 {
2330 *_errno() = EINVAL;
2331 return -1;
2332 }
2333
2334 if((fdinfo->wxflag&WX_TEXT) && (fdinfo->exflag&EF_UTF8))
2335 return read_utf8(fdinfo, buf, count);
2336
2337 if (fdinfo->lookahead[0]!='\n' || ReadFile(fdinfo->handle, bufstart, count, &num_read, NULL))
2338 {
2339 if (fdinfo->lookahead[0] != '\n')
2340 {
2341 bufstart[0] = fdinfo->lookahead[0];
2342 fdinfo->lookahead[0] = '\n';
2343
2344 if (utf16)
2345 {
2346 bufstart[1] = fdinfo->lookahead[1];
2347 fdinfo->lookahead[1] = '\n';
2348 }
2349
2350 if(count>1+utf16 && ReadFile(fdinfo->handle, bufstart+1+utf16, count-1-utf16, &num_read, NULL))
2351 num_read += 1+utf16;
2352 else
2353 num_read = 1+utf16;
2354 }
2355
2356 if(utf16 && (num_read&1))
2357 {
2358 /* msvcr90 uses uninitialized value from the buffer in this case */
2359 /* msvcrt ignores additional data */
2360 ERR("got odd number of bytes in UTF16 mode\n");
2361 num_read--;
2362 }
2363
2364 if (count != 0 && num_read == 0)
2365 {
2366 fdinfo->wxflag |= WX_ATEOF;
2367 TRACE(":EOF %s\n",debugstr_an(buf,num_read));
2368 }
2369 else if (fdinfo->wxflag & WX_TEXT)
2370 {
2371 DWORD i, j;
2372
2373 if (bufstart[0]=='\n' && (!utf16 || bufstart[1]==0))
2374 fdinfo->wxflag |= WX_READNL;
2375 else
2376 fdinfo->wxflag &= ~WX_READNL;
2377
2378 for (i=0, j=0; i<num_read; i+=1+utf16)
2379 {
2380 /* in text mode, a ctrl-z signals EOF */
2381 if (bufstart[i]==0x1a && (!utf16 || bufstart[i+1]==0))
2382 {
2383 fdinfo->wxflag |= WX_ATEOF;
2384 TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
2385 break;
2386 }
2387
2388 /* in text mode, strip \r if followed by \n */
2389 if (bufstart[i]=='\r' && (!utf16 || bufstart[i+1]==0) && i+1+utf16==num_read)
2390 {
2391 char lookahead[2];
2392 DWORD len;
2393
2394 lookahead[1] = '\n';
2395 if (ReadFile(fdinfo->handle, lookahead, 1+utf16, &len, NULL) && len)
2396 {
2397 if(lookahead[0]=='\n' && (!utf16 || lookahead[1]==0) && j==0)
2398 {
2399 bufstart[j++] = '\n';
2400 if(utf16) bufstart[j++] = 0;
2401 }
2402 else
2403 {
2404 if(lookahead[0]!='\n' || (utf16 && lookahead[1]!=0))
2405 {
2406 bufstart[j++] = '\r';
2407 if(utf16) bufstart[j++] = 0;
2408 }
2409
2410 if (fdinfo->wxflag & (WX_PIPE | WX_TTY))
2411 {
2412 if (lookahead[0]=='\n' && (!utf16 || !lookahead[1]))
2413 {
2414 bufstart[j++] = '\n';
2415 if (utf16) bufstart[j++] = 0;
2416 }
2417 else
2418 {
2419 fdinfo->lookahead[0] = lookahead[0];
2420 fdinfo->lookahead[1] = lookahead[1];
2421 }
2422 }
2423 else
2424 SetFilePointer(fdinfo->handle, -1-utf16, NULL, FILE_CURRENT);
2425 }
2426 }
2427 else
2428 {
2429 bufstart[j++] = '\r';
2430 if(utf16) bufstart[j++] = 0;
2431 }
2432 }
2433 else if((bufstart[i]!='\r' || (utf16 && bufstart[i+1]!=0))
2434 || (bufstart[i+1+utf16]!='\n' || (utf16 && bufstart[i+3]!=0)))
2435 {
2436 bufstart[j++] = bufstart[i];
2437 if(utf16) bufstart[j++] = bufstart[i+1];
2438 }
2439 }
2440 num_read = j;
2441 }
2442 }
2443 else
2444 {
2446 {
2447 TRACE(":end-of-pipe\n");
2448 fdinfo->wxflag |= WX_ATEOF;
2449 return 0;
2450 }
2451 else
2452 {
2453 TRACE(":failed-last error (%d)\n",GetLastError());
2454 return -1;
2455 }
2456 }
2457
2458 if (count > 4)
2459 TRACE("(%u), %s\n",num_read,debugstr_an(buf, num_read));
2460 return num_read;
2461}
static __inline const char * debugstr_an(const char *s, int n)
Definition: compat.h:55
static int read_utf8(ioinfo *fdinfo, wchar_t *buf, unsigned int count)
Definition: file.c:2130
#define ERROR_BROKEN_PIPE
Definition: winerror.h:183

Referenced by _read().

◆ read_utf8()

static int read_utf8 ( ioinfo fdinfo,
wchar_t buf,
unsigned int  count 
)
static

Definition at line 2130 of file file.c.

2131{
2132 HANDLE hand = fdinfo->handle;
2133 char min_buf[4], *readbuf, lookahead;
2134 DWORD readbuf_size, pos=0, num_read=1, char_len, i, j;
2135
2136 /* make the buffer big enough to hold at least one character */
2137 /* read bytes have to fit to output and lookahead buffers */
2138 count /= 2;
2139 readbuf_size = count < 4 ? 4 : count;
2140 if(readbuf_size<=4 || !(readbuf = malloc(readbuf_size))) {
2141 readbuf_size = 4;
2142 readbuf = min_buf;
2143 }
2144
2145 if(fdinfo->lookahead[0] != '\n') {
2146 readbuf[pos++] = fdinfo->lookahead[0];
2147 fdinfo->lookahead[0] = '\n';
2148
2149 if(fdinfo->lookahead[1] != '\n') {
2150 readbuf[pos++] = fdinfo->lookahead[1];
2151 fdinfo->lookahead[1] = '\n';
2152
2153 if(fdinfo->lookahead[2] != '\n') {
2154 readbuf[pos++] = fdinfo->lookahead[2];
2155 fdinfo->lookahead[2] = '\n';
2156 }
2157 }
2158 }
2159
2160 /* NOTE: this case is broken in native dll, reading
2161 * sometimes fails when small buffer is passed
2162 */
2163 if(count < 4) {
2164 if(!pos && !ReadFile(hand, readbuf, 1, &num_read, NULL)) {
2166 fdinfo->wxflag |= WX_ATEOF;
2167 return 0;
2168 }else {
2170 return -1;
2171 }
2172 }else if(!num_read) {
2173 fdinfo->wxflag |= WX_ATEOF;
2174 return 0;
2175 }else {
2176 pos++;
2177 }
2178
2179 char_len = get_utf8_char_len(readbuf[0]);
2180 if(char_len>pos) {
2181 if(ReadFile(hand, readbuf+pos, char_len-pos, &num_read, NULL))
2182 pos += num_read;
2183 }
2184
2185 if(readbuf[0] == '\n')
2186 fdinfo->wxflag |= WX_READNL;
2187 else
2188 fdinfo->wxflag &= ~WX_READNL;
2189
2190 if(readbuf[0] == 0x1a) {
2191 fdinfo->wxflag |= WX_ATEOF;
2192 return 0;
2193 }
2194
2195 if(readbuf[0] == '\r') {
2196 if(!ReadFile(hand, &lookahead, 1, &num_read, NULL) || num_read!=1)
2197 buf[0] = '\r';
2198 else if(lookahead == '\n')
2199 buf[0] = '\n';
2200 else {
2201 buf[0] = '\r';
2202 if(fdinfo->wxflag & (WX_PIPE | WX_TTY))
2203 fdinfo->lookahead[0] = lookahead;
2204 else
2205 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2206 }
2207 return 2;
2208 }
2209
2210 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2212 return -1;
2213 }
2214
2215 return num_read*2;
2216 }
2217
2218 if(!ReadFile(hand, readbuf+pos, readbuf_size-pos, &num_read, NULL)) {
2219 if(pos) {
2220 num_read = 0;
2221 }else if(GetLastError() == ERROR_BROKEN_PIPE) {
2222 fdinfo->wxflag |= WX_ATEOF;
2223 if (readbuf != min_buf) free(readbuf);
2224 return 0;
2225 }else {
2227 if (readbuf != min_buf) free(readbuf);
2228 return -1;
2229 }
2230 }else if(!pos && !num_read) {
2231 fdinfo->wxflag |= WX_ATEOF;
2232 if (readbuf != min_buf) free(readbuf);
2233 return 0;
2234 }
2235
2236 pos += num_read;
2237 if(readbuf[0] == '\n')
2238 fdinfo->wxflag |= WX_READNL;
2239 else
2240 fdinfo->wxflag &= ~WX_READNL;
2241
2242 /* Find first byte of last character (may be incomplete) */
2243 for(i=pos-1; i>0 && i>pos-4; i--)
2244 if((readbuf[i]&0xc0) != 0x80)
2245 break;
2246 char_len = get_utf8_char_len(readbuf[i]);
2247 if(char_len+i <= pos)
2248 i += char_len;
2249
2250 if(fdinfo->wxflag & (WX_PIPE | WX_TTY)) {
2251 if(i < pos)
2252 fdinfo->lookahead[0] = readbuf[i];
2253 if(i+1 < pos)
2254 fdinfo->lookahead[1] = readbuf[i+1];
2255 if(i+2 < pos)
2256 fdinfo->lookahead[2] = readbuf[i+2];
2257 }else if(i < pos) {
2259 }
2260 pos = i;
2261
2262 for(i=0, j=0; i<pos; i++) {
2263 if(readbuf[i] == 0x1a) {
2264 fdinfo->wxflag |= WX_ATEOF;
2265 break;
2266 }
2267
2268 /* strip '\r' if followed by '\n' */
2269 if(readbuf[i] == '\r' && i+1==pos) {
2270 if(fdinfo->lookahead[0] != '\n' || !ReadFile(hand, &lookahead, 1, &num_read, NULL) || !num_read) {
2271 readbuf[j++] = '\r';
2272 }else if(lookahead == '\n' && j==0) {
2273 readbuf[j++] = '\n';
2274 }else {
2275 if(lookahead != '\n')
2276 readbuf[j++] = '\r';
2277
2278 if(fdinfo->wxflag & (WX_PIPE | WX_TTY))
2279 fdinfo->lookahead[0] = lookahead;
2280 else
2281 SetFilePointer(fdinfo->handle, -1, NULL, FILE_CURRENT);
2282 }
2283 }else if(readbuf[i]!='\r' || readbuf[i+1]!='\n') {
2284 readbuf[j++] = readbuf[i];
2285 }
2286 }
2287 pos = j;
2288
2289 if(!(num_read = MultiByteToWideChar(CP_UTF8, 0, readbuf, pos, buf, count))) {
2291 if (readbuf != min_buf) free(readbuf);
2292 return -1;
2293 }
2294
2295 if (readbuf != min_buf) free(readbuf);
2296 return num_read*2;
2297}
static int get_utf8_char_len(char ch)
Definition: file.c:2116

Referenced by read_i().

◆ release_ioinfo()

void release_ioinfo ( ioinfo info)

◆ remove()

int CDECL remove ( const char path)

Definition at line 3750 of file file.c.

3751{
3752 TRACE("(%s)\n",path);
3753 if (DeleteFileA(path))
3754 return 0;
3755 TRACE(":failed (%d)\n",GetLastError());
3757 return -1;
3758}

◆ remove_std_buffer()

static void remove_std_buffer ( FILE file)
static

Definition at line 610 of file file.c.

611{
613 file->_ptr = file->_base = NULL;
614 file->_bufsiz = file->_cnt = 0;
615 file->_flag &= ~_USERBUF;
616}

Referenced by fputws().

◆ rename()

int CDECL rename ( const char oldpath,
const char newpath 
)

Definition at line 3776 of file file.c.

3777{
3778 TRACE(":from %s to %s\n",oldpath,newpath);
3779 if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED))
3780 return 0;
3781 TRACE(":failed (%d)\n",GetLastError());
3783 return -1;
3784}
BOOL WINAPI MoveFileExA(IN LPCSTR lpExistingFileName, IN LPCSTR lpNewFileName OPTIONAL, IN DWORD dwFlags)
Definition: move.c:1153

◆ rewind()

void CDECL rewind ( FILE file)

Definition at line 1375 of file file.c.

1376{
1377 TRACE(":file (%p) fd (%d)\n",file,file->_file);
1378
1380 fseek(file, 0L, SEEK_SET);
1381 clearerr(file);
1383}
_Check_return_opt_ _CRTIMP int __cdecl fseek(_Inout_ FILE *_File, _In_ long _Offset, _In_ int _Origin)
void CDECL clearerr(FILE *file)
Definition: file.c:1363

◆ setbuf()

void CDECL setbuf ( FILE file,
char buf 
)

Definition at line 3842 of file file.c.

3843{
3845}
#define _IOFBF
Definition: stdio.h:127
_Check_return_opt_ _CRTIMP int __cdecl setvbuf(_Inout_ FILE *_File, _Inout_updates_opt_z_(_Size) char *_Buf, _In_ int _Mode, _In_ size_t _Size)

◆ setvbuf()

int CDECL setvbuf ( FILE file,
char buf,
int  mode,
size_t  size 
)

Definition at line 3802 of file file.c.

3803{
3804 if(!MSVCRT_CHECK_PMT(file != NULL)) return -1;
3805 if(!MSVCRT_CHECK_PMT(mode==_IONBF || mode==_IOFBF || mode==_IOLBF)) return -1;
3806 if(!MSVCRT_CHECK_PMT(mode==_IONBF || (size>=2 && size<=INT_MAX))) return -1;
3807
3809
3810 fflush(file);
3811 if(file->_flag & _IOMYBUF)
3812 free(file->_base);
3813 file->_flag &= ~(_IONBF | _IOMYBUF | _USERBUF);
3814 file->_cnt = 0;
3815
3816 if(mode == _IONBF) {
3817 file->_flag |= _IONBF;
3818 file->_base = file->_ptr = (char*)&file->_charbuf;
3819 file->_bufsiz = 2;
3820 }else if(buf) {
3821 file->_base = file->_ptr = buf;
3822 file->_flag |= _USERBUF;
3823 file->_bufsiz = size;
3824 }else {
3825 file->_base = file->_ptr = malloc(size);
3826 if(!file->_base) {
3827 file->_bufsiz = 0;
3829 return -1;
3830 }
3831
3832 file->_flag |= _IOMYBUF;
3833 file->_bufsiz = size;
3834 }
3836 return 0;
3837}
#define _IOLBF
Definition: stdio.h:128

◆ split_oflags()

unsigned split_oflags ( unsigned  oflags)

Definition at line 1671 of file file.c.

1672{
1673 int wxflags = 0;
1674 unsigned unsupp; /* until we support everything */
1675
1676 if (oflags & _O_APPEND) wxflags |= WX_APPEND;
1677 if (oflags & _O_BINARY) {/* Nothing to do */}
1678 else if (oflags & _O_TEXT) wxflags |= WX_TEXT;
1679 else if (oflags & _O_WTEXT) wxflags |= WX_TEXT;
1680 else if (oflags & _O_U16TEXT) wxflags |= WX_TEXT;
1681 else if (oflags & _O_U8TEXT) wxflags |= WX_TEXT;
1682 else if (*__p__fmode() & _O_BINARY) {/* Nothing to do */}
1683 else wxflags |= WX_TEXT; /* default to TEXT*/
1684 if (oflags & _O_NOINHERIT) wxflags |= WX_DONTINHERIT;
1685
1686 if ((unsupp = oflags & ~(
1693 )))
1694 ERR(":unsupported oflags 0x%04x\n",unsupp);
1695
1696 return wxflags;
1697}
#define _O_SEQUENTIAL
Definition: cabinet.h:43
#define _O_RANDOM
Definition: cabinet.h:42
int * __p__fmode(void)
Definition: fmode.c:9

Referenced by _open_osfhandle(), _pipe(), _tpopen(), and _wsopen_s().

◆ tmpfile()

FILE *CDECL tmpfile ( void  )

Definition at line 3914 of file file.c.

3915{
3916 char *filename = tmpnam(NULL);
3917 int fd;
3918 FILE* file = NULL;
3919
3920 LOCK_FILES();
3923 if (fd != -1 && (file = msvcrt_alloc_fp()))
3924 {
3925 if (msvcrt_init_fp(file, fd, _IORW) == -1)
3926 {
3927 file->_flag = 0;
3928 file = NULL;
3929 }
3930 else file->_tmpfname = _strdup(filename);
3931 }
3932
3933 if(fd != -1 && !file)
3934 _close(fd);
3935 UNLOCK_FILES();
3936 return file;
3937}
_CRTIMP char *__cdecl tmpnam(_Pre_maybenull_ _Post_z_ char *_Buffer)

Referenced by CCFDATAStorage::Create(), create_temp_binary(), HANDLE_CustomType7(), jpeg_open_backing_store(), JSPROXY_InternetInitializeAutoProxyDll(), open_file_test(), patch_file(), test_color_contexts(), test_CreateDirectoryA(), test_CryptCATAdminAddRemoveCatalog(), test_GetNamedSecurityInfoA(), test_install_class(), test_retrieveObjectByUrl(), test_SetupCopyOEMInf(), transfer_file_http(), CCFDATAStorage::Truncate(), VerInstallFileA(), and VerInstallFileW().

◆ tmpnam()

char *CDECL tmpnam ( char s)

Definition at line 3850 of file file.c.

3851{
3852 char tmpstr[16];
3853 char *p;
3854 int count, size;
3855
3856 if (!s) {
3858
3859 if(!data->tmpnam_buffer)
3860 data->tmpnam_buffer = malloc(MAX_PATH);
3861
3862 s = data->tmpnam_buffer;
3863 }
3864
3866 p = s + sprintf(s, "\\s%s.", tmpstr);
3867 for (count = 0; count < TMP_MAX; count++)
3868 {
3870 memcpy(p, tmpstr, size);
3871 p[size] = '\0';
3874 break;
3875 }
3876 return s;
3877}
#define sprintf(buf, format,...)
Definition: sprintf.c:55
static int msvcrt_int_to_base32(int num, char *str)
Definition: file.c:619

◆ ungetc()

int CDECL ungetc ( int  c,
FILE file 
)

Definition at line 3942 of file file.c.

3943{
3944 if(!MSVCRT_CHECK_PMT(file != NULL)) return EOF;
3945
3946 if (c == EOF || !(file->_flag&_IOREAD ||
3947 (file->_flag&_IORW && !(file->_flag&_IOWRT))))
3948 return EOF;
3949
3951 if((!(file->_flag & (_IONBF | _IOMYBUF | _USERBUF))
3953 || (!file->_cnt && file->_ptr==file->_base))
3954 file->_ptr++;
3955
3956 if(file->_ptr>file->_base) {
3957 file->_ptr--;
3958 if(file->_flag & _IOSTRG) {
3959 if(*file->_ptr != c) {
3960 file->_ptr++;
3962 return EOF;
3963 }
3964 }else {
3965 *file->_ptr = c;
3966 }
3967 file->_cnt++;
3968 clearerr(file);
3969 file->_flag |= _IOREAD;
3971 return c;
3972 }
3973
3975 return EOF;
3976}

◆ ungetwc()

wint_t CDECL ungetwc ( wint_t  wc,
FILE file 
)

Definition at line 3981 of file file.c.

3982{
3983 wchar_t mwc = wc;
3984
3985 if (wc == WEOF)
3986 return WEOF;
3987
3989
3990 if((get_ioinfo_nolock(file->_file)->exflag & (EF_UTF8 | EF_UTF16))
3991 || !(get_ioinfo_nolock(file->_file)->wxflag & WX_TEXT)) {
3992 unsigned char * pp = (unsigned char *)&mwc;
3993 int i;
3994
3995 for(i=sizeof(wchar_t)-1;i>=0;i--) {
3996 if(pp[i] != ungetc(pp[i],file)) {
3998 return WEOF;
3999 }
4000 }
4001 }else {
4002 char mbs[MB_LEN_MAX];
4003 int len;
4004
4005 len = wctomb(mbs, mwc);
4006 if(len == -1) {
4008 return WEOF;
4009 }
4010
4011 for(len--; len>=0; len--) {
4012 if(mbs[len] != ungetc(mbs[len], file)) {
4014 return WEOF;
4015 }
4016 }
4017 }
4018
4020 return mwc;
4021}
_Check_return_opt_ _CRTIMP_ALT int __cdecl ungetc(_In_ int _Ch, _Inout_ FILE *_File)

Variable Documentation

◆ __badioinfo

◆ __pioinfo

Definition at line 121 of file file.c.

◆ _commode

int _commode
extern

Definition at line 31 of file environ.c.

Referenced by __p__commode(), and msvcrt_get_flags().

◆ _iob

◆ MSVCRT_file_cs

static CRITICAL_SECTION MSVCRT_file_cs = { &MSVCRT_file_cs_debug, -1, 0, 0, 0, 0 }
static

Definition at line 147 of file file.c.

◆ MSVCRT_file_cs_debug

CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug
static
Initial value:
=
{
0, 0, { (DWORD_PTR)(__FILE__ ": MSVCRT_file_cs") }
}
static CRITICAL_SECTION MSVCRT_file_cs
Definition: file.c:147
static CRITICAL_SECTION_DEBUG MSVCRT_file_cs_debug
Definition: file.c:148
LIST_ENTRY ProcessLocksList
Definition: winbase.h:883
#define DWORD_PTR
Definition: treelist.c:76

Definition at line 148 of file file.c.

◆ MSVCRT_fstream

file_crit* MSVCRT_fstream[MSVCRT_MAX_FILES/MSVCRT_FD_BLOCK_SIZE] = { NULL }
static

Definition at line 134 of file file.c.

Referenced by msvcrt_free_io(), and msvcrt_get_file().

◆ MSVCRT_max_streams

int MSVCRT_max_streams = 512
static

Definition at line 135 of file file.c.

Referenced by _getmaxstdio(), _setmaxstdio(), msvcrt_alloc_fp(), and msvcrt_get_file().

◆ MSVCRT_stream_idx

int MSVCRT_stream_idx
static

◆ MSVCRT_umask

int MSVCRT_umask = 0
static

Definition at line 138 of file file.c.

Referenced by _umask().

◆ tmpnam_unique

int tmpnam_unique
static

Definition at line 141 of file file.c.

Referenced by _wtmpnam(), and tmpnam().

◆ utf16_bom

char utf16_bom[2] = { 0xff, 0xfe }
static

Definition at line 109 of file file.c.

Referenced by _wsopen_s(), and check_bom().

◆ utf8_bom

char utf8_bom[3] = { 0xef, 0xbb, 0xbf }
static

Definition at line 108 of file file.c.

Referenced by _wsopen_s(), check_bom(), ME_HandleMessage(), and parse_file().