环境变量 [英] Environment Variables

查看:112
本文介绍了环境变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述




访问环境有什么区别

变量通过


int main(int argc,char * argv [],char * envp [])





extern char ** environ;

上述两种方法中是否有任何优势?

解决方案

" learner" < v _ ******** @ yahoo.com>写道:

通过
int main(int argc,char * argv [],char * envp访问环境变量之间的区别是什么? [])



extern char ** environ;
上述两种方法中是否有任何优势?




编号两者都是过时的,非ISO方式尝试阅读环境

变量,并且您可能会遇到使用

的可移植性问题他们。在便携式中读取环境变量的正确方法是使用getenv()函数。


Richard


"学习者" < v _ ******** @ yahoo.com>写道:



访问环境
变量有什么区别通过

int main(int argc,char * argv [],char * envp [])



extern char ** environ;


没有区别,它们都是非标准和不可移植的。实际上,environ

不是一个保留的标识符,所以实现的任何尝试都要提供它的定义,更不用说除了通常的空指针之外的任何值了。 />
初始化,将是不合适的。


Cygwin的GCC 3.3.1似乎有一个错误!如果我尝试定义我自己的外部

变量,我会收到此错误:

/usr/lib/gcc-lib/i686-pc-cygwin/3.3.1/ ../../../libcygwin.a(_cygwin_crt0_comm

on.o)(。bss + 0x0):`_ environ'的多重定义'

/ cygdrive /c/DOCUME~1/SIMONB~1/LOCALS~1/Temp/ccMMv8Qk.o(.data+0x0):environ2.c

:首先在这里定义


我应该被允许创建我自己的''extern''变量 - 名字不是

保留。


MS'' CL 13.10.3052确实提供了environ的定义,但它的链接器也允许我创建自己的内容,它会覆盖内部的内容。这可能是符合'as-if'规则的标准符号。


如果使用environ,Borland BCC32 5.5.1正确地给出错误但不是

定义:

错误:未解析的外部''_ environ''引自

C:\DOCUMENT \ PROG \ C \ ENVIRON.OBJ


LCC-Win32的行为与bcc32正确,因为我收到链接器错误

environ未定义:

environ.obj .text:对''_ environ'的未定义引用'

链接器返回2

但是出于一些奇怪的原因它仍然继续创建一个可执行文件!

可执行文件因为它的价值而崩溃。

以上两种方法中是否有任何优势?



编号我建议您使用标准C解决方案,这是一个库函数:

char * getenv(const char * name);


它将在列表中搜索给定名称的环境变量,并且

返回指向包含该变量值的字符串的指针。如果找不到

名称,将返回空指针。


-

Simon。




文章< 41 **************** @ news.individual.net>, rl *@hoekstra-uitgeverij.nl (Richard Bos)写道:

" learner" < v _ ******** @ yahoo.com>写道:

通过
int main(int argc,char * argv [],char * envp访问环境变量之间的区别是什么? [])



extern char ** environ;
以上两种都有任何优势吗?
不。两者都已过时,非ISO方式尝试阅读环境
变量,




虽然environ不属于ISO 9899,C标准,但它包括在内ISO 9945(POSIX)中的
,因此非ISO可能会误导。虽然

comp.lang.c特别针对ISO 9899,但有用的程序通常需要在该标准的范围之外运行,所以它是

通常有助于指出此类操作何时适用于另一个

标准,这可能适用于用户的实施。

你可能遇到使用它们之一的可移植性问题。在便携式,ISO标准C中读取环境变量的正确方法是使用getenv()函数。




我相信它是'值得注意的是,事实上,与C getenv相比,

POSIX环境机制有一个优势,因此environ是通过

而不是过时。 environ允许在环境列表中迭代

中的所有值; getenv没有。 (environ因此预先假定

环境列表以某种有意义的方式支持迭代,当然,这是C不需要的
。)


在环境列表中迭代是某些环境中某些

程序的重要特性。 (例如,其中一些对于

安全目的很有用。)


-

Michael Wojcik mi ************ @ microfocus.com


如果现在男人在Dors或者Dors中悼念我,我会在世界上给世界带来一个世界上最奇妙的奇迹之一

- - 主蒂莫西·德克斯特,_知识的泡菜_


Hi,

What is the difference between accessing environment
variables through

int main(int argc,char *argv[],char *envp[])

and

extern char **environ;
Is there any advantage in any of the above two?

解决方案

"learner" <v_********@yahoo.com> wrote:

What is the difference between accessing environment
variables through

int main(int argc,char *argv[],char *envp[])

and

extern char **environ;
Is there any advantage in any of the above two?



No. Both are obsolete, non-ISO ways of trying to read environment
variables, and you may run into portability problems using either of
them. The proper way to read an environment variable in portable,
ISO-Standard C is to use the getenv() functions.

Richard


"learner" <v_********@yahoo.com> wrote:

Hi,

What is the difference between accessing environment
variables through

int main(int argc,char *argv[],char *envp[])

and

extern char **environ;
No difference, they are both non-standard and unportable. In fact, environ
is not a reserved identifier so any attempt by the implementation to supply
a definition of it, let alone any value other than the usual null-pointer
initialisation, would be improper.

Cygwin''s GCC 3.3.1 appears to have a bug! If I try to define my own extern
variable, I get this error:
/usr/lib/gcc-lib/i686-pc-cygwin/3.3.1/../../../libcygwin.a(_cygwin_crt0_comm
on.o)(.bss+0x0): multiple definition of `_environ''
/cygdrive/c/DOCUME~1/SIMONB~1/LOCALS~1/Temp/ccMMv8Qk.o(.data+0x0):environ2.c
: first defined here

I should be allowed to create my own ''extern'' variable -- the name is not
reserved.

MS''s CL 13.10.3052 does provide a definition of environ, but its linker also
allows me to create my own, which overrides the internal one. This is
probably standard-conforming under the ''as-if'' rule.

Borland BCC32 5.5.1 correctly gives an error if environ is used but not
defined:
Error: Unresolved external ''_environ'' referenced from
C:\DOCUMENT\PROG\C\ENVIRON.OBJ

LCC-Win32 correctly behaves like bcc32, in that I get a linker error that
environ is not defined:
environ.obj .text: undefined reference to ''_environ''
linker returned 2
but for some weird reason it goes on to create an executable anyway! The
executable crashes, for what it''s worth.
Is there any advantage in any of the above two?



No. I suggest you use the standard C solution, which is a library function:
char *getenv(const char *name);

It will search the list for an environment variable of the name given, and
return a pointer to a string containing the value of that variable. If the
name is not found, a null pointer will be returned.

--
Simon.



In article <41****************@news.individual.net>, rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:

"learner" <v_********@yahoo.com> wrote:

What is the difference between accessing environment
variables through

int main(int argc,char *argv[],char *envp[])

and

extern char **environ;
Is there any advantage in any of the above two?
No. Both are obsolete, non-ISO ways of trying to read environment
variables,



While environ is not part of ISO 9899, the C standard, it is included
in ISO 9945 (POSIX), and hence "non-ISO" could be misleading. While
comp.lang.c deals specifically with ISO 9899, useful programs generally
have to operate in part outside the scope of that standard, so it is
often useful to point out when such operation is covered under another
standard, which may apply to the user''s implementation.
and you may run into portability problems using either of
them. The proper way to read an environment variable in portable,
ISO-Standard C is to use the getenv() functions.



I believe it''s worth noting that in fact there is an advantage to the
POSIX environ mechanism over the C getenv one, and so environ is by
no means "obsolete". environ permits iterating over all the values in
the environment list; getenv does not. (environ thus presupposes
that the environment list supports iteration in some meaningful way,
which C does not require, of course.)

Iterating over the environment list is an important feature for some
programs in some environments. (In some of these it is useful for
security purposes, for example.)

--
Michael Wojcik mi************@microfocus.com

I will shoue the world one of the grate Wonders of the world in 15
months if Now man mourders me in Dors or out Dors
-- "Lord" Timothy Dexter, _A Pickle for the Knowing Ones_


这篇关于环境变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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