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