OSDN Git Service

Improve usage notes in stdlib.h, for mkstemp() function.
[mingw/mingw-org-wsl.git] / mingwrt / include / stdlib.h
index 13790e0..cc705b5 100644 (file)
@@ -17,6 +17,7 @@
 #define __need_size_t
 #define __need_wchar_t
 #define __need_NULL
+
 #ifndef RC_INVOKED
 #include <stddef.h>
 #endif /* RC_INVOKED */
@@ -56,9 +57,7 @@
 
 #ifndef RC_INVOKED
 
-#ifdef __cplusplus
-extern "C" {
-#endif
+_BEGIN_C_DECLS
 
 #if !defined (__STRICT_ANSI__)
 
@@ -496,8 +495,8 @@ _CRTIMP char* __cdecl __MINGW_NOTHROW       gcvt (double, int, char*);
 /* C99 name for _exit */
 void __cdecl __MINGW_NOTHROW _Exit(int) __MINGW_ATTRIB_NORETURN;
 #if !defined __NO_INLINE__ && !defined __STRICT_ANSI__
-__CRT_INLINE void __cdecl __MINGW_NOTHROW _Exit(int __status)
-       {  _exit (__status); }
+__CRT_INLINE __JMPSTUB__(( FUNCTION = _Exit, REMAPPED = _exit ))
+void __cdecl __MINGW_NOTHROW _Exit( int __status ){ _exit (__status); }
 #endif
 
 typedef struct { long long quot, rem; } lldiv_t;
@@ -506,8 +505,12 @@ lldiv_t    __cdecl __MINGW_NOTHROW lldiv (long long, long long) __MINGW_ATTRIB_CONS
 
 long long __cdecl __MINGW_NOTHROW llabs(long long);
 #ifndef __NO_INLINE__
-__CRT_INLINE long long __cdecl __MINGW_NOTHROW llabs(long long _j)
-  {return (_j >= 0 ? _j : -_j);}
+__CRT_INLINE
+/* No JMPSTUB or LIBIMPL reference here -- we provide a free-standing
+ * implementation, along with imaxabs(), in mingwex/imaxabs.c
+ */
+long long __cdecl __MINGW_NOTHROW llabs( long long __j )
+{ return __j >= 0 ? __j : -__j; }
 #endif
 
 long long  __cdecl __MINGW_NOTHROW strtoll (const char* __restrict__, char** __restrict, int);
@@ -523,33 +526,107 @@ char* __cdecl __MINGW_NOTHROW ulltoa (unsigned long long , char *, int);
 wchar_t* __cdecl __MINGW_NOTHROW lltow (long long, wchar_t *, int);
 wchar_t* __cdecl __MINGW_NOTHROW ulltow (unsigned long long, wchar_t *, int);
 
-  /* inline using non-ansi functions */
-#ifndef __NO_INLINE__
-__CRT_INLINE long long  __cdecl __MINGW_NOTHROW atoll (const char * _c)
-       { return _atoi64 (_c); }
-__CRT_INLINE char*  __cdecl __MINGW_NOTHROW lltoa (long long _n, char * _c, int _i)
-       { return _i64toa (_n, _c, _i); }
-__CRT_INLINE char*  __cdecl __MINGW_NOTHROW ulltoa (unsigned long long _n, char * _c, int _i)
-       { return _ui64toa (_n, _c, _i); }
-__CRT_INLINE long long  __cdecl __MINGW_NOTHROW wtoll (const wchar_t * _w)
-       { return _wtoi64 (_w); }
-__CRT_INLINE wchar_t*  __cdecl __MINGW_NOTHROW lltow (long long _n, wchar_t * _w, int _i)
-       { return _i64tow (_n, _w, _i); }
-__CRT_INLINE wchar_t*  __cdecl __MINGW_NOTHROW ulltow (unsigned long long _n, wchar_t * _w, int _i)
-       { return _ui64tow (_n, _w, _i); }
+#ifndef __NO_INLINE__ /* inline support for non-ansi functions */
+
+__CRT_INLINE __JMPSTUB__(( FUNCTION = atoll, REMAPPED = _atoi64 ))
+long long __cdecl __MINGW_NOTHROW atoll (const char * _c){ return _atoi64 (_c); }
+
+__CRT_INLINE __JMPSTUB__(( FUNCTION = lltoa, REMAPPED = _i64toa ))
+char* __cdecl __MINGW_NOTHROW lltoa (long long _n, char * _c, int _i)
+{ return _i64toa (_n, _c, _i); }
+
+__CRT_INLINE __JMPSTUB__(( FUNCTION = ulltoa, REMAPPED = _ui64toa ))
+char* __cdecl __MINGW_NOTHROW ulltoa (unsigned long long _n, char * _c, int _i)
+{ return _ui64toa (_n, _c, _i); }
+
+__CRT_INLINE __JMPSTUB__(( FUNCTION = wtoll, REMAPPED = _wtoi64 ))
+long long __cdecl __MINGW_NOTHROW wtoll (const wchar_t * _w){ return _wtoi64 (_w); }
+
+__CRT_INLINE __JMPSTUB__(( FUNCTION = lltow, REMAPPED = _i64tow ))
+wchar_t* __cdecl __MINGW_NOTHROW lltow (long long _n, wchar_t * _w, int _i)
+{ return _i64tow (_n, _w, _i); }
+
+__CRT_INLINE __JMPSTUB__(( FUNCTION = ulltow, REMAPPED = _ui64tow ))
+wchar_t* __cdecl __MINGW_NOTHROW ulltow (unsigned long long _n, wchar_t * _w, int _i)
+{ return _ui64tow (_n, _w, _i); }
+
 #endif /* (__NO_INLINE__) */
 #endif /* (__STRICT_ANSI__)  */
 
 #endif /* __MSVCRT__ */
-
 #endif /* !__NO_ISOCEXT */
 
+/* POSIX/BSD extensions in libmingwex.a; these should be exposed only on
+ * the basis of appropriate POSIX or BSD specific feature tests...
+ *
+ * mkstemp(3) function support; added per feature request #2003.
+ * POSIX wants _XOPEN_SOURCE >= 500, (implying _POSIX_C_SOURCE >= 200112L).
+ */
+#if _POSIX_C_SOURCE >= 200112L
+
+int __cdecl __MINGW_NOTHROW mkstemp( char * );
+int __cdecl __MINGW_NOTHROW __mingw_mkstemp( int, char * );
 
-#ifdef __cplusplus
-}
+/* On POSIX platforms, programmers may adopt an idiom such as:
+ *
+ *   if( mkstemp( template ) >= 0 )
+ *   { unlink( template );
+ *     . . .
+ *   }
+ *
+ * to ensure that a temporary file does NOT persist after it is
+ * closed; MS-Windows does not allow such use of unlink(2), while
+ * the file remains open.  Thus, MS-Windows programmers must take
+ * extra care, to close and unlink temporary files AFTER use, if
+ * similar behaviour is desired.
+ *
+ * To mitigate this MS-Windows limitation, we provide support for
+ * an alternative, MinGW specific idiom:
+ *
+ *   #include <fcntl.h>
+ *
+ *   _MKSTEMP_SETMODE( _O_TEMPORARY );
+ *   if( mkstemp( template ) >= 0 )
+ *   {
+ *     . . .
+ *   }
+ *
+ * to achieve a similar effect to that of the above POSIX idiom; the
+ * following macros are a MinGW specific extension, to facilite such
+ * use of _O_TEMPORARY, (in addition to the POSIX required attributes),
+ * when creating the temporary file.  Note that they require fcntl.h,
+ * which stdlib.h should NOT automatically include; we leave the onus
+ * on the user to explicitly include it, if using _MKSTEMP_SETMODE.
+ */
+#define _MKSTEMP_INVOKE       0
+#define _MKSTEMP_DEFAULT     _O_CREAT | _O_EXCL | _O_RDWR
+#define _MKSTEMP_SETMODE(M) __mingw_mkstemp( _MKSTEMP_DEFAULT | (M), NULL )
+
+#ifndef _NO_OLDNAMES
+#define MKSTEMP_SETMODE(M)  __mingw_mkstemp( _MKSTEMP_DEFAULT | (M), NULL )
 #endif
 
-#endif /* Not RC_INVOKED */
+__CRT_ALIAS __LIBIMPL__(( FUNCTION = mkstemp ))
+int __cdecl __MINGW_NOTHROW mkstemp( char *__filename_template )
+{ return __mingw_mkstemp( _MKSTEMP_INVOKE, __filename_template ); }
 
-#endif /* Not _STDLIB_H_ */
+#endif /* _POSIX_C_SOURCE >= 200112L (for mkstemp()) */
 
+/* mkdtemp(3) function support: added as adjunct to feature request #2003.
+ * POSIX wants _XOPEN_SOURCE >= 700, (implying _POSIX_C_SOURCE >= 200809L).
+ */
+#if _POSIX_C_SOURCE >= 200809L
+
+char * __cdecl __MINGW_NOTHROW mkdtemp( char * );
+char * __cdecl __MINGW_NOTHROW __mingw_mkdtemp( char * );
+
+__CRT_ALIAS __JMPSTUB__(( FUNCTION = mkdtemp ))
+char * __cdecl __MINGW_NOTHROW mkdtemp( char *__dirname_template )
+{ return __mingw_mkdtemp( __dirname_template ); }
+
+#endif /* _POSIX_C_SOURCE >= 200809L (for mkdtemp()) */
+
+_END_C_DECLS
+
+#endif /* Not RC_INVOKED */
+#endif /* Not _STDLIB_H_ */