views:

14183

answers:

8

I'm porting a relatively simple console program written for unix to the Windows platform. (VC++ 2005). 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 funcs, 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: Has someone done 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.

+7  A: 

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 apps 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 here.

e.James
+1  A: 

Create your own unistd.h header and include the needed headers for function prototypes.

Iulian Şerbănoiu
I know I can do do that, I'll edit the question to make that clearer. The point of the question was: has anyone else done it for me?
AShelly
+1  A: 

http://technet.microsoft.com/en-us/interopmigration/bb380242.aspx

not sure, HTH

plan9assembler
+1 for the link
Tony Lee
A: 

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 config files and environment variables in addition to command line options.

ceretullis
funny googling for "getopt windows" give this as the first hit: http://www.codeproject.com/KB/cpp/xgetopt.aspx
Evan Teran
The point was not having to re-write the existing code to use Boost or some other parser.
AShelly
@AShelly: I'm afraid if you code using *nix only API's, then you have no choice but to either re-write existing code to be portable, or to provide your own implementation of the missing functionality. If you used boost::program_options to begin with it will work on many platforms without change.
ceretullis
But _I_ didn't write the original code: I found a useful open-source command-line utility written for *nix. I wanted to create a version for my workplace environment which is fixed on VS2005.
AShelly
@AShelly: fair enough.
ceretullis
+3  A: 

So far, I haven't found a pre-existing unistd.h, but I did find a drop-in getopt replacement at http://www.pwilson.net/sample.html. It compiled on VC2005 without any code changes needed.

AShelly
A: 

AShelly, were you able to find an unistd.h for VS2005 environment? Or did you write one yourself? I am in the process of tyring to re-complie to C++ project in VS2005, but I am stumped because of missing unistd.h :(

I wrote one myself, see the accepted answer
AShelly
+9  A: 

Since we can't find a version on the interweb, 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 file intended to serve as a drop-in replacement for 
 *  unistd.h on Windows
 *  Please add functionality as neeeded 
 */

#include <stdlib.h>
#include <io.h>
#include <getopt.h> /* getopt from: http://www.pwilson.net/sample.html. */

#define srandom srand
#define random rand

const W_OK = 2;
const R_OK = 4;

#define access _access
#define ftruncate _chsize

#define ssize_t int

#endif /* unistd.h  */
AShelly
A: 

Try including io.h file - seems to be the visual studio's equivalent of unistd.h.

HTH

anonymous