| 1 | /* | 
|---|
| 2 | * msvcrt.dll file functions | 
|---|
| 3 | * | 
|---|
| 4 | * Copyright 1996,1998 Marcus Meissner | 
|---|
| 5 | * Copyright 1996 Jukka Iivonen | 
|---|
| 6 | * Copyright 1997,2000 Uwe Bonnes | 
|---|
| 7 | * Copyright 2000 Jon Griffiths | 
|---|
| 8 | * | 
|---|
| 9 | * This library is free software; you can redistribute it and/or | 
|---|
| 10 | * modify it under the terms of the GNU Lesser General Public | 
|---|
| 11 | * License as published by the Free Software Foundation; either | 
|---|
| 12 | * version 2.1 of the License, or (at your option) any later version. | 
|---|
| 13 | * | 
|---|
| 14 | * This library is distributed in the hope that it will be useful, | 
|---|
| 15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | 
|---|
| 16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU | 
|---|
| 17 | * Lesser General Public License for more details. | 
|---|
| 18 | * | 
|---|
| 19 | * You should have received a copy of the GNU Lesser General Public | 
|---|
| 20 | * License along with this library; if not, write to the Free Software | 
|---|
| 21 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA | 
|---|
| 22 | */ | 
|---|
| 23 | #ifdef __WIN32OS2__ | 
|---|
| 24 | #include <emxheader.h> | 
|---|
| 25 | /* thanks to stupid unistd.h in win dir! */ | 
|---|
| 26 | #include <emxruntime\unistd.h> | 
|---|
| 27 | #include <winbase.h> | 
|---|
| 28 | #else | 
|---|
| 29 | #include "config.h" | 
|---|
| 30 | #include "wine/port.h" | 
|---|
| 31 | #endif | 
|---|
| 32 |  | 
|---|
| 33 | #include <stdlib.h> | 
|---|
| 34 | #include <stdio.h> | 
|---|
| 35 | #include <ctype.h> | 
|---|
| 36 | #include <string.h> | 
|---|
| 37 | #include <time.h> | 
|---|
| 38 |  | 
|---|
| 39 |  | 
|---|
| 40 | #include "winternl.h" | 
|---|
| 41 | #include "debugstr.h" | 
|---|
| 42 | #include "msvcrt.h" | 
|---|
| 43 | #include "msvcrt/errno.h" | 
|---|
| 44 |  | 
|---|
| 45 | #include "wine/unicode.h" | 
|---|
| 46 | #include "msvcrt/direct.h" | 
|---|
| 47 | #include "msvcrt/fcntl.h" | 
|---|
| 48 | #include "msvcrt/io.h" | 
|---|
| 49 | #include "msvcrt/sys/locking.h" | 
|---|
| 50 | #include "msvcrt/stdio.h" | 
|---|
| 51 | #include "msvcrt/stdlib.h" | 
|---|
| 52 | #include "msvcrt/string.h" | 
|---|
| 53 | #include "msvcrt/sys/stat.h" | 
|---|
| 54 | #include "msvcrt/sys/utime.h" | 
|---|
| 55 | #include "msvcrt/time.h" | 
|---|
| 56 | #include "msvcrt/share.h" | 
|---|
| 57 | #include "msvcrt/wctype.h" | 
|---|
| 58 |  | 
|---|
| 59 | #include "wine/debug.h" | 
|---|
| 60 |  | 
|---|
| 61 | WINE_DEFAULT_DEBUG_CHANNEL(msvcrt); | 
|---|
| 62 |  | 
|---|
| 63 | /* for stat mode, permissions apply to all,owner and group */ | 
|---|
| 64 | #define MSVCRT_S_IREAD  (_S_IREAD  | (_S_IREAD  >> 3) | (_S_IREAD  >> 6)) | 
|---|
| 65 | #define MSVCRT_S_IWRITE (_S_IWRITE | (_S_IWRITE >> 3) | (_S_IWRITE >> 6)) | 
|---|
| 66 | #define MSVCRT_S_IEXEC  (_S_IEXEC  | (_S_IEXEC  >> 3) | (_S_IEXEC  >> 6)) | 
|---|
| 67 |  | 
|---|
| 68 | /* _access() bit flags FIXME: incomplete */ | 
|---|
| 69 | #define MSVCRT_W_OK      0x02 | 
|---|
| 70 |  | 
|---|
| 71 |  | 
|---|
| 72 | /* FIXME: Make this dynamic */ | 
|---|
| 73 | #define MSVCRT_MAX_FILES 257 | 
|---|
| 74 |  | 
|---|
| 75 | HANDLE MSVCRT_handles[MSVCRT_MAX_FILES]; | 
|---|
| 76 | MSVCRT_FILE* MSVCRT_files[MSVCRT_MAX_FILES]; | 
|---|
| 77 | int  MSVCRT_flags[MSVCRT_MAX_FILES]; | 
|---|
| 78 | char *MSVCRT_tempfiles[MSVCRT_MAX_FILES]; | 
|---|
| 79 | MSVCRT_FILE MSVCRT__iob[3]; | 
|---|
| 80 | #define MSVCRT_stdin       (MSVCRT__iob+STDIN_FILENO) | 
|---|
| 81 | #define MSVCRT_stdout      (MSVCRT__iob+STDOUT_FILENO) | 
|---|
| 82 | #define MSVCRT_stderr      (MSVCRT__iob+STDERR_FILENO) | 
|---|
| 83 |  | 
|---|
| 84 | static int MSVCRT_fdstart = 3; /* first unallocated fd */ | 
|---|
| 85 | static int MSVCRT_fdend = 3; /* highest allocated fd */ | 
|---|
| 86 |  | 
|---|
| 87 | /* INTERNAL: process umask */ | 
|---|
| 88 | static int MSVCRT__umask = 0; | 
|---|
| 89 |  | 
|---|
| 90 | /* INTERNAL: Static buffer for temp file name */ | 
|---|
| 91 | static char MSVCRT_tmpname[MAX_PATH]; | 
|---|
| 92 |  | 
|---|
| 93 | static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e'; | 
|---|
| 94 | static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't'; | 
|---|
| 95 | static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd'; | 
|---|
| 96 | static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm'; | 
|---|
| 97 |  | 
|---|
| 98 | #define TOUL(x) (ULONGLONG)(x) | 
|---|
| 99 | static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e'); | 
|---|
| 100 | static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t'); | 
|---|
| 101 | static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d'); | 
|---|
| 102 | static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m'); | 
|---|
| 103 |  | 
|---|
| 104 | extern CRITICAL_SECTION MSVCRT_file_cs; | 
|---|
| 105 | #define LOCK_FILES     EnterCriticalSection(&MSVCRT_file_cs) | 
|---|
| 106 | #define UNLOCK_FILES   LeaveCriticalSection(&MSVCRT_file_cs) | 
|---|
| 107 |  | 
|---|
| 108 | static void msvcrt_cp_from_stati64(const struct _stati64 *bufi64, struct _stat *buf) | 
|---|
| 109 | { | 
|---|
| 110 | buf->st_dev   = bufi64->st_dev; | 
|---|
| 111 | buf->st_ino   = bufi64->st_ino; | 
|---|
| 112 | buf->st_mode  = bufi64->st_mode; | 
|---|
| 113 | buf->st_nlink = bufi64->st_nlink; | 
|---|
| 114 | buf->st_uid   = bufi64->st_uid; | 
|---|
| 115 | buf->st_gid   = bufi64->st_gid; | 
|---|
| 116 | buf->st_rdev  = bufi64->st_rdev; | 
|---|
| 117 | buf->st_size  = bufi64->st_size; | 
|---|
| 118 | buf->st_atime = bufi64->st_atime; | 
|---|
| 119 | buf->st_mtime = bufi64->st_mtime; | 
|---|
| 120 | buf->st_ctime = bufi64->st_ctime; | 
|---|
| 121 | } | 
|---|
| 122 |  | 
|---|
| 123 | /* INTERNAL: Get the HANDLE for a fd */ | 
|---|
| 124 | static HANDLE msvcrt_fdtoh(int fd) | 
|---|
| 125 | { | 
|---|
| 126 | if (fd < 0 || fd >= MSVCRT_fdend || | 
|---|
| 127 | MSVCRT_handles[fd] == INVALID_HANDLE_VALUE) | 
|---|
| 128 | { | 
|---|
| 129 | WARN(":fd (%d) - no handle!\n",fd); | 
|---|
| 130 | *MSVCRT_doserrno() = 0; | 
|---|
| 131 | *MSVCRT__errno() = MSVCRT_EBADF; | 
|---|
| 132 | return INVALID_HANDLE_VALUE; | 
|---|
| 133 | } | 
|---|
| 134 | return MSVCRT_handles[fd]; | 
|---|
| 135 | } | 
|---|
| 136 |  | 
|---|
| 137 | /* INTERNAL: free a file entry fd */ | 
|---|
| 138 | static void msvcrt_free_fd(int fd) | 
|---|
| 139 | { | 
|---|
| 140 | MSVCRT_handles[fd] = INVALID_HANDLE_VALUE; | 
|---|
| 141 | MSVCRT_files[fd] = 0; | 
|---|
| 142 | MSVCRT_flags[fd] = 0; | 
|---|
| 143 | TRACE(":fd (%d) freed\n",fd); | 
|---|
| 144 | if (fd < 3) | 
|---|
| 145 | return; /* dont use 0,1,2 for user files */ | 
|---|
| 146 | if (fd == MSVCRT_fdend - 1) | 
|---|
| 147 | MSVCRT_fdend--; | 
|---|
| 148 | if (fd < MSVCRT_fdstart) | 
|---|
| 149 | MSVCRT_fdstart = fd; | 
|---|
| 150 | } | 
|---|
| 151 |  | 
|---|
| 152 | /* INTERNAL: Allocate an fd slot from a Win32 HANDLE */ | 
|---|
| 153 | static int msvcrt_alloc_fd(HANDLE hand, int flag) | 
|---|
| 154 | { | 
|---|
| 155 | int fd = MSVCRT_fdstart; | 
|---|
| 156 |  | 
|---|
| 157 | TRACE(":handle (%p) allocating fd (%d)\n",hand,fd); | 
|---|
| 158 | if (fd >= MSVCRT_MAX_FILES) | 
|---|
| 159 | { | 
|---|
| 160 | WARN(":files exhausted!\n"); | 
|---|
| 161 | return -1; | 
|---|
| 162 | } | 
|---|
| 163 | MSVCRT_handles[fd] = hand; | 
|---|
| 164 | MSVCRT_flags[fd] = flag; | 
|---|
| 165 |  | 
|---|
| 166 | /* locate next free slot */ | 
|---|
| 167 | if (fd == MSVCRT_fdend) | 
|---|
| 168 | MSVCRT_fdstart = ++MSVCRT_fdend; | 
|---|
| 169 | else | 
|---|
| 170 | while(MSVCRT_fdstart < MSVCRT_fdend && | 
|---|
| 171 | MSVCRT_handles[MSVCRT_fdstart] != INVALID_HANDLE_VALUE) | 
|---|
| 172 | MSVCRT_fdstart++; | 
|---|
| 173 |  | 
|---|
| 174 | return fd; | 
|---|
| 175 | } | 
|---|
| 176 |  | 
|---|
| 177 | /* INTERNAL: Allocate a FILE* for an fd slot | 
|---|
| 178 | * This is done lazily to avoid memory wastage for low level open/write | 
|---|
| 179 | * usage when a FILE* is not requested (but may be later). | 
|---|
| 180 | */ | 
|---|
| 181 | static MSVCRT_FILE* msvcrt_alloc_fp(int fd) | 
|---|
| 182 | { | 
|---|
| 183 | TRACE("MSVCRT: fd (%d) allocating FILE*\n",fd); | 
|---|
| 184 | if (fd < 0 || fd >= MSVCRT_fdend || | 
|---|
| 185 | MSVCRT_handles[fd] == INVALID_HANDLE_VALUE) | 
|---|
| 186 | { | 
|---|
| 187 | WARN(":invalid fd %d\n",fd); | 
|---|
| 188 | *MSVCRT_doserrno() = 0; | 
|---|
| 189 | *MSVCRT__errno() = MSVCRT_EBADF; | 
|---|
| 190 | return NULL; | 
|---|
| 191 | } | 
|---|
| 192 | if (!MSVCRT_files[fd]) | 
|---|
| 193 | { | 
|---|
| 194 | if ((MSVCRT_files[fd] = MSVCRT_calloc(sizeof(MSVCRT_FILE),1))) | 
|---|
| 195 | { | 
|---|
| 196 | MSVCRT_files[fd]->_file = fd; | 
|---|
| 197 | MSVCRT_files[fd]->_flag = MSVCRT_flags[fd]; | 
|---|
| 198 | MSVCRT_files[fd]->_flag &= ~MSVCRT__IOAPPEND; /* mask out, see above */ | 
|---|
| 199 | } | 
|---|
| 200 | } | 
|---|
| 201 | TRACE(":got FILE* (%p)\n",MSVCRT_files[fd]); | 
|---|
| 202 | return MSVCRT_files[fd]; | 
|---|
| 203 | } | 
|---|
| 204 |  | 
|---|
| 205 |  | 
|---|
| 206 | /* INTERNAL: Set up stdin, stderr and stdout */ | 
|---|
| 207 | void msvcrt_init_io(void) | 
|---|
| 208 | { | 
|---|
| 209 | int i; | 
|---|
| 210 | memset(MSVCRT__iob,0,3*sizeof(MSVCRT_FILE)); | 
|---|
| 211 | MSVCRT_handles[0] = GetStdHandle(STD_INPUT_HANDLE); | 
|---|
| 212 | MSVCRT_flags[0] = MSVCRT__iob[0]._flag = MSVCRT__IOREAD; | 
|---|
| 213 | MSVCRT_handles[1] = GetStdHandle(STD_OUTPUT_HANDLE); | 
|---|
| 214 | MSVCRT_flags[1] = MSVCRT__iob[1]._flag = MSVCRT__IOWRT; | 
|---|
| 215 | MSVCRT_handles[2] = GetStdHandle(STD_ERROR_HANDLE); | 
|---|
| 216 | MSVCRT_flags[2] = MSVCRT__iob[2]._flag = MSVCRT__IOWRT; | 
|---|
| 217 |  | 
|---|
| 218 | TRACE(":handles (%p)(%p)(%p)\n",MSVCRT_handles[0], | 
|---|
| 219 | MSVCRT_handles[1],MSVCRT_handles[2]); | 
|---|
| 220 |  | 
|---|
| 221 | for (i = 0; i < 3; i++) | 
|---|
| 222 | { | 
|---|
| 223 | /* FILE structs for stdin/out/err are static and never deleted */ | 
|---|
| 224 | MSVCRT_files[i] = &MSVCRT__iob[i]; | 
|---|
| 225 | MSVCRT__iob[i]._file = i; | 
|---|
| 226 | MSVCRT_tempfiles[i] = NULL; | 
|---|
| 227 | } | 
|---|
| 228 | } | 
|---|
| 229 |  | 
|---|
| 230 | /* free everything on process exit */ | 
|---|
| 231 | void msvcrt_free_io(void) | 
|---|
| 232 | { | 
|---|
| 233 | MSVCRT__fcloseall(); | 
|---|
| 234 | MSVCRT__close(0); | 
|---|
| 235 | MSVCRT__close(1); | 
|---|
| 236 | MSVCRT__close(2); | 
|---|
| 237 | } | 
|---|
| 238 |  | 
|---|
| 239 | /* INTERNAL: Flush stdio file buffer */ | 
|---|
| 240 | static int msvcrt_flush_buffer(MSVCRT_FILE* file) | 
|---|
| 241 | { | 
|---|
| 242 | if(file->_bufsiz) { | 
|---|
| 243 | int cnt=file->_ptr-file->_base; | 
|---|
| 244 | if(cnt>0 && MSVCRT__write(file->_file, file->_base, cnt) != cnt) { | 
|---|
| 245 | return MSVCRT_EOF; | 
|---|
| 246 | } | 
|---|
| 247 | file->_ptr=file->_base; | 
|---|
| 248 | file->_cnt=file->_bufsiz; | 
|---|
| 249 | } | 
|---|
| 250 | return 0; | 
|---|
| 251 | } | 
|---|
| 252 |  | 
|---|
| 253 | /* INTERNAL: Allocate stdio file buffer */ | 
|---|
| 254 | static void msvcrt_alloc_buffer(MSVCRT_FILE* file) | 
|---|
| 255 | { | 
|---|
| 256 | file->_base = MSVCRT_calloc(MSVCRT_BUFSIZ,1); | 
|---|
| 257 | if(file->_base) { | 
|---|
| 258 | file->_bufsiz = MSVCRT_BUFSIZ; | 
|---|
| 259 | file->_flag |= MSVCRT__IOMYBUF; | 
|---|
| 260 | } else { | 
|---|
| 261 | file->_base = (unsigned char *)(&file->_charbuf); | 
|---|
| 262 | /* put here 2 ??? */ | 
|---|
| 263 | file->_bufsiz = sizeof(file->_charbuf); | 
|---|
| 264 | } | 
|---|
| 265 | file->_ptr = file->_base; | 
|---|
| 266 | file->_cnt = 0; | 
|---|
| 267 | } | 
|---|
| 268 |  | 
|---|
| 269 | /********************************************************************* | 
|---|
| 270 | *              __p__iob(MSVCRT.@) | 
|---|
| 271 | */ | 
|---|
| 272 | MSVCRT_FILE *__p__iob(void) | 
|---|
| 273 | { | 
|---|
| 274 | dprintf(("MSVCRT: __p__iob request")); | 
|---|
| 275 | return &MSVCRT__iob[0]; | 
|---|
| 276 | } | 
|---|
| 277 |  | 
|---|
| 278 | /********************************************************************* | 
|---|
| 279 | *              _access (MSVCRT.@) | 
|---|
| 280 | */ | 
|---|
| 281 | int MSVCRT__access(const char *filename, int mode) | 
|---|
| 282 | { | 
|---|
| 283 | DWORD attr = GetFileAttributesA(filename); | 
|---|
| 284 |  | 
|---|
| 285 | TRACE("MSVCRT _access (%s,%d) %ld\n",filename,mode,attr); | 
|---|
| 286 |  | 
|---|
| 287 | if (!filename || attr == 0xffffffff) | 
|---|
| 288 | { | 
|---|
| 289 | MSVCRT__set_errno(GetLastError()); | 
|---|
| 290 | return -1; | 
|---|
| 291 | } | 
|---|
| 292 | if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK)) | 
|---|
| 293 | { | 
|---|
| 294 | MSVCRT__set_errno(ERROR_ACCESS_DENIED); | 
|---|
| 295 | return -1; | 
|---|
| 296 | } | 
|---|
| 297 | return 0; | 
|---|
| 298 | } | 
|---|
| 299 |  | 
|---|
| 300 | /********************************************************************* | 
|---|
| 301 | *              _waccess (MSVCRT.@) | 
|---|
| 302 | */ | 
|---|
| 303 | int _waccess(const MSVCRT_wchar_t *filename, int mode) | 
|---|
| 304 | { | 
|---|
| 305 | DWORD attr = GetFileAttributesW(filename); | 
|---|
| 306 |  | 
|---|
| 307 | TRACE("MSVCRT: _waccess (%s,%d) %ld\n",debugstr_w(filename),mode,attr); | 
|---|
| 308 |  | 
|---|
| 309 | if (!filename || attr == 0xffffffff) | 
|---|
| 310 | { | 
|---|
| 311 | MSVCRT__set_errno(GetLastError()); | 
|---|
| 312 | return -1; | 
|---|
| 313 | } | 
|---|
| 314 | if ((attr & FILE_ATTRIBUTE_READONLY) && (mode & MSVCRT_W_OK)) | 
|---|
| 315 | { | 
|---|
| 316 | MSVCRT__set_errno(ERROR_ACCESS_DENIED); | 
|---|
| 317 | return -1; | 
|---|
| 318 | } | 
|---|
| 319 | return 0; | 
|---|
| 320 | } | 
|---|
| 321 |  | 
|---|
| 322 | /********************************************************************* | 
|---|
| 323 | *              _chmod (MSVCRT.@) | 
|---|
| 324 | */ | 
|---|
| 325 | int MSVCRT__chmod(const char *path, int flags) | 
|---|
| 326 | { | 
|---|
| 327 | DWORD oldFlags = GetFileAttributesA(path); | 
|---|
| 328 |  | 
|---|
| 329 | dprintf(("MSVCRT: _chmod %s",path)); | 
|---|
| 330 |  | 
|---|
| 331 | if (oldFlags != 0x0FFFFFFFF) | 
|---|
| 332 | { | 
|---|
| 333 | DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY: | 
|---|
| 334 | oldFlags | FILE_ATTRIBUTE_READONLY; | 
|---|
| 335 |  | 
|---|
| 336 | if (newFlags == oldFlags || SetFileAttributesA(path, newFlags)) | 
|---|
| 337 | return 0; | 
|---|
| 338 | } | 
|---|
| 339 | MSVCRT__set_errno(GetLastError()); | 
|---|
| 340 | return -1; | 
|---|
| 341 | } | 
|---|
| 342 |  | 
|---|
| 343 | /********************************************************************* | 
|---|
| 344 | *              _wchmod (MSVCRT.@) | 
|---|
| 345 | */ | 
|---|
| 346 | int _wchmod(const MSVCRT_wchar_t *path, int flags) | 
|---|
| 347 | { | 
|---|
| 348 | DWORD oldFlags = GetFileAttributesW(path); | 
|---|
| 349 |  | 
|---|
| 350 | dprintf(("MSVCRT: _wchmod %s",debugstr_w(path))); | 
|---|
| 351 |  | 
|---|
| 352 | if (oldFlags != 0x0FFFFFFFF) | 
|---|
| 353 | { | 
|---|
| 354 | DWORD newFlags = (flags & _S_IWRITE)? oldFlags & ~FILE_ATTRIBUTE_READONLY: | 
|---|
| 355 | oldFlags | FILE_ATTRIBUTE_READONLY; | 
|---|
| 356 |  | 
|---|
| 357 | if (newFlags == oldFlags || SetFileAttributesW(path, newFlags)) | 
|---|
| 358 | return 0; | 
|---|
| 359 | } | 
|---|
| 360 | MSVCRT__set_errno(GetLastError()); | 
|---|
| 361 | return -1; | 
|---|
| 362 | } | 
|---|
| 363 |  | 
|---|
| 364 | /********************************************************************* | 
|---|
| 365 | *              _unlink (MSVCRT.@) | 
|---|
| 366 | */ | 
|---|
| 367 | int MSVCRT__unlink(const char *path) | 
|---|
| 368 | { | 
|---|
| 369 | TRACE("MSVCRT: _unlink (%s)\n",path); | 
|---|
| 370 | if(DeleteFileA(path)) | 
|---|
| 371 | return 0; | 
|---|
| 372 | TRACE("failed (%ld)\n",GetLastError()); | 
|---|
| 373 | MSVCRT__set_errno(GetLastError()); | 
|---|
| 374 | return -1; | 
|---|
| 375 | } | 
|---|
| 376 |  | 
|---|
| 377 | /********************************************************************* | 
|---|
| 378 | *              _wunlink (MSVCRT.@) | 
|---|
| 379 | */ | 
|---|
| 380 | int _wunlink(const MSVCRT_wchar_t *path) | 
|---|
| 381 | { | 
|---|
| 382 | TRACE("(%s)\n",debugstr_w(path)); | 
|---|
| 383 | if(DeleteFileW(path)) | 
|---|
| 384 | return 0; | 
|---|
| 385 | TRACE("failed (%ld)\n",GetLastError()); | 
|---|
| 386 | MSVCRT__set_errno(GetLastError()); | 
|---|
| 387 | return -1; | 
|---|
| 388 | } | 
|---|
| 389 |  | 
|---|
| 390 | /********************************************************************* | 
|---|
| 391 | *              _close (MSVCRT.@) | 
|---|
| 392 | */ | 
|---|
| 393 | int MSVCRT__close(int fd) | 
|---|
| 394 | { | 
|---|
| 395 | HANDLE hand = msvcrt_fdtoh(fd); | 
|---|
| 396 |  | 
|---|
| 397 | TRACE("MSVCRT: _close fd (%d) handle (%p)\n",fd,hand); | 
|---|
| 398 | if (hand == INVALID_HANDLE_VALUE) | 
|---|
| 399 | return -1; | 
|---|
| 400 | /* flush stdio buffers */ | 
|---|
| 401 | if(MSVCRT_files[fd]) { | 
|---|
| 402 | if(MSVCRT_files[fd]->_flag & MSVCRT__IOWRT) | 
|---|
| 403 | MSVCRT_fflush(MSVCRT_files[fd]); | 
|---|
| 404 |  | 
|---|
| 405 | if(MSVCRT_files[fd]->_flag & MSVCRT__IOMYBUF) | 
|---|
| 406 | MSVCRT_free(MSVCRT_files[fd]->_base); | 
|---|
| 407 | } | 
|---|
| 408 |  | 
|---|
| 409 | /* Dont free std FILE*'s, they are not dynamic */ | 
|---|
| 410 | if (fd > 2 && MSVCRT_files[fd]) | 
|---|
| 411 | MSVCRT_free(MSVCRT_files[fd]); | 
|---|
| 412 |  | 
|---|
| 413 | msvcrt_free_fd(fd); | 
|---|
| 414 |  | 
|---|
| 415 | if (!CloseHandle(hand)) | 
|---|
| 416 | { | 
|---|
| 417 | WARN(":failed-last error (%ld)\n",GetLastError()); | 
|---|
| 418 | MSVCRT__set_errno(GetLastError()); | 
|---|
| 419 | return -1; | 
|---|
| 420 | } | 
|---|
| 421 | if (MSVCRT_tempfiles[fd]) | 
|---|
| 422 | { | 
|---|
| 423 | TRACE("deleting temporary file '%s'\n",MSVCRT_tempfiles[fd]); | 
|---|
| 424 | MSVCRT__unlink(MSVCRT_tempfiles[fd]); | 
|---|
| 425 | MSVCRT_free(MSVCRT_tempfiles[fd]); | 
|---|
| 426 | MSVCRT_tempfiles[fd] = NULL; | 
|---|
| 427 | } | 
|---|
| 428 |  | 
|---|
| 429 | TRACE("MSVCRT: _close ok\n"); | 
|---|
| 430 | return 0; | 
|---|
| 431 | } | 
|---|
| 432 |  | 
|---|
| 433 | /********************************************************************* | 
|---|
| 434 | *              _commit (MSVCRT.@) | 
|---|
| 435 | */ | 
|---|
| 436 | int _commit(int fd) | 
|---|
| 437 | { | 
|---|
| 438 | HANDLE hand = msvcrt_fdtoh(fd); | 
|---|
| 439 |  | 
|---|
| 440 | TRACE("MSVCRT: _commit fd (%d) handle (%p)\n",fd,hand); | 
|---|
| 441 | if (hand == INVALID_HANDLE_VALUE) | 
|---|
| 442 | return -1; | 
|---|
| 443 |  | 
|---|
| 444 | if (!FlushFileBuffers(hand)) | 
|---|
| 445 | { | 
|---|
| 446 | if (GetLastError() == ERROR_INVALID_HANDLE) | 
|---|
| 447 | { | 
|---|
| 448 | /* FlushFileBuffers fails for console handles | 
|---|
| 449 | * so we ignore this error. | 
|---|
| 450 | */ | 
|---|
| 451 | return 0; | 
|---|
| 452 | } | 
|---|
| 453 | TRACE(":failed-last error (%ld)\n",GetLastError()); | 
|---|
| 454 | MSVCRT__set_errno(GetLastError()); | 
|---|
| 455 | return -1; | 
|---|
| 456 | } | 
|---|
| 457 | TRACE(":ok\n"); | 
|---|
| 458 | return 0; | 
|---|
| 459 | } | 
|---|
| 460 |  | 
|---|
| 461 | /********************************************************************* | 
|---|
| 462 | *              _eof (MSVCRT.@) | 
|---|
| 463 | */ | 
|---|
| 464 | int _eof(int fd) | 
|---|
| 465 | { | 
|---|
| 466 | DWORD curpos,endpos; | 
|---|
| 467 | HANDLE hand = msvcrt_fdtoh(fd); | 
|---|
| 468 |  | 
|---|
| 469 | TRACE("MSVCRT: _eof fd (%d) handle (%p)\n",fd,hand); | 
|---|
| 470 |  | 
|---|
| 471 | if (hand == INVALID_HANDLE_VALUE) | 
|---|
| 472 | return -1; | 
|---|
| 473 |  | 
|---|
| 474 | /* If we have a FILE* for this file, the EOF flag | 
|---|
| 475 | * will be set by the read()/write() functions. | 
|---|
| 476 | */ | 
|---|
| 477 | if (MSVCRT_files[fd]) | 
|---|
| 478 | return MSVCRT_flags[fd] & MSVCRT__IOEOF; | 
|---|
| 479 |  | 
|---|
| 480 | /* Otherwise we do it the hard way */ | 
|---|
| 481 | curpos = SetFilePointer(hand, 0, NULL, SEEK_CUR); | 
|---|
| 482 | endpos = SetFilePointer(hand, 0, NULL, FILE_END); | 
|---|
| 483 |  | 
|---|
| 484 | if (curpos == endpos) | 
|---|
| 485 | return TRUE; | 
|---|
| 486 |  | 
|---|
| 487 | SetFilePointer(hand, curpos, 0, FILE_BEGIN); | 
|---|
| 488 | return FALSE; | 
|---|
| 489 | } | 
|---|
| 490 |  | 
|---|
| 491 | /********************************************************************* | 
|---|
| 492 | *              _fcloseall (MSVCRT.@) | 
|---|
| 493 | */ | 
|---|
| 494 | int MSVCRT__fcloseall(void) | 
|---|
| 495 | { | 
|---|
| 496 | int num_closed = 0, i; | 
|---|
| 497 |  | 
|---|
| 498 | dprintf(("MSVCRT: _fcloseall")); | 
|---|
| 499 |  | 
|---|
| 500 | for (i = 3; i < MSVCRT_fdend; i++) | 
|---|
| 501 | if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE) | 
|---|
| 502 | { | 
|---|
| 503 | MSVCRT__close(i); | 
|---|
| 504 | num_closed++; | 
|---|
| 505 | } | 
|---|
| 506 |  | 
|---|
| 507 | TRACE(":closed (%d) handles\n",num_closed); | 
|---|
| 508 | return num_closed; | 
|---|
| 509 | } | 
|---|
| 510 |  | 
|---|
| 511 | /********************************************************************* | 
|---|
| 512 | *              _lseek (MSVCRT.@) | 
|---|
| 513 | */ | 
|---|
| 514 | __int64 _lseeki64(int fd, __int64 offset, int whence) | 
|---|
| 515 | { | 
|---|
| 516 | DWORD ret, hoffset = (DWORD) (offset >> 32); | 
|---|
| 517 | HANDLE hand = msvcrt_fdtoh(fd); | 
|---|
| 518 |  | 
|---|
| 519 | TRACE("MSVCRT: _lseeki64 fd (%d) handle (%p)\n",fd,hand); | 
|---|
| 520 | if (hand == INVALID_HANDLE_VALUE) | 
|---|
| 521 | return -1; | 
|---|
| 522 |  | 
|---|
| 523 | if (whence < 0 || whence > 2) | 
|---|
| 524 | { | 
|---|
| 525 | *MSVCRT__errno() = MSVCRT_EINVAL; | 
|---|
| 526 | return -1; | 
|---|
| 527 | } | 
|---|
| 528 |  | 
|---|
| 529 | TRACE("MSVCRT: _lseek fd (%d) to 0x%08lx%08lx pos %s\n", | 
|---|
| 530 | fd,hoffset,(long)offset, | 
|---|
| 531 | (whence==SEEK_SET)?"SEEK_SET": | 
|---|
| 532 | (whence==SEEK_CUR)?"SEEK_CUR": | 
|---|
| 533 | (whence==SEEK_END)?"SEEK_END":"UNKNOWN"); | 
|---|
| 534 |  | 
|---|
| 535 | if (((ret = SetFilePointer(hand, (long)offset, &hoffset, | 
|---|
| 536 | whence)) != INVALID_SET_FILE_POINTER) || !GetLastError()) | 
|---|
| 537 | { | 
|---|
| 538 | if (MSVCRT_files[fd]) | 
|---|
| 539 | MSVCRT_files[fd]->_flag &= ~MSVCRT__IOEOF; | 
|---|
| 540 | /* FIXME: What if we seek _to_ EOF - is EOF set? */ | 
|---|
| 541 |  | 
|---|
| 542 | return ((__int64)hoffset << 32) | ret; | 
|---|
| 543 | } | 
|---|
| 544 | TRACE(":error-last error (%ld)\n",GetLastError()); | 
|---|
| 545 | if (MSVCRT_files[fd]) | 
|---|
| 546 | switch(GetLastError()) | 
|---|
| 547 | { | 
|---|
| 548 | case ERROR_NEGATIVE_SEEK: | 
|---|
| 549 | case ERROR_SEEK_ON_DEVICE: | 
|---|
| 550 | MSVCRT__set_errno(GetLastError()); | 
|---|
| 551 | MSVCRT_files[fd]->_flag |= MSVCRT__IOERR; | 
|---|
| 552 | break; | 
|---|
| 553 | default: | 
|---|
| 554 | break; | 
|---|
| 555 | } | 
|---|
| 556 | return -1; | 
|---|
| 557 | } | 
|---|
| 558 |  | 
|---|
| 559 | /********************************************************************* | 
|---|
| 560 | *              _lseek (MSVCRT.@) | 
|---|
| 561 | */ | 
|---|
| 562 | LONG MSVCRT__lseek(int fd, LONG offset, int whence) | 
|---|
| 563 | { | 
|---|
| 564 | return _lseeki64(fd, offset, whence); | 
|---|
| 565 | } | 
|---|
| 566 |  | 
|---|
| 567 | /********************************************************************* | 
|---|
| 568 | *              _locking (MSVCRT.@) | 
|---|
| 569 | * | 
|---|
| 570 | * This is untested; the underlying LockFile doesn't work yet. | 
|---|
| 571 | */ | 
|---|
| 572 | int _locking(int fd, int mode, LONG nbytes) | 
|---|
| 573 | { | 
|---|
| 574 | BOOL ret; | 
|---|
| 575 | DWORD cur_locn; | 
|---|
| 576 | HANDLE hand = msvcrt_fdtoh(fd); | 
|---|
| 577 |  | 
|---|
| 578 | TRACE("MSVCRT: _locking fd (%d) handle (%p)\n",fd,hand); | 
|---|
| 579 | if (hand == INVALID_HANDLE_VALUE) | 
|---|
| 580 | return -1; | 
|---|
| 581 |  | 
|---|
| 582 | if (mode < 0 || mode > 4) | 
|---|
| 583 | { | 
|---|
| 584 | *MSVCRT__errno() = MSVCRT_EINVAL; | 
|---|
| 585 | return -1; | 
|---|
| 586 | } | 
|---|
| 587 |  | 
|---|
| 588 | TRACE(":fd (%d) by 0x%08lx mode %s\n", | 
|---|
| 589 | fd,nbytes,(mode==_LK_UNLCK)?"_LK_UNLCK": | 
|---|
| 590 | (mode==_LK_LOCK)?"_LK_LOCK": | 
|---|
| 591 | (mode==_LK_NBLCK)?"_LK_NBLCK": | 
|---|
| 592 | (mode==_LK_RLCK)?"_LK_RLCK": | 
|---|
| 593 | (mode==_LK_NBRLCK)?"_LK_NBRLCK": | 
|---|
| 594 | "UNKNOWN"); | 
|---|
| 595 |  | 
|---|
| 596 | if ((cur_locn = SetFilePointer(hand, 0L, NULL, SEEK_CUR)) == 0xffffffff) | 
|---|
| 597 | { | 
|---|
| 598 | FIXME ("Seek failed\n"); | 
|---|
| 599 | *MSVCRT__errno() = MSVCRT_EINVAL; /* FIXME */ | 
|---|
| 600 | return -1; | 
|---|
| 601 | } | 
|---|
| 602 | if (mode == _LK_LOCK || mode == _LK_RLCK) | 
|---|
| 603 | { | 
|---|
| 604 | int nretry = 10; | 
|---|
| 605 | ret = 1; /* just to satisfy gcc */ | 
|---|
| 606 | while (nretry--) | 
|---|
| 607 | { | 
|---|
| 608 | ret = LockFile(hand, cur_locn, 0L, nbytes, 0L); | 
|---|
| 609 | if (ret) break; | 
|---|
| 610 | Sleep (1); | 
|---|
| 611 | } | 
|---|
| 612 | } | 
|---|
| 613 | else if (mode == _LK_UNLCK) | 
|---|
| 614 | ret = UnlockFile(hand, cur_locn, 0L, nbytes, 0L); | 
|---|
| 615 | else | 
|---|
| 616 | ret = LockFile(hand, cur_locn, 0L, nbytes, 0L); | 
|---|
| 617 | /* FIXME - what about error settings? */ | 
|---|
| 618 | return ret ? 0 : -1; | 
|---|
| 619 | } | 
|---|
| 620 |  | 
|---|
| 621 | /********************************************************************* | 
|---|
| 622 | *              rewind (MSVCRT.@) | 
|---|
| 623 | */ | 
|---|
| 624 | void MSVCRT_rewind(MSVCRT_FILE* file) | 
|---|
| 625 | { | 
|---|
| 626 | TRACE("MSVCRT: rewind file (%p) fd (%d)\n",file,file->_file); | 
|---|
| 627 | MSVCRT_fseek(file, 0L, SEEK_SET); | 
|---|
| 628 | MSVCRT_clearerr(file); | 
|---|
| 629 | } | 
|---|
| 630 |  | 
|---|
| 631 | /********************************************************************* | 
|---|
| 632 | *              _fdopen (MSVCRT.@) | 
|---|
| 633 | */ | 
|---|
| 634 | MSVCRT_FILE* MSVCRT__fdopen(int fd, const char *mode) | 
|---|
| 635 | { | 
|---|
| 636 | MSVCRT_FILE* file = msvcrt_alloc_fp(fd); | 
|---|
| 637 |  | 
|---|
| 638 | TRACE("MSVCRT: _fdopen fd (%d) mode (%s) FILE* (%p)\n",fd,mode,file); | 
|---|
| 639 |  | 
|---|
| 640 | return file; | 
|---|
| 641 | } | 
|---|
| 642 |  | 
|---|
| 643 | /********************************************************************* | 
|---|
| 644 | *              _wfdopen (MSVCRT.@) | 
|---|
| 645 | */ | 
|---|
| 646 | MSVCRT_FILE* _wfdopen(int fd, const MSVCRT_wchar_t *mode) | 
|---|
| 647 | { | 
|---|
| 648 | MSVCRT_FILE* file = msvcrt_alloc_fp(fd); | 
|---|
| 649 |  | 
|---|
| 650 | TRACE("MSVCRT: _wfdopen fd (%d) mode (%s) FILE* (%p)\n",fd,debugstr_w(mode),file); | 
|---|
| 651 | if (file) | 
|---|
| 652 | MSVCRT_rewind(file); | 
|---|
| 653 |  | 
|---|
| 654 | return file; | 
|---|
| 655 | } | 
|---|
| 656 |  | 
|---|
| 657 | /********************************************************************* | 
|---|
| 658 | *              _filelength (MSVCRT.@) | 
|---|
| 659 | */ | 
|---|
| 660 | LONG MSVCRT__filelength(int fd) | 
|---|
| 661 | { | 
|---|
| 662 | LONG curPos = MSVCRT__lseek(fd, 0, SEEK_CUR); | 
|---|
| 663 |  | 
|---|
| 664 | dprintf(("MSVCRT: _filelength")); | 
|---|
| 665 |  | 
|---|
| 666 | if (curPos != -1) | 
|---|
| 667 | { | 
|---|
| 668 | LONG endPos = MSVCRT__lseek(fd, 0, SEEK_END); | 
|---|
| 669 | if (endPos != -1) | 
|---|
| 670 | { | 
|---|
| 671 | if (endPos != curPos) | 
|---|
| 672 | MSVCRT__lseek(fd, curPos, SEEK_SET); | 
|---|
| 673 | return endPos; | 
|---|
| 674 | } | 
|---|
| 675 | } | 
|---|
| 676 | return -1; | 
|---|
| 677 | } | 
|---|
| 678 |  | 
|---|
| 679 | /********************************************************************* | 
|---|
| 680 | *              _fileno (MSVCRT.@) | 
|---|
| 681 | */ | 
|---|
| 682 | int MSVCRT__fileno(MSVCRT_FILE* file) | 
|---|
| 683 | { | 
|---|
| 684 | TRACE("MSVCRT: _fileno FILE* (%p) fd (%d)\n",file,file->_file); | 
|---|
| 685 | return file->_file; | 
|---|
| 686 | } | 
|---|
| 687 |  | 
|---|
| 688 | /********************************************************************* | 
|---|
| 689 | *              _flushall (MSVCRT.@) | 
|---|
| 690 | */ | 
|---|
| 691 | int MSVCRT__flushall(void) | 
|---|
| 692 | { | 
|---|
| 693 | int num_flushed = 0, i = 3; | 
|---|
| 694 |  | 
|---|
| 695 | dprintf(("MSVCRT: _flushall")); | 
|---|
| 696 |  | 
|---|
| 697 | while(i < MSVCRT_fdend) | 
|---|
| 698 | if (MSVCRT_handles[i] != INVALID_HANDLE_VALUE) | 
|---|
| 699 | { | 
|---|
| 700 | #if 0 | 
|---|
| 701 | /* FIXME: flush, do not commit */ | 
|---|
| 702 | if (_commit(i) == -1) | 
|---|
| 703 | if (MSVCRT_files[i]) | 
|---|
| 704 | MSVCRT_files[i]->_flag |= MSVCRT__IOERR; | 
|---|
| 705 | #endif | 
|---|
| 706 | if(MSVCRT_files[i] && MSVCRT_files[i]->_flag & MSVCRT__IOWRT) { | 
|---|
| 707 | MSVCRT_fflush(MSVCRT_files[i]); | 
|---|
| 708 | num_flushed++; | 
|---|
| 709 | } | 
|---|
| 710 | } | 
|---|
| 711 |  | 
|---|
| 712 | TRACE(":flushed (%d) handles\n",num_flushed); | 
|---|
| 713 | return num_flushed; | 
|---|
| 714 | } | 
|---|
| 715 |  | 
|---|
| 716 | /********************************************************************* | 
|---|
| 717 | *              _fstati64 (MSVCRT.@) | 
|---|
| 718 | */ | 
|---|
| 719 | int _fstati64(int fd, struct _stati64* buf) | 
|---|
| 720 | { | 
|---|
| 721 | DWORD dw; | 
|---|
| 722 | BY_HANDLE_FILE_INFORMATION hfi; | 
|---|
| 723 | HANDLE hand = msvcrt_fdtoh(fd); | 
|---|
| 724 |  | 
|---|
| 725 | TRACE("MSVCRT: _fstati64 fd (%d) stat (%p)\n",fd,buf); | 
|---|
| 726 | if (hand == INVALID_HANDLE_VALUE) | 
|---|
| 727 | return -1; | 
|---|
| 728 |  | 
|---|
| 729 | if (!buf) | 
|---|
| 730 | { | 
|---|
| 731 | WARN(":failed-NULL buf\n"); | 
|---|
| 732 | MSVCRT__set_errno(ERROR_INVALID_PARAMETER); | 
|---|
| 733 | return -1; | 
|---|
| 734 | } | 
|---|
| 735 |  | 
|---|
| 736 | memset(&hfi, 0, sizeof(hfi)); | 
|---|
| 737 | memset(buf, 0, sizeof(struct _stati64)); | 
|---|
| 738 | if (!GetFileInformationByHandle(hand, &hfi)) | 
|---|
| 739 | { | 
|---|
| 740 | WARN("MSVCRT: _fstati64 failed-last error (%ld)\n",GetLastError()); | 
|---|
| 741 | MSVCRT__set_errno(ERROR_INVALID_PARAMETER); | 
|---|
| 742 | return -1; | 
|---|
| 743 | } | 
|---|
| 744 | FIXME(":dwFileAttributes = %ld, mode set to 0\n",hfi.dwFileAttributes); | 
|---|
| 745 | buf->st_nlink = hfi.nNumberOfLinks; | 
|---|
| 746 | buf->st_size  = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow; | 
|---|
| 747 | RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw); | 
|---|
| 748 | buf->st_atime = dw; | 
|---|
| 749 | RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw); | 
|---|
| 750 | buf->st_mtime = buf->st_ctime = dw; | 
|---|
| 751 | return 0; | 
|---|
| 752 | } | 
|---|
| 753 |  | 
|---|
| 754 |  | 
|---|
| 755 | /********************************************************************* | 
|---|
| 756 | *              _fstat (MSVCRT.@) | 
|---|
| 757 | */ | 
|---|
| 758 | int MSVCRT__fstat(int fd, struct _stat* buf) | 
|---|
| 759 | { int ret; | 
|---|
| 760 | struct _stati64 bufi64; | 
|---|
| 761 |  | 
|---|
| 762 | ret = _fstati64(fd, &bufi64); | 
|---|
| 763 | if (!ret) | 
|---|
| 764 | msvcrt_cp_from_stati64(&bufi64, buf); | 
|---|
| 765 | return ret; | 
|---|
| 766 | } | 
|---|
| 767 |  | 
|---|
| 768 | /********************************************************************* | 
|---|
| 769 | *              _futime (MSVCRT.@) | 
|---|
| 770 | */ | 
|---|
| 771 | int _futime(int fd, struct _utimbuf *t) | 
|---|
| 772 | { | 
|---|
| 773 | HANDLE hand = msvcrt_fdtoh(fd); | 
|---|
| 774 | FILETIME at, wt; | 
|---|
| 775 |  | 
|---|
| 776 | if (!t) | 
|---|
| 777 | { | 
|---|
| 778 | MSVCRT_time_t currTime; | 
|---|
| 779 | MSVCRT_time(&currTime); | 
|---|
| 780 | RtlSecondsSince1970ToTime(currTime, (LARGE_INTEGER *)&at); | 
|---|
| 781 | memcpy(&wt, &at, sizeof(wt)); | 
|---|
| 782 | } | 
|---|
| 783 | else | 
|---|
| 784 | { | 
|---|
| 785 | RtlSecondsSince1970ToTime(t->actime, (LARGE_INTEGER *)&at); | 
|---|
| 786 | if (t->actime == t->modtime) | 
|---|
| 787 | memcpy(&wt, &at, sizeof(wt)); | 
|---|
| 788 | else | 
|---|
| 789 | RtlSecondsSince1970ToTime(t->modtime, (LARGE_INTEGER *)&wt); | 
|---|
| 790 | } | 
|---|
| 791 |  | 
|---|
| 792 | if (!SetFileTime(hand, NULL, &at, &wt)) | 
|---|
| 793 | { | 
|---|
| 794 | MSVCRT__set_errno(GetLastError()); | 
|---|
| 795 | return -1 ; | 
|---|
| 796 | } | 
|---|
| 797 | return 0; | 
|---|
| 798 | } | 
|---|
| 799 |  | 
|---|
| 800 | /********************************************************************* | 
|---|
| 801 | *              _get_osfhandle (MSVCRT.@) | 
|---|
| 802 | */ | 
|---|
| 803 | long _get_osfhandle(int fd) | 
|---|
| 804 | { | 
|---|
| 805 | HANDLE hand = msvcrt_fdtoh(fd); | 
|---|
| 806 | HANDLE newhand = hand; | 
|---|
| 807 | TRACE(":fd (%d) handle (%p)\n",fd,hand); | 
|---|
| 808 |  | 
|---|
| 809 | if (hand != INVALID_HANDLE_VALUE) | 
|---|
| 810 | { | 
|---|
| 811 | /* FIXME: I'm not convinced that I should be copying the | 
|---|
| 812 | * handle here - it may be leaked if the app doesn't | 
|---|
| 813 | * close it (and the API docs dont say that it should) | 
|---|
| 814 | * Not duplicating it means that it can't be inherited | 
|---|
| 815 | * and so lcc's wedit doesn't cope when it passes it to | 
|---|
| 816 | * child processes. I've an idea that it should either | 
|---|
| 817 | * be copied by CreateProcess, or marked as inheritable | 
|---|
| 818 | * when initialised, or maybe both? JG 21-9-00. | 
|---|
| 819 | */ | 
|---|
| 820 | DuplicateHandle(GetCurrentProcess(),hand,GetCurrentProcess(), | 
|---|
| 821 | &newhand,0,TRUE,DUPLICATE_SAME_ACCESS); | 
|---|
| 822 | } | 
|---|
| 823 | return (long)newhand; | 
|---|
| 824 | } | 
|---|
| 825 |  | 
|---|
| 826 | /********************************************************************* | 
|---|
| 827 | *              _isatty (MSVCRT.@) | 
|---|
| 828 | */ | 
|---|
| 829 | int MSVCRT__isatty(int fd) | 
|---|
| 830 | { | 
|---|
| 831 | HANDLE hand = msvcrt_fdtoh(fd); | 
|---|
| 832 |  | 
|---|
| 833 | TRACE(":fd (%d) handle (%p)\n",fd,hand); | 
|---|
| 834 | if (hand == INVALID_HANDLE_VALUE) | 
|---|
| 835 | return 0; | 
|---|
| 836 |  | 
|---|
| 837 | return GetFileType(hand) == FILE_TYPE_CHAR? 1 : 0; | 
|---|
| 838 | } | 
|---|
| 839 |  | 
|---|
| 840 | /********************************************************************* | 
|---|
| 841 | *              _mktemp (MSVCRT.@) | 
|---|
| 842 | */ | 
|---|
| 843 | char *_mktemp(char *pattern) | 
|---|
| 844 | { | 
|---|
| 845 | int numX = 0; | 
|---|
| 846 | char *retVal = pattern; | 
|---|
| 847 | int id; | 
|---|
| 848 | char letter = 'a'; | 
|---|
| 849 |  | 
|---|
| 850 | while(*pattern) | 
|---|
| 851 | numX = (*pattern++ == 'X')? numX + 1 : 0; | 
|---|
| 852 | if (numX < 5) | 
|---|
| 853 | return NULL; | 
|---|
| 854 | pattern--; | 
|---|
| 855 | id = GetCurrentProcessId(); | 
|---|
| 856 | numX = 6; | 
|---|
| 857 | while(numX--) | 
|---|
| 858 | { | 
|---|
| 859 | int tempNum = id / 10; | 
|---|
| 860 | *pattern-- = id - (tempNum * 10) + '0'; | 
|---|
| 861 | id = tempNum; | 
|---|
| 862 | } | 
|---|
| 863 | pattern++; | 
|---|
| 864 | do | 
|---|
| 865 | { | 
|---|
| 866 | if (GetFileAttributesA(retVal) == 0xFFFFFFFF && | 
|---|
| 867 | GetLastError() == ERROR_FILE_NOT_FOUND) | 
|---|
| 868 | return retVal; | 
|---|
| 869 | *pattern = letter++; | 
|---|
| 870 | } while(letter != '|'); | 
|---|
| 871 | return NULL; | 
|---|
| 872 | } | 
|---|
| 873 |  | 
|---|
| 874 | /********************************************************************* | 
|---|
| 875 | *              _wmktemp (MSVCRT.@) | 
|---|
| 876 | */ | 
|---|
| 877 | MSVCRT_wchar_t *_wmktemp(MSVCRT_wchar_t *pattern) | 
|---|
| 878 | { | 
|---|
| 879 | int numX = 0; | 
|---|
| 880 | MSVCRT_wchar_t *retVal = pattern; | 
|---|
| 881 | int id; | 
|---|
| 882 | MSVCRT_wchar_t letter = 'a'; | 
|---|
| 883 |  | 
|---|
| 884 | while(*pattern) | 
|---|
| 885 | numX = (*pattern++ == 'X')? numX + 1 : 0; | 
|---|
| 886 | if (numX < 5) | 
|---|
| 887 | return NULL; | 
|---|
| 888 | pattern--; | 
|---|
| 889 | id = GetCurrentProcessId(); | 
|---|
| 890 | numX = 6; | 
|---|
| 891 | while(numX--) | 
|---|
| 892 | { | 
|---|
| 893 | int tempNum = id / 10; | 
|---|
| 894 | *pattern-- = id - (tempNum * 10) + '0'; | 
|---|
| 895 | id = tempNum; | 
|---|
| 896 | } | 
|---|
| 897 | pattern++; | 
|---|
| 898 | do | 
|---|
| 899 | { | 
|---|
| 900 | if (GetFileAttributesW(retVal) == 0xFFFFFFFF && | 
|---|
| 901 | GetLastError() == ERROR_FILE_NOT_FOUND) | 
|---|
| 902 | return retVal; | 
|---|
| 903 | *pattern = letter++; | 
|---|
| 904 | } while(letter != '|'); | 
|---|
| 905 | return NULL; | 
|---|
| 906 | } | 
|---|
| 907 |  | 
|---|
| 908 | /********************************************************************* | 
|---|
| 909 | *              _sopen (MSVCRT.@) | 
|---|
| 910 | */ | 
|---|
| 911 | int MSVCRT__sopen( const char *path, int oflags, int shflags, ... ) | 
|---|
| 912 | { | 
|---|
| 913 | va_list ap; | 
|---|
| 914 | int pmode; | 
|---|
| 915 | DWORD access = 0, creation = 0; | 
|---|
| 916 | DWORD sharing; | 
|---|
| 917 | int ioflag = 0, fd; | 
|---|
| 918 | HANDLE hand; | 
|---|
| 919 | SECURITY_ATTRIBUTES sa; | 
|---|
| 920 |  | 
|---|
| 921 |  | 
|---|
| 922 | TRACE(":file (%s) oflags: 0x%04x shflags: 0x%04x\n", | 
|---|
| 923 | path, oflags, shflags); | 
|---|
| 924 |  | 
|---|
| 925 | switch(oflags & (_O_RDONLY | _O_WRONLY | _O_RDWR)) | 
|---|
| 926 | { | 
|---|
| 927 | case _O_RDONLY: | 
|---|
| 928 | access |= GENERIC_READ; | 
|---|
| 929 | ioflag |= MSVCRT__IOREAD; | 
|---|
| 930 | break; | 
|---|
| 931 | case _O_WRONLY: | 
|---|
| 932 | access |= GENERIC_WRITE; | 
|---|
| 933 | ioflag |= MSVCRT__IOWRT; | 
|---|
| 934 | break; | 
|---|
| 935 | case _O_RDWR: | 
|---|
| 936 | access |= GENERIC_WRITE | GENERIC_READ; | 
|---|
| 937 | ioflag |= MSVCRT__IORW; | 
|---|
| 938 | break; | 
|---|
| 939 | } | 
|---|
| 940 |  | 
|---|
| 941 | if (oflags & _O_CREAT) | 
|---|
| 942 | { | 
|---|
| 943 | va_start(ap, shflags); | 
|---|
| 944 | pmode = va_arg(ap, int); | 
|---|
| 945 | va_end(ap); | 
|---|
| 946 |  | 
|---|
| 947 | FIXME(": pmode 0x%04x ignored\n", pmode); | 
|---|
| 948 |  | 
|---|
| 949 | if (oflags & _O_EXCL) | 
|---|
| 950 | creation = CREATE_NEW; | 
|---|
| 951 | else if (oflags & _O_TRUNC) | 
|---|
| 952 | creation = CREATE_ALWAYS; | 
|---|
| 953 | else | 
|---|
| 954 | creation = OPEN_ALWAYS; | 
|---|
| 955 | } | 
|---|
| 956 | else  /* no _O_CREAT */ | 
|---|
| 957 | { | 
|---|
| 958 | if (oflags & _O_TRUNC) | 
|---|
| 959 | creation = TRUNCATE_EXISTING; | 
|---|
| 960 | else | 
|---|
| 961 | creation = OPEN_EXISTING; | 
|---|
| 962 | } | 
|---|
| 963 | if (oflags & _O_APPEND) | 
|---|
| 964 | ioflag |= MSVCRT__IOAPPEND; | 
|---|
| 965 |  | 
|---|
| 966 |  | 
|---|
| 967 | if (oflags & _O_BINARY) | 
|---|
| 968 | ioflag |= _O_BINARY; | 
|---|
| 969 | else if (oflags & _O_TEXT) | 
|---|
| 970 | ioflag |= _O_TEXT; | 
|---|
| 971 | else if (*__p__fmode() & _O_BINARY) | 
|---|
| 972 | ioflag |= _O_BINARY; | 
|---|
| 973 | else | 
|---|
| 974 | ioflag |= _O_TEXT; /* default to TEXT*/ | 
|---|
| 975 |  | 
|---|
| 976 | switch( shflags ) | 
|---|
| 977 | { | 
|---|
| 978 | case _SH_DENYRW: | 
|---|
| 979 | sharing = 0L; | 
|---|
| 980 | break; | 
|---|
| 981 | case _SH_DENYWR: | 
|---|
| 982 | sharing = FILE_SHARE_READ; | 
|---|
| 983 | break; | 
|---|
| 984 | case _SH_DENYRD: | 
|---|
| 985 | sharing = FILE_SHARE_WRITE; | 
|---|
| 986 | break; | 
|---|
| 987 | case _SH_DENYNO: | 
|---|
| 988 | sharing = FILE_SHARE_READ | FILE_SHARE_WRITE; | 
|---|
| 989 | break; | 
|---|
| 990 | default: | 
|---|
| 991 | ERR( "Unhandled shflags 0x%x\n", shflags ); | 
|---|
| 992 | return -1; | 
|---|
| 993 | } | 
|---|
| 994 |  | 
|---|
| 995 | if (oflags & ~(_O_BINARY|_O_TEXT|_O_APPEND|_O_TRUNC|_O_EXCL | 
|---|
| 996 | |_O_CREAT|_O_RDWR|_O_TEMPORARY|_O_NOINHERIT)) | 
|---|
| 997 | TRACE(":unsupported oflags 0x%04x\n",oflags); | 
|---|
| 998 |  | 
|---|
| 999 | sa.nLength              = sizeof( SECURITY_ATTRIBUTES ); | 
|---|
| 1000 | sa.lpSecurityDescriptor = NULL; | 
|---|
| 1001 | sa.bInheritHandle       = (oflags & _O_NOINHERIT) ? FALSE : TRUE; | 
|---|
| 1002 |  | 
|---|
| 1003 | hand = CreateFileA(path, access, sharing, | 
|---|
| 1004 | &sa, creation, FILE_ATTRIBUTE_NORMAL, 0); | 
|---|
| 1005 |  | 
|---|
| 1006 | if (hand == INVALID_HANDLE_VALUE)  { | 
|---|
| 1007 | WARN(":failed-last error (%ld)\n",GetLastError()); | 
|---|
| 1008 | MSVCRT__set_errno(GetLastError()); | 
|---|
| 1009 | return -1; | 
|---|
| 1010 | } | 
|---|
| 1011 |  | 
|---|
| 1012 | fd = msvcrt_alloc_fd(hand, ioflag); | 
|---|
| 1013 |  | 
|---|
| 1014 | TRACE(":fd (%d) handle (%p)\n",fd, hand); | 
|---|
| 1015 |  | 
|---|
| 1016 | if (fd > 0) | 
|---|
| 1017 | { | 
|---|
| 1018 | if (oflags & _O_TEMPORARY) | 
|---|
| 1019 | MSVCRT_tempfiles[fd] = MSVCRT__strdup(path); | 
|---|
| 1020 | if (ioflag & MSVCRT__IOAPPEND) | 
|---|
| 1021 | MSVCRT__lseek(fd, 0, FILE_END); | 
|---|
| 1022 | } | 
|---|
| 1023 |  | 
|---|
| 1024 | return fd; | 
|---|
| 1025 | } | 
|---|
| 1026 |  | 
|---|
| 1027 | /********************************************************************* | 
|---|
| 1028 | *              _wsopen (MSVCRT.@) | 
|---|
| 1029 | */ | 
|---|
| 1030 | int MSVCRT__wsopen( const MSVCRT_wchar_t* path, int oflags, int shflags, ... ) | 
|---|
| 1031 | { | 
|---|
| 1032 | const unsigned int len = strlenW(path); | 
|---|
| 1033 | char *patha = MSVCRT_calloc(len + 1,1); | 
|---|
| 1034 | va_list ap; | 
|---|
| 1035 | int pmode; | 
|---|
| 1036 |  | 
|---|
| 1037 | va_start(ap, shflags); | 
|---|
| 1038 | pmode = va_arg(ap, int); | 
|---|
| 1039 | va_end(ap); | 
|---|
| 1040 |  | 
|---|
| 1041 | if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL)) | 
|---|
| 1042 | { | 
|---|
| 1043 | int retval = MSVCRT__sopen(patha,oflags,shflags,pmode); | 
|---|
| 1044 | MSVCRT_free(patha); | 
|---|
| 1045 | return retval; | 
|---|
| 1046 | } | 
|---|
| 1047 |  | 
|---|
| 1048 | MSVCRT__set_errno(GetLastError()); | 
|---|
| 1049 | return -1; | 
|---|
| 1050 | } | 
|---|
| 1051 |  | 
|---|
| 1052 | /********************************************************************* | 
|---|
| 1053 | *              _open (MSVCRT.@) | 
|---|
| 1054 | */ | 
|---|
| 1055 | int MSVCRT__open( const char *path, int flags, ... ) | 
|---|
| 1056 | { | 
|---|
| 1057 | va_list ap; | 
|---|
| 1058 |  | 
|---|
| 1059 | if (flags & _O_CREAT) | 
|---|
| 1060 | { | 
|---|
| 1061 | int pmode; | 
|---|
| 1062 | va_start(ap, flags); | 
|---|
| 1063 | pmode = va_arg(ap, int); | 
|---|
| 1064 | va_end(ap); | 
|---|
| 1065 | return MSVCRT__sopen( path, flags, _SH_DENYNO, pmode ); | 
|---|
| 1066 | } | 
|---|
| 1067 | else | 
|---|
| 1068 | return MSVCRT__sopen( path, flags, _SH_DENYNO); | 
|---|
| 1069 | } | 
|---|
| 1070 |  | 
|---|
| 1071 | /********************************************************************* | 
|---|
| 1072 | *              _wopen (MSVCRT.@) | 
|---|
| 1073 | */ | 
|---|
| 1074 | int _wopen(const MSVCRT_wchar_t *path,int flags,...) | 
|---|
| 1075 | { | 
|---|
| 1076 | const unsigned int len = strlenW(path); | 
|---|
| 1077 | char *patha = MSVCRT_calloc(len + 1,1); | 
|---|
| 1078 | va_list ap; | 
|---|
| 1079 | int pmode; | 
|---|
| 1080 |  | 
|---|
| 1081 | va_start(ap, flags); | 
|---|
| 1082 | pmode = va_arg(ap, int); | 
|---|
| 1083 | va_end(ap); | 
|---|
| 1084 |  | 
|---|
| 1085 | if (patha && WideCharToMultiByte(CP_ACP,0,path,len,patha,len,NULL,NULL)) | 
|---|
| 1086 | { | 
|---|
| 1087 | int retval = MSVCRT__open(patha,flags,pmode); | 
|---|
| 1088 | MSVCRT_free(patha); | 
|---|
| 1089 | return retval; | 
|---|
| 1090 | } | 
|---|
| 1091 |  | 
|---|
| 1092 | MSVCRT__set_errno(GetLastError()); | 
|---|
| 1093 | return -1; | 
|---|
| 1094 | } | 
|---|
| 1095 |  | 
|---|
| 1096 | /********************************************************************* | 
|---|
| 1097 | *              _creat (MSVCRT.@) | 
|---|
| 1098 | */ | 
|---|
| 1099 | int MSVCRT__creat(const char *path, int flags) | 
|---|
| 1100 | { | 
|---|
| 1101 | int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC; | 
|---|
| 1102 | return MSVCRT__open(path, usedFlags); | 
|---|
| 1103 | } | 
|---|
| 1104 |  | 
|---|
| 1105 | /********************************************************************* | 
|---|
| 1106 | *              _wcreat (MSVCRT.@) | 
|---|
| 1107 | */ | 
|---|
| 1108 | int _wcreat(const MSVCRT_wchar_t *path, int flags) | 
|---|
| 1109 | { | 
|---|
| 1110 | int usedFlags = (flags & _O_TEXT)| _O_CREAT| _O_WRONLY| _O_TRUNC; | 
|---|
| 1111 | return _wopen(path, usedFlags); | 
|---|
| 1112 | } | 
|---|
| 1113 |  | 
|---|
| 1114 | /********************************************************************* | 
|---|
| 1115 | *              _open_osfhandle (MSVCRT.@) | 
|---|
| 1116 | */ | 
|---|
| 1117 | int _open_osfhandle(long hand, int flags) | 
|---|
| 1118 | { | 
|---|
| 1119 | /* _O_RDONLY (0) always matches, so set the read flag*/ | 
|---|
| 1120 | /* FIXME: handle more flags */ | 
|---|
| 1121 | int fd= msvcrt_alloc_fd((HANDLE)hand,flags|MSVCRT__IOREAD); | 
|---|
| 1122 | TRACE(":handle (%ld) fd (%d) flags 0x%08x\n",hand,fd, flags |MSVCRT__IOREAD); | 
|---|
| 1123 | return fd; | 
|---|
| 1124 | } | 
|---|
| 1125 |  | 
|---|
| 1126 | /********************************************************************* | 
|---|
| 1127 | *              _rmtmp (MSVCRT.@) | 
|---|
| 1128 | */ | 
|---|
| 1129 | int MSVCRT__rmtmp(void) | 
|---|
| 1130 | { | 
|---|
| 1131 | int num_removed = 0, i; | 
|---|
| 1132 |  | 
|---|
| 1133 | for (i = 3; i < MSVCRT_fdend; i++) | 
|---|
| 1134 | if (MSVCRT_tempfiles[i]) | 
|---|
| 1135 | { | 
|---|
| 1136 | MSVCRT__close(i); | 
|---|
| 1137 | num_removed++; | 
|---|
| 1138 | } | 
|---|
| 1139 |  | 
|---|
| 1140 | if (num_removed) | 
|---|
| 1141 | TRACE(":removed (%d) temp files\n",num_removed); | 
|---|
| 1142 | return num_removed; | 
|---|
| 1143 | } | 
|---|
| 1144 |  | 
|---|
| 1145 | /********************************************************************* | 
|---|
| 1146 | *              _read (MSVCRT.@) | 
|---|
| 1147 | */ | 
|---|
| 1148 | int MSVCRT__read(int fd, void *buf, unsigned int count) | 
|---|
| 1149 | { | 
|---|
| 1150 | DWORD num_read; | 
|---|
| 1151 | HANDLE hand = msvcrt_fdtoh(fd); | 
|---|
| 1152 |  | 
|---|
| 1153 | /* Dont trace small reads, it gets *very* annoying */ | 
|---|
| 1154 | if (count > 4) | 
|---|
| 1155 | TRACE(":fd (%d) handle (%p) buf (%p) len (%d)\n",fd,hand,buf,count); | 
|---|
| 1156 | if (hand == INVALID_HANDLE_VALUE) | 
|---|
| 1157 | return -1; | 
|---|
| 1158 |  | 
|---|
| 1159 | if (MSVCRT_flags[fd]& _O_BINARY) | 
|---|
| 1160 | { | 
|---|
| 1161 | if (ReadFile(hand, buf, count, &num_read, NULL)) | 
|---|
| 1162 | { | 
|---|
| 1163 | if (num_read != count && MSVCRT_files[fd]) | 
|---|
| 1164 | { | 
|---|
| 1165 | TRACE(":EOF\n"); | 
|---|
| 1166 | MSVCRT_flags[fd] |= MSVCRT__IOEOF; | 
|---|
| 1167 | /* | 
|---|
| 1168 | MSVCRT_files[fd]->_flag |= MSVCRT__IOEOF; | 
|---|
| 1169 | */ | 
|---|
| 1170 | } | 
|---|
| 1171 | dprintf(("%s\n",debugstr_an(buf,num_read))); | 
|---|
| 1172 | return num_read; | 
|---|
| 1173 | } | 
|---|
| 1174 | TRACE(":failed-last error (%ld)\n",GetLastError()); | 
|---|
| 1175 | if (MSVCRT_files[fd]) | 
|---|
| 1176 | MSVCRT_files[fd]->_flag |= MSVCRT__IOERR; | 
|---|
| 1177 | return -1; | 
|---|
| 1178 | } | 
|---|
| 1179 | else | 
|---|
| 1180 | { | 
|---|
| 1181 | char cc, *s=(char*)buf,* buf_start=(char*)buf; | 
|---|
| 1182 | unsigned int i; | 
|---|
| 1183 |  | 
|---|
| 1184 | for (i = 0 , num_read = 1; i < count && (num_read == 1);) | 
|---|
| 1185 | { | 
|---|
| 1186 | if (ReadFile(hand, &cc, 1, &num_read, NULL)) | 
|---|
| 1187 | if (num_read == 1) | 
|---|
| 1188 | if ((cc != '\r') || MSVCRT_flags[fd] & _O_BINARY) | 
|---|
| 1189 | { | 
|---|
| 1190 | *s++ = (char)cc; | 
|---|
| 1191 | i++; | 
|---|
| 1192 | } | 
|---|
| 1193 | } | 
|---|
| 1194 | if (num_read != 1) | 
|---|
| 1195 | { | 
|---|
| 1196 | TRACE(":EOF\n"); | 
|---|
| 1197 | if (MSVCRT_files[fd]) | 
|---|
| 1198 | MSVCRT_flags[fd] |= MSVCRT__IOEOF; | 
|---|
| 1199 | /* | 
|---|
| 1200 | MSVCRT_files[fd]->_flag |= MSVCRT__IOEOF; | 
|---|
| 1201 | */ | 
|---|
| 1202 | } | 
|---|
| 1203 |  | 
|---|
| 1204 | if (count > 4) | 
|---|
| 1205 | dprintf(("%s\n",debugstr_an(buf_start, s-buf_start))); | 
|---|
| 1206 | return s-buf_start; | 
|---|
| 1207 | } | 
|---|
| 1208 | return 0; | 
|---|
| 1209 | } | 
|---|
| 1210 |  | 
|---|
| 1211 | /********************************************************************* | 
|---|
| 1212 | *              _getw (MSVCRT.@) | 
|---|
| 1213 | */ | 
|---|
| 1214 | int MSVCRT__getw(MSVCRT_FILE* file) | 
|---|
| 1215 | { | 
|---|
| 1216 | int i; | 
|---|
| 1217 | if (MSVCRT__read(file->_file, &i, sizeof(int)) != 1) | 
|---|
| 1218 | return MSVCRT_EOF; | 
|---|
| 1219 | return i; | 
|---|
| 1220 | } | 
|---|
| 1221 |  | 
|---|
| 1222 | /********************************************************************* | 
|---|
| 1223 | *              _setmode (MSVCRT.@) | 
|---|
| 1224 | */ | 
|---|
| 1225 | int MSVCRT__setmode(int fd,int mode) | 
|---|
| 1226 | { | 
|---|
| 1227 | int ret = MSVCRT_flags[fd] & (_O_TEXT | _O_BINARY); | 
|---|
| 1228 | if (mode & (~(_O_TEXT|_O_BINARY))) | 
|---|
| 1229 | FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode); | 
|---|
| 1230 | MSVCRT_flags[fd] &= ~(_O_TEXT|_O_BINARY); | 
|---|
| 1231 | MSVCRT_flags[fd] |= mode & (_O_TEXT | _O_BINARY); | 
|---|
| 1232 | return ret; | 
|---|
| 1233 | } | 
|---|
| 1234 |  | 
|---|
| 1235 | /********************************************************************* | 
|---|
| 1236 | *              _stati64 (MSVCRT.@) | 
|---|
| 1237 | */ | 
|---|
| 1238 | int _stati64(const char* path, struct _stati64 * buf) | 
|---|
| 1239 | { | 
|---|
| 1240 | DWORD dw; | 
|---|
| 1241 | WIN32_FILE_ATTRIBUTE_DATA hfi; | 
|---|
| 1242 | unsigned short mode = MSVCRT_S_IREAD; | 
|---|
| 1243 | int plen; | 
|---|
| 1244 |  | 
|---|
| 1245 | TRACE(":file (%s) buf(%p)\n",path,buf); | 
|---|
| 1246 |  | 
|---|
| 1247 | if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi)) | 
|---|
| 1248 | { | 
|---|
| 1249 | TRACE("failed (%ld)\n",GetLastError()); | 
|---|
| 1250 | MSVCRT__set_errno(ERROR_FILE_NOT_FOUND); | 
|---|
| 1251 | return -1; | 
|---|
| 1252 | } | 
|---|
| 1253 |  | 
|---|
| 1254 | memset(buf,0,sizeof(struct _stati64)); | 
|---|
| 1255 |  | 
|---|
| 1256 | /* FIXME: rdev isnt drive num,despite what the docs say-what is it? | 
|---|
| 1257 | Bon 011120: This FIXME seems incorrect | 
|---|
| 1258 | Also a letter as first char isn't enough to be classify | 
|---|
| 1259 | as drive letter | 
|---|
| 1260 | */ | 
|---|
| 1261 | if (isalpha((int)*path) && (*(path+1)==':')) | 
|---|
| 1262 | buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */ | 
|---|
| 1263 | else | 
|---|
| 1264 | buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1; | 
|---|
| 1265 |  | 
|---|
| 1266 | plen = strlen(path); | 
|---|
| 1267 |  | 
|---|
| 1268 | /* Dir, or regular file? */ | 
|---|
| 1269 | if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) || | 
|---|
| 1270 | (path[plen-1] == '\\')) | 
|---|
| 1271 | mode |= (_S_IFDIR | MSVCRT_S_IEXEC); | 
|---|
| 1272 | else | 
|---|
| 1273 | { | 
|---|
| 1274 | mode |= _S_IFREG; | 
|---|
| 1275 | /* executable? */ | 
|---|
| 1276 | if (plen > 6 && path[plen-4] == '.')  /* shortest exe: "\x.exe" */ | 
|---|
| 1277 | { | 
|---|
| 1278 | unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) | | 
|---|
| 1279 | (tolower(path[plen-3]) << 16); | 
|---|
| 1280 | if (ext == EXE || ext == BAT || ext == CMD || ext == COM) | 
|---|
| 1281 | mode |= MSVCRT_S_IEXEC; | 
|---|
| 1282 | } | 
|---|
| 1283 | } | 
|---|
| 1284 |  | 
|---|
| 1285 | if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) | 
|---|
| 1286 | mode |= MSVCRT_S_IWRITE; | 
|---|
| 1287 |  | 
|---|
| 1288 | buf->st_mode  = mode; | 
|---|
| 1289 | buf->st_nlink = 1; | 
|---|
| 1290 | buf->st_size  = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow; | 
|---|
| 1291 | RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw); | 
|---|
| 1292 | buf->st_atime = dw; | 
|---|
| 1293 | RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw); | 
|---|
| 1294 | buf->st_mtime = buf->st_ctime = dw; | 
|---|
| 1295 | TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink, | 
|---|
| 1296 | (long)(buf->st_size >> 32),(long)buf->st_size, | 
|---|
| 1297 | buf->st_atime,buf->st_mtime, buf->st_ctime); | 
|---|
| 1298 | return 0; | 
|---|
| 1299 | } | 
|---|
| 1300 |  | 
|---|
| 1301 | /********************************************************************* | 
|---|
| 1302 | *              _stat (MSVCRT.@) | 
|---|
| 1303 | */ | 
|---|
| 1304 | int MSVCRT__stat(const char* path, struct _stat * buf) | 
|---|
| 1305 | { int ret; | 
|---|
| 1306 | struct _stati64 bufi64; | 
|---|
| 1307 |  | 
|---|
| 1308 | ret = _stati64( path, &bufi64); | 
|---|
| 1309 | if (!ret) | 
|---|
| 1310 | msvcrt_cp_from_stati64(&bufi64, buf); | 
|---|
| 1311 | return ret; | 
|---|
| 1312 | } | 
|---|
| 1313 |  | 
|---|
| 1314 | /********************************************************************* | 
|---|
| 1315 | *              _wstat (MSVCRT.@) | 
|---|
| 1316 | */ | 
|---|
| 1317 | int _wstat(const MSVCRT_wchar_t* path, struct _stat * buf) | 
|---|
| 1318 | { | 
|---|
| 1319 | DWORD dw; | 
|---|
| 1320 | WIN32_FILE_ATTRIBUTE_DATA hfi; | 
|---|
| 1321 | unsigned short mode = MSVCRT_S_IREAD; | 
|---|
| 1322 | int plen; | 
|---|
| 1323 |  | 
|---|
| 1324 | TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf); | 
|---|
| 1325 |  | 
|---|
| 1326 | if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi)) | 
|---|
| 1327 | { | 
|---|
| 1328 | TRACE("failed (%ld)\n",GetLastError()); | 
|---|
| 1329 | MSVCRT__set_errno(ERROR_FILE_NOT_FOUND); | 
|---|
| 1330 | return -1; | 
|---|
| 1331 | } | 
|---|
| 1332 |  | 
|---|
| 1333 | memset(buf,0,sizeof(struct _stat)); | 
|---|
| 1334 |  | 
|---|
| 1335 | /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */ | 
|---|
| 1336 | if (MSVCRT_iswalpha(*path)) | 
|---|
| 1337 | buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */ | 
|---|
| 1338 | else | 
|---|
| 1339 | buf->st_dev = buf->st_rdev = MSVCRT__getdrive() - 1; | 
|---|
| 1340 |  | 
|---|
| 1341 | plen = strlenW(path); | 
|---|
| 1342 |  | 
|---|
| 1343 | /* Dir, or regular file? */ | 
|---|
| 1344 | if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) || | 
|---|
| 1345 | (path[plen-1] == '\\')) | 
|---|
| 1346 | mode |= (_S_IFDIR | MSVCRT_S_IEXEC); | 
|---|
| 1347 | else | 
|---|
| 1348 | { | 
|---|
| 1349 | mode |= _S_IFREG; | 
|---|
| 1350 | /* executable? */ | 
|---|
| 1351 | if (plen > 6 && path[plen-4] == '.')  /* shortest exe: "\x.exe" */ | 
|---|
| 1352 | { | 
|---|
| 1353 | ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) | | 
|---|
| 1354 | ((ULONGLONG)tolowerW(path[plen-3]) << 32); | 
|---|
| 1355 | if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM) | 
|---|
| 1356 | mode |= MSVCRT_S_IEXEC; | 
|---|
| 1357 | } | 
|---|
| 1358 | } | 
|---|
| 1359 |  | 
|---|
| 1360 | if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY)) | 
|---|
| 1361 | mode |= MSVCRT_S_IWRITE; | 
|---|
| 1362 |  | 
|---|
| 1363 | buf->st_mode  = mode; | 
|---|
| 1364 | buf->st_nlink = 1; | 
|---|
| 1365 | buf->st_size  = hfi.nFileSizeLow; | 
|---|
| 1366 | RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw); | 
|---|
| 1367 | buf->st_atime = dw; | 
|---|
| 1368 | RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw); | 
|---|
| 1369 | buf->st_mtime = buf->st_ctime = dw; | 
|---|
| 1370 | TRACE("\n%d %d %d %ld %ld %ld\n", buf->st_mode,buf->st_nlink,buf->st_size, | 
|---|
| 1371 | buf->st_atime,buf->st_mtime, buf->st_ctime); | 
|---|
| 1372 | return 0; | 
|---|
| 1373 | } | 
|---|
| 1374 |  | 
|---|
| 1375 | /********************************************************************* | 
|---|
| 1376 | *              _tell (MSVCRT.@) | 
|---|
| 1377 | */ | 
|---|
| 1378 | LONG MSVCRT__tell(int fd) | 
|---|
| 1379 | { | 
|---|
| 1380 | return MSVCRT__lseek(fd, 0, SEEK_CUR); | 
|---|
| 1381 | } | 
|---|
| 1382 |  | 
|---|
| 1383 | /********************************************************************* | 
|---|
| 1384 | *              _tempnam (MSVCRT.@) | 
|---|
| 1385 | */ | 
|---|
| 1386 | char *MSVCRT__tempnam(const char *dir, const char *prefix) | 
|---|
| 1387 | { | 
|---|
| 1388 | char tmpbuf[MAX_PATH]; | 
|---|
| 1389 |  | 
|---|
| 1390 | TRACE("dir (%s) prefix (%s)\n",dir,prefix); | 
|---|
| 1391 | if (GetTempFileNameA(dir,prefix,0,tmpbuf)) | 
|---|
| 1392 | { | 
|---|
| 1393 | TRACE("got name (%s)\n",tmpbuf); | 
|---|
| 1394 | return MSVCRT__strdup(tmpbuf); | 
|---|
| 1395 | } | 
|---|
| 1396 | TRACE("failed (%ld)\n",GetLastError()); | 
|---|
| 1397 | return NULL; | 
|---|
| 1398 | } | 
|---|
| 1399 |  | 
|---|
| 1400 | /********************************************************************* | 
|---|
| 1401 | *              _wtempnam (MSVCRT.@) | 
|---|
| 1402 | */ | 
|---|
| 1403 | MSVCRT_wchar_t *_wtempnam(const MSVCRT_wchar_t *dir, const MSVCRT_wchar_t *prefix) | 
|---|
| 1404 | { | 
|---|
| 1405 | MSVCRT_wchar_t tmpbuf[MAX_PATH]; | 
|---|
| 1406 |  | 
|---|
| 1407 | TRACE("dir (%s) prefix (%s)\n",debugstr_w(dir),debugstr_w(prefix)); | 
|---|
| 1408 | if (GetTempFileNameW(dir,prefix,0,tmpbuf)) | 
|---|
| 1409 | { | 
|---|
| 1410 | TRACE("got name (%s)\n",debugstr_w(tmpbuf)); | 
|---|
| 1411 | return _wcsdup(tmpbuf); | 
|---|
| 1412 | } | 
|---|
| 1413 | TRACE("failed (%ld)\n",GetLastError()); | 
|---|
| 1414 | return NULL; | 
|---|
| 1415 | } | 
|---|
| 1416 |  | 
|---|
| 1417 | /********************************************************************* | 
|---|
| 1418 | *              _umask (MSVCRT.@) | 
|---|
| 1419 | */ | 
|---|
| 1420 | int MSVCRT_umask(int umask) | 
|---|
| 1421 | { | 
|---|
| 1422 | int old_umask = MSVCRT__umask; | 
|---|
| 1423 | TRACE("MSVCRT: _umask (%d)\n",umask); | 
|---|
| 1424 | MSVCRT__umask = umask; | 
|---|
| 1425 | return old_umask; | 
|---|
| 1426 | } | 
|---|
| 1427 |  | 
|---|
| 1428 | /********************************************************************* | 
|---|
| 1429 | *              _utime (MSVCRT.@) | 
|---|
| 1430 | */ | 
|---|
| 1431 | int MSVCRT_utime(const char* path, struct _utimbuf *t) | 
|---|
| 1432 | { | 
|---|
| 1433 | int fd = MSVCRT__open(path, _O_WRONLY | _O_BINARY); | 
|---|
| 1434 |  | 
|---|
| 1435 | if (fd > 0) | 
|---|
| 1436 | { | 
|---|
| 1437 | int retVal = _futime(fd, t); | 
|---|
| 1438 | MSVCRT__close(fd); | 
|---|
| 1439 | return retVal; | 
|---|
| 1440 | } | 
|---|
| 1441 | return -1; | 
|---|
| 1442 | } | 
|---|
| 1443 |  | 
|---|
| 1444 | /********************************************************************* | 
|---|
| 1445 | *              _wutime (MSVCRT.@) | 
|---|
| 1446 | */ | 
|---|
| 1447 | int _wutime(const MSVCRT_wchar_t* path, struct _utimbuf *t) | 
|---|
| 1448 | { | 
|---|
| 1449 | int fd = _wopen(path, _O_WRONLY | _O_BINARY); | 
|---|
| 1450 |  | 
|---|
| 1451 | if (fd > 0) | 
|---|
| 1452 | { | 
|---|
| 1453 | int retVal = _futime(fd, t); | 
|---|
| 1454 | MSVCRT__close(fd); | 
|---|
| 1455 | return retVal; | 
|---|
| 1456 | } | 
|---|
| 1457 | return -1; | 
|---|
| 1458 | } | 
|---|
| 1459 |  | 
|---|
| 1460 | /********************************************************************* | 
|---|
| 1461 | *              _write (MSVCRT.@) | 
|---|
| 1462 | */ | 
|---|
| 1463 | int MSVCRT__write(int fd, const void* buf, unsigned int count) | 
|---|
| 1464 | { | 
|---|
| 1465 | DWORD num_written; | 
|---|
| 1466 | HANDLE hand = msvcrt_fdtoh(fd); | 
|---|
| 1467 |  | 
|---|
| 1468 | /* Dont trace small writes, it gets *very* annoying */ | 
|---|
| 1469 | #if 0 | 
|---|
| 1470 | if (count > 32) | 
|---|
| 1471 | TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count); | 
|---|
| 1472 | #endif | 
|---|
| 1473 | if (hand == INVALID_HANDLE_VALUE) | 
|---|
| 1474 | { | 
|---|
| 1475 | *MSVCRT__errno() = MSVCRT_EBADF; | 
|---|
| 1476 | return -1; | 
|---|
| 1477 | } | 
|---|
| 1478 |  | 
|---|
| 1479 | /* If appending, go to EOF */ | 
|---|
| 1480 | if (MSVCRT_flags[fd] & MSVCRT__IOAPPEND) | 
|---|
| 1481 | MSVCRT__lseek(fd, 0, FILE_END); | 
|---|
| 1482 |  | 
|---|
| 1483 | if (MSVCRT_flags[fd] & _O_BINARY) | 
|---|
| 1484 | { | 
|---|
| 1485 | if (WriteFile(hand, buf, count, &num_written, NULL) | 
|---|
| 1486 | &&  (num_written >= count)) | 
|---|
| 1487 | return num_written; | 
|---|
| 1488 | TRACE(":failed-last error (%ld)\n",GetLastError()); | 
|---|
| 1489 | if (MSVCRT_files[fd]) | 
|---|
| 1490 | { | 
|---|
| 1491 | MSVCRT_files[fd]->_flag |= MSVCRT__IOERR; | 
|---|
| 1492 | *MSVCRT__errno() = MSVCRT_ENOSPC; | 
|---|
| 1493 | } | 
|---|
| 1494 | } | 
|---|
| 1495 | else | 
|---|
| 1496 | { | 
|---|
| 1497 | char *s=(char*)buf, *buf_start=(char*)buf, *p; | 
|---|
| 1498 | char crlf[]= {'\r','\n'}; | 
|---|
| 1499 | unsigned int i; | 
|---|
| 1500 | DWORD num_to_write; | 
|---|
| 1501 | for (i = 0; i< count && !(MSVCRT_flags[fd] & MSVCRT__IOERR);i++, s++) | 
|---|
| 1502 | { | 
|---|
| 1503 | if (*s == '\n') | 
|---|
| 1504 | { | 
|---|
| 1505 | p = crlf; | 
|---|
| 1506 | num_to_write = 2; | 
|---|
| 1507 | } | 
|---|
| 1508 | else | 
|---|
| 1509 | { | 
|---|
| 1510 | p = s; | 
|---|
| 1511 | num_to_write = 1; | 
|---|
| 1512 | } | 
|---|
| 1513 | if ((WriteFile(hand, p, num_to_write, &num_written, NULL) == 0 ) || (num_written != num_to_write)) | 
|---|
| 1514 | { | 
|---|
| 1515 | TRACE(":failed-last error (%ld) num_written %ld\n",GetLastError(),num_written); | 
|---|
| 1516 | if (MSVCRT_files[fd]) | 
|---|
| 1517 | { | 
|---|
| 1518 | MSVCRT_files[fd]->_flag |= MSVCRT__IOERR; | 
|---|
| 1519 | *MSVCRT__errno() = MSVCRT_ENOSPC; | 
|---|
| 1520 | return s - buf_start; | 
|---|
| 1521 | } | 
|---|
| 1522 | } | 
|---|
| 1523 | } | 
|---|
| 1524 | return s - buf_start; | 
|---|
| 1525 | } | 
|---|
| 1526 | return -1; | 
|---|
| 1527 | } | 
|---|
| 1528 |  | 
|---|
| 1529 | /********************************************************************* | 
|---|
| 1530 | *              _putw (MSVCRT.@) | 
|---|
| 1531 | */ | 
|---|
| 1532 | int MSVCRT__putw(int val, MSVCRT_FILE* file) | 
|---|
| 1533 | { | 
|---|
| 1534 | return MSVCRT__write(file->_file, &val, sizeof(val)) == 1? val : MSVCRT_EOF; | 
|---|
| 1535 | } | 
|---|
| 1536 |  | 
|---|
| 1537 | /********************************************************************* | 
|---|
| 1538 | *              clearerr (MSVCRT.@) | 
|---|
| 1539 | */ | 
|---|
| 1540 | void MSVCRT_clearerr(MSVCRT_FILE* file) | 
|---|
| 1541 | { | 
|---|
| 1542 | TRACE(":file (%p) fd (%d)\n",file,file->_file); | 
|---|
| 1543 | file->_flag &= ~(MSVCRT__IOERR | MSVCRT__IOEOF); | 
|---|
| 1544 | } | 
|---|
| 1545 |  | 
|---|
| 1546 | /********************************************************************* | 
|---|
| 1547 | *              fclose (MSVCRT.@) | 
|---|
| 1548 | */ | 
|---|
| 1549 | int MSVCRT_fclose(MSVCRT_FILE* file) | 
|---|
| 1550 | { | 
|---|
| 1551 | int r; | 
|---|
| 1552 | r=MSVCRT__close(file->_file); | 
|---|
| 1553 | return ((r==MSVCRT_EOF) || (file->_flag & MSVCRT__IOERR) ? MSVCRT_EOF : 0); | 
|---|
| 1554 | } | 
|---|
| 1555 |  | 
|---|
| 1556 | /********************************************************************* | 
|---|
| 1557 | *              feof (MSVCRT.@) | 
|---|
| 1558 | */ | 
|---|
| 1559 | int MSVCRT_feof(MSVCRT_FILE* file) | 
|---|
| 1560 | { | 
|---|
| 1561 | return file->_flag & MSVCRT__IOEOF; | 
|---|
| 1562 | } | 
|---|
| 1563 |  | 
|---|
| 1564 | /********************************************************************* | 
|---|
| 1565 | *              ferror (MSVCRT.@) | 
|---|
| 1566 | */ | 
|---|
| 1567 | int MSVCRT_ferror(MSVCRT_FILE* file) | 
|---|
| 1568 | { | 
|---|
| 1569 | return file->_flag & MSVCRT__IOERR; | 
|---|
| 1570 | } | 
|---|
| 1571 |  | 
|---|
| 1572 | /********************************************************************* | 
|---|
| 1573 | *              fflush (MSVCRT.@) | 
|---|
| 1574 | */ | 
|---|
| 1575 | int MSVCRT_fflush(MSVCRT_FILE* file) | 
|---|
| 1576 | { | 
|---|
| 1577 | if(!file) { | 
|---|
| 1578 | MSVCRT__flushall(); | 
|---|
| 1579 | return 0; | 
|---|
| 1580 | } else { | 
|---|
| 1581 | int res=msvcrt_flush_buffer(file); | 
|---|
| 1582 | return res; | 
|---|
| 1583 | } | 
|---|
| 1584 | } | 
|---|
| 1585 |  | 
|---|
| 1586 | /********************************************************************* | 
|---|
| 1587 | *              fgetc (MSVCRT.@) | 
|---|
| 1588 | */ | 
|---|
| 1589 | int MSVCRT_fgetc(MSVCRT_FILE* file) | 
|---|
| 1590 | { | 
|---|
| 1591 | if (file->_cnt>0) { | 
|---|
| 1592 | file->_cnt--; | 
|---|
| 1593 | return *(unsigned char *)file->_ptr++; | 
|---|
| 1594 | } else { | 
|---|
| 1595 | return _filbuf(file); | 
|---|
| 1596 | } | 
|---|
| 1597 | } | 
|---|
| 1598 |  | 
|---|
| 1599 | /********************************************************************* | 
|---|
| 1600 | *              _fgetchar (MSVCRT.@) | 
|---|
| 1601 | */ | 
|---|
| 1602 | int MSVCRT__fgetchar(void) | 
|---|
| 1603 | { | 
|---|
| 1604 | return MSVCRT_fgetc(MSVCRT_stdin); | 
|---|
| 1605 | } | 
|---|
| 1606 |  | 
|---|
| 1607 | /********************************************************************* | 
|---|
| 1608 | *              _filbuf (MSVCRT.@) | 
|---|
| 1609 | */ | 
|---|
| 1610 | int _filbuf(MSVCRT_FILE* file) | 
|---|
| 1611 | { | 
|---|
| 1612 |  | 
|---|
| 1613 | /* Allocate buffer if needed */ | 
|---|
| 1614 | if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF) ) { | 
|---|
| 1615 | msvcrt_alloc_buffer(file); | 
|---|
| 1616 | } | 
|---|
| 1617 | if(!(file->_flag & MSVCRT__IOREAD)) { | 
|---|
| 1618 | if(file->_flag & MSVCRT__IORW) { | 
|---|
| 1619 | file->_flag |= MSVCRT__IOREAD; | 
|---|
| 1620 | } else { | 
|---|
| 1621 | return MSVCRT_EOF; | 
|---|
| 1622 | } | 
|---|
| 1623 | } | 
|---|
| 1624 | if(file->_flag & MSVCRT__IONBF) { | 
|---|
| 1625 | unsigned char c; | 
|---|
| 1626 | if (MSVCRT__read(file->_file,&c,1) != 1) { | 
|---|
| 1627 | file->_flag |= MSVCRT__IOEOF; | 
|---|
| 1628 | return MSVCRT_EOF; | 
|---|
| 1629 | } | 
|---|
| 1630 | return c; | 
|---|
| 1631 | } else { | 
|---|
| 1632 | file->_cnt = MSVCRT__read(file->_file, file->_base, file->_bufsiz); | 
|---|
| 1633 | if(file->_cnt<0) file->_cnt = 0; | 
|---|
| 1634 | if(!file->_cnt) { | 
|---|
| 1635 | file->_flag |= MSVCRT__IOEOF; | 
|---|
| 1636 | return MSVCRT_EOF; | 
|---|
| 1637 | } | 
|---|
| 1638 | file->_cnt--; | 
|---|
| 1639 | file->_ptr = file->_base+1; | 
|---|
| 1640 | return *(unsigned char *)file->_base; | 
|---|
| 1641 | } | 
|---|
| 1642 | } | 
|---|
| 1643 |  | 
|---|
| 1644 | /********************************************************************* | 
|---|
| 1645 | *              fgetpos (MSVCRT.@) | 
|---|
| 1646 | */ | 
|---|
| 1647 | int MSVCRT_fgetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos) | 
|---|
| 1648 | { | 
|---|
| 1649 | *pos = MSVCRT_ftell(file); | 
|---|
| 1650 | return (*pos == -1? -1 : 0); | 
|---|
| 1651 | } | 
|---|
| 1652 |  | 
|---|
| 1653 | /********************************************************************* | 
|---|
| 1654 | *              fgets (MSVCRT.@) | 
|---|
| 1655 | */ | 
|---|
| 1656 | char *MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file) | 
|---|
| 1657 | { | 
|---|
| 1658 | int    cc; | 
|---|
| 1659 | char * buf_start = s; | 
|---|
| 1660 |  | 
|---|
| 1661 | TRACE(":file(%p) fd (%d) str (%p) len (%d)\n", | 
|---|
| 1662 | file,file->_file,s,size); | 
|---|
| 1663 |  | 
|---|
| 1664 | for(cc = MSVCRT_fgetc(file); cc != MSVCRT_EOF && cc != '\n'; | 
|---|
| 1665 | cc = MSVCRT_fgetc(file)) | 
|---|
| 1666 | if (cc != '\r') | 
|---|
| 1667 | { | 
|---|
| 1668 | if (--size <= 0) break; | 
|---|
| 1669 | *s++ = (char)cc; | 
|---|
| 1670 | } | 
|---|
| 1671 | if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/ | 
|---|
| 1672 | { | 
|---|
| 1673 | TRACE(":nothing read\n"); | 
|---|
| 1674 | return 0; | 
|---|
| 1675 | } | 
|---|
| 1676 | if (cc == '\n') | 
|---|
| 1677 | if (--size > 0) | 
|---|
| 1678 | *s++ = '\n'; | 
|---|
| 1679 | *s = '\0'; | 
|---|
| 1680 | TRACE(":got '%s'\n", buf_start); | 
|---|
| 1681 | return buf_start; | 
|---|
| 1682 | } | 
|---|
| 1683 |  | 
|---|
| 1684 | /********************************************************************* | 
|---|
| 1685 | *              fgetwc (MSVCRT.@) | 
|---|
| 1686 | * | 
|---|
| 1687 | * In _O_TEXT mode, bultibyte characters are read from the file, dropping | 
|---|
| 1688 | * the CR from CR/LF combinations | 
|---|
| 1689 | */ | 
|---|
| 1690 | MSVCRT_wint_t MSVCRT_fgetwc(MSVCRT_FILE* file) | 
|---|
| 1691 | { | 
|---|
| 1692 | char c; | 
|---|
| 1693 |  | 
|---|
| 1694 | if (file->_flag & _O_BINARY) | 
|---|
| 1695 | { | 
|---|
| 1696 | MSVCRT_wchar_t wc; | 
|---|
| 1697 | if (MSVCRT__read(file->_file, &wc, sizeof(wc)) != sizeof(wc)) | 
|---|
| 1698 | return MSVCRT_WEOF; | 
|---|
| 1699 | return wc; | 
|---|
| 1700 | } | 
|---|
| 1701 | c = MSVCRT_fgetc(file); | 
|---|
| 1702 | if ((*__p___mb_cur_max() > 1) && MSVCRT_isleadbyte(c)) | 
|---|
| 1703 | { | 
|---|
| 1704 | FIXME("Treat Multibyte characters\n"); | 
|---|
| 1705 | } | 
|---|
| 1706 | if (c == MSVCRT_EOF) | 
|---|
| 1707 | return MSVCRT_WEOF; | 
|---|
| 1708 | else | 
|---|
| 1709 | return (MSVCRT_wint_t)c; | 
|---|
| 1710 | } | 
|---|
| 1711 |  | 
|---|
| 1712 | /********************************************************************* | 
|---|
| 1713 | *              getwc (MSVCRT.@) | 
|---|
| 1714 | */ | 
|---|
| 1715 | MSVCRT_wint_t MSVCRT_getwc(MSVCRT_FILE* file) | 
|---|
| 1716 | { | 
|---|
| 1717 | return MSVCRT_fgetwc(file); | 
|---|
| 1718 | } | 
|---|
| 1719 |  | 
|---|
| 1720 | /********************************************************************* | 
|---|
| 1721 | *              _fgetwchar (MSVCRT.@) | 
|---|
| 1722 | */ | 
|---|
| 1723 | MSVCRT_wint_t _fgetwchar(void) | 
|---|
| 1724 | { | 
|---|
| 1725 | return MSVCRT_fgetwc(MSVCRT_stdin); | 
|---|
| 1726 | } | 
|---|
| 1727 |  | 
|---|
| 1728 | /********************************************************************* | 
|---|
| 1729 | *              getwchar (MSVCRT.@) | 
|---|
| 1730 | */ | 
|---|
| 1731 | MSVCRT_wint_t MSVCRT_getwchar(void) | 
|---|
| 1732 | { | 
|---|
| 1733 | return _fgetwchar(); | 
|---|
| 1734 | } | 
|---|
| 1735 |  | 
|---|
| 1736 | /********************************************************************* | 
|---|
| 1737 | *              fgetws (MSVCRT.@) | 
|---|
| 1738 | */ | 
|---|
| 1739 | MSVCRT_wchar_t *MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file) | 
|---|
| 1740 | { | 
|---|
| 1741 | int    cc; | 
|---|
| 1742 | MSVCRT_wchar_t * buf_start = s; | 
|---|
| 1743 |  | 
|---|
| 1744 | TRACE(":file(%p) fd (%d) str (%p) len (%d)\n", | 
|---|
| 1745 | file,file->_file,s,size); | 
|---|
| 1746 |  | 
|---|
| 1747 | for(cc = MSVCRT_fgetwc(file); cc != MSVCRT_WEOF && cc != L'\n'; | 
|---|
| 1748 | cc = MSVCRT_fgetwc(file)) | 
|---|
| 1749 | if (cc != L'\r') | 
|---|
| 1750 | { | 
|---|
| 1751 | if (--size <= 0) break; | 
|---|
| 1752 | *s++ = cc; | 
|---|
| 1753 | } | 
|---|
| 1754 | if ((cc == MSVCRT_EOF) && (s == buf_start)) /* If nothing read, return 0*/ | 
|---|
| 1755 | { | 
|---|
| 1756 | TRACE(":nothing read\n"); | 
|---|
| 1757 | return 0; | 
|---|
| 1758 | } | 
|---|
| 1759 | if (cc == L'\n') | 
|---|
| 1760 | if (--size > 0) | 
|---|
| 1761 | *s++ = '\n'; | 
|---|
| 1762 | *s = '\0'; | 
|---|
| 1763 | /*  TRACE(":got '%s'\n", buf_start); */ | 
|---|
| 1764 | return buf_start; | 
|---|
| 1765 | } | 
|---|
| 1766 |  | 
|---|
| 1767 |  | 
|---|
| 1768 | /********************************************************************* | 
|---|
| 1769 | *              fputwc (MSVCRT.@) | 
|---|
| 1770 | */ | 
|---|
| 1771 | MSVCRT_wint_t MSVCRT_fputwc(MSVCRT_wint_t wc, MSVCRT_FILE* file) | 
|---|
| 1772 | { | 
|---|
| 1773 | MSVCRT_wchar_t mwc=wc; | 
|---|
| 1774 | if (MSVCRT_fwrite( &mwc, sizeof(mwc), 1, file) != 1) | 
|---|
| 1775 | return MSVCRT_WEOF; | 
|---|
| 1776 | return wc; | 
|---|
| 1777 | } | 
|---|
| 1778 |  | 
|---|
| 1779 | /********************************************************************* | 
|---|
| 1780 | *              _fputwchar (MSVCRT.@) | 
|---|
| 1781 | */ | 
|---|
| 1782 | MSVCRT_wint_t _fputwchar(MSVCRT_wint_t wc) | 
|---|
| 1783 | { | 
|---|
| 1784 | return MSVCRT_fputwc(wc, MSVCRT_stdout); | 
|---|
| 1785 | } | 
|---|
| 1786 |  | 
|---|
| 1787 | /********************************************************************* | 
|---|
| 1788 | *              fopen (MSVCRT.@) | 
|---|
| 1789 | */ | 
|---|
| 1790 | MSVCRT_FILE* MSVCRT_fopen(const char *path, const char *mode) | 
|---|
| 1791 | { | 
|---|
| 1792 | MSVCRT_FILE* file; | 
|---|
| 1793 | int flags = 0, plus = 0, fd; | 
|---|
| 1794 | const char* search = mode; | 
|---|
| 1795 |  | 
|---|
| 1796 | TRACE("(%s,%s)\n",path,mode); | 
|---|
| 1797 |  | 
|---|
| 1798 | while (*search) | 
|---|
| 1799 | if (*search++ == '+') | 
|---|
| 1800 | plus = 1; | 
|---|
| 1801 |  | 
|---|
| 1802 | /* map mode string to open() flags. "man fopen" for possibilities. */ | 
|---|
| 1803 | switch(*mode++) | 
|---|
| 1804 | { | 
|---|
| 1805 | case 'R': case 'r': | 
|---|
| 1806 | flags = (plus ? _O_RDWR : _O_RDONLY); | 
|---|
| 1807 | break; | 
|---|
| 1808 | case 'W': case 'w': | 
|---|
| 1809 | flags = _O_CREAT | _O_TRUNC | (plus  ? _O_RDWR : _O_WRONLY); | 
|---|
| 1810 | break; | 
|---|
| 1811 | case 'A': case 'a': | 
|---|
| 1812 | flags = _O_CREAT | _O_APPEND | (plus  ? _O_RDWR : _O_WRONLY); | 
|---|
| 1813 | break; | 
|---|
| 1814 | default: | 
|---|
| 1815 | return NULL; | 
|---|
| 1816 | } | 
|---|
| 1817 |  | 
|---|
| 1818 | while (*mode) | 
|---|
| 1819 | switch (*mode++) | 
|---|
| 1820 | { | 
|---|
| 1821 | case 'B': case 'b': | 
|---|
| 1822 | flags |=  _O_BINARY; | 
|---|
| 1823 | flags &= ~_O_TEXT; | 
|---|
| 1824 | break; | 
|---|
| 1825 | case 'T': case 't': | 
|---|
| 1826 | flags |=  _O_TEXT; | 
|---|
| 1827 | flags &= ~_O_BINARY; | 
|---|
| 1828 | break; | 
|---|
| 1829 | case '+': | 
|---|
| 1830 | break; | 
|---|
| 1831 | default: | 
|---|
| 1832 | FIXME(":unknown flag %c not supported\n",mode[-1]); | 
|---|
| 1833 | } | 
|---|
| 1834 |  | 
|---|
| 1835 | fd = MSVCRT__open(path, flags); | 
|---|
| 1836 |  | 
|---|
| 1837 | if (fd < 0) | 
|---|
| 1838 | return NULL; | 
|---|
| 1839 |  | 
|---|
| 1840 | file = msvcrt_alloc_fp(fd); | 
|---|
| 1841 | TRACE(":got (%p)\n",file); | 
|---|
| 1842 | if (!file) | 
|---|
| 1843 | MSVCRT__close(fd); | 
|---|
| 1844 |  | 
|---|
| 1845 | return file; | 
|---|
| 1846 | } | 
|---|
| 1847 |  | 
|---|
| 1848 | /********************************************************************* | 
|---|
| 1849 | *              _wfopen (MSVCRT.@) | 
|---|
| 1850 | */ | 
|---|
| 1851 | MSVCRT_FILE *_wfopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode) | 
|---|
| 1852 | { | 
|---|
| 1853 | const unsigned int plen = strlenW(path), mlen = strlenW(mode); | 
|---|
| 1854 | char *patha = MSVCRT_calloc(plen + 1, 1); | 
|---|
| 1855 | char *modea = MSVCRT_calloc(mlen + 1, 1); | 
|---|
| 1856 |  | 
|---|
| 1857 | TRACE("(%s,%s)\n",debugstr_w(path),debugstr_w(mode)); | 
|---|
| 1858 |  | 
|---|
| 1859 | if (patha && modea && | 
|---|
| 1860 | WideCharToMultiByte(CP_ACP,0,path,plen,patha,plen,NULL,NULL) && | 
|---|
| 1861 | WideCharToMultiByte(CP_ACP,0,mode,mlen,modea,mlen,NULL,NULL)) | 
|---|
| 1862 | { | 
|---|
| 1863 | MSVCRT_FILE *retval = MSVCRT_fopen(patha,modea); | 
|---|
| 1864 | MSVCRT_free(patha); | 
|---|
| 1865 | MSVCRT_free(modea); | 
|---|
| 1866 | return retval; | 
|---|
| 1867 | } | 
|---|
| 1868 |  | 
|---|
| 1869 | MSVCRT__set_errno(GetLastError()); | 
|---|
| 1870 | return NULL; | 
|---|
| 1871 | } | 
|---|
| 1872 |  | 
|---|
| 1873 | /********************************************************************* | 
|---|
| 1874 | *              _fsopen (MSVCRT.@) | 
|---|
| 1875 | */ | 
|---|
| 1876 | MSVCRT_FILE*  MSVCRT__fsopen(const char *path, const char *mode, int share) | 
|---|
| 1877 | { | 
|---|
| 1878 | FIXME(":(%s,%s,%d),ignoring share mode!\n",path,mode,share); | 
|---|
| 1879 | return MSVCRT_fopen(path,mode); | 
|---|
| 1880 | } | 
|---|
| 1881 |  | 
|---|
| 1882 | /********************************************************************* | 
|---|
| 1883 | *              _wfsopen (MSVCRT.@) | 
|---|
| 1884 | */ | 
|---|
| 1885 | MSVCRT_FILE*  _wfsopen(const MSVCRT_wchar_t *path, const MSVCRT_wchar_t *mode, int share) | 
|---|
| 1886 | { | 
|---|
| 1887 | FIXME(":(%s,%s,%d),ignoring share mode!\n", | 
|---|
| 1888 | debugstr_w(path),debugstr_w(mode),share); | 
|---|
| 1889 | return _wfopen(path,mode); | 
|---|
| 1890 | } | 
|---|
| 1891 |  | 
|---|
| 1892 | /********************************************************************* | 
|---|
| 1893 | *              fputc (MSVCRT.@) | 
|---|
| 1894 | */ | 
|---|
| 1895 | int MSVCRT_fputc(int c, MSVCRT_FILE* file) | 
|---|
| 1896 | { | 
|---|
| 1897 | if(file->_cnt>0) { | 
|---|
| 1898 | *file->_ptr++=c; | 
|---|
| 1899 | file->_cnt--; | 
|---|
| 1900 | return c; | 
|---|
| 1901 | } else { | 
|---|
| 1902 | return _flsbuf(c, file); | 
|---|
| 1903 | } | 
|---|
| 1904 | } | 
|---|
| 1905 |  | 
|---|
| 1906 | /********************************************************************* | 
|---|
| 1907 | *              _flsbuf (MSVCRT.@) | 
|---|
| 1908 | */ | 
|---|
| 1909 | int _flsbuf(int c, MSVCRT_FILE* file) | 
|---|
| 1910 | { | 
|---|
| 1911 | /* Flush output buffer */ | 
|---|
| 1912 | if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) { | 
|---|
| 1913 | msvcrt_alloc_buffer(file); | 
|---|
| 1914 | } | 
|---|
| 1915 | if(!(file->_flag & MSVCRT__IOWRT)) { | 
|---|
| 1916 | if(file->_flag & MSVCRT__IORW) { | 
|---|
| 1917 | file->_flag |= MSVCRT__IOWRT; | 
|---|
| 1918 | } else { | 
|---|
| 1919 | return MSVCRT_EOF; | 
|---|
| 1920 | } | 
|---|
| 1921 | } | 
|---|
| 1922 | if(file->_bufsiz) { | 
|---|
| 1923 | int res=msvcrt_flush_buffer(file); | 
|---|
| 1924 | return res?res : MSVCRT_fputc(c, file); | 
|---|
| 1925 | } else { | 
|---|
| 1926 | unsigned char cc=c; | 
|---|
| 1927 | return MSVCRT__write(file->_file, &cc, 1) == 1? c : MSVCRT_EOF; | 
|---|
| 1928 | } | 
|---|
| 1929 | } | 
|---|
| 1930 |  | 
|---|
| 1931 | /********************************************************************* | 
|---|
| 1932 | *              _fputchar (MSVCRT.@) | 
|---|
| 1933 | */ | 
|---|
| 1934 | int MSVCRT__fputchar(int c) | 
|---|
| 1935 | { | 
|---|
| 1936 | return MSVCRT_fputc(c, MSVCRT_stdout); | 
|---|
| 1937 | } | 
|---|
| 1938 |  | 
|---|
| 1939 | /********************************************************************* | 
|---|
| 1940 | *              fread (MSVCRT.@) | 
|---|
| 1941 | */ | 
|---|
| 1942 | MSVCRT_size_t MSVCRT_fread(void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file) | 
|---|
| 1943 | { MSVCRT_size_t rcnt=size * nmemb; | 
|---|
| 1944 | MSVCRT_size_t read=0; | 
|---|
| 1945 | int pread=0; | 
|---|
| 1946 | /* first buffered data */ | 
|---|
| 1947 | if(file->_cnt>0) { | 
|---|
| 1948 | int pcnt= (rcnt>file->_cnt)? file->_cnt:rcnt; | 
|---|
| 1949 | memcpy(ptr, file->_ptr, pcnt); | 
|---|
| 1950 | file->_cnt -= pcnt; | 
|---|
| 1951 | file->_ptr += pcnt; | 
|---|
| 1952 | read += pcnt ; | 
|---|
| 1953 | rcnt -= pcnt ; | 
|---|
| 1954 | ptr = (char*)ptr + pcnt; | 
|---|
| 1955 | } else if(!(file->_flag & MSVCRT__IOREAD )) { | 
|---|
| 1956 | if(file->_flag & MSVCRT__IORW) { | 
|---|
| 1957 | file->_flag |= MSVCRT__IOREAD; | 
|---|
| 1958 | } else | 
|---|
| 1959 | return 0; | 
|---|
| 1960 | } | 
|---|
| 1961 | if(rcnt) pread = MSVCRT__read(file->_file,ptr, rcnt); | 
|---|
| 1962 | if (MSVCRT_flags[file->_file] & MSVCRT__IOEOF) | 
|---|
| 1963 | /* expose feof condition in the flags | 
|---|
| 1964 | MFC tests file->_flag for feof, and doesn't not call feof()) | 
|---|
| 1965 | */ | 
|---|
| 1966 | file->_flag |= MSVCRT__IOEOF; | 
|---|
| 1967 | if (pread <= 0) | 
|---|
| 1968 | pread = 0; | 
|---|
| 1969 | read+=pread; | 
|---|
| 1970 | return read / size; | 
|---|
| 1971 | } | 
|---|
| 1972 |  | 
|---|
| 1973 | /********************************************************************* | 
|---|
| 1974 | *              freopen (MSVCRT.@) | 
|---|
| 1975 | * | 
|---|
| 1976 | */ | 
|---|
| 1977 | MSVCRT_FILE* MSVCRT_freopen(const char *path, const char *mode,MSVCRT_FILE* file) | 
|---|
| 1978 | { | 
|---|
| 1979 | MSVCRT_FILE* newfile; | 
|---|
| 1980 | int fd; | 
|---|
| 1981 |  | 
|---|
| 1982 | TRACE(":path (%p) mode (%s) file (%p) fd (%d)\n",path,mode,file,file->_file); | 
|---|
| 1983 | if (!file || ((fd = file->_file) < 0) || fd > MSVCRT_fdend) | 
|---|
| 1984 | return NULL; | 
|---|
| 1985 |  | 
|---|
| 1986 | if (fd > 2) | 
|---|
| 1987 | { | 
|---|
| 1988 | #if 0 | 
|---|
| 1989 | FIXME(":reopen on user file not implemented!\n"); | 
|---|
| 1990 | MSVCRT__set_errno(ERROR_CALL_NOT_IMPLEMENTED); | 
|---|
| 1991 | return NULL; | 
|---|
| 1992 | #endif | 
|---|
| 1993 | if(MSVCRT_fclose(file)) | 
|---|
| 1994 | return NULL; | 
|---|
| 1995 | return MSVCRT_fopen(path, mode); | 
|---|
| 1996 | } | 
|---|
| 1997 |  | 
|---|
| 1998 | /* first, create the new file */ | 
|---|
| 1999 | if ((newfile = MSVCRT_fopen(path,mode)) == NULL) | 
|---|
| 2000 | return NULL; | 
|---|
| 2001 |  | 
|---|
| 2002 | if (fd < 3 && SetStdHandle(fd == 0 ? STD_INPUT_HANDLE : | 
|---|
| 2003 | (fd == 1? STD_OUTPUT_HANDLE : STD_ERROR_HANDLE), | 
|---|
| 2004 | MSVCRT_handles[newfile->_file])) | 
|---|
| 2005 | { | 
|---|
| 2006 | /* Redirecting std handle to file , copy over.. */ | 
|---|
| 2007 | MSVCRT_handles[fd] = MSVCRT_handles[newfile->_file]; | 
|---|
| 2008 | MSVCRT_flags[fd] = MSVCRT_flags[newfile->_file]; | 
|---|
| 2009 | memcpy(&MSVCRT__iob[fd], newfile, sizeof (MSVCRT_FILE)); | 
|---|
| 2010 | MSVCRT__iob[fd]._file = fd; | 
|---|
| 2011 | /* And free up the resources allocated by fopen, but | 
|---|
| 2012 | * not the HANDLE we copied. */ | 
|---|
| 2013 | MSVCRT_free(MSVCRT_files[fd]); | 
|---|
| 2014 | msvcrt_free_fd(newfile->_file); | 
|---|
| 2015 | return &MSVCRT__iob[fd]; | 
|---|
| 2016 | } | 
|---|
| 2017 |  | 
|---|
| 2018 | WARN(":failed-last error (%ld)\n",GetLastError()); | 
|---|
| 2019 | MSVCRT_fclose(newfile); | 
|---|
| 2020 | MSVCRT__set_errno(GetLastError()); | 
|---|
| 2021 | return NULL; | 
|---|
| 2022 | } | 
|---|
| 2023 |  | 
|---|
| 2024 | /********************************************************************* | 
|---|
| 2025 | *              fsetpos (MSVCRT.@) | 
|---|
| 2026 | */ | 
|---|
| 2027 | int MSVCRT_fsetpos(MSVCRT_FILE* file, MSVCRT_fpos_t *pos) | 
|---|
| 2028 | { | 
|---|
| 2029 | return MSVCRT__lseek(file->_file,*pos,SEEK_SET); | 
|---|
| 2030 | } | 
|---|
| 2031 |  | 
|---|
| 2032 | /********************************************************************* | 
|---|
| 2033 | *              fseek (MSVCRT.@) | 
|---|
| 2034 | */ | 
|---|
| 2035 | int MSVCRT_fseek(MSVCRT_FILE* file, long offset, int whence) | 
|---|
| 2036 | { | 
|---|
| 2037 | /* Flush output if needed */ | 
|---|
| 2038 | if(file->_flag & MSVCRT__IOWRT) | 
|---|
| 2039 | msvcrt_flush_buffer(file); | 
|---|
| 2040 |  | 
|---|
| 2041 | if(whence == SEEK_CUR && file->_flag & MSVCRT__IOREAD ) { | 
|---|
| 2042 | offset -= file->_cnt; | 
|---|
| 2043 | } | 
|---|
| 2044 | /* Discard buffered input */ | 
|---|
| 2045 | file->_cnt = 0; | 
|---|
| 2046 | file->_ptr = file->_base; | 
|---|
| 2047 | /* Reset direction of i/o */ | 
|---|
| 2048 | if(file->_flag & MSVCRT__IORW) { | 
|---|
| 2049 | file->_flag &= ~(MSVCRT__IOREAD|MSVCRT__IOWRT); | 
|---|
| 2050 | } | 
|---|
| 2051 | return (MSVCRT__lseek(file->_file,offset,whence) == -1)?-1:0; | 
|---|
| 2052 | } | 
|---|
| 2053 |  | 
|---|
| 2054 | /********************************************************************* | 
|---|
| 2055 | *              ftell (MSVCRT.@) | 
|---|
| 2056 | */ | 
|---|
| 2057 | LONG MSVCRT_ftell(MSVCRT_FILE* file) | 
|---|
| 2058 | { | 
|---|
| 2059 | int off=0; | 
|---|
| 2060 | long pos; | 
|---|
| 2061 | if(file->_bufsiz)  { | 
|---|
| 2062 | if( file->_flag & MSVCRT__IOWRT ) { | 
|---|
| 2063 | off = file->_ptr - file->_base; | 
|---|
| 2064 | } else { | 
|---|
| 2065 | off = -file->_cnt; | 
|---|
| 2066 | } | 
|---|
| 2067 | } | 
|---|
| 2068 | pos = MSVCRT__tell(file->_file); | 
|---|
| 2069 | if(pos == -1) return pos; | 
|---|
| 2070 | return off + pos; | 
|---|
| 2071 | } | 
|---|
| 2072 |  | 
|---|
| 2073 | /********************************************************************* | 
|---|
| 2074 | *              fwrite (MSVCRT.@) | 
|---|
| 2075 | */ | 
|---|
| 2076 | MSVCRT_size_t MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t nmemb, MSVCRT_FILE* file) | 
|---|
| 2077 | { | 
|---|
| 2078 | MSVCRT_size_t wrcnt=size * nmemb; | 
|---|
| 2079 | int written = 0; | 
|---|
| 2080 | if (size == 0) | 
|---|
| 2081 | return 0; | 
|---|
| 2082 | if(file->_cnt) { | 
|---|
| 2083 | int pcnt=(file->_cnt>wrcnt)? wrcnt: file->_cnt; | 
|---|
| 2084 | memcpy(file->_ptr, ptr, pcnt); | 
|---|
| 2085 | file->_cnt -= pcnt; | 
|---|
| 2086 | file->_ptr += pcnt; | 
|---|
| 2087 | written = pcnt; | 
|---|
| 2088 | wrcnt -= pcnt; | 
|---|
| 2089 | ptr = (char*)ptr + pcnt; | 
|---|
| 2090 | } else if(!(file->_flag & MSVCRT__IOWRT)) { | 
|---|
| 2091 | if(file->_flag & MSVCRT__IORW) { | 
|---|
| 2092 | file->_flag |= MSVCRT__IOWRT; | 
|---|
| 2093 | } else | 
|---|
| 2094 | return 0; | 
|---|
| 2095 | } | 
|---|
| 2096 | if(wrcnt) { | 
|---|
| 2097 | /* Flush buffer */ | 
|---|
| 2098 | int res=msvcrt_flush_buffer(file); | 
|---|
| 2099 | if(!res) { | 
|---|
| 2100 | int pwritten = MSVCRT__write(file->_file, ptr, wrcnt); | 
|---|
| 2101 | if (pwritten <= 0) pwritten=0; | 
|---|
| 2102 | written += pwritten; | 
|---|
| 2103 | } | 
|---|
| 2104 | } | 
|---|
| 2105 | return written / size; | 
|---|
| 2106 | } | 
|---|
| 2107 |  | 
|---|
| 2108 | /********************************************************************* | 
|---|
| 2109 | *              fputs (MSVCRT.@) | 
|---|
| 2110 | */ | 
|---|
| 2111 | int MSVCRT_fputs(const char *s, MSVCRT_FILE* file) | 
|---|
| 2112 | { | 
|---|
| 2113 | size_t i, len = strlen(s); | 
|---|
| 2114 | if (file->_flag & _O_BINARY) | 
|---|
| 2115 | return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF; | 
|---|
| 2116 | for (i=0; i<len; i++) | 
|---|
| 2117 | if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF) | 
|---|
| 2118 | return MSVCRT_EOF; | 
|---|
| 2119 | return 0; | 
|---|
| 2120 | } | 
|---|
| 2121 |  | 
|---|
| 2122 | /********************************************************************* | 
|---|
| 2123 | *              fputws (MSVCRT.@) | 
|---|
| 2124 | */ | 
|---|
| 2125 | int MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file) | 
|---|
| 2126 | { | 
|---|
| 2127 | size_t i, len = strlenW(s); | 
|---|
| 2128 | if (file->_flag & _O_BINARY) | 
|---|
| 2129 | return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF; | 
|---|
| 2130 | for (i=0; i<len; i++) | 
|---|
| 2131 | { | 
|---|
| 2132 | if ((s[i] == L'\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF)) | 
|---|
| 2133 | return MSVCRT_WEOF; | 
|---|
| 2134 | if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF) | 
|---|
| 2135 | return MSVCRT_WEOF; | 
|---|
| 2136 | } | 
|---|
| 2137 | return 0; | 
|---|
| 2138 | } | 
|---|
| 2139 |  | 
|---|
| 2140 |  | 
|---|
| 2141 | /********************************************************************* | 
|---|
| 2142 | *              getchar (MSVCRT.@) | 
|---|
| 2143 | */ | 
|---|
| 2144 | int MSVCRT_getchar(void) | 
|---|
| 2145 | { | 
|---|
| 2146 | return MSVCRT_fgetc(MSVCRT_stdin); | 
|---|
| 2147 | } | 
|---|
| 2148 |  | 
|---|
| 2149 | /********************************************************************* | 
|---|
| 2150 | *              getc (MSVCRT.@) | 
|---|
| 2151 | */ | 
|---|
| 2152 | int MSVCRT_getc(MSVCRT_FILE* file) | 
|---|
| 2153 | { | 
|---|
| 2154 | return MSVCRT_fgetc(file); | 
|---|
| 2155 | } | 
|---|
| 2156 |  | 
|---|
| 2157 | /********************************************************************* | 
|---|
| 2158 | *              gets (MSVCRT.@) | 
|---|
| 2159 | */ | 
|---|
| 2160 | char *MSVCRT_gets(char *buf) | 
|---|
| 2161 | { | 
|---|
| 2162 | int    cc; | 
|---|
| 2163 | char * buf_start = buf; | 
|---|
| 2164 |  | 
|---|
| 2165 | for(cc = MSVCRT_fgetc(MSVCRT_stdin); cc != MSVCRT_EOF && cc != '\n'; | 
|---|
| 2166 | cc = MSVCRT_fgetc(MSVCRT_stdin)) | 
|---|
| 2167 | if(cc != '\r') *buf++ = (char)cc; | 
|---|
| 2168 |  | 
|---|
| 2169 | *buf = '\0'; | 
|---|
| 2170 |  | 
|---|
| 2171 | TRACE("got '%s'\n", buf_start); | 
|---|
| 2172 | return buf_start; | 
|---|
| 2173 | } | 
|---|
| 2174 |  | 
|---|
| 2175 | /********************************************************************* | 
|---|
| 2176 | *              _getws (MSVCRT.@) | 
|---|
| 2177 | */ | 
|---|
| 2178 | MSVCRT_wchar_t* MSVCRT__getws(MSVCRT_wchar_t* buf) | 
|---|
| 2179 | { | 
|---|
| 2180 | MSVCRT_wint_t cc; | 
|---|
| 2181 | MSVCRT_wchar_t* ws = buf; | 
|---|
| 2182 |  | 
|---|
| 2183 | for (cc = MSVCRT_fgetwc(MSVCRT_stdin); cc != MSVCRT_WEOF && cc != '\n'; | 
|---|
| 2184 | cc = MSVCRT_fgetwc(MSVCRT_stdin)) | 
|---|
| 2185 | { | 
|---|
| 2186 | if (cc != '\r') | 
|---|
| 2187 | *buf++ = (MSVCRT_wchar_t)cc; | 
|---|
| 2188 | } | 
|---|
| 2189 | *buf = '\0'; | 
|---|
| 2190 |  | 
|---|
| 2191 | TRACE("got '%s'\n", debugstr_w(ws)); | 
|---|
| 2192 | return ws; | 
|---|
| 2193 | } | 
|---|
| 2194 |  | 
|---|
| 2195 | /********************************************************************* | 
|---|
| 2196 | *              putc (MSVCRT.@) | 
|---|
| 2197 | */ | 
|---|
| 2198 | int MSVCRT_putc(int c, MSVCRT_FILE* file) | 
|---|
| 2199 | { | 
|---|
| 2200 | return MSVCRT_fputc(c, file); | 
|---|
| 2201 | } | 
|---|
| 2202 |  | 
|---|
| 2203 | /********************************************************************* | 
|---|
| 2204 | *              putchar (MSVCRT.@) | 
|---|
| 2205 | */ | 
|---|
| 2206 | int MSVCRT_putchar(int c) | 
|---|
| 2207 | { | 
|---|
| 2208 | return MSVCRT_fputc(c, MSVCRT_stdout); | 
|---|
| 2209 | } | 
|---|
| 2210 |  | 
|---|
| 2211 | /********************************************************************* | 
|---|
| 2212 | *              puts (MSVCRT.@) | 
|---|
| 2213 | */ | 
|---|
| 2214 | int MSVCRT_puts(const char *s) | 
|---|
| 2215 | { | 
|---|
| 2216 | size_t len = strlen(s); | 
|---|
| 2217 | if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF; | 
|---|
| 2218 | return MSVCRT_fwrite("\n",1,1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF; | 
|---|
| 2219 | } | 
|---|
| 2220 |  | 
|---|
| 2221 | /********************************************************************* | 
|---|
| 2222 | *              _putws (MSVCRT.@) | 
|---|
| 2223 | */ | 
|---|
| 2224 | int _putws(const MSVCRT_wchar_t *s) | 
|---|
| 2225 | { | 
|---|
| 2226 | static const MSVCRT_wchar_t nl = '\n'; | 
|---|
| 2227 | size_t len = strlenW(s); | 
|---|
| 2228 | if (MSVCRT_fwrite(s,sizeof(*s),len,MSVCRT_stdout) != len) return MSVCRT_EOF; | 
|---|
| 2229 | return MSVCRT_fwrite(&nl,sizeof(nl),1,MSVCRT_stdout) == 1 ? 0 : MSVCRT_EOF; | 
|---|
| 2230 | } | 
|---|
| 2231 |  | 
|---|
| 2232 | /********************************************************************* | 
|---|
| 2233 | *              remove (MSVCRT.@) | 
|---|
| 2234 | */ | 
|---|
| 2235 | int MSVCRT_remove(const char *path) | 
|---|
| 2236 | { | 
|---|
| 2237 | TRACE("(%s)\n",path); | 
|---|
| 2238 | if (DeleteFileA(path)) | 
|---|
| 2239 | return 0; | 
|---|
| 2240 | TRACE(":failed (%ld)\n",GetLastError()); | 
|---|
| 2241 | MSVCRT__set_errno(GetLastError()); | 
|---|
| 2242 | return -1; | 
|---|
| 2243 | } | 
|---|
| 2244 |  | 
|---|
| 2245 | /********************************************************************* | 
|---|
| 2246 | *              _wremove (MSVCRT.@) | 
|---|
| 2247 | */ | 
|---|
| 2248 | int _wremove(const MSVCRT_wchar_t *path) | 
|---|
| 2249 | { | 
|---|
| 2250 | TRACE("(%s)\n",debugstr_w(path)); | 
|---|
| 2251 | if (DeleteFileW(path)) | 
|---|
| 2252 | return 0; | 
|---|
| 2253 | TRACE(":failed (%ld)\n",GetLastError()); | 
|---|
| 2254 | MSVCRT__set_errno(GetLastError()); | 
|---|
| 2255 | return -1; | 
|---|
| 2256 | } | 
|---|
| 2257 |  | 
|---|
| 2258 | /********************************************************************* | 
|---|
| 2259 | *              scanf (MSVCRT.@) | 
|---|
| 2260 | */ | 
|---|
| 2261 | int MSVCRT_scanf(const char *format, ...) | 
|---|
| 2262 | { | 
|---|
| 2263 | va_list valist; | 
|---|
| 2264 | int res; | 
|---|
| 2265 |  | 
|---|
| 2266 | va_start(valist, format); | 
|---|
| 2267 | res = MSVCRT_fscanf(MSVCRT_stdin, format, valist); | 
|---|
| 2268 | va_end(valist); | 
|---|
| 2269 | return res; | 
|---|
| 2270 | } | 
|---|
| 2271 |  | 
|---|
| 2272 | /********************************************************************* | 
|---|
| 2273 | *              wscanf (MSVCRT.@) | 
|---|
| 2274 | */ | 
|---|
| 2275 | int MSVCRT_wscanf(const MSVCRT_wchar_t *format, ...) | 
|---|
| 2276 | { | 
|---|
| 2277 | va_list valist; | 
|---|
| 2278 | int res; | 
|---|
| 2279 |  | 
|---|
| 2280 | va_start(valist, format); | 
|---|
| 2281 | res = MSVCRT_fwscanf(MSVCRT_stdin, format, valist); | 
|---|
| 2282 | va_end(valist); | 
|---|
| 2283 | return res; | 
|---|
| 2284 | } | 
|---|
| 2285 |  | 
|---|
| 2286 | /********************************************************************* | 
|---|
| 2287 | *              rename (MSVCRT.@) | 
|---|
| 2288 | */ | 
|---|
| 2289 | int MSVCRT_rename(const char *oldpath,const char *newpath) | 
|---|
| 2290 | { | 
|---|
| 2291 | TRACE(":from %s to %s\n",oldpath,newpath); | 
|---|
| 2292 | if (MoveFileExA(oldpath, newpath, MOVEFILE_COPY_ALLOWED)) | 
|---|
| 2293 | return 0; | 
|---|
| 2294 | TRACE(":failed (%ld)\n",GetLastError()); | 
|---|
| 2295 | MSVCRT__set_errno(GetLastError()); | 
|---|
| 2296 | return -1; | 
|---|
| 2297 | } | 
|---|
| 2298 |  | 
|---|
| 2299 | /********************************************************************* | 
|---|
| 2300 | *              _wrename (MSVCRT.@) | 
|---|
| 2301 | */ | 
|---|
| 2302 | int _wrename(const MSVCRT_wchar_t *oldpath,const MSVCRT_wchar_t *newpath) | 
|---|
| 2303 | { | 
|---|
| 2304 | TRACE(":from %s to %s\n",debugstr_w(oldpath),debugstr_w(newpath)); | 
|---|
| 2305 | if (MoveFileExW(oldpath, newpath, MOVEFILE_COPY_ALLOWED)) | 
|---|
| 2306 | return 0; | 
|---|
| 2307 | TRACE(":failed (%ld)\n",GetLastError()); | 
|---|
| 2308 | MSVCRT__set_errno(GetLastError()); | 
|---|
| 2309 | return -1; | 
|---|
| 2310 | } | 
|---|
| 2311 |  | 
|---|
| 2312 | /********************************************************************* | 
|---|
| 2313 | *              setvbuf (MSVCRT.@) | 
|---|
| 2314 | */ | 
|---|
| 2315 | int MSVCRT_setvbuf(MSVCRT_FILE* file, char *buf, int mode, MSVCRT_size_t size) | 
|---|
| 2316 | { | 
|---|
| 2317 | /* TODO: Check if file busy */ | 
|---|
| 2318 | if(file->_bufsiz) { | 
|---|
| 2319 | MSVCRT_free(file->_base); | 
|---|
| 2320 | file->_bufsiz = 0; | 
|---|
| 2321 | file->_cnt = 0; | 
|---|
| 2322 | } | 
|---|
| 2323 | if(mode == MSVCRT__IOFBF) { | 
|---|
| 2324 | file->_flag &= ~MSVCRT__IONBF; | 
|---|
| 2325 | file->_base = file->_ptr = buf; | 
|---|
| 2326 | if(buf) { | 
|---|
| 2327 | file->_bufsiz = size; | 
|---|
| 2328 | } | 
|---|
| 2329 | } else { | 
|---|
| 2330 | file->_flag |= MSVCRT__IONBF; | 
|---|
| 2331 | } | 
|---|
| 2332 | return 0; | 
|---|
| 2333 | } | 
|---|
| 2334 |  | 
|---|
| 2335 | /********************************************************************* | 
|---|
| 2336 | *              setbuf (MSVCRT.@) | 
|---|
| 2337 | */ | 
|---|
| 2338 | void MSVCRT_setbuf(MSVCRT_FILE* file, char *buf) | 
|---|
| 2339 | { | 
|---|
| 2340 | MSVCRT_setvbuf(file, buf, buf ? MSVCRT__IOFBF : MSVCRT__IONBF, MSVCRT_BUFSIZ); | 
|---|
| 2341 | } | 
|---|
| 2342 |  | 
|---|
| 2343 | /********************************************************************* | 
|---|
| 2344 | *              tmpnam (MSVCRT.@) | 
|---|
| 2345 | */ | 
|---|
| 2346 | char *MSVCRT_tmpnam(char *s) | 
|---|
| 2347 | { | 
|---|
| 2348 | char tmpbuf[MAX_PATH]; | 
|---|
| 2349 | char* prefix = "TMP"; | 
|---|
| 2350 | if (!GetTempPathA(MAX_PATH,tmpbuf) || | 
|---|
| 2351 | !GetTempFileNameA(tmpbuf,prefix,0,MSVCRT_tmpname)) | 
|---|
| 2352 | { | 
|---|
| 2353 | TRACE(":failed-last error (%ld)\n",GetLastError()); | 
|---|
| 2354 | return NULL; | 
|---|
| 2355 | } | 
|---|
| 2356 | TRACE(":got tmpnam %s\n",MSVCRT_tmpname); | 
|---|
| 2357 | s = MSVCRT_tmpname; | 
|---|
| 2358 | return s; | 
|---|
| 2359 | } | 
|---|
| 2360 |  | 
|---|
| 2361 | /********************************************************************* | 
|---|
| 2362 | *              tmpfile (MSVCRT.@) | 
|---|
| 2363 | */ | 
|---|
| 2364 | MSVCRT_FILE* MSVCRT_tmpfile(void) | 
|---|
| 2365 | { | 
|---|
| 2366 | char *filename = MSVCRT_tmpnam(NULL); | 
|---|
| 2367 | int fd; | 
|---|
| 2368 | fd = MSVCRT__open(filename, _O_CREAT | _O_BINARY | _O_RDWR | _O_TEMPORARY); | 
|---|
| 2369 | if (fd != -1) | 
|---|
| 2370 | return msvcrt_alloc_fp(fd); | 
|---|
| 2371 | return NULL; | 
|---|
| 2372 | } | 
|---|
| 2373 |  | 
|---|
| 2374 | /********************************************************************* | 
|---|
| 2375 | *              vfprintf (MSVCRT.@) | 
|---|
| 2376 | */ | 
|---|
| 2377 | int MSVCRT_vfprintf(MSVCRT_FILE* file, const char *format, va_list valist) | 
|---|
| 2378 | { | 
|---|
| 2379 | char buf[2048], *mem = buf; | 
|---|
| 2380 | int written, resize = sizeof(buf), retval; | 
|---|
| 2381 | /* There are two conventions for vsnprintf failing: | 
|---|
| 2382 | * Return -1 if we truncated, or | 
|---|
| 2383 | * Return the number of bytes that would have been written | 
|---|
| 2384 | * The code below handles both cases | 
|---|
| 2385 | */ | 
|---|
| 2386 | dprintf(("MSVCRT: vfprintf %p %s",file,format)); | 
|---|
| 2387 | while ((written = _vsnprintf(mem, resize, format, valist)) == -1 || | 
|---|
| 2388 | written > resize) | 
|---|
| 2389 | { | 
|---|
| 2390 | resize = (written == -1 ? resize * 2 : written + 1); | 
|---|
| 2391 | if (mem != buf) | 
|---|
| 2392 | MSVCRT_free (mem); | 
|---|
| 2393 | if (!(mem = (char *)MSVCRT_malloc(resize))) | 
|---|
| 2394 | return MSVCRT_EOF; | 
|---|
| 2395 | } | 
|---|
| 2396 | retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file); | 
|---|
| 2397 | if (mem != buf) | 
|---|
| 2398 | MSVCRT_free (mem); | 
|---|
| 2399 | return retval; | 
|---|
| 2400 | } | 
|---|
| 2401 |  | 
|---|
| 2402 | /********************************************************************* | 
|---|
| 2403 | *              vfwprintf (MSVCRT.@) | 
|---|
| 2404 | * FIXME: | 
|---|
| 2405 | * Is final char included in written (then resize is too big) or not | 
|---|
| 2406 | * (then we must test for equality too)? | 
|---|
| 2407 | */ | 
|---|
| 2408 | int MSVCRT_vfwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, va_list valist) | 
|---|
| 2409 | { | 
|---|
| 2410 | MSVCRT_wchar_t buf[2048], *mem = buf; | 
|---|
| 2411 | int written, resize = sizeof(buf) / sizeof(MSVCRT_wchar_t), retval; | 
|---|
| 2412 | /* See vfprintf comments */ | 
|---|
| 2413 | while ((written = _vsnwprintf(mem, resize, format, valist)) == -1 || | 
|---|
| 2414 | written > resize) | 
|---|
| 2415 | { | 
|---|
| 2416 | resize = (written == -1 ? resize * 2 : written + sizeof(MSVCRT_wchar_t)); | 
|---|
| 2417 | if (mem != buf) | 
|---|
| 2418 | MSVCRT_free (mem); | 
|---|
| 2419 | if (!(mem = (MSVCRT_wchar_t *)MSVCRT_malloc(resize*sizeof(*mem)))) | 
|---|
| 2420 | return MSVCRT_EOF; | 
|---|
| 2421 | } | 
|---|
| 2422 | retval = MSVCRT_fwrite(mem, sizeof(*mem), written, file); | 
|---|
| 2423 | if (mem != buf) | 
|---|
| 2424 | MSVCRT_free (mem); | 
|---|
| 2425 | return retval; | 
|---|
| 2426 | } | 
|---|
| 2427 |  | 
|---|
| 2428 | /********************************************************************* | 
|---|
| 2429 | *              vprintf (MSVCRT.@) | 
|---|
| 2430 | */ | 
|---|
| 2431 | int MSVCRT_vprintf(const char *format, va_list valist) | 
|---|
| 2432 | { | 
|---|
| 2433 | return MSVCRT_vfprintf(MSVCRT_stdout,format,valist); | 
|---|
| 2434 | } | 
|---|
| 2435 |  | 
|---|
| 2436 | /********************************************************************* | 
|---|
| 2437 | *              vwprintf (MSVCRT.@) | 
|---|
| 2438 | */ | 
|---|
| 2439 | int MSVCRT_vwprintf(const MSVCRT_wchar_t *format, va_list valist) | 
|---|
| 2440 | { | 
|---|
| 2441 | return MSVCRT_vfwprintf(MSVCRT_stdout,format,valist); | 
|---|
| 2442 | } | 
|---|
| 2443 |  | 
|---|
| 2444 | /********************************************************************* | 
|---|
| 2445 | *              fprintf (MSVCRT.@) | 
|---|
| 2446 | */ | 
|---|
| 2447 | int MSVCRT_fprintf(MSVCRT_FILE* file, const char *format, ...) | 
|---|
| 2448 | { | 
|---|
| 2449 | va_list valist; | 
|---|
| 2450 | int res; | 
|---|
| 2451 | va_start(valist, format); | 
|---|
| 2452 | res = MSVCRT_vfprintf(file, format, valist); | 
|---|
| 2453 | va_end(valist); | 
|---|
| 2454 | return res; | 
|---|
| 2455 | } | 
|---|
| 2456 |  | 
|---|
| 2457 | /********************************************************************* | 
|---|
| 2458 | *              fwprintf (MSVCRT.@) | 
|---|
| 2459 | */ | 
|---|
| 2460 | int MSVCRT_fwprintf(MSVCRT_FILE* file, const MSVCRT_wchar_t *format, ...) | 
|---|
| 2461 | { | 
|---|
| 2462 | va_list valist; | 
|---|
| 2463 | int res; | 
|---|
| 2464 | va_start(valist, format); | 
|---|
| 2465 | res = MSVCRT_vfwprintf(file, format, valist); | 
|---|
| 2466 | va_end(valist); | 
|---|
| 2467 | return res; | 
|---|
| 2468 | } | 
|---|
| 2469 |  | 
|---|
| 2470 | /********************************************************************* | 
|---|
| 2471 | *              printf (MSVCRT.@) | 
|---|
| 2472 | */ | 
|---|
| 2473 | int MSVCRT_printf(const char *format, ...) | 
|---|
| 2474 | { | 
|---|
| 2475 | va_list valist; | 
|---|
| 2476 | int res; | 
|---|
| 2477 | va_start(valist, format); | 
|---|
| 2478 | res = MSVCRT_vfprintf(MSVCRT_stdout, format, valist); | 
|---|
| 2479 | va_end(valist); | 
|---|
| 2480 | return res; | 
|---|
| 2481 | } | 
|---|
| 2482 |  | 
|---|
| 2483 | /********************************************************************* | 
|---|
| 2484 | *              ungetc (MSVCRT.@) | 
|---|
| 2485 | */ | 
|---|
| 2486 | int MSVCRT_ungetc(int c, MSVCRT_FILE * file) | 
|---|
| 2487 | { | 
|---|
| 2488 | if(file->_bufsiz == 0 && !(file->_flag & MSVCRT__IONBF)) { | 
|---|
| 2489 | msvcrt_alloc_buffer(file); | 
|---|
| 2490 | file->_ptr++; | 
|---|
| 2491 | } | 
|---|
| 2492 | if(file->_ptr>file->_base) { | 
|---|
| 2493 | file->_ptr--; | 
|---|
| 2494 | *file->_ptr=c; | 
|---|
| 2495 | file->_cnt++; | 
|---|
| 2496 | return c; | 
|---|
| 2497 | } | 
|---|
| 2498 | return MSVCRT_EOF; | 
|---|
| 2499 | } | 
|---|
| 2500 |  | 
|---|
| 2501 | /********************************************************************* | 
|---|
| 2502 | *              ungetwc (MSVCRT.@) | 
|---|
| 2503 | */ | 
|---|
| 2504 | MSVCRT_wint_t MSVCRT_ungetwc(MSVCRT_wint_t wc, MSVCRT_FILE * file) | 
|---|
| 2505 | { | 
|---|
| 2506 | MSVCRT_wchar_t mwc = wc; | 
|---|
| 2507 | char * pp = (char *)&mwc; | 
|---|
| 2508 | int i; | 
|---|
| 2509 | for(i=sizeof(MSVCRT_wchar_t)-1;i>=0;i--) { | 
|---|
| 2510 | if(pp[i] != MSVCRT_ungetc(pp[i],file)) | 
|---|
| 2511 | return MSVCRT_WEOF; | 
|---|
| 2512 | } | 
|---|
| 2513 | return mwc; | 
|---|
| 2514 | } | 
|---|
| 2515 |  | 
|---|
| 2516 | /********************************************************************* | 
|---|
| 2517 | *              wprintf (MSVCRT.@) | 
|---|
| 2518 | */ | 
|---|
| 2519 | int MSVCRT_wprintf(const MSVCRT_wchar_t *format, ...) | 
|---|
| 2520 | { | 
|---|
| 2521 | va_list valist; | 
|---|
| 2522 | int res; | 
|---|
| 2523 | va_start(valist, format); | 
|---|
| 2524 | res = MSVCRT_vwprintf(format, valist); | 
|---|
| 2525 | va_end(valist); | 
|---|
| 2526 | return res; | 
|---|
| 2527 | } | 
|---|
| 2528 |  | 
|---|
| 2529 | /********************************************************************* | 
|---|
| 2530 | *              _wstati64 (MSVCRT.@) | 
|---|
| 2531 | */ | 
|---|
| 2532 | int _wstati64(const MSVCRT(wchar_t)* path, struct _stati64 * buf) | 
|---|
| 2533 | { | 
|---|
| 2534 | LPSTR asciipath; | 
|---|
| 2535 | int   ret,len; | 
|---|
| 2536 |  | 
|---|
| 2537 | TRACE("MSVCRT: _wstati64 file (%s) %x buf(%p)\n",debugstr_w(path),sizeof(*buf),buf); | 
|---|
| 2538 |  | 
|---|
| 2539 | len = WideCharToMultiByte( CP_ACP, 0, path, -1, NULL, 0, 0, NULL); | 
|---|
| 2540 | asciipath = (LPSTR)MSVCRT_malloc(len); | 
|---|
| 2541 | WideCharToMultiByte(CP_ACP, 0, path, -1, asciipath, len, 0, NULL ); | 
|---|
| 2542 |  | 
|---|
| 2543 | ret = _stati64(asciipath,buf); | 
|---|
| 2544 |  | 
|---|
| 2545 | MSVCRT_free(asciipath); | 
|---|
| 2546 |  | 
|---|
| 2547 | return 0; | 
|---|
| 2548 | } | 
|---|