OSDN Git Service

Avoid user namespace pollution by non-standard type 'off64_t'.
authorKeith Marshall <keithmarshall@users.sourceforge.net>
Thu, 31 Dec 2015 21:20:24 +0000 (21:20 +0000)
committerKeith Marshall <keithmarshall@users.sourceforge.net>
Thu, 31 Dec 2015 21:20:24 +0000 (21:20 +0000)
mingwrt/ChangeLog
mingwrt/include/io.h
mingwrt/include/stdio.h
mingwrt/include/sys/types.h
mingwrt/mingwex/mingw-fseek.c
mingwrt/mingwex/stdio/fseeko64.c

index 0c7350d..88ae58a 100644 (file)
@@ -1,3 +1,27 @@
+2015-12-30  Keith Marshall  <keithmarshall@users.sourceforge.net>
+
+       Avoid user namespace pollution by non-standard type 'off64_t'.
+
+       * include/sys/types.h (off64_t): Rename it as...
+       (__off64_t): ...this implementation-private alternative name, so
+       eliminating the potential for user namespace pollution.
+
+       * include/io.h (lseek64): Use '__off64_t', instead of 'off64_t', as
+       return type, and type of offset argument, in both prototype and inline
+       implementation; note that this addresses the issue of pollution in the
+       user namespace, while avoiding the issue of MinGW-Bug [#].
+
+       * include/stdio.h (fseeko64, __mingw_fseeko64): Use '__off64_t'
+       instead of 'off64_t', as offset argument type in function prototypes.
+       (ftello64): Likewise, for return type of inline function.
+
+       * mingwex/mingw-fseek.c: Assert copyright; tidy layout.
+       (WIN32_LEAN_AND_MEAN): Define, to minimize impact of <windows.h>.
+       (__mingw_fseeko64): Use '__off64_t' per modified function prototype.
+
+       * mingwex/stdio/fseeko64.c: Assert copyright.
+       (fseeko64): Use '__off64_t' per modified function prototype.
+
 2015-12-27  Keith Marshall  <keithmarshall@users.sourceforge.net>
 
        Improve ISO-C conformity in MinGW printf(); cf. MinGW-Bug [#1761]
index 535c658..f059f7e 100644 (file)
@@ -6,6 +6,7 @@
  *
  * System level I/O functions and types.
  *
+ * TODO: File requires review; rationalization and refactoring recommended.
  */
 #ifndef        _IO_H_
 #define        _IO_H_
@@ -295,12 +296,10 @@ __CRT_ALIAS int __cdecl __MINGW_NOTHROW   _findnexti64 (intptr_t _v1, struct _find
 #endif /* __MSVCRT_VERSION__ >= 0x0800 */
 
 #ifndef __NO_MINGW_LFS
-__CRT_INLINE off64_t lseek64 (int, off64_t, int);
+__CRT_INLINE __off64_t lseek64 (int, __off64_t, int);
 __CRT_INLINE __JMPSTUB__(( FUNCTION = lseek64, REMAPPED = _lseeki64 ))
-off64_t lseek64 (int fd, off64_t offset, int whence)
-{
-  return _lseeki64(fd, (__int64) offset, whence);
-}
+__off64_t lseek64 (int fd, __off64_t offset, int whence)
+{ return _lseeki64(fd, (__int64)(offset), whence); }
 #endif
 
 #endif /* __MSVCRT__ */
index 094ebad..2fc3c47 100644 (file)
@@ -10,6 +10,7 @@
  * NOTE: The file manipulation functions provided by Microsoft seem to
  * work with either slash (/) or backslash (\) as the directory separator.
  *
+ * TODO: File requires review; rationalization and refactoring recommended.
  */
 
 #ifndef _STDIO_H_
@@ -625,34 +626,27 @@ _CRTIMP int __cdecl __MINGW_NOTHROW       fileno (FILE*);
 #define fileno(__F) ((__F)->_file)
 #endif
 
-#if defined (__MSVCRT__) && !defined (__NO_MINGW_LFS)
+#if defined (__MSVCRT__) && ! defined (__NO_MINGW_LFS)
 #include <sys/types.h>
 __CRT_INLINE __JMPSTUB__(( FUNCTION = fopen64, REMAPPED = fopen ))
 FILE* __cdecl __MINGW_NOTHROW fopen64 (const char* filename, const char* mode)
 { return fopen (filename, mode); }
 
-int __cdecl __MINGW_NOTHROW fseeko64 (FILE*, off64_t, int);
+int __cdecl __MINGW_NOTHROW fseeko64 (FILE*, __off64_t, int);
 
 #ifdef __USE_MINGW_FSEEK
-int __cdecl __MINGW_NOTHROW __mingw_fseeko64 (FILE *, off64_t, int);
+int __cdecl __MINGW_NOTHROW __mingw_fseeko64 (FILE *, __off64_t, int);
 #define fseeko64(fp, offset, whence)  __mingw_fseeko64(fp, offset, whence)
 #endif
 
 __CRT_INLINE __LIBIMPL__(( FUNCTION = ftello64 ))
-off64_t __cdecl __MINGW_NOTHROW ftello64 (FILE * stream)
-{
-  fpos_t pos;
-  if (fgetpos(stream, &pos))
-    return  -1LL;
-  else
-   return ((off64_t) pos);
-}
-#endif /* __NO_MINGW_LFS */
+__off64_t __cdecl __MINGW_NOTHROW ftello64 (FILE * stream)
+{ fpos_t __pos; return (fgetpos(stream, &__pos)) ? -1LL : (__off64_t)(__pos); }
 
-#endif /* Not __STRICT_ANSI__ */
+#endif /* __MSVCRT__ && !__NO_MINGW_LFS */
+#endif /* !__STRICT_ANSI__ */
 
 /* Wide  versions */
-
 #ifndef _WSTDIO_DEFINED
 /*  also in wchar.h - keep in sync */
 _CRTIMP int __cdecl __MINGW_NOTHROW    fwprintf (FILE*, const wchar_t*, ...);
index 2113f77..007b6b4 100644 (file)
@@ -128,7 +128,7 @@ typedef long long fpos64_t;
 
 #ifndef _OFF64_T_
 #define _OFF64_T_
-typedef long long off64_t;
+typedef __int64 __off64_t;
 #endif
 
 #if _POSIX_C_SOURCE
index 9154fa3..27b08ee 100644 (file)
@@ -1,9 +1,38 @@
 /*
- * Workaround for limitations on win9x where a file contents are
- * not zero'd out if you seek past the end and then write.
- * Copied from ming local-patch to binutils/bfd/libbfd.c written by
- * Mumit Khan  <khan@xraylith.wisc.edu>
+ * mingw-fseek.c
+ *
+ * Workaround for limitations on Win9x where extended file content
+ * is not zeroed out if you seek past the end and then write.
+ *
+ * $Id$
+ *
+ * Written by Mumit Khan  <khan@xraylith.wisc.edu>
+ * Copyright (C) 1999, 2002-2005, 2011, 2015, MinGW.org Project.
+ *
+ * Abstracted from MinGW local patch to binutils/bfd/libbfd.c
+ *
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice, this permission notice, and the following
+ * disclaimer shall be included in all copies or substantial portions of
+ * the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OF OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
  */
+#define WIN32_LEAN_AND_MEAN
 
 #include <windows.h>
 #include <stdio.h>
 static int __mingw_fseek_called;
 
 /* The fseek in Win9x runtime does not zero out the file if seeking past
-   the end; if you don't want random stuff from your disk included in your
-   output DLL/executable, use this version instead. On WinNT/Win2k, it
-   just calls runtime fseek().
-
-   CHECK/FIXME: Does this work for both text and binary modes?? */
-
-
+ * the end; if you don't want random stuff from your disk included in your
+ * output DLL/executable, use this version instead. On WinNT/Win2k, it
+ * just calls runtime fseek().
+ *
+ * CHECK/FIXME: Does this work for both text and binary modes??
+ */
 int
 __mingw_fseek (FILE *fp, long offset, int whence)
 {
@@ -30,7 +58,7 @@ __mingw_fseek (FILE *fp, long offset, int whence)
 }
 
 int
-__mingw_fseeko64 (FILE *fp, off64_t offset, int whence)
+__mingw_fseeko64 (FILE *fp, __off64_t offset, int whence)
 {
 # undef fseeko64
   __mingw_fseek_called = 1;
@@ -42,65 +70,69 @@ __mingw_fwrite (const void *buffer, size_t size, size_t count, FILE *fp)
 {
 # undef fwrite
   if ((_osver & 0x8000) &&  __mingw_fseek_called)
-    {
-      ULARGE_INTEGER actual_length;
-      LARGE_INTEGER current_position = {{0LL}};
-      __mingw_fseek_called = 0;
-      fflush (fp);
-      actual_length.u.LowPart = GetFileSize ((HANDLE) _get_osfhandle (fileno (fp)),
-                                            &actual_length.u.HighPart);
-      if (actual_length.u.LowPart == 0xFFFFFFFF
-          && GetLastError() != NO_ERROR )
-        return -1;
-      current_position.u.LowPart = SetFilePointer ((HANDLE) _get_osfhandle (fileno (fp)),
-                                                  current_position.u.LowPart,
-                                                  &current_position.u.HighPart,
-                                                FILE_CURRENT);
-      if (current_position.u.LowPart == 0xFFFFFFFF
-          && GetLastError() != NO_ERROR )
-        return -1;
+  {
+    ULARGE_INTEGER actual_length;
+    LARGE_INTEGER current_position = {{0LL}};
+    __mingw_fseek_called = 0;
+    fflush (fp);
+    actual_length.u.LowPart = GetFileSize
+      ( (HANDLE) _get_osfhandle (fileno (fp)), &actual_length.u.HighPart
+      );
+    if (actual_length.u.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
+      return -1;
+    current_position.u.LowPart = SetFilePointer
+      ( (HANDLE) _get_osfhandle (fileno (fp)), current_position.u.LowPart,
+       &current_position.u.HighPart, FILE_CURRENT
+      );
+    if (current_position.u.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
+      return -1;
 
-#ifdef DEBUG
-      printf ("__mingw_fwrite: current %I64u, actual %I64u\n",
-             current_position.QuadPart, actual_length.QuadPart);
-#endif /* DEBUG */
-      if (current_position.QuadPart > actual_length.QuadPart)
-       {
-         static char __mingw_zeros[ZEROBLOCKSIZE];
-         long long numleft;
-
-         SetFilePointer ((HANDLE) _get_osfhandle (fileno (fp)),
-                         0, 0, FILE_END);
-         numleft = current_position.QuadPart - actual_length.QuadPart;
+#   ifdef DEBUG
+      printf( "__mingw_fwrite: current %I64u, actual %I64u\n",
+         current_position.QuadPart, actual_length.QuadPart
+       );
+#   endif /* DEBUG */
+    if( current_position.QuadPart > actual_length.QuadPart )
+    {
+      static char __mingw_zeros[ZEROBLOCKSIZE];
+      long long numleft;
 
-#ifdef DEBUG
-         printf ("__mingw_fwrite: Seeking %I64d bytes past end\n", numleft);
-#endif /* DEBUG */
-         while (numleft > 0LL)
-           {
-             DWORD nzeros = (numleft > ZEROBLOCKSIZE)
-                            ? ZEROBLOCKSIZE : numleft;
-             DWORD written;
-             if (! WriteFile ((HANDLE) _get_osfhandle (fileno (fp)),
-                              __mingw_zeros, nzeros, &written, NULL))
-               {
-                 /* Best we can hope for, or at least DJ says so. */
-                 SetFilePointer ((HANDLE) _get_osfhandle (fileno (fp)),
-                                 0, 0, FILE_BEGIN);
-                 return -1;
-               }
-             if (written < nzeros)
-               {
-                 /* Likewise. */
-                 SetFilePointer ((HANDLE) _get_osfhandle (fileno (fp)),
-                                 0, 0, FILE_BEGIN);
-                 return -1;
-               }
+      SetFilePointer( (HANDLE) _get_osfhandle (fileno (fp)), 0, 0, FILE_END );
+      numleft = current_position.QuadPart - actual_length.QuadPart;
 
-             numleft -= written;
-           }
-           FlushFileBuffers ((HANDLE) _get_osfhandle (fileno (fp)));
+#     ifdef DEBUG
+       printf( "__mingw_fwrite: Seeking %I64d bytes past end\n", numleft );
+#     endif /* DEBUG */
+      while( numleft > 0LL )
+      {
+       DWORD nzeros = (numleft > ZEROBLOCKSIZE)
+         ? ZEROBLOCKSIZE
+         : numleft;
+       DWORD written;
+       if( ! WriteFile ((HANDLE) _get_osfhandle (fileno (fp)),
+             __mingw_zeros, nzeros, &written, NULL)
+         )
+         { /* Best we can hope for, or at least DJ says so.
+            */
+           SetFilePointer( (HANDLE) _get_osfhandle (fileno (fp)),
+               0, 0, FILE_BEGIN
+             );
+           return -1;
+         }
+       if( written < nzeros )
+       {
+         /* Likewise. */
+         SetFilePointer( (HANDLE) _get_osfhandle (fileno (fp)),
+             0, 0, FILE_BEGIN
+           );
+         return -1;
        }
+       numleft -= written;
+      }
+      FlushFileBuffers ((HANDLE) _get_osfhandle (fileno (fp)));
     }
-  return (fwrite) (buffer, size, count, fp);
+  }
+  return (fwrite)(buffer, size, count, fp);
 }
+
+/* $RCSfile$: end of file */
index d8dcc0c..aba0fed 100644 (file)
@@ -1,9 +1,42 @@
-#include <stdio.h>
+/*
+ * fseeko64.c
+ *
+ * Seek to 64-offset within a file stream; uses same reference bases
+ * as fseek(), but offset is an implementation specific __off64_t type.
+ *
+ * $Id$
+ *
+ * Written by Kees Zeelenberg <kzlg@users.sourceforge.net>
+ * and Danny Smith <dannysmith@users.sourceforge.net>
+ * Copyright (C) 2004, 2005, 2015, MinGW.org Project
+ *
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining a
+ * copy of this software and associated documentation files (the "Software"),
+ * to deal in the Software without restriction, including without limitation
+ * the rights to use, copy, modify, merge, publish, distribute, sublicense,
+ * and/or sell copies of the Software, and to permit persons to whom the
+ * Software is furnished to do so, subject to the following conditions:
+ *
+ * The above copyright notice, this permission notice, and the following
+ * disclaimer shall be included in all copies or substantial portions of
+ * the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
+ * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
+ * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
+ * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OF OR OTHER
+ * DEALINGS IN THE SOFTWARE.
+ *
+ */
 #include <io.h>
+#include <stdio.h>
 #include <errno.h>
 
 int __cdecl
-fseeko64 (FILE* stream, off64_t offset, int whence)
+fseeko64 (FILE* stream, __off64_t offset, int whence)
 {
   fpos_t pos;
   if (whence == SEEK_CUR)
@@ -11,16 +44,16 @@ fseeko64 (FILE* stream, off64_t offset, int whence)
       /* If stream is invalid, fgetpos sets errno. */
       if (fgetpos (stream, &pos))
         return (-1);
-      pos += (fpos_t) offset;
+      pos += (fpos_t)(offset);
     }
   else if (whence == SEEK_END)
     {
       /* If writing, we need to flush before getting file length.  */
       fflush (stream);
-      pos = (fpos_t) (_filelengthi64 (_fileno (stream)) + offset);
+      pos = (fpos_t)(_filelengthi64 (_fileno (stream)) + offset);
     }
   else if (whence == SEEK_SET)
-    pos = (fpos_t) offset;
+    pos = (fpos_t)(offset);
   else
     {
       errno = EINVAL;
@@ -28,3 +61,5 @@ fseeko64 (FILE* stream, off64_t offset, int whence)
     }
   return fsetpos (stream, &pos);
 }
+
+/* $RCSfile$: end of file */