Is There a Replacement For Unistd.H For Windows (Visual C)

Is there a replacement for unistd.h for Windows (Visual C)?

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 */

How to use <unistd.h> library in visual studio?

If you are using Visual Studio, it probably means you are targetting Windows...? fork() is not part of Windows API, you must then find another way to create a new process:

  • Directly use Windows API: CreateProcess
  • Use a cross-platform library (that will callCreateProcess on Windows and probably fork on Linux. Have a look at Boost.Process for instance (but there's probably others, like Qt's QProcess

I would personnaly recommand the second approach (I use Boost.Process).

Implementing a custom unistd.h for windows to work on Visual Studio - problems

It appears that all your header and source files should be in the same VS project folder - the original header and source files where on my computer in another folder and after creating a new project I used add existing item and found and chose them to add.

The unistd.h file I created later by adding a new item to the project - effectively both file groups where in two different places and that contributed to the fact that they didn't "see" the custom unistd.h. It wasn't a problem with the code or the way I included the header file, but with the placing of the files.

problem with unistd.h in VC++

Why? It's just not there. It looks like you try to compile a program that is not portable or you try to port the program to Windows.

unistd.h is a not a standard header. Probably you find it only on Unix-like systems.

See: Wiki

write function requires unistd.h on Unix, what about windows?

Your best bet is to use MinGW to compile the program, which includes (among other things) GCC (and its headers). Try installing that and then compiling your program and see if everything works OK.



Related Topics



Leave a reply



Submit