是否有一个unistd.h中替代Windows(的Visual C)? [英] Is there a replacement for unistd.h for Windows (Visual C)?

查看:1544
本文介绍了是否有一个unistd.h中替代Windows(的Visual C)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我移植为Unix写入Windows平台相对简单的控制台程序(的Visual C ++ 8.0 )。所有源文件包括unistd.h中,这不存在。删除它,我得到投诉misssing原型'srandom','随机'和'getopt的'。
我知道我可以代替随机函数,我pretty相信我能找到/下锅了getopt的执行情况。

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.

不过我相信其他人遇到同样的挑战。
我的问题是:是否有unistd.h中到Windows端口?至少有一个containg那些做功能具有本机Windows实现 - 我不需要管或分叉

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.

修改

我知道我可以创建自己的unistd.h中,其中包含了我所需要的东西的替代品 - 尤其是在这种情况下,因为它是一组有限的。但是,因为它似乎是一个常见的​​问题,我想知道是否有人做的工作已经为功能的一个子集,更大

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.

切换到不同的编译器或环境是不可能的工作 - 我卡与Visual Studio

Switching to a different compiler or environment isn't possible at work - I'm stuck with Visual Studio.

推荐答案

既然我们无法在互联网上找到一个版本,让我们开始一个在这里。结果
大多数港口到Windows可能只需要完整的Unix文件的子集。结果
这里是一个起点。请根据需要添加定义。

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 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 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(). */

#define ssize_t int

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

这篇关于是否有一个unistd.h中替代Windows(的Visual C)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆