getenv, _wgetenv
Description
The C library function char *getenv(const char *name) searches for the environment string pointed to by name and returns the associated value to the string.
Declaration
Following is the declaration for getenv() function.
char *getenv(const char *name)
Parameters
name -- This is the C string containing the name of the requested variable.
Return Value
This function returns a null-terminated string with the value of the requested environment variable, or NULL if that environment variable does not exist.
Example
The following example shows the usage of getenv() function.
#include <stdio.h>
#include <stdlib.h> int main ()
{
printf("PATH : %s\n", getenv("PATH"));
printf("HOME : %s\n", getenv("HOME"));
printf("ROOT : %s\n", getenv("ROOT")); return(0);
}
Let us compile and run the above program that will produce the following result:
PATH : /sbin:/usr/sbin:/bin:/usr/bin:/usr/local/bin
HOME : /
ROOT : (null)
std::getenv
Defined in header
<cstdlib> |
||
char* getenv( const char* env_var );
|
||
Searches the environment list provided by the host environment (the OS), for a string that matches the C string pointed to by env_var
and returns a pointer to the C string that is associated with the matched environment list member.
This function is not required to be thread-safe. Another call to getenv, as well as a call to the POSIX functions setenv(), unsetenv(), and putenv() may invalidate the pointer returned by a previous call or modify the string obtained from a previous call. |
(until C++11) |
This function is thread-safe (calling it from multiple threads does not introduce a data race) as long as no other function modifies the host environment. In particular, the POSIX functions setenv(), unsetenv(), and putenv() would introduce a data race if called without synchronization. |
(since C++11) |
Modifying the string returned by getenv
invokes undefined behavior.
Parameters
env_var | - | null-terminated character string identifying the name of the environmental variable to look for |
Return value
Character string identifying the value of the environmental variable or null pointer if such variable is not found.
Notes
On POSIX systems, the environment variables are also accessible through the global variable environ
, declared as extern char **environ; in <unistd.h>
, and through the optional third argument, envp
, of the main function.
Example
#include <iostream>
#include <cstdlib>
int main()
{
if(const char* env_p = std::getenv("PATH"))
std::cout << "Your PATH is: " << env_p << '\n';
}
Possible output:
Your PATH is: /usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games
getenv, _wgetenv
Gets a value from the current environment. More secure versions of these functions are available; see getenv_s, _wgetenv_s.
Important |
---|
This API cannot be used in applications that execute in the Windows Runtime. For more information, see CRT functions not supported with /ZW. |
Return Value
Returns a pointer to the environment table entry containing varname. It is not safe to modify the value of the environment variable using the returned pointer. Use the _putenv function to modify the value of an environment variable. The return value is NULL if varname is not found in the environment table.
Remarks
The getenv function searches the list of environment variables for varname. getenv is not case sensitive in the Windows operating system. getenvand _putenv use the copy of the environment pointed to by the global variable _environ to access the environment. getenv operates only on the data structures accessible to the run-time library and not on the environment "segment" created for the process by the operating system. Therefore, programs that use the envp argument to main or wmain may retrieve invalid information.
If varname is NULL, this function invokes an invalid parameter handler, as described in Parameter Validation. If execution is allowed to continue, this function sets errno to EINVAL and returns NULL.
_wgetenv is a wide-character version of getenv; the argument and return value of _wgetenv are wide-character strings. The _wenviron global variable is a wide-character version of _environ.
In an MBCS program (for example, in an SBCS ASCII program), _wenviron is initially NULL because the environment is composed of multibyte-character strings. Then, on the first call to _wputenv, or on the first call to _wgetenv if an (MBCS) environment already exists, a corresponding wide-character string environment is created and is then pointed to by _wenviron.
Similarly in a Unicode (_wmain) program, _environ is initially NULL because the environment is composed of wide-character strings. Then, on the first call to _putenv, or on the first call to getenv if a (Unicode) environment already exists, a corresponding MBCS environment is created and is then pointed to by _environ.
When two copies of the environment (MBCS and Unicode) exist simultaneously in a program, the run-time system must maintain both copies, resulting in slower execution time. For example, whenever you call _putenv, a call to _wputenv is also executed automatically, so that the two environment strings correspond.
Caution |
---|
In rare instances, when the run-time system is maintaining both a Unicode version and a multibyte version of the environment, these two environment versions may not correspond exactly. This is because, although any unique multibyte-character string maps to a unique Unicode string, the mapping from a unique Unicode string to a multibyte-character string is not necessarily unique. For more information, see _environ, _wenviron. |
Note |
---|
The _putenv and _getenv families of functions are not thread-safe. _getenv could return a string pointer while _putenv is modifying the string, causing random failures. Make sure that calls to these functions are synchronized. |
Generic-Text Routine Mappings
TCHAR.H routine |
_UNICODE & _MBCS not defined |
_MBCS defined |
_UNICODE defined |
---|---|---|---|
_tgetenv |
getenv |
getenv |
_wgetenv |
To check or change the value of the TZ environment variable, use getenv, _putenv and _tzset as necessary. For more information about TZ, see_tzset and _daylight, timezone, and _tzname.
Requirements
Routine |
Required header |
---|---|
getenv |
<stdlib.h> |
_wgetenv |
<stdlib.h> or <wchar.h> |
For additional compatibility information, see Compatibility.
Example
// crt_getenv.c
// compile with: /W3
// This program uses getenv to retrieve
// the LIB environment variable and then uses
// _putenv to change it to a new value. #include <stdlib.h>
#include <stdio.h> int main( void )
{
char *libvar; // Get the value of the LIB environment variable.
libvar = getenv( "LIB" ); // C4996
// Note: getenv is deprecated; consider using getenv_s instead if( libvar != NULL )
printf( "Original LIB variable is: %s\n", libvar ); // Attempt to change path. Note that this only affects the environment
// variable of the current process. The command processor's
// environment is not changed.
_putenv( "LIB=c:\\mylib;c:\\yourlib" ); // C4996
// Note: _putenv is deprecated; consider using putenv_s instead // Get new value.
libvar = getenv( "LIB" ); // C4996 if( libvar != NULL )
printf( "New LIB variable is: %s\n", libvar );
}
Original LIB variable is: C:\progra~1\devstu~1\vc\lib
New LIB variable is: c:\mylib;c:\yourlib
getenv, _wgetenv的更多相关文章
- C++ Unicode SBCS 函数对照表
C++ Unicode SBCS 函数对照表,以备日后查阅 Generic SBCS UNICODE TCHAR char wchar_t _TEOF EOF WEOF _TINT int wint_ ...
- C++ Unicode SBCS 函数对照表,以备日后查阅(很全)
C++ Unicode SBCS 函数对照表,以备日后查阅 Generic SBCS UNICODE TCHAR char wchar_t _TEOF EOF WEOF _TINT int wint_ ...
- window 环境变量保存位置在哪里?
注册表-regedit.exe 用户变量: HKEY_CURRENT_USER\Environment 系统变量: HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSe ...
- VC++关于UNICODE版本的开发
关于UNICODE版本的开发 代码转换方案 概述 在VC6.0中,相应的有一些宏来代替ANSI的函数.宏或数据类型,这些宏在ANSI编译条件中处理字符串为单字节,而在UNICODE中处理字符串为双字节 ...
- 使你的C/C++代码支持Unicode(CRT字符串处理的所有API列表,甚至有WEOF字符存在)
悉Microsoft支持Unicode的方式. 它的主要目的是方便你查询相关的数据类型和函数,以及修正相应的拼写错误. I18nGuy 主页 XenCraft (Unicode 咨询公司) Engli ...
- Unicode 和 ANSI
Project Properties -> General-> Character set,里面显示了是不是unicode. Unicode处理String的方式不一样,一定要注意!! ...
- [置顶]
getenv、setenv函数(获取和设置系统环境变量) 与 环境变量
1.getenv() 函数名: getenv 功 能: 从环境中取字符串,获取环境变量的值 头文件: stdlib.h 用 法:char *getenv(char *envvar); 函数说明:get ...
- java中System.getenv和System.getProperties的区别
System.getenv获取的是系统的环境变量(就是用户在操作系统中设置的环境变量),windows和linux下环境变量的设置就不说了哦. System.getProperties获取的是系统的相 ...
- System.Properties和System.getenv区别
网上很多使用的是getProperties.说获得系统变量,但是其实不正确.getProperties中所谓的"system properties"其实是指"java s ...
随机推荐
- Hive将SQL转化为MapReduce的过程
Hive将SQL转化为MapReduce的过程: Antlr定义SQL的语法规则,完成SQL词法,语法解析,将SQL转化为抽象语法树AST Tree 遍历AST Tree,抽象出查询的基本组成单元Qu ...
- 33、Android Support兼容包详解(转载)
原文转自:微信分享 2015-03-31 22:11 背景 来自于知乎上邀请回答的一个问题Android中AppCompat和Holo的一个问题?, 看来很多人还是对这些兼容包搞不清楚,那么干脆写篇博 ...
- CSU-1974 神奇药水
CSU-1974 神奇药水 Description 对于csuxushu来说,能够在CSU(California State University)组织2017年的ACM暑期集训让他感到十分荣幸. ...
- 后缀数组(SA)学习记录
一只只会后缀自动机却不会后缀数组的弱鸡做了一下HDU - 1403,结果SAM被卡内存了,然后学习了一下SA. 以下两道题都是求LCS,区别在于字符串长度. 参考blog:https://www.cn ...
- SQLAlchemy 学习笔记(三):ORM 中的关系构建
个人笔记,不保证正确. 关系构建:ForeignKey 与 relationship 关系构建的重点,在于搞清楚这两个函数的用法.ForeignKey 的用法已经在 SQL表达式语言 - 表定义中的约 ...
- 查找jar包中.class文件关键字(变量名,字符串)
有时查看日志,常常会发现由框架底层打印的错误日志.要修改这个错误的时候,如果不是对框架特别熟悉,就需要按照可能产生这个错误日志的流程一步一步找,一时半会不一定能找到.比如本人最近对smartfoxse ...
- PAT1028
某城镇进行人口普查,得到了全体居民的生日.现请你写个程序,找出镇上最年长和最年轻的人. 这里确保每个输入的日期都是合法的,但不一定是合理的——假设已知镇上没有超过200岁的老人,而今天是2014年9月 ...
- 逆向中静态分析工具——IDA初学者笔记之字符串分析
逆向中静态分析工具——IDA初学者笔记之字符串分析 程序中往往包含很多字符串资源,这些资源存在于PE文件的rdata段,使用IDA反编译后,可以查找到这些字符串, 逆向破解程序通常需要一个突破点,而这 ...
- ABC128F Frog Jump
题目链接 题目大意 给定一个长为 $n$ 的数组 $s$,下标从 $0$ 开始.$ 3 \le n \le 10^5$,$-10^9 \le s_i \le 10^9$,$s_0 = s_{n - 1 ...
- 微信小程序统一服务消息下发接口 公众号和小程序消息都可以在一个接口推送了
昨天晚上,2018年9月11日,微信官方又更新了一大波的小程序功能.重点我们来谈谈这个功能,微信叫做统一服务消息下发接口. 这个是官方的文档 统一服务消息 · 小程序https://develope ...