Home >Backend Development >C++ >Is There a unistd.h Equivalent for Windows (Visual C )?
Is there a replacement for unistd.h in Windows (Visual C)?
In order to port a relatively simple console program written for Unix to the Windows platform (Visual C 8.0), all source files include "unistd.h" which does not exist in Windows. After removing it, the system complains that prototypes for 'srandom', 'random' and 'getopt' are not found.
I understand that I can replace the random function, and I'm pretty sure I can find/get a getopt implementation.
But I believe others have encountered the same challenges. My question is: Is there a port of "unistd.h" to Windows? At least those functions that have Windows native implementation should be included, I don't need piping or forking.
Edit:
I know I can create my own "unistd.h" that contains the replacements for what I need, especially in this case because it is a limited collection. But since it seems to be a common problem, I'm wondering if anyone has done this for a wider range of functionality.
Switching to a different compiler or environment on the job is not possible - I can only use Visual Studio.
Alternatives:
Since we can’t find a version on the internet, let’s start here. Most files ported to Windows will probably only require a subset of the entire Unix file.
This is 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 needed. * 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 */
The above is the detailed content of Is There a unistd.h Equivalent for Windows (Visual C )?. For more information, please follow other related articles on the PHP Chinese website!