sync trunk head (r37928)
[reactos.git] / reactos / lib / sdk / crt / stdio / file.c
index 30ab65f..4df12aa 100644 (file)
@@ -69,11 +69,6 @@ int *__p___mb_cur_max(void);
 #undef vprintf
 #undef vwprintf
 
-/* for stat mode, permissions apply to all,owner and group */
-#define ALL_S_IREAD  (_S_IREAD  | (_S_IREAD  >> 3) | (_S_IREAD  >> 6))
-#define ALL_S_IWRITE (_S_IWRITE | (_S_IWRITE >> 3) | (_S_IWRITE >> 6))
-#define ALL_S_IEXEC  (_S_IEXEC  | (_S_IEXEC  >> 3) | (_S_IEXEC  >> 6))
-
 /* _access() bit flags FIXME: incomplete */
 /* defined in crt/io.h */
 
@@ -95,7 +90,7 @@ typedef struct {
     DWORD               unkn[7]; /* critical section and init flag */       
 } ioinfo;
 
-/*static */ioinfo fdesc[MAX_FILES];
+ioinfo fdesc[MAX_FILES];
 
 FILE _iob[3] = { { 0 } };
 
@@ -111,17 +106,6 @@ static int MSVCRT_umask = 0;
 /* INTERNAL: Static buffer for temp file name */
 static char tmpname[MAX_PATH];
 
-static const unsigned int EXE = 'e' << 16 | 'x' << 8 | 'e';
-static const unsigned int BAT = 'b' << 16 | 'a' << 8 | 't';
-static const unsigned int CMD = 'c' << 16 | 'm' << 8 | 'd';
-static const unsigned int COM = 'c' << 16 | 'o' << 8 | 'm';
-
-#define TOUL(x) (ULONGLONG)(x)
-static const ULONGLONG WCEXE = TOUL('e') << 32 | TOUL('x') << 16 | TOUL('e');
-static const ULONGLONG WCBAT = TOUL('b') << 32 | TOUL('a') << 16 | TOUL('t');
-static const ULONGLONG WCCMD = TOUL('c') << 32 | TOUL('m') << 16 | TOUL('d');
-static const ULONGLONG WCCOM = TOUL('c') << 32 | TOUL('o') << 16 | TOUL('m');
-
 /* This critical section protects the tables fdesc and fstreams,
  * and their related indexes, fdstart, fdend,
  * and stream_idx, from race conditions.
@@ -133,35 +117,20 @@ static CRITICAL_SECTION FILE_cs;
 #define LOCK_FILES()    do { EnterCriticalSection(&FILE_cs); } while (0)
 #define UNLOCK_FILES()  do { LeaveCriticalSection(&FILE_cs); } while (0)
 
-static void stat64_to_stat(const struct __stat64 *buf64, struct _stat *buf)
-{
-    buf->st_dev   = buf64->st_dev;
-    buf->st_ino   = buf64->st_ino;
-    buf->st_mode  = buf64->st_mode;
-    buf->st_nlink = buf64->st_nlink;
-    buf->st_uid   = buf64->st_uid;
-    buf->st_gid   = buf64->st_gid;
-    buf->st_rdev  = buf64->st_rdev;
-    buf->st_size  = buf64->st_size;
-    buf->st_atime = buf64->st_atime;
-    buf->st_mtime = buf64->st_mtime;
-    buf->st_ctime = buf64->st_ctime;
-}
-
-static void stat64_to_stati64(const struct __stat64 *buf64, struct _stati64 *buf)
-{
-    buf->st_dev   = buf64->st_dev;
-    buf->st_ino   = buf64->st_ino;
-    buf->st_mode  = buf64->st_mode;
-    buf->st_nlink = buf64->st_nlink;
-    buf->st_uid   = buf64->st_uid;
-    buf->st_gid   = buf64->st_gid;
-    buf->st_rdev  = buf64->st_rdev;
-    buf->st_size  = buf64->st_size;
-    buf->st_atime = buf64->st_atime;
-    buf->st_mtime = buf64->st_mtime;
-    buf->st_ctime = buf64->st_ctime;
-}
+//void stat64_to_stati64(const struct __stat64 *buf64, struct _stati64 *buf)
+//{
+//    buf->st_dev   = buf64->st_dev;
+//    buf->st_ino   = buf64->st_ino;
+//    buf->st_mode  = buf64->st_mode;
+//    buf->st_nlink = buf64->st_nlink;
+//    buf->st_uid   = buf64->st_uid;
+//    buf->st_gid   = buf64->st_gid;
+//    buf->st_rdev  = buf64->st_rdev;
+//    buf->st_size  = buf64->st_size;
+//    buf->st_atime = buf64->st_atime;
+//    buf->st_mtime = buf64->st_mtime;
+//    buf->st_ctime = buf64->st_ctime;
+//}
 
 static inline BOOL is_valid_fd(int fd)
 {
@@ -174,7 +143,7 @@ static inline BOOL is_valid_fd(int fd)
  * it returns a valid handle which is about to be closed, a subsequent call
  * will fail, most likely in a sane way.
  */
-static HANDLE fdtoh(int fd)
+HANDLE fdtoh(int fd)
 {
   if (!is_valid_fd(fd))
   {
@@ -495,7 +464,7 @@ static void int_to_base32(int num, char *str)
  */
 FILE * CDECL __p__iob(void)
 {
- return &_iob[0];
+ return _iob;
 }
 
 /*********************************************************************
@@ -1168,92 +1137,6 @@ int CDECL _fileno(FILE* file)
   return file->_file;
 }
 
-/*********************************************************************
- *             _fstat64 (MSVCRT.@)
- */
-int CDECL _fstat64(int fd, struct __stat64* buf)
-{
-  DWORD dw;
-  DWORD type;
-  BY_HANDLE_FILE_INFORMATION hfi;
-  HANDLE hand = fdtoh(fd);
-
-  TRACE(":fd (%d) stat (%p)\n",fd,buf);
-  if (hand == INVALID_HANDLE_VALUE)
-    return -1;
-
-  if (!buf)
-  {
-    WARN(":failed-NULL buf\n");
-    __set_errno(ERROR_INVALID_PARAMETER);
-    return -1;
-  }
-
-  memset(&hfi, 0, sizeof(hfi));
-  memset(buf, 0, sizeof(struct __stat64));
-  type = GetFileType(hand);
-  if (type == FILE_TYPE_PIPE)
-  {
-    buf->st_dev = buf->st_rdev = fd;
-    buf->st_mode = S_IFIFO;
-    buf->st_nlink = 1;
-  }
-  else if (type == FILE_TYPE_CHAR)
-  {
-    buf->st_dev = buf->st_rdev = fd;
-    buf->st_mode = S_IFCHR;
-    buf->st_nlink = 1;
-  }
-  else /* FILE_TYPE_DISK etc. */
-  {
-    if (!GetFileInformationByHandle(hand, &hfi))
-    {
-      WARN(":failed-last error (%d)\n",GetLastError());
-      __set_errno(ERROR_INVALID_PARAMETER);
-      return -1;
-    }
-    buf->st_mode = S_IFREG | S_IREAD;
-    if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
-      buf->st_mode |= S_IWRITE;
-    buf->st_size  = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
-    RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
-    buf->st_atime = dw;
-    RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
-    buf->st_mtime = buf->st_ctime = dw;
-    buf->st_nlink = hfi.nNumberOfLinks;
-  }
-  TRACE(":dwFileAttributes = 0x%x, mode set to 0x%x\n",hfi.dwFileAttributes,
-   buf->st_mode);
-  return 0;
-}
-
-/*********************************************************************
- *             _fstati64 (MSVCRT.@)
- */
-int CDECL _fstati64(int fd, struct _stati64* buf)
-{
-  int ret;
-  struct __stat64 buf64;
-
-  ret = _fstat64(fd, &buf64);
-  if (!ret)
-    stat64_to_stati64(&buf64, buf);
-  return ret;
-}
-
-/*********************************************************************
- *             _fstat (MSVCRT.@)
- */
-int CDECL _fstat(int fd, struct _stat* buf)
-{ int ret;
-  struct __stat64 buf64;
-
-  ret = _fstat64(fd, &buf64);
-  if (!ret)
-      stat64_to_stat(&buf64, buf);
-  return ret;
-}
-
 /*********************************************************************
  *             _futime (MSVCRT.@)
  */
@@ -1292,12 +1175,12 @@ int CDECL _futime(int fd, struct _utimbuf *t)
 /*********************************************************************
  *             _get_osfhandle (MSVCRT.@)
  */
-long CDECL _get_osfhandle(int fd)
+intptr_t CDECL _get_osfhandle(int fd)
 {
   HANDLE hand = fdtoh(fd);
   TRACE(":fd (%d) handle (%p)\n",fd,hand);
 
-  return (long)hand;
+  return (long)(LONG_PTR)hand;
 }
 
 /*********************************************************************
@@ -1647,7 +1530,7 @@ int CDECL _wcreat(const wchar_t *path, int flags)
 /*********************************************************************
  *             _open_osfhandle (MSVCRT.@)
  */
-int CDECL _open_osfhandle(long handle, int oflags)
+int CDECL _open_osfhandle(intptr_t handle, int oflags)
 {
   int fd;
 
@@ -1660,7 +1543,7 @@ int CDECL _open_osfhandle(long handle, int oflags)
   if (!(oflags & (_O_BINARY | _O_TEXT)))
       oflags |= _O_BINARY;
 
-  fd = alloc_fd((HANDLE)handle, split_oflags(oflags));
+  fd = alloc_fd((HANDLE)(LONG_PTR)handle, split_oflags(oflags));
   TRACE(":handle (%ld) fd (%d) flags 0x%08x\n", handle, fd, oflags);
   return fd;
 }
@@ -1802,188 +1685,6 @@ int CDECL _setmode(int fd,int mode)
   return ret;
 }
 
-/*********************************************************************
- *             _stat64 (MSVCRT.@)
- */
-int CDECL _stat64(const char* path, struct __stat64 * buf)
-{
-  DWORD dw;
-  WIN32_FILE_ATTRIBUTE_DATA hfi;
-  unsigned short mode = ALL_S_IREAD;
-  int plen;
-
-  TRACE(":file (%s) buf(%p)\n",path,buf);
-
-  if (!GetFileAttributesExA(path, GetFileExInfoStandard, &hfi))
-  {
-      TRACE("failed (%d)\n",GetLastError());
-      __set_errno(ERROR_FILE_NOT_FOUND);
-      return -1;
-  }
-
-  memset(buf,0,sizeof(struct __stat64));
-
-  /* FIXME: rdev isn't drive num, despite what the docs say-what is it?
-     Bon 011120: This FIXME seems incorrect
-                 Also a letter as first char isn't enough to be classified
-                as a drive letter
-  */
-  if (isalpha(*path)&& (*(path+1)==':'))
-    buf->st_dev = buf->st_rdev = toupper(*path) - 'A'; /* drive num */
-  else
-    buf->st_dev = buf->st_rdev = _getdrive() - 1;
-
-  plen = strlen(path);
-
-  /* Dir, or regular file? */
-  if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
-      (path[plen-1] == '\\'))
-    mode |= (_S_IFDIR | ALL_S_IEXEC);
-  else
-  {
-    mode |= _S_IFREG;
-    /* executable? */
-    if (plen > 6 && path[plen-4] == '.')  /* shortest exe: "\x.exe" */
-    {
-      unsigned int ext = tolower(path[plen-1]) | (tolower(path[plen-2]) << 8) |
-                                 (tolower(path[plen-3]) << 16);
-      if (ext == EXE || ext == BAT || ext == CMD || ext == COM)
-          mode |= ALL_S_IEXEC;
-    }
-  }
-
-  if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
-    mode |= ALL_S_IWRITE;
-
-  buf->st_mode  = mode;
-  buf->st_nlink = 1;
-  buf->st_size  = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
-  RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
-  buf->st_atime = dw;
-  RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
-  buf->st_mtime = buf->st_ctime = dw;
-  TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
-        (long)(buf->st_size >> 32),(long)buf->st_size,
-        (long)buf->st_atime,(long)buf->st_mtime,(long)buf->st_ctime);
-  return 0;
-}
-
-/*********************************************************************
- *             _stati64 (MSVCRT.@)
- */
-int CDECL _stati64(const char* path, struct _stati64 * buf)
-{
-  int ret;
-  struct __stat64 buf64;
-
-  ret = _stat64(path, &buf64);
-  if (!ret)
-    stat64_to_stati64(&buf64, buf);
-  return ret;
-}
-
-/*********************************************************************
- *             _stat (MSVCRT.@)
- */
-int CDECL _stat(const char* path, struct _stat * buf)
-{ int ret;
-  struct __stat64 buf64;
-
-  ret = _stat64( path, &buf64);
-  if (!ret)
-      stat64_to_stat(&buf64, buf);
-  return ret;
-}
-
-/*********************************************************************
- *             _wstat64 (MSVCRT.@)
- */
-int CDECL _wstat64(const wchar_t* path, struct __stat64 * buf)
-{
-  DWORD dw;
-  WIN32_FILE_ATTRIBUTE_DATA hfi;
-  unsigned short mode = ALL_S_IREAD;
-  int plen;
-
-  TRACE(":file (%s) buf(%p)\n",debugstr_w(path),buf);
-
-  if (!GetFileAttributesExW(path, GetFileExInfoStandard, &hfi))
-  {
-      TRACE("failed (%d)\n",GetLastError());
-      __set_errno(ERROR_FILE_NOT_FOUND);
-      return -1;
-  }
-
-  memset(buf,0,sizeof(struct __stat64));
-
-  /* FIXME: rdev isn't drive num, despite what the docs says-what is it? */
-  if (iswalpha(*path))
-    buf->st_dev = buf->st_rdev = toupperW(*path - 'A'); /* drive num */
-  else
-    buf->st_dev = buf->st_rdev = _getdrive() - 1;
-
-  plen = strlenW(path);
-
-  /* Dir, or regular file? */
-  if ((hfi.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ||
-      (path[plen-1] == '\\'))
-    mode |= (_S_IFDIR | ALL_S_IEXEC);
-  else
-  {
-    mode |= _S_IFREG;
-    /* executable? */
-    if (plen > 6 && path[plen-4] == '.')  /* shortest exe: "\x.exe" */
-    {
-      ULONGLONG ext = tolowerW(path[plen-1]) | (tolowerW(path[plen-2]) << 16) |
-                               ((ULONGLONG)tolowerW(path[plen-3]) << 32);
-      if (ext == WCEXE || ext == WCBAT || ext == WCCMD || ext == WCCOM)
-        mode |= ALL_S_IEXEC;
-    }
-  }
-
-  if (!(hfi.dwFileAttributes & FILE_ATTRIBUTE_READONLY))
-    mode |= ALL_S_IWRITE;
-
-  buf->st_mode  = mode;
-  buf->st_nlink = 1;
-  buf->st_size  = ((__int64)hfi.nFileSizeHigh << 32) + hfi.nFileSizeLow;
-  RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastAccessTime, &dw);
-  buf->st_atime = dw;
-  RtlTimeToSecondsSince1970((LARGE_INTEGER *)&hfi.ftLastWriteTime, &dw);
-  buf->st_mtime = buf->st_ctime = dw;
-  TRACE("%d %d 0x%08lx%08lx %ld %ld %ld\n", buf->st_mode,buf->st_nlink,
-        (long)(buf->st_size >> 32),(long)buf->st_size,
-        (long)buf->st_atime,(long)buf->st_mtime,(long)buf->st_ctime);
-  return 0;
-}
-
-/*********************************************************************
- *             _wstati64 (MSVCRT.@)
- */
-int CDECL _wstati64(const wchar_t* path, struct _stati64 * buf)
-{
-  int ret;
-  struct __stat64 buf64;
-
-  ret = _wstat64(path, &buf64);
-  if (!ret)
-    stat64_to_stati64(&buf64, buf);
-  return ret;
-}
-
-/*********************************************************************
- *             _wstat (MSVCRT.@)
- */
-int CDECL _wstat(const wchar_t* path, struct _stat * buf)
-{
-  int ret;
-  struct __stat64 buf64;
-
-  ret = _wstat64( path, &buf64 );
-  if (!ret) stat64_to_stat(&buf64, buf);
-  return ret;
-}
-
 /*********************************************************************
  *             _tell (MSVCRT.@)
  */