Related
I'm porting a relatively simple console program written for Unix to the Windows platform (Visual C++ 8.0). All the source files include "unistd.h", which doesn't exist. Removing it, I get complaints about misssing prototypes for 'srandom', 'random', and 'getopt'.
I know I can replace the random functions, and I'm pretty sure I can find/hack-up a getopt implementation.
But I'm sure others have run into the same challenge.
My question is: is there a port of "unistd.h" to Windows? At least one containg those functions which do have a native Windows implementation - I don't need pipes or forking.
EDIT:
I know I can create my very own "unistd.h" which contains replacements for the things I need - especially in this case, since it is a limited set. But since it seems like a common problem, I was wondering if someone had done the work already for a bigger subset of the functionality.
Switching to a different compiler or environment isn't possible at work - I'm stuck with Visual Studio.
Since we can't find a version on the Internet, let's start one here.
Most ports to Windows probably only need a subset of the complete Unix file.
Here's a starting point. Please add definitions as needed.
#ifndef _UNISTD_H
#define _UNISTD_H 1
/* This is intended as a drop-in replacement for unistd.h on Windows.
* Please add functionality as neeeded.
* https://stackoverflow.com/a/826027/1202830
*/
#include <stdlib.h>
#include <io.h>
#include <getopt.h> /* getopt at: https://gist.github.com/ashelly/7776712 */
#include <process.h> /* for getpid() and the exec..() family */
#include <direct.h> /* for _getcwd() and _chdir() */
#define srandom srand
#define random rand
/* Values for the second argument to access.
These may be OR'd together. */
#define R_OK 4 /* Test for read permission. */
#define W_OK 2 /* Test for write permission. */
//#define X_OK 1 /* execute permission - unsupported in windows*/
#define F_OK 0 /* Test for existence. */
#define access _access
#define dup2 _dup2
#define execve _execve
#define ftruncate _chsize
#define unlink _unlink
#define fileno _fileno
#define getcwd _getcwd
#define chdir _chdir
#define isatty _isatty
#define lseek _lseek
/* read, write, and close are NOT being #defined here, because while there are file handle specific versions for Windows, they probably don't work for sockets. You need to look at your app and consider whether to call e.g. closesocket(). */
#ifdef _WIN64
#define ssize_t __int64
#else
#define ssize_t long
#endif
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
/* should be in some equivalent to <sys/types.h> */
typedef __int8 int8_t;
typedef __int16 int16_t;
typedef __int32 int32_t;
typedef __int64 int64_t;
typedef unsigned __int8 uint8_t;
typedef unsigned __int16 uint16_t;
typedef unsigned __int32 uint32_t;
typedef unsigned __int64 uint64_t;
#endif /* unistd.h */
Try including the io.h file. It seems to be the Visual Studio's equivalent of unistd.h.
I would recommend using mingw/msys as a development environment. Especially if you are porting simple console programs. Msys implements a Unix-like shell on Windows, and mingw is a port of the GNU compiler collection (GCC) and other GNU build tools to the Windows platform. It is an open-source project, and well-suited to the task. I currently use it to build utility programs and console applications for Windows XP, and it most certainly has that unistd.h header you are looking for.
The install procedure can be a little bit tricky, but I found that the best place to start is in MSYS.
I stumbled on this thread while trying to find a Windows alternative for getpid() (defined in unistd.h). It turns out that including process.h does the trick. Maybe this helps people who find this thread in the future.
No, IIRC there is no getopt() on Windows.
Boost, however, has the program_options library... which works okay. It will seem like overkill at first, but it isn't terrible, especially considering it can handle setting program options in configuration files and environment variables in addition to command line options.
Yes, there is: https://github.com/robinrowe/libunistd
Clone the repository and add path\to\libunistd\unistd to the INCLUDE environment variable.
The equivalent of unistd.h on Windows is windows.h
MinGW 4.x has unistd.h in \MinGW\include, \MinGW\include\sys and \MinGW\lib\gcc\mingw32\4.6.2\include\ssp
Here is the code for the MinGW version, by Rob Savoye; modified by Earnie Boyd, Danny Smith, Ramiro Polla, Gregory McGarry and Keith Marshall:
/*
* unistd.h
*
* Standard header file declaring MinGW's POSIX compatibility features.
*
* $Id: unistd.h,v c3ebd36f8211 2016/02/16 16:05:39 keithmarshall $
*
* Written by Rob Savoye <rob#cygnus.com>
* Modified by Earnie Boyd <earnie#users.sourceforge.net>
* Danny Smith <dannysmith#users.sourceforge.net>
* Ramiro Polla <ramiro#lisha.ufsc.br>
* Gregory McGarry <gregorymcgarry#users.sourceforge.net>
* Keith Marshall <keithmarshall#users.sourceforge.net>
* Copyright (C) 1997, 1999, 2002-2004, 2007-2009, 2014-2016,
* 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.
*
*/
#ifndef _UNISTD_H
#define _UNISTD_H 1
#pragma GCC system_header
/* All MinGW headers MUST include _mingw.h before anything else,
* to ensure proper initialization of feature test macros.
*/
#include <_mingw.h>
/* unistd.h maps (roughly) to Microsoft's <io.h>
* Other headers included by <unistd.h> may be selectively processed;
* __UNISTD_H_SOURCED__ enables such selective processing.
*/
#define __UNISTD_H_SOURCED__ 1
#include <io.h>
#include <process.h>
#include <getopt.h>
/* These are defined in stdio.h. POSIX also requires that they
* are to be consistently defined here; don't guard against prior
* definitions, as this might conceal inconsistencies.
*/
#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2
#if _POSIX_C_SOURCE
/* POSIX process/thread suspension functions; all are supported by a
* common MinGW API in libmingwex.a, providing for suspension periods
* ranging from mean values of ~7.5 milliseconds, (see the comments in
* <time.h>), extending up to a maximum of ~136 years.
*
* Note that, whereas POSIX supports early wake-up of any suspended
* process/thread, in response to a signal, this implementation makes
* no attempt to emulate this signalling behaviour, (since signals are
* not well supported by Windows); thus, unless impeded by an invalid
* argument, this implementation always returns an indication as if
* the sleeping period ran to completion.
*/
_BEGIN_C_DECLS
__cdecl __MINGW_NOTHROW
int __mingw_sleep( unsigned long, unsigned long );
/* The nanosleep() function provides the most general purpose API for
* process/thread suspension; it is declared in <time.h>, (where it is
* accompanied by an in-line implementation), rather than here, and it
* provides for specification of suspension periods in the range from
* ~7.5 ms mean, (on WinNT derivatives; ~27.5 ms on Win9x), extending
* up to ~136 years, (effectively eternity).
*
* The usleep() function, and its associated useconds_t type specifier
* were made obsolete in POSIX.1-2008; declared here, only for backward
* compatibility, its continued use is not recommended. (It is limited
* to specification of suspension periods ranging from ~7.5 ms mean up
* to a maximum of 999,999 microseconds only).
*/
typedef unsigned long useconds_t __MINGW_ATTRIB_DEPRECATED;
int __cdecl __MINGW_NOTHROW usleep( useconds_t )__MINGW_ATTRIB_DEPRECATED;
#ifndef __NO_INLINE__
__CRT_INLINE __LIBIMPL__(( FUNCTION = usleep ))
int usleep( useconds_t period ){ return __mingw_sleep( 0, 1000 * period ); }
#endif
/* The sleep() function is, perhaps, the most commonly used of all the
* process/thread suspension APIs; it provides support for specification
* of suspension periods ranging from 1 second to ~136 years. (However,
* POSIX recommends limiting the maximum period to 65535 seconds, to
* maintain portability to platforms with only 16-bit ints).
*/
unsigned __cdecl __MINGW_NOTHROW sleep( unsigned );
#ifndef __NO_INLINE__
__CRT_INLINE __LIBIMPL__(( FUNCTION = sleep ))
unsigned sleep( unsigned period ){ return __mingw_sleep( period, 0 ); }
#endif
/* POSIX ftruncate() function.
*
* Microsoft's _chsize() function is incorrectly described, on MSDN,
* as a preferred replacement for the POSIX chsize() function. There
* never was any such POSIX function; the actual POSIX equivalent is
* the ftruncate() function.
*/
int __cdecl ftruncate( int, off_t );
#ifndef __NO_INLINE__
__CRT_INLINE __JMPSTUB__(( FUNCTION = ftruncate, REMAPPED = _chsize ))
int ftruncate( int __fd, off_t __length ){ return _chsize( __fd, __length ); }
#endif
_END_C_DECLS
#endif /* _POSIX_C_SOURCE */
#undef __UNISTD_H_SOURCED__
#endif /* ! _UNISTD_H: $RCSfile: unistd.h,v $: end of file */
This file requires the inclusion of _mingw.h, which is as follows:
#ifndef __MINGW_H
/*
* _mingw.h
*
* MinGW specific macros included by ALL mingwrt include files; (this file
* is part of the MinGW32 runtime library package).
*
* $Id: _mingw.h.in,v 7daa0459f602 2016/05/03 17:40:54 keithmarshall $
*
* Written by Mumit Khan <khan#xraylith.wisc.edu>
* Copyright (C) 1999, 2001-2011, 2014-2016, 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 and this permission notice (including the next
* paragraph) 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 OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
#define __MINGW_H
/* In previous versions, __MINGW32_VERSION was expressed as a dotted
* numeric pair, representing major.minor; unfortunately, this doesn't
* adapt well to the inclusion of a patch-level component, since the
* major.minor.patch dotted triplet representation is not valid as a
* numeric entity. Thus, for this version, we adopt a representation
* which encodes the version as a long integer value, expressing:
*
* __MINGW32_VERSION = 1,000,000 * major + 1,000 * minor + patch
*
* DO NOT EDIT these package version assignments manually; they are
* derived from the package version specification within configure.ac,
* whence they are propagated automatically, at package build time.
*/
#define __MINGW32_VERSION 3022001L
#define __MINGW32_MAJOR_VERSION 3
#define __MINGW32_MINOR_VERSION 22
#define __MINGW32_PATCHLEVEL 1
#if __GNUC__ >= 3 && ! defined __PCC__
#pragma GCC system_header
#endif
#ifndef _MSVCRTVER_H
/* Legacy versions of mingwrt use the macro __MSVCRT_VERSION__ to
* enable evolving features of different MSVCRT.DLL versions. This
* usage is no longer recommended, but the __MSVCRT_VERSION__ macro
* remains useful when a non-freely distributable MSVCRxx.DLL is to
* be substituted for MSVCRT.DLL; for such usage, the substitute
* MSVCRxx.DLL may be identified as specified in...
*/
# include <msvcrtver.h>
#endif
/* A better inference than __MSVCRT_VERSION__, of the capabilities
* supported by the operating system default MSVCRT.DLL, is provided
* by the Windows API version identification macros.
*/
#include <w32api.h>
/* The following are defined by the user (or by the compiler), to specify how
* identifiers are imported from a DLL. All headers should include this first,
* and then use __DECLSPEC_SUPPORTED to choose between the old ``__imp__name''
* style or the __MINGW_IMPORT style for declarations.
*
* __DECLSPEC_SUPPORTED Defined if dllimport attribute is supported.
* __MINGW_IMPORT The attribute definition to specify imported
* variables/functions.
* _CRTIMP As above. For MS compatibility.
*
* Macros to enable MinGW features which deviate from standard MSVC
* compatible behaviour; these may be specified directly in user code,
* activated implicitly, (e.g. by specifying _POSIX_C_SOURCE or such),
* or by inclusion in __MINGW_FEATURES__:
*
* __USE_MINGW_ANSI_STDIO Select a more ANSI C99 compatible
* implementation of printf() and friends;
* (users should not set this directly).
*
* Other macros:
*
* __int64 define to be long long. Using a typedef
* doesn't work for "unsigned __int64"
*
*
* Manifest definitions for flags to control globbing of the command line
* during application start up, (before main() is called). The first pair,
* when assigned as bit flags within _CRT_glob, select the globbing algorithm
* to be used; (the MINGW algorithm overrides MSCVRT, if both are specified).
* Prior to mingwrt-3.21, only the MSVCRT option was supported; this choice
* may produce different results, depending on which particular version of
* MSVCRT.DLL is in use; (in recent versions, it seems to have become
* definitively broken, when globbing within double quotes).
*/
#define __CRT_GLOB_USE_MSVCRT__ 0x0001
/* From mingwrt-3.21 onward, this should be the preferred choice; it will
* produce consistent results, regardless of the MSVCRT.DLL version in use.
*/
#define __CRT_GLOB_USE_MINGW__ 0x0002
/* When the __CRT_GLOB_USE_MINGW__ flag is set, within _CRT_glob, the
* following additional options are also available; they are not enabled
* by default, but the user may elect to enable any combination of them,
* by setting _CRT_glob to the boolean sum (i.e. logical OR combination)
* of __CRT_GLOB_USE_MINGW__ and the desired options.
*
* __CRT_GLOB_USE_SINGLE_QUOTE__ allows use of single (apostrophe)
* quoting characters, analogously to
* POSIX usage, as an alternative to
* double quotes, for collection of
* arguments separated by white space
* into a single logical argument.
*
* __CRT_GLOB_BRACKET_GROUPS__ enable interpretation of bracketed
* character groups as POSIX compatible
* globbing patterns, matching any one
* character which is either included
* in, or excluded from the group.
*
* __CRT_GLOB_CASE_SENSITIVE__ enable case sensitive matching for
* globbing patterns; this is default
* behaviour for POSIX, but because of
* the case insensitive nature of the
* MS-Windows file system, it is more
* appropriate to use case insensitive
* globbing as the MinGW default.
*
*/
#define __CRT_GLOB_USE_SINGLE_QUOTE__ 0x0010
#define __CRT_GLOB_BRACKET_GROUPS__ 0x0020
#define __CRT_GLOB_CASE_SENSITIVE__ 0x0040
/* The MinGW globbing algorithm uses the ASCII DEL control code as a marker
* for globbing characters which were embedded within quoted arguments; (the
* quotes are stripped away BEFORE the argument is globbed; the globbing code
* treats the marked character as immutable, and strips out the DEL markers,
* before storing the resultant argument). The DEL code is mapped to this
* function here; DO NOT change it, without rebuilding the runtime.
*/
#define __CRT_GLOB_ESCAPE_CHAR__ (char)(127)
/* Manifest definitions identifying the flag bits, controlling activation
* of MinGW features, as specified by the user in __MINGW_FEATURES__.
*/
#define __MINGW_ANSI_STDIO__ 0x0000000000000001ULL
/*
* The following three are not yet formally supported; they are
* included here, to document anticipated future usage.
*/
#define __MINGW_LC_EXTENSIONS__ 0x0000000000000050ULL
#define __MINGW_LC_MESSAGES__ 0x0000000000000010ULL
#define __MINGW_LC_ENVVARS__ 0x0000000000000040ULL
/* Try to avoid problems with outdated checks for GCC __attribute__ support.
*/
#undef __attribute__
#if defined (__PCC__)
# undef __DECLSPEC_SUPPORTED
# ifndef __MINGW_IMPORT
# define __MINGW_IMPORT extern
# endif
# ifndef _CRTIMP
# define _CRTIMP
# endif
# ifndef __cdecl
# define __cdecl _Pragma("cdecl")
# endif
# ifndef __stdcall
# define __stdcall _Pragma("stdcall")
# endif
# ifndef __int64
# define __int64 long long
# endif
# ifndef __int32
# define __int32 long
# endif
# ifndef __int16
# define __int16 short
# endif
# ifndef __int8
# define __int8 char
# endif
# ifndef __small
# define __small char
# endif
# ifndef __hyper
# define __hyper long long
# endif
# ifndef __volatile__
# define __volatile__ volatile
# endif
# ifndef __restrict__
# define __restrict__ restrict
# endif
# define NONAMELESSUNION
#elif defined(__GNUC__)
# ifdef __declspec
# ifndef __MINGW_IMPORT
/* Note the extern. This is needed to work around GCC's
limitations in handling dllimport attribute. */
# define __MINGW_IMPORT extern __attribute__((__dllimport__))
# endif
# ifndef _CRTIMP
# ifdef __USE_CRTIMP
# define _CRTIMP __attribute__((dllimport))
# else
# define _CRTIMP
# endif
# endif
# define __DECLSPEC_SUPPORTED
# else /* __declspec */
# undef __DECLSPEC_SUPPORTED
# undef __MINGW_IMPORT
# ifndef _CRTIMP
# define _CRTIMP
# endif
# endif /* __declspec */
/*
* The next two defines can cause problems if user code adds the
* __cdecl attribute like so:
* void __attribute__ ((__cdecl)) foo(void);
*/
# ifndef __cdecl
# define __cdecl __attribute__((__cdecl__))
# endif
# ifndef __stdcall
# define __stdcall __attribute__((__stdcall__))
# endif
# ifndef __int64
# define __int64 long long
# endif
# ifndef __int32
# define __int32 long
# endif
# ifndef __int16
# define __int16 short
# endif
# ifndef __int8
# define __int8 char
# endif
# ifndef __small
# define __small char
# endif
# ifndef __hyper
# define __hyper long long
# endif
#else /* ! __GNUC__ && ! __PCC__ */
# ifndef __MINGW_IMPORT
# define __MINGW_IMPORT __declspec(dllimport)
# endif
# ifndef _CRTIMP
# define _CRTIMP __declspec(dllimport)
# endif
# define __DECLSPEC_SUPPORTED
# define __attribute__(x) /* nothing */
#endif
#if defined (__GNUC__) && defined (__GNUC_MINOR__)
#define __MINGW_GNUC_PREREQ(major, minor) \
(__GNUC__ > (major) \
|| (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
#else
#define __MINGW_GNUC_PREREQ(major, minor) 0
#endif
#ifdef __cplusplus
# define __CRT_INLINE inline
#else
# if __GNUC_STDC_INLINE__
# define __CRT_INLINE extern inline __attribute__((__gnu_inline__))
# else
# define __CRT_INLINE extern __inline__
# endif
#endif
# ifdef __GNUC__
/* A special form of __CRT_INLINE is provided; it will ALWAYS request
* inlining when possible. Originally specified as _CRTALIAS, this is
* now deprecated in favour of __CRT_ALIAS, for syntactic consistency
* with __CRT_INLINE itself.
*/
# define _CRTALIAS __CRT_INLINE __attribute__((__always_inline__))
# define __CRT_ALIAS __CRT_INLINE __attribute__((__always_inline__))
# else
# define _CRTALIAS __CRT_INLINE /* deprecated form */
# define __CRT_ALIAS __CRT_INLINE /* preferred form */
# endif
/*
* Each function which is implemented as a __CRT_ALIAS should also be
* accompanied by an externally visible interface. The following pair
* of macros provide a mechanism for implementing this, either as a stub
* redirecting to an alternative external function, or by compilation of
* the normally inlined code into free standing object code; each macro
* provides a way for us to offer arbitrary hints for use by the build
* system, while remaining transparent to the compiler.
*/
#define __JMPSTUB__(__BUILD_HINT__)
#define __LIBIMPL__(__BUILD_HINT__)
#ifdef __cplusplus
# define __UNUSED_PARAM(x)
#else
# ifdef __GNUC__
# define __UNUSED_PARAM(x) x __attribute__((__unused__))
# else
# define __UNUSED_PARAM(x) x
# endif
#endif
#ifdef __GNUC__
#define __MINGW_ATTRIB_NORETURN __attribute__((__noreturn__))
#define __MINGW_ATTRIB_CONST __attribute__((__const__))
#else
#define __MINGW_ATTRIB_NORETURN
#define __MINGW_ATTRIB_CONST
#endif
#if __MINGW_GNUC_PREREQ (3, 0)
#define __MINGW_ATTRIB_MALLOC __attribute__((__malloc__))
#define __MINGW_ATTRIB_PURE __attribute__((__pure__))
#else
#define __MINGW_ATTRIB_MALLOC
#define __MINGW_ATTRIB_PURE
#endif
/* Attribute `nonnull' was valid as of gcc 3.3. We don't use GCC's
variadiac macro facility, because variadic macros cause syntax
errors with --traditional-cpp. */
#if __MINGW_GNUC_PREREQ (3, 3)
#define __MINGW_ATTRIB_NONNULL(arg) __attribute__((__nonnull__(arg)))
#else
#define __MINGW_ATTRIB_NONNULL(arg)
#endif /* GNUC >= 3.3 */
#if __MINGW_GNUC_PREREQ (3, 1)
#define __MINGW_ATTRIB_DEPRECATED __attribute__((__deprecated__))
#else
#define __MINGW_ATTRIB_DEPRECATED
#endif /* GNUC >= 3.1 */
#if __MINGW_GNUC_PREREQ (3, 3)
#define __MINGW_NOTHROW __attribute__((__nothrow__))
#else
#define __MINGW_NOTHROW
#endif /* GNUC >= 3.3 */
/* TODO: Mark (almost) all CRT functions as __MINGW_NOTHROW. This will
allow GCC to optimize away some EH unwind code, at least in DW2 case. */
/* Activation of MinGW specific extended features:
*/
#ifndef __USE_MINGW_ANSI_STDIO
/* Users should not set this directly; rather, define one (or more)
* of the feature test macros (tabulated below), or specify any of the
* compiler's command line options, (e.g. -posix, -ansi, or -std=c...),
* which cause _POSIX_SOURCE, or __STRICT_ANSI__ to be defined.
*
* We must check this BEFORE we specifiy any implicit _POSIX_C_SOURCE,
* otherwise we would always implicitly choose __USE_MINGW_ANSI_STDIO,
* even if none of these selectors are specified explicitly...
*/
# if defined __STRICT_ANSI__ || defined _ISOC99_SOURCE \
|| defined _POSIX_SOURCE || defined _POSIX_C_SOURCE \
|| defined _XOPEN_SOURCE || defined _XOPEN_SOURCE_EXTENDED \
|| defined _GNU_SOURCE || defined _BSD_SOURCE \
|| defined _SVID_SOURCE
/*
* but where any of these source code qualifiers are specified,
* then assume ANSI I/O standards are preferred over Microsoft's...
*/
# define __USE_MINGW_ANSI_STDIO 1
# else
/* otherwise use whatever __MINGW_FEATURES__ specifies...
*/
# define __USE_MINGW_ANSI_STDIO (__MINGW_FEATURES__ & __MINGW_ANSI_STDIO__)
# endif
#endif
#ifndef _POSIX_C_SOURCE
/* Users may define this, either directly or indirectly, to explicitly
* enable a particular level of visibility for the subset of those POSIX
* features which are supported by MinGW; (notice that this offers no
* guarantee that any particular POSIX feature will be supported).
*/
# if defined _XOPEN_SOURCE
/* Specifying this is the preferred method for setting _POSIX_C_SOURCE;
* (POSIX defines an explicit relationship to _XOPEN_SOURCE). Note that
* any such explicit setting will augment the set of features which are
* available to any compilation unit, even if it seeks to be strictly
* ANSI-C compliant.
*/
# if _XOPEN_SOURCE < 500
# define _POSIX_C_SOURCE 1L /* POSIX.1-1990 / SUSv1 */
# elif _XOPEN_SOURCE < 600
# define _POSIX_C_SOURCE 199506L /* POSIX.1-1996 / SUSv2 */
# elif _XOPEN_SOURCE < 700
# define _POSIX_C_SOURCE 200112L /* POSIX.1-2001 / SUSv3 */
# else
# define _POSIX_C_SOURCE 200809L /* POSIX.1-2008 / SUSv4 */
# endif
# elif defined _GNU_SOURCE || defined _BSD_SOURCE || ! defined __STRICT_ANSI__
/*
* No explicit level of support has been specified; implicitly grant
* the most comprehensive level to any compilation unit which requests
* either GNU or BSD feature support, or does not seek to be strictly
* ANSI-C compliant.
*/
# define _POSIX_C_SOURCE 200809L
# elif defined _POSIX_SOURCE
/* Now formally deprecated by POSIX, some old code may specify this;
* it will enable a minimal level of POSIX support, in addition to the
* limited feature set enabled for strict ANSI-C conformity.
*/
# define _POSIX_C_SOURCE 1L
# endif
#endif
#ifndef _ISOC99_SOURCE
/* libmingwex.a provides free-standing implementations for many of the
* functions which were introduced in C99; MinGW headers do not expose
* prototypes for these, unless this feature test macro is defined, by
* the user, or implied by other standards...
*/
# if __STDC_VERSION__ >= 199901L || _POSIX_C_SOURCE >= 200112L
# define _ISOC99_SOURCE 1
# endif
#endif
#if ! defined _MINGW32_SOURCE_EXTENDED && ! defined __STRICT_ANSI__
/*
* Enable mingw32 extensions by default, except when __STRICT_ANSI__
* conformity mode has been enabled.
*/
# define _MINGW32_SOURCE_EXTENDED 1
#endif
#endif /* __MINGW_H: $RCSfile: _mingw.h.in,v $: end of file */
The rest of the includes should be standard to your environment.
type stdout macro in the c/cpp file and try to find the definition of stdout. then you will see in the corecrt_wstdio.h actual macro definition of stdout and etc
#define stdin (__acrt_iob_func(0))
#define stdout (__acrt_iob_func(1))
#define stderr (__acrt_iob_func(2))
so then I think it should be the POSIX equivalent to windows.
rand,srand equivalent to random, srandom respectively which declared in stdlib.h
Create your own unistd.h header and include the needed headers for function prototypes.
I added "Mersenne Twist Pseudorandom Number Generator Package" (one header file and one source file) to my Clion C project, but I can not build the project.
the error is :
multiple definition of `mts_lrand'
I can successfully compile and run the program using just command line and gcc but in Clion i can not.
I think it has something to do with macros, but I do not know how to fix it.
here is some part of the code i added:
mtwist.h
.
.
.
.
/*
* In gcc, inline functions must be declared extern or they'll produce
* assembly code (and thus linking errors). We have to work around
* that difficulty with the MT_EXTERN define.
*/
#ifndef MT_EXTERN
#ifdef __cplusplus
#define MT_EXTERN /* C++ doesn't need static */
#else /* __cplusplus */
#define MT_EXTERN extern /* C (at least gcc) needs extern */
#endif /* __cplusplus */
#endif /* MT_EXTERN */
/*
* Make it possible for mtwist.c to disable the inline keyword. We
* use our own keyword so that we don't interfere with inlining in
* C/C++ header files, above.
*/
#ifndef MT_INLINE
#define MT_INLINE inline /* Compiler has inlining */
#endif /* MT_INLINE */
/*
* Try to guess whether the compiler is one (like gcc) that requires
* inline code to be available in the header file, or a smarter one
* that gets inlines directly from object files. But if we've been
* given the information, trust it.
*/
#ifndef MT_GENERATE_CODE_IN_HEADER
#ifdef __GNUC__
#define MT_GENERATE_CODE_IN_HEADER 1
#endif /* __GNUC__ */
#if defined(__INTEL_COMPILER) || defined(_MSC_VER)
#define MT_GENERATE_CODE_IN_HEADER 0
#endif /* __INTEL_COMPILER || _MSC_VER */
#endif /* MT_GENERATE_CODE_IN_HEADER */
#if MT_GENERATE_CODE_IN_HEADER
/*
* Generate a random number in the range 0 to 2^32-1, inclusive, working
* from a given state vector.
*
* The generator is optimized for speed. The primary optimization is that
* the pseudorandom numbers are generated in batches of MT_STATE_SIZE. This
* saves the cost of a modulus operation in the critical path.
*/
MT_EXTERN MT_INLINE uint32_t mts_lrand(
register mt_state* state) /* State for the PRNG */
{
register uint32_t random_value; /* Pseudorandom value generated */
if (state->stateptr <= 0)
mts_refresh(state);
random_value = state->statevec[--state->stateptr];
MT_PRE_TEMPER(random_value);
return MT_FINAL_TEMPER(random_value);
}
.
.
.
.
you see the function definition is in header file, i think this is the problem but I don't know what to do
you can see complete code here.
I'm porting a relatively simple console program written for Unix to the Windows platform (Visual C++ 8.0). All the source files include "unistd.h", which doesn't exist. Removing it, I get complaints about misssing prototypes for 'srandom', 'random', and 'getopt'.
I know I can replace the random functions, and I'm pretty sure I can find/hack-up a getopt implementation.
But I'm sure others have run into the same challenge.
My question is: is there a port of "unistd.h" to Windows? At least one containg those functions which do have a native Windows implementation - I don't need pipes or forking.
EDIT:
I know I can create my very own "unistd.h" which contains replacements for the things I need - especially in this case, since it is a limited set. But since it seems like a common problem, I was wondering if someone had done the work already for a bigger subset of the functionality.
Switching to a different compiler or environment isn't possible at work - I'm stuck with Visual Studio.
Since we can't find a version on the Internet, let's start one here.
Most ports to Windows probably only need a subset of the complete Unix file.
Here's a starting point. Please add definitions as needed.
#ifndef _UNISTD_H
#define _UNISTD_H 1
/* This is intended as a drop-in replacement for unistd.h on Windows.
* Please add functionality as neeeded.
* https://stackoverflow.com/a/826027/1202830
*/
#include <stdlib.h>
#include <io.h>
#include <getopt.h> /* getopt at: https://gist.github.com/ashelly/7776712 */
#include <process.h> /* for getpid() and the exec..() family */
#include <direct.h> /* for _getcwd() and _chdir() */
#define srandom srand
#define random rand
/* Values for the second argument to access.
These may be OR'd together. */
#define R_OK 4 /* Test for read permission. */
#define W_OK 2 /* Test for write permission. */
//#define X_OK 1 /* execute permission - unsupported in windows*/
#define F_OK 0 /* Test for existence. */
#define access _access
#define dup2 _dup2
#define execve _execve
#define ftruncate _chsize
#define unlink _unlink
#define fileno _fileno
#define getcwd _getcwd
#define chdir _chdir
#define isatty _isatty
#define lseek _lseek
/* read, write, and close are NOT being #defined here, because while there are file handle specific versions for Windows, they probably don't work for sockets. You need to look at your app and consider whether to call e.g. closesocket(). */
#ifdef _WIN64
#define ssize_t __int64
#else
#define ssize_t long
#endif
#define STDIN_FILENO 0
#define STDOUT_FILENO 1
#define STDERR_FILENO 2
/* should be in some equivalent to <sys/types.h> */
typedef __int8 int8_t;
typedef __int16 int16_t;
typedef __int32 int32_t;
typedef __int64 int64_t;
typedef unsigned __int8 uint8_t;
typedef unsigned __int16 uint16_t;
typedef unsigned __int32 uint32_t;
typedef unsigned __int64 uint64_t;
#endif /* unistd.h */
Try including the io.h file. It seems to be the Visual Studio's equivalent of unistd.h.
I would recommend using mingw/msys as a development environment. Especially if you are porting simple console programs. Msys implements a Unix-like shell on Windows, and mingw is a port of the GNU compiler collection (GCC) and other GNU build tools to the Windows platform. It is an open-source project, and well-suited to the task. I currently use it to build utility programs and console applications for Windows XP, and it most certainly has that unistd.h header you are looking for.
The install procedure can be a little bit tricky, but I found that the best place to start is in MSYS.
I stumbled on this thread while trying to find a Windows alternative for getpid() (defined in unistd.h). It turns out that including process.h does the trick. Maybe this helps people who find this thread in the future.
No, IIRC there is no getopt() on Windows.
Boost, however, has the program_options library... which works okay. It will seem like overkill at first, but it isn't terrible, especially considering it can handle setting program options in configuration files and environment variables in addition to command line options.
Yes, there is: https://github.com/robinrowe/libunistd
Clone the repository and add path\to\libunistd\unistd to the INCLUDE environment variable.
The equivalent of unistd.h on Windows is windows.h
MinGW 4.x has unistd.h in \MinGW\include, \MinGW\include\sys and \MinGW\lib\gcc\mingw32\4.6.2\include\ssp
Here is the code for the MinGW version, by Rob Savoye; modified by Earnie Boyd, Danny Smith, Ramiro Polla, Gregory McGarry and Keith Marshall:
/*
* unistd.h
*
* Standard header file declaring MinGW's POSIX compatibility features.
*
* $Id: unistd.h,v c3ebd36f8211 2016/02/16 16:05:39 keithmarshall $
*
* Written by Rob Savoye <rob#cygnus.com>
* Modified by Earnie Boyd <earnie#users.sourceforge.net>
* Danny Smith <dannysmith#users.sourceforge.net>
* Ramiro Polla <ramiro#lisha.ufsc.br>
* Gregory McGarry <gregorymcgarry#users.sourceforge.net>
* Keith Marshall <keithmarshall#users.sourceforge.net>
* Copyright (C) 1997, 1999, 2002-2004, 2007-2009, 2014-2016,
* 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.
*
*/
#ifndef _UNISTD_H
#define _UNISTD_H 1
#pragma GCC system_header
/* All MinGW headers MUST include _mingw.h before anything else,
* to ensure proper initialization of feature test macros.
*/
#include <_mingw.h>
/* unistd.h maps (roughly) to Microsoft's <io.h>
* Other headers included by <unistd.h> may be selectively processed;
* __UNISTD_H_SOURCED__ enables such selective processing.
*/
#define __UNISTD_H_SOURCED__ 1
#include <io.h>
#include <process.h>
#include <getopt.h>
/* These are defined in stdio.h. POSIX also requires that they
* are to be consistently defined here; don't guard against prior
* definitions, as this might conceal inconsistencies.
*/
#define SEEK_SET 0
#define SEEK_CUR 1
#define SEEK_END 2
#if _POSIX_C_SOURCE
/* POSIX process/thread suspension functions; all are supported by a
* common MinGW API in libmingwex.a, providing for suspension periods
* ranging from mean values of ~7.5 milliseconds, (see the comments in
* <time.h>), extending up to a maximum of ~136 years.
*
* Note that, whereas POSIX supports early wake-up of any suspended
* process/thread, in response to a signal, this implementation makes
* no attempt to emulate this signalling behaviour, (since signals are
* not well supported by Windows); thus, unless impeded by an invalid
* argument, this implementation always returns an indication as if
* the sleeping period ran to completion.
*/
_BEGIN_C_DECLS
__cdecl __MINGW_NOTHROW
int __mingw_sleep( unsigned long, unsigned long );
/* The nanosleep() function provides the most general purpose API for
* process/thread suspension; it is declared in <time.h>, (where it is
* accompanied by an in-line implementation), rather than here, and it
* provides for specification of suspension periods in the range from
* ~7.5 ms mean, (on WinNT derivatives; ~27.5 ms on Win9x), extending
* up to ~136 years, (effectively eternity).
*
* The usleep() function, and its associated useconds_t type specifier
* were made obsolete in POSIX.1-2008; declared here, only for backward
* compatibility, its continued use is not recommended. (It is limited
* to specification of suspension periods ranging from ~7.5 ms mean up
* to a maximum of 999,999 microseconds only).
*/
typedef unsigned long useconds_t __MINGW_ATTRIB_DEPRECATED;
int __cdecl __MINGW_NOTHROW usleep( useconds_t )__MINGW_ATTRIB_DEPRECATED;
#ifndef __NO_INLINE__
__CRT_INLINE __LIBIMPL__(( FUNCTION = usleep ))
int usleep( useconds_t period ){ return __mingw_sleep( 0, 1000 * period ); }
#endif
/* The sleep() function is, perhaps, the most commonly used of all the
* process/thread suspension APIs; it provides support for specification
* of suspension periods ranging from 1 second to ~136 years. (However,
* POSIX recommends limiting the maximum period to 65535 seconds, to
* maintain portability to platforms with only 16-bit ints).
*/
unsigned __cdecl __MINGW_NOTHROW sleep( unsigned );
#ifndef __NO_INLINE__
__CRT_INLINE __LIBIMPL__(( FUNCTION = sleep ))
unsigned sleep( unsigned period ){ return __mingw_sleep( period, 0 ); }
#endif
/* POSIX ftruncate() function.
*
* Microsoft's _chsize() function is incorrectly described, on MSDN,
* as a preferred replacement for the POSIX chsize() function. There
* never was any such POSIX function; the actual POSIX equivalent is
* the ftruncate() function.
*/
int __cdecl ftruncate( int, off_t );
#ifndef __NO_INLINE__
__CRT_INLINE __JMPSTUB__(( FUNCTION = ftruncate, REMAPPED = _chsize ))
int ftruncate( int __fd, off_t __length ){ return _chsize( __fd, __length ); }
#endif
_END_C_DECLS
#endif /* _POSIX_C_SOURCE */
#undef __UNISTD_H_SOURCED__
#endif /* ! _UNISTD_H: $RCSfile: unistd.h,v $: end of file */
This file requires the inclusion of _mingw.h, which is as follows:
#ifndef __MINGW_H
/*
* _mingw.h
*
* MinGW specific macros included by ALL mingwrt include files; (this file
* is part of the MinGW32 runtime library package).
*
* $Id: _mingw.h.in,v 7daa0459f602 2016/05/03 17:40:54 keithmarshall $
*
* Written by Mumit Khan <khan#xraylith.wisc.edu>
* Copyright (C) 1999, 2001-2011, 2014-2016, 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 and this permission notice (including the next
* paragraph) 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 OR OTHER
* DEALINGS IN THE SOFTWARE.
*
*/
#define __MINGW_H
/* In previous versions, __MINGW32_VERSION was expressed as a dotted
* numeric pair, representing major.minor; unfortunately, this doesn't
* adapt well to the inclusion of a patch-level component, since the
* major.minor.patch dotted triplet representation is not valid as a
* numeric entity. Thus, for this version, we adopt a representation
* which encodes the version as a long integer value, expressing:
*
* __MINGW32_VERSION = 1,000,000 * major + 1,000 * minor + patch
*
* DO NOT EDIT these package version assignments manually; they are
* derived from the package version specification within configure.ac,
* whence they are propagated automatically, at package build time.
*/
#define __MINGW32_VERSION 3022001L
#define __MINGW32_MAJOR_VERSION 3
#define __MINGW32_MINOR_VERSION 22
#define __MINGW32_PATCHLEVEL 1
#if __GNUC__ >= 3 && ! defined __PCC__
#pragma GCC system_header
#endif
#ifndef _MSVCRTVER_H
/* Legacy versions of mingwrt use the macro __MSVCRT_VERSION__ to
* enable evolving features of different MSVCRT.DLL versions. This
* usage is no longer recommended, but the __MSVCRT_VERSION__ macro
* remains useful when a non-freely distributable MSVCRxx.DLL is to
* be substituted for MSVCRT.DLL; for such usage, the substitute
* MSVCRxx.DLL may be identified as specified in...
*/
# include <msvcrtver.h>
#endif
/* A better inference than __MSVCRT_VERSION__, of the capabilities
* supported by the operating system default MSVCRT.DLL, is provided
* by the Windows API version identification macros.
*/
#include <w32api.h>
/* The following are defined by the user (or by the compiler), to specify how
* identifiers are imported from a DLL. All headers should include this first,
* and then use __DECLSPEC_SUPPORTED to choose between the old ``__imp__name''
* style or the __MINGW_IMPORT style for declarations.
*
* __DECLSPEC_SUPPORTED Defined if dllimport attribute is supported.
* __MINGW_IMPORT The attribute definition to specify imported
* variables/functions.
* _CRTIMP As above. For MS compatibility.
*
* Macros to enable MinGW features which deviate from standard MSVC
* compatible behaviour; these may be specified directly in user code,
* activated implicitly, (e.g. by specifying _POSIX_C_SOURCE or such),
* or by inclusion in __MINGW_FEATURES__:
*
* __USE_MINGW_ANSI_STDIO Select a more ANSI C99 compatible
* implementation of printf() and friends;
* (users should not set this directly).
*
* Other macros:
*
* __int64 define to be long long. Using a typedef
* doesn't work for "unsigned __int64"
*
*
* Manifest definitions for flags to control globbing of the command line
* during application start up, (before main() is called). The first pair,
* when assigned as bit flags within _CRT_glob, select the globbing algorithm
* to be used; (the MINGW algorithm overrides MSCVRT, if both are specified).
* Prior to mingwrt-3.21, only the MSVCRT option was supported; this choice
* may produce different results, depending on which particular version of
* MSVCRT.DLL is in use; (in recent versions, it seems to have become
* definitively broken, when globbing within double quotes).
*/
#define __CRT_GLOB_USE_MSVCRT__ 0x0001
/* From mingwrt-3.21 onward, this should be the preferred choice; it will
* produce consistent results, regardless of the MSVCRT.DLL version in use.
*/
#define __CRT_GLOB_USE_MINGW__ 0x0002
/* When the __CRT_GLOB_USE_MINGW__ flag is set, within _CRT_glob, the
* following additional options are also available; they are not enabled
* by default, but the user may elect to enable any combination of them,
* by setting _CRT_glob to the boolean sum (i.e. logical OR combination)
* of __CRT_GLOB_USE_MINGW__ and the desired options.
*
* __CRT_GLOB_USE_SINGLE_QUOTE__ allows use of single (apostrophe)
* quoting characters, analogously to
* POSIX usage, as an alternative to
* double quotes, for collection of
* arguments separated by white space
* into a single logical argument.
*
* __CRT_GLOB_BRACKET_GROUPS__ enable interpretation of bracketed
* character groups as POSIX compatible
* globbing patterns, matching any one
* character which is either included
* in, or excluded from the group.
*
* __CRT_GLOB_CASE_SENSITIVE__ enable case sensitive matching for
* globbing patterns; this is default
* behaviour for POSIX, but because of
* the case insensitive nature of the
* MS-Windows file system, it is more
* appropriate to use case insensitive
* globbing as the MinGW default.
*
*/
#define __CRT_GLOB_USE_SINGLE_QUOTE__ 0x0010
#define __CRT_GLOB_BRACKET_GROUPS__ 0x0020
#define __CRT_GLOB_CASE_SENSITIVE__ 0x0040
/* The MinGW globbing algorithm uses the ASCII DEL control code as a marker
* for globbing characters which were embedded within quoted arguments; (the
* quotes are stripped away BEFORE the argument is globbed; the globbing code
* treats the marked character as immutable, and strips out the DEL markers,
* before storing the resultant argument). The DEL code is mapped to this
* function here; DO NOT change it, without rebuilding the runtime.
*/
#define __CRT_GLOB_ESCAPE_CHAR__ (char)(127)
/* Manifest definitions identifying the flag bits, controlling activation
* of MinGW features, as specified by the user in __MINGW_FEATURES__.
*/
#define __MINGW_ANSI_STDIO__ 0x0000000000000001ULL
/*
* The following three are not yet formally supported; they are
* included here, to document anticipated future usage.
*/
#define __MINGW_LC_EXTENSIONS__ 0x0000000000000050ULL
#define __MINGW_LC_MESSAGES__ 0x0000000000000010ULL
#define __MINGW_LC_ENVVARS__ 0x0000000000000040ULL
/* Try to avoid problems with outdated checks for GCC __attribute__ support.
*/
#undef __attribute__
#if defined (__PCC__)
# undef __DECLSPEC_SUPPORTED
# ifndef __MINGW_IMPORT
# define __MINGW_IMPORT extern
# endif
# ifndef _CRTIMP
# define _CRTIMP
# endif
# ifndef __cdecl
# define __cdecl _Pragma("cdecl")
# endif
# ifndef __stdcall
# define __stdcall _Pragma("stdcall")
# endif
# ifndef __int64
# define __int64 long long
# endif
# ifndef __int32
# define __int32 long
# endif
# ifndef __int16
# define __int16 short
# endif
# ifndef __int8
# define __int8 char
# endif
# ifndef __small
# define __small char
# endif
# ifndef __hyper
# define __hyper long long
# endif
# ifndef __volatile__
# define __volatile__ volatile
# endif
# ifndef __restrict__
# define __restrict__ restrict
# endif
# define NONAMELESSUNION
#elif defined(__GNUC__)
# ifdef __declspec
# ifndef __MINGW_IMPORT
/* Note the extern. This is needed to work around GCC's
limitations in handling dllimport attribute. */
# define __MINGW_IMPORT extern __attribute__((__dllimport__))
# endif
# ifndef _CRTIMP
# ifdef __USE_CRTIMP
# define _CRTIMP __attribute__((dllimport))
# else
# define _CRTIMP
# endif
# endif
# define __DECLSPEC_SUPPORTED
# else /* __declspec */
# undef __DECLSPEC_SUPPORTED
# undef __MINGW_IMPORT
# ifndef _CRTIMP
# define _CRTIMP
# endif
# endif /* __declspec */
/*
* The next two defines can cause problems if user code adds the
* __cdecl attribute like so:
* void __attribute__ ((__cdecl)) foo(void);
*/
# ifndef __cdecl
# define __cdecl __attribute__((__cdecl__))
# endif
# ifndef __stdcall
# define __stdcall __attribute__((__stdcall__))
# endif
# ifndef __int64
# define __int64 long long
# endif
# ifndef __int32
# define __int32 long
# endif
# ifndef __int16
# define __int16 short
# endif
# ifndef __int8
# define __int8 char
# endif
# ifndef __small
# define __small char
# endif
# ifndef __hyper
# define __hyper long long
# endif
#else /* ! __GNUC__ && ! __PCC__ */
# ifndef __MINGW_IMPORT
# define __MINGW_IMPORT __declspec(dllimport)
# endif
# ifndef _CRTIMP
# define _CRTIMP __declspec(dllimport)
# endif
# define __DECLSPEC_SUPPORTED
# define __attribute__(x) /* nothing */
#endif
#if defined (__GNUC__) && defined (__GNUC_MINOR__)
#define __MINGW_GNUC_PREREQ(major, minor) \
(__GNUC__ > (major) \
|| (__GNUC__ == (major) && __GNUC_MINOR__ >= (minor)))
#else
#define __MINGW_GNUC_PREREQ(major, minor) 0
#endif
#ifdef __cplusplus
# define __CRT_INLINE inline
#else
# if __GNUC_STDC_INLINE__
# define __CRT_INLINE extern inline __attribute__((__gnu_inline__))
# else
# define __CRT_INLINE extern __inline__
# endif
#endif
# ifdef __GNUC__
/* A special form of __CRT_INLINE is provided; it will ALWAYS request
* inlining when possible. Originally specified as _CRTALIAS, this is
* now deprecated in favour of __CRT_ALIAS, for syntactic consistency
* with __CRT_INLINE itself.
*/
# define _CRTALIAS __CRT_INLINE __attribute__((__always_inline__))
# define __CRT_ALIAS __CRT_INLINE __attribute__((__always_inline__))
# else
# define _CRTALIAS __CRT_INLINE /* deprecated form */
# define __CRT_ALIAS __CRT_INLINE /* preferred form */
# endif
/*
* Each function which is implemented as a __CRT_ALIAS should also be
* accompanied by an externally visible interface. The following pair
* of macros provide a mechanism for implementing this, either as a stub
* redirecting to an alternative external function, or by compilation of
* the normally inlined code into free standing object code; each macro
* provides a way for us to offer arbitrary hints for use by the build
* system, while remaining transparent to the compiler.
*/
#define __JMPSTUB__(__BUILD_HINT__)
#define __LIBIMPL__(__BUILD_HINT__)
#ifdef __cplusplus
# define __UNUSED_PARAM(x)
#else
# ifdef __GNUC__
# define __UNUSED_PARAM(x) x __attribute__((__unused__))
# else
# define __UNUSED_PARAM(x) x
# endif
#endif
#ifdef __GNUC__
#define __MINGW_ATTRIB_NORETURN __attribute__((__noreturn__))
#define __MINGW_ATTRIB_CONST __attribute__((__const__))
#else
#define __MINGW_ATTRIB_NORETURN
#define __MINGW_ATTRIB_CONST
#endif
#if __MINGW_GNUC_PREREQ (3, 0)
#define __MINGW_ATTRIB_MALLOC __attribute__((__malloc__))
#define __MINGW_ATTRIB_PURE __attribute__((__pure__))
#else
#define __MINGW_ATTRIB_MALLOC
#define __MINGW_ATTRIB_PURE
#endif
/* Attribute `nonnull' was valid as of gcc 3.3. We don't use GCC's
variadiac macro facility, because variadic macros cause syntax
errors with --traditional-cpp. */
#if __MINGW_GNUC_PREREQ (3, 3)
#define __MINGW_ATTRIB_NONNULL(arg) __attribute__((__nonnull__(arg)))
#else
#define __MINGW_ATTRIB_NONNULL(arg)
#endif /* GNUC >= 3.3 */
#if __MINGW_GNUC_PREREQ (3, 1)
#define __MINGW_ATTRIB_DEPRECATED __attribute__((__deprecated__))
#else
#define __MINGW_ATTRIB_DEPRECATED
#endif /* GNUC >= 3.1 */
#if __MINGW_GNUC_PREREQ (3, 3)
#define __MINGW_NOTHROW __attribute__((__nothrow__))
#else
#define __MINGW_NOTHROW
#endif /* GNUC >= 3.3 */
/* TODO: Mark (almost) all CRT functions as __MINGW_NOTHROW. This will
allow GCC to optimize away some EH unwind code, at least in DW2 case. */
/* Activation of MinGW specific extended features:
*/
#ifndef __USE_MINGW_ANSI_STDIO
/* Users should not set this directly; rather, define one (or more)
* of the feature test macros (tabulated below), or specify any of the
* compiler's command line options, (e.g. -posix, -ansi, or -std=c...),
* which cause _POSIX_SOURCE, or __STRICT_ANSI__ to be defined.
*
* We must check this BEFORE we specifiy any implicit _POSIX_C_SOURCE,
* otherwise we would always implicitly choose __USE_MINGW_ANSI_STDIO,
* even if none of these selectors are specified explicitly...
*/
# if defined __STRICT_ANSI__ || defined _ISOC99_SOURCE \
|| defined _POSIX_SOURCE || defined _POSIX_C_SOURCE \
|| defined _XOPEN_SOURCE || defined _XOPEN_SOURCE_EXTENDED \
|| defined _GNU_SOURCE || defined _BSD_SOURCE \
|| defined _SVID_SOURCE
/*
* but where any of these source code qualifiers are specified,
* then assume ANSI I/O standards are preferred over Microsoft's...
*/
# define __USE_MINGW_ANSI_STDIO 1
# else
/* otherwise use whatever __MINGW_FEATURES__ specifies...
*/
# define __USE_MINGW_ANSI_STDIO (__MINGW_FEATURES__ & __MINGW_ANSI_STDIO__)
# endif
#endif
#ifndef _POSIX_C_SOURCE
/* Users may define this, either directly or indirectly, to explicitly
* enable a particular level of visibility for the subset of those POSIX
* features which are supported by MinGW; (notice that this offers no
* guarantee that any particular POSIX feature will be supported).
*/
# if defined _XOPEN_SOURCE
/* Specifying this is the preferred method for setting _POSIX_C_SOURCE;
* (POSIX defines an explicit relationship to _XOPEN_SOURCE). Note that
* any such explicit setting will augment the set of features which are
* available to any compilation unit, even if it seeks to be strictly
* ANSI-C compliant.
*/
# if _XOPEN_SOURCE < 500
# define _POSIX_C_SOURCE 1L /* POSIX.1-1990 / SUSv1 */
# elif _XOPEN_SOURCE < 600
# define _POSIX_C_SOURCE 199506L /* POSIX.1-1996 / SUSv2 */
# elif _XOPEN_SOURCE < 700
# define _POSIX_C_SOURCE 200112L /* POSIX.1-2001 / SUSv3 */
# else
# define _POSIX_C_SOURCE 200809L /* POSIX.1-2008 / SUSv4 */
# endif
# elif defined _GNU_SOURCE || defined _BSD_SOURCE || ! defined __STRICT_ANSI__
/*
* No explicit level of support has been specified; implicitly grant
* the most comprehensive level to any compilation unit which requests
* either GNU or BSD feature support, or does not seek to be strictly
* ANSI-C compliant.
*/
# define _POSIX_C_SOURCE 200809L
# elif defined _POSIX_SOURCE
/* Now formally deprecated by POSIX, some old code may specify this;
* it will enable a minimal level of POSIX support, in addition to the
* limited feature set enabled for strict ANSI-C conformity.
*/
# define _POSIX_C_SOURCE 1L
# endif
#endif
#ifndef _ISOC99_SOURCE
/* libmingwex.a provides free-standing implementations for many of the
* functions which were introduced in C99; MinGW headers do not expose
* prototypes for these, unless this feature test macro is defined, by
* the user, or implied by other standards...
*/
# if __STDC_VERSION__ >= 199901L || _POSIX_C_SOURCE >= 200112L
# define _ISOC99_SOURCE 1
# endif
#endif
#if ! defined _MINGW32_SOURCE_EXTENDED && ! defined __STRICT_ANSI__
/*
* Enable mingw32 extensions by default, except when __STRICT_ANSI__
* conformity mode has been enabled.
*/
# define _MINGW32_SOURCE_EXTENDED 1
#endif
#endif /* __MINGW_H: $RCSfile: _mingw.h.in,v $: end of file */
The rest of the includes should be standard to your environment.
type stdout macro in the c/cpp file and try to find the definition of stdout. then you will see in the corecrt_wstdio.h actual macro definition of stdout and etc
#define stdin (__acrt_iob_func(0))
#define stdout (__acrt_iob_func(1))
#define stderr (__acrt_iob_func(2))
so then I think it should be the POSIX equivalent to windows.
rand,srand equivalent to random, srandom respectively which declared in stdlib.h
Create your own unistd.h header and include the needed headers for function prototypes.
Pardon me if this sounds a question that has been asked many times but I assure you this is a little different.
I use Codeblocks for C programming and lately I have started to wonder why would someone use a header file in C. I understand it is used for declaring and/or defining variables structures. But here is something i tried and now I am confused.
I have a header file named
test1.h
#ifndef TEST1_H_INCLUDED
#define TEST1_H_INCLUDED
static int testvar = 233;
extern int one;
extern void show();
#endif // TEST1_H_INCLUDED
and two other files
headertest.c
#include"test1.h"
#include<stdio.h>
int one = 232;
int main()
{
testvar = 12;
printf("The address of one is %p and value is %d",&testvar,testvar);
printf("value of one is %d",one);
show();
return 0;
}
headertest2.c
#include "test1.h"
#include<stdio.h>
extern int one;
extern void show()
{
//extern int one;
testvar = 34;
printf("\nThe address of one is %p and value is %d",&testvar, testvar);
printf("The value of one is %d", one);
}
Now my point is that even if i comment out the declaration
extern int one;
extern void show();
from the header file test1.h I get no warning or error during compilation and execution. When I can directly access int one and void show() from headertest.c because of them having external linkage implicitly then whats the use of Header file here? Why would someone declare some variable or function as extern in a header file that can otherwise be accessed directly!!
Thank you
A header file is used so that you won't repeat yourself. In your example, you didn't need to write
extern int one;
in headertest2.c, because it would already get included in that file via the header file.
Not repeating yourself is not a small thing. Imagine you have a hundred files that use this global variable (one). Do you want to write extern int one in all of them? What if there were 20 of these variables and 50 more functions? What if you wanted to change int to uint32_t?
Surely, duplicating definitions is tedious and error-prone.
Let's take a look at stdio.h for example. Without headers, you would have to copy-paste the following code in every file that wanted to do I/O:
/* Define ISO C stdio on top of C++ iostreams.
Copyright (C) 1991, 1994-2008, 2009, 2010 Free Software Foundation, Inc.
This file is part of the GNU C Library.
The GNU C Library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
The GNU C Library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with the GNU C Library; if not, write to the Free
Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
02111-1307 USA. */
/*
* ISO C99 Standard: 7.19 Input/output <stdio.h>
*/
#ifndef _STDIO_H
#if !defined __need_FILE && !defined __need___FILE
# define _STDIO_H 1
# include <features.h>
__BEGIN_DECLS
# define __need_size_t
# define __need_NULL
# include <stddef.h>
# include <bits/types.h>
# define __need_FILE
# define __need___FILE
#endif /* Don't need FILE. */
#if !defined __FILE_defined && defined __need_FILE
/* Define outside of namespace so the C++ is happy. */
struct _IO_FILE;
__BEGIN_NAMESPACE_STD
/* The opaque type of streams. This is the definition used elsewhere. */
typedef struct _IO_FILE FILE;
__END_NAMESPACE_STD
#if defined __USE_LARGEFILE64 || defined __USE_SVID || defined __USE_POSIX \
|| defined __USE_BSD || defined __USE_ISOC99 || defined __USE_XOPEN \
|| defined __USE_POSIX2
__USING_NAMESPACE_STD(FILE)
#endif
# define __FILE_defined 1
#endif /* FILE not defined. */
#undef __need_FILE
#if !defined ____FILE_defined && defined __need___FILE
/* The opaque type of streams. This is the definition used elsewhere. */
typedef struct _IO_FILE __FILE;
# define ____FILE_defined 1
#endif /* __FILE not defined. */
#undef __need___FILE
#ifdef _STDIO_H
#define _STDIO_USES_IOSTREAM
#include <libio.h>
#if defined __USE_XOPEN || defined __USE_XOPEN2K8
# ifdef __GNUC__
# ifndef _VA_LIST_DEFINED
typedef _G_va_list va_list;
# define _VA_LIST_DEFINED
# endif
# else
# include <stdarg.h>
# endif
#endif
#ifdef __USE_XOPEN2K8
# ifndef __off_t_defined
# ifndef __USE_FILE_OFFSET64
typedef __off_t off_t;
# else
typedef __off64_t off_t;
# endif
# define __off_t_defined
# endif
# if defined __USE_LARGEFILE64 && !defined __off64_t_defined
typedef __off64_t off64_t;
# define __off64_t_defined
# endif
# ifndef __ssize_t_defined
typedef __ssize_t ssize_t;
# define __ssize_t_defined
# endif
#endif
/* The type of the second argument to `fgetpos' and `fsetpos'. */
__BEGIN_NAMESPACE_STD
#ifndef __USE_FILE_OFFSET64
typedef _G_fpos_t fpos_t;
#else
typedef _G_fpos64_t fpos_t;
#endif
__END_NAMESPACE_STD
#ifdef __USE_LARGEFILE64
typedef _G_fpos64_t fpos64_t;
#endif
/* The possibilities for the third argument to `setvbuf'. */
#define _IOFBF 0 /* Fully buffered. */
#define _IOLBF 1 /* Line buffered. */
#define _IONBF 2 /* No buffering. */
/* Default buffer size. */
#ifndef BUFSIZ
# define BUFSIZ _IO_BUFSIZ
#endif
/* End of file character.
Some things throughout the library rely on this being -1. */
#ifndef EOF
# define EOF (-1)
#endif
/* The possibilities for the third argument to `fseek'.
These values should not be changed. */
#define SEEK_SET 0 /* Seek from beginning of file. */
#define SEEK_CUR 1 /* Seek from current position. */
#define SEEK_END 2 /* Seek from end of file. */
#if defined __USE_SVID || defined __USE_XOPEN
/* Default path prefix for `tempnam' and `tmpnam'. */
# define P_tmpdir "/tmp"
#endif
/* Get the values:
L_tmpnam How long an array of chars must be to be passed to `tmpnam'.
TMP_MAX The minimum number of unique filenames generated by tmpnam
(and tempnam when it uses tmpnam's name space),
or tempnam (the two are separate).
L_ctermid How long an array to pass to `ctermid'.
L_cuserid How long an array to pass to `cuserid'.
FOPEN_MAX Minimum number of files that can be open at once.
FILENAME_MAX Maximum length of a filename. */
#include <bits/stdio_lim.h>
/* Standard streams. */
extern struct _IO_FILE *stdin; /* Standard input stream. */
extern struct _IO_FILE *stdout; /* Standard output stream. */
extern struct _IO_FILE *stderr; /* Standard error output stream. */
/* C89/C99 say they're macros. Make them happy. */
#define stdin stdin
#define stdout stdout
#define stderr stderr
__BEGIN_NAMESPACE_STD
/* Remove file FILENAME. */
extern int remove (__const char *__filename) __THROW;
/* Rename file OLD to NEW. */
extern int rename (__const char *__old, __const char *__new) __THROW;
__END_NAMESPACE_STD
#ifdef __USE_ATFILE
/* Rename file OLD relative to OLDFD to NEW relative to NEWFD. */
extern int renameat (int __oldfd, __const char *__old, int __newfd,
__const char *__new) __THROW;
#endif
__BEGIN_NAMESPACE_STD
/* Create a temporary file and open it read/write.
This function is a possible cancellation points and therefore not
marked with __THROW. */
#ifndef __USE_FILE_OFFSET64
extern FILE *tmpfile (void) __wur;
#else
# ifdef __REDIRECT
extern FILE *__REDIRECT (tmpfile, (void), tmpfile64) __wur;
# else
# define tmpfile tmpfile64
# endif
#endif
#ifdef __USE_LARGEFILE64
extern FILE *tmpfile64 (void) __wur;
#endif
/* Generate a temporary filename. */
extern char *tmpnam (char *__s) __THROW __wur;
__END_NAMESPACE_STD
#ifdef __USE_MISC
/* This is the reentrant variant of `tmpnam'. The only difference is
that it does not allow S to be NULL. */
extern char *tmpnam_r (char *__s) __THROW __wur;
#endif
#error Omitted due to post length limit
__BEGIN_NAMESPACE_STD
#ifndef __USE_FILE_OFFSET64
/* Get STREAM's position.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern int fgetpos (FILE *__restrict __stream, fpos_t *__restrict __pos);
/* Set STREAM's position.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern int fsetpos (FILE *__stream, __const fpos_t *__pos);
#else
# ifdef __REDIRECT
extern int __REDIRECT (fgetpos, (FILE *__restrict __stream,
fpos_t *__restrict __pos), fgetpos64);
extern int __REDIRECT (fsetpos,
(FILE *__stream, __const fpos_t *__pos), fsetpos64);
# else
# define fgetpos fgetpos64
# define fsetpos fsetpos64
# endif
#endif
__END_NAMESPACE_STD
#ifdef __USE_LARGEFILE64
extern int fseeko64 (FILE *__stream, __off64_t __off, int __whence);
extern __off64_t ftello64 (FILE *__stream) __wur;
extern int fgetpos64 (FILE *__restrict __stream, fpos64_t *__restrict __pos);
extern int fsetpos64 (FILE *__stream, __const fpos64_t *__pos);
#endif
__BEGIN_NAMESPACE_STD
/* Clear the error and EOF indicators for STREAM. */
extern void clearerr (FILE *__stream) __THROW;
/* Return the EOF indicator for STREAM. */
extern int feof (FILE *__stream) __THROW __wur;
/* Return the error indicator for STREAM. */
extern int ferror (FILE *__stream) __THROW __wur;
__END_NAMESPACE_STD
#ifdef __USE_MISC
/* Faster versions when locking is not required. */
extern void clearerr_unlocked (FILE *__stream) __THROW;
extern int feof_unlocked (FILE *__stream) __THROW __wur;
extern int ferror_unlocked (FILE *__stream) __THROW __wur;
#endif
__BEGIN_NAMESPACE_STD
/* Print a message describing the meaning of the value of errno.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern void perror (__const char *__s);
__END_NAMESPACE_STD
/* Provide the declarations for `sys_errlist' and `sys_nerr' if they
are available on this system. Even if available, these variables
should not be used directly. The `strerror' function provides
all the necessary functionality. */
#include <bits/sys_errlist.h>
#ifdef __USE_POSIX
/* Return the system file descriptor for STREAM. */
extern int fileno (FILE *__stream) __THROW __wur;
#endif /* Use POSIX. */
#ifdef __USE_MISC
/* Faster version when locking is not required. */
extern int fileno_unlocked (FILE *__stream) __THROW __wur;
#endif
#if (defined __USE_POSIX2 || defined __USE_SVID || defined __USE_BSD || \
defined __USE_MISC)
/* Create a new stream connected to a pipe running the given command.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern FILE *popen (__const char *__command, __const char *__modes) __wur;
/* Close a stream opened by popen and return the status of its child.
This function is a possible cancellation point and therefore not
marked with __THROW. */
extern int pclose (FILE *__stream);
#endif
#ifdef __USE_POSIX
/* Return the name of the controlling terminal. */
extern char *ctermid (char *__s) __THROW;
#endif /* Use POSIX. */
#ifdef __USE_XOPEN
/* Return the name of the current user. */
extern char *cuserid (char *__s);
#endif /* Use X/Open, but not issue 6. */
#ifdef __USE_GNU
struct obstack; /* See <obstack.h>. */
/* Write formatted output to an obstack. */
extern int obstack_printf (struct obstack *__restrict __obstack,
__const char *__restrict __format, ...)
__THROW __attribute__ ((__format__ (__printf__, 2, 3)));
extern int obstack_vprintf (struct obstack *__restrict __obstack,
__const char *__restrict __format,
_G_va_list __args)
__THROW __attribute__ ((__format__ (__printf__, 2, 0)));
#endif /* Use GNU. */
#if defined __USE_POSIX || defined __USE_MISC
/* These are defined in POSIX.1:1996. */
/* Acquire ownership of STREAM. */
extern void flockfile (FILE *__stream) __THROW;
/* Try to acquire ownership of STREAM but do not block if it is not
possible. */
extern int ftrylockfile (FILE *__stream) __THROW __wur;
/* Relinquish the ownership granted for STREAM. */
extern void funlockfile (FILE *__stream) __THROW;
#endif /* POSIX || misc */
#if defined __USE_XOPEN && !defined __USE_XOPEN2K && !defined __USE_GNU
/* The X/Open standard requires some functions and variables to be
declared here which do not belong into this header. But we have to
follow. In GNU mode we don't do this nonsense. */
# define __need_getopt
# include <getopt.h>
#endif /* X/Open, but not issue 6 and not for GNU. */
/* If we are compiling with optimizing read this file. It contains
several optimizing inline functions and macros. */
#ifdef __USE_EXTERN_INLINES
# include <bits/stdio.h>
#endif
#if __USE_FORTIFY_LEVEL > 0 && defined __extern_always_inline
# include <bits/stdio2.h>
#endif
#ifdef __LDBL_COMPAT
# include <bits/stdio-ldbl.h>
#endif
__END_DECLS
#endif /* <stdio.h> included. */
#endif /* !_STDIO_H */
That, is why we have header files.
Providing function declarations in a header will at least protect you from stupid errors like passing arguments of incompatible types.
Moreover, headers are used not only for function declarations. What's about struct/enum definitions, global typedefs, macros, inline functions, etc?
Finally, header can be considered as an external API for your module. One can briefly take a look at a header to understand how to use it without dealing with an implementation (which sometimes is not available at all).
C header files are a way to share global pointers, macros (#define ...), common structure types declared as uninstatated structures or typedefs. One of the biggest uses of a header file is to share function declarations across C modules, as well.
I am using statvfs function call on AIX. And using GCC compiler.
I would like statvfs call to resolve to statvfs64 by preprocessor.
Ex: In Solaris, using "-D_LARGEFILE64_SOURCE -D_FILE_OFFSET_BITS=64" flags with gcc i am resolved to statvfs64.
Could you please help in getting the similar flags on AIX which resolves me to statvfs64 from statvfs.
Thanks & Regards,
Sivaram T
Thanks a lot for immediate response.
Unfortunately there is no "_LARGEFILE64_SOURCE" define on AIX include file.
I come to know the following options
"-maix64 -mpowerpc64" can resolve to the statvfs64. Not sure whether these are right to use or not.
Please find the following sys/statvfs.h file
=================================================
#ifndef _H_STATVFS
#define _H_STATVFS
#ifndef _H_STANDARDS
#include <standards.h>
#endif
#if _XOPEN_SOURCE_EXTENDED==1
#include <strict_stdtypes.h>
#ifndef _H_TYPES
#include <sys/types.h>
#endif
#include <end_strict_stdtypes.h>
#define _FSTYPSIZ 16
#ifdef _ALL_SOURCE
#include <sys/vmount.h>
#define FSTYPSIZ _FSTYPSIZ
#endif
/*
* statvfs system call return structure
*/
struct statvfs {
ulong_t f_bsize; /* preferred file system block size */
ulong_t f_frsize; /* fundamental file system block size */
fsblkcnt_t f_blocks; /* total # of blocks of f_frsize in fs */
fsblkcnt_t f_bfree; /* total # of free blocks */
fsblkcnt_t f_bavail; /* # of blocks available to non super user */
fsfilcnt_t f_files; /* total # of file nodes (inode in JFS) */
fsfilcnt_t f_ffree; /* total # of free file nodes */
fsfilcnt_t f_favail; /* # of nodes available to non super user */
#ifdef _ALL_SOURCE
fsid_t f_fsid; /* file system id */
#else
ulong_t f_fsid; /* file system id */
#ifndef __64BIT__
ulong_t f_fstype; /* file system type */
#endif
#endif /* _ALL_SOURCE */
char f_basetype[_FSTYPSIZ]; /* Filesystem type name (eg. jfs) */
ulong_t f_flag; /* bit mask of flags */
ulong_t f_namemax; /* maximum filename length */
char f_fstr[32]; /* filesystem-specific string */
ulong_t f_filler[16];/* reserved for future use */
};
#define ST_NOSUID 0x0040 /* don't maintain SUID capability */
#define ST_RDONLY 0x0001 /* file system mounted read only */
#define ST_NODEV 0x0080 /* don't allow device access across */
/* this mount */
/*
* Prototypes
*/
#ifdef _NO_PROTO
extern int statvfs();
extern int fstatvfs();
#else
extern int statvfs(const char *__restrict__, struct statvfs *__restrict__);
extern int fstatvfs(int, struct statvfs *);
#endif
/*
* statvfs64 system call return structure
*/
#ifdef _ALL_SOURCE
struct statvfs64 {
blksize64_t f_bsize; /* preferred file system block size */
blksize64_t f_frsize; /* fundamental file system block size */
blkcnt64_t f_blocks; /* total # of blocks of f_frsize in fs */
blkcnt64_t f_bfree; /* total # of free blocks */
blkcnt64_t f_bavail; /* # of blocks available to non super user */
blkcnt64_t f_files; /* total # of file nodes (inode in JFS) */
blkcnt64_t f_ffree; /* total # of free file nodes */
blkcnt64_t f_favail; /* # of nodes available to non super user */
fsid64_t f_fsid; /* file system id */
char f_basetype[FSTYPSIZ]; /* Filesystem type name (eg. jfs) */
ulong_t f_flag; /* bit mask of flags */
ulong_t f_namemax; /* maximum filename length */
char f_fstr[32]; /* filesystem-specific string */
ulong_t f_filler[16];/* reserved for future use */
};
/*
* Prototypes
*/
#ifdef _NO_PROTO
extern int statvfs64();
extern int fstatvfs64();
#else
extern int statvfs64(const char *__restrict__, struct statvfs64 *__restrict__);
extern int fstatvfs64(int, struct statvfs64 *);
#endif
#endif /* _ALL_SOURCE */
#endif /* _XOPEN_SOURCE_EXTENDED */
#endif /* _H_STATVFS */
=================================================
I don't have an AIX system, so I can't tell you the flags to set. However, on Solaris you can view sys/statvfs.h and see how this works, e.g search for statvfs64 and look for the #ifdef blocks surrounding it. You'll see the lines
#if defined(_LARGEFILE64_SOURCE)
typedef struct statvfs64 {
.....
} statvfs64_t;
#endif
#if !defined(_LP64) && _FILE_OFFSET_BITS == 64
...
#define statvfs_t statvfs64_t
#define statvfs statvfs64
#define fstatvfs fstatvfs64
#endif
You will be able to do exactly the same thing on AIX. However, AIX may behave differently and not use the pre-processor to switch between the 32 and 64 bit versions.
If it's not obvious to you, then you could post the contents of sys/statvfs.h up here and we can take a look for you.
iirc on AIX you need the _LARGE_FILES token set, which will enable implicit large file support.
-D_LARGE_FILES
If you want to call statvfs64 explicittly, you have to do
-D_LARGE_FILE_API