`
aigo
  • 浏览: 2541652 次
  • 性别: Icon_minigender_1
  • 来自: 宜昌
社区版块
存档分类
最新评论

Clang setenv()用法(windows对应函数是_putenv_s)

C 
阅读更多

以下是Linux平台的,windows平台对应的是 _putenv_s(name, value);

windows上自己实现一个类似setenv的函数:

int setenv(const char *name, const char *value, int overwrite)
{
    int errcode = 0;
    if(!overwrite) {
        size_t envsize = 0;
        errcode = getenv_s(&envsize, NULL, 0, name);
        if(errcode || envsize) return errcode;
    }
    return _putenv_s(name, value);
}

 

 

原文:http://stackoverflow.com/questions/17929414/how-to-use-setenv-to-export-a-variable-in-c

 

From the setenv() manual entry:

SYNOPSIS

#include <stdlib.h>  
int setenv(const char *envname, const char *envval, int overwrite);

DESCRIPTION
The setenv() function shall update or add a variable in the environment of the calling process. The envname argument points to a string containing the name of an environment variable to be added or altered. The environment variable shall be set to the value to which envval points. The function shall fail if envname points to a string which contains an '=' character. If the environment variable named by envname already exists and the value of overwrite is non-zero, the function shall return success and the environment shall be updated. If the environment variable named by envname already exists and the value of overwrite is zero, the function shall return success and the environment shall remain unchanged.

If the application modifies environ or the pointers to which it points, the behavior of setenv() is undefined. The setenv() function shall update the list of pointers to which environ points.

The strings described by envname and envval are copied by this function.

The setenv() function need not be reentrant. A function that is not required to be reentrant is not required to be thread-safe.

RETURN VALUE
Upon successful completion, zero shall be returned. Otherwise, -1 shall be returned, errno set to indicate the error, and the environment shall be unchanged.

So you should call

setenv("ROS_HOSTNAME","xxx",1);

or

setenv("ROS_HOSTNAME","xxx",0);
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics