如何读取Glibc代码中的环境变量
我想通过读取环境变量来禁用/启用 printf()
输出到屏幕,类似于 LD_DEBUG
的工作方式。
我想控制./stdio-common/printf.c
。
因此,如果在环境变量中表示禁用 printf() 它会调用,
int
__printf (const char *format, ...)
{
return done;
}
否则它会执行原始代码。我将如何实施这个?
I want to disable/enable printf()
output to screen by reading an environment variable, similar to how LD_DEBUG
works.
I want to control ./stdio-common/printf.c
.
So if in environment variable says disable printf() it call
int
__printf (const char *format, ...)
{
return done;
}
else it executes the original code. How would I implement this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
使用 getenv。有关详细信息,请参阅
getenv(3)
。Use
getenv
. Seegetenv(3)
for details.推荐的方法是使用 ANSI 提供的解决方案作为 getenv() 函数以获得最大的可移植性:
还有其他非标准方法可以执行相同的操作,但不推荐。
main()
的第三个参数可以是char **envp
广泛用于获取有关环境的信息,并且是非标准
。在
main()
中使用第三个参数不严格符合标准。还有另一种广泛使用的非标准访问环境变量的方法,那就是通过
environ
外部变量。注意:这些示例不完整并且缺乏错误处理。
The recommended way is to use the solution provided by ANSI as
getenv()
function for maximum portability:There are other non-standard ways of doing the same which is not recommended.
The third argument to
main()
could bechar **envp
is used widely to get the information about the environment and isnon-standard
.Using the third argument in
main()
is not strictly conforming to standard.There is another widely used non-standard way of accessing the environmental variables and that is through the
environ
external variable.NOTE: The examples are in-complete and lacks error handling.