ENGLISH 意见建议 网站地图 网站帮助
广泛智力汇聚   高效成果传播   先进机制培育
联盟首页  |  协同开发  |  开放源码库  |  安全告警  |  开源导航  |  文档中心  |  服务支持  |  共创论坛  |  关于联盟


注册会员 网站帮助
    您的位置 »
    今天是: 2010年11月22日    
项目搜索

完全匹配   
开源软件
软件分类表
新发布软件
其它网站镜像
代码片断
协同开发
文档
论坛
寻求协助
热点项目
站点状态
编译工厂

联系我们
关于联盟

代码片段库:
查看代码片段

浏览 | 提交新的代码片段 | 创建代码包

显示环境变量

类型:
Sample Code (HOWTO)
类别:
Other
许可证:
GNU General Public License
语言:
C
 
描述:
显示环境变量

该代码片段的版本系列:

片段ID 下载版本 提交时间 提交人 删除
70.12001-07-24 10:59bbsadmin

点击"下载版本"来下载该代码片段.


最新版本的代码片段: 0.1


#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

/* -------------------------------------------------------------- 

  environment 
  
  Part of the SourceLib Project at LinuxProgramming.com
  http://www.linuxprogramming.com/SourceLib

  This sample program has been released into the public domain.

  Purpose: Demonstrate the various ways to access a program's
  environment variables.

  Use the command "make" to build this program.

  ---------------------------------------------------------------

  First we use the global variable "environ" (declared in unistd.h)
  to count the number of strings in the environment and list
  them all.

  Then we demonstrate how to query an environment value with 
  getenv() (see the showEnvVar() function), as well as how to set it
  with the putenv() and setenv() functions.

  Note: putenv() is considered more portable because it's part of 
  the POSIX standard, while setenv() isn't.

  --------------------------------------------------------------- */

#define env_var1 "OSTYPE"
#define env_var2 "Fred"
#define env_var2_value "Gravel_pit_worker"
#define env_var_put "Fred=Flintstone"

/* --------------------------------------------------------------
*/

void showEnvVar(char *var)
{
  char *env_variable;

  if((env_variable = getenv(var)) != NULL)
    printf("The environment variable \"%s\" has the value \"%s\".\n\n",
      var,env_variable);
  else
    printf("The environment variable \"%s\" isn't set.\n\n",var);
}

/* --------------------------------------------------------------
*/

int main(int argc, char **argv)
{
  extern char **environ;
  int count, i;

  /* environ is an array of pointers to null-delimited C strings, 
     with the last pointer in the array being NULL.  We can 
     therefore step through them and stop counting at the first
     NULL pointer. */

  count = 0;
  while(environ[count] != NULL)
    count++;

  /* We know how many strings there are--tell the user, then 
     display the strings.  Notice that each is in the format
     VARIABLE_NAME=VALUE. */

  printf("Counted %d environment strings.\n\n",count);

  for(i = 0; i < count; i++)
    printf("env. var. #%3d: \"%s\"\n",i,environ[i]);

  printf("\nHit ENTER to continue... ");
  getchar();
  printf("\n");

  /* Display a commonly set environment variable. */

  printf("Quering an environment variable that should be set:\n");
  showEnvVar(env_var1);  

  /* Now display one that we made up. */

  printf("Quering an environment variable that should not be set:\n");
  showEnvVar(env_var2);  

  /* putenv() takes a single argument of the form VAR_NAME=VALUE,
     the same format as the strings pointed to by the environ array. 

     If the environment variable already exists, its value is replaced 
     with the new one. */ 

  if(putenv(env_var_put) != 0)
  {
    perror("Unable to putenv");
    exit(EXIT_FAILURE);
  }

  printf("We've set the variable, let's see it:\n");
  showEnvVar(env_var2);  

  /* setenv() uses different syntax than putenv().  It takes
     three parameters: the name of the environment variable, the value, 
     and a flag indicating whether it should be overwritten if the 
     variable already exists. 

     This first setenv() call should refuse to overwrite the variable
     because the flag is 0.  setenv() indicates success even when it
     doesn't change the environment variable because it already
     existed. */ 

  if(setenv(env_var2,env_var2_value,0) != 0)
  {
    perror("Unable to setenv");
    exit(EXIT_FAILURE);
  }

  printf("It should be the same since we didn't allow setenv() to overwrite it:\n");
  showEnvVar(env_var2);  

  /* This time setenv() should overwrite the variable because we're passing
     a flag of 1. */

  if(setenv(env_var2,env_var2_value,1) != 0)
  {
    perror("Unable to setenv");
    exit(EXIT_FAILURE);
  }

  printf("This time setenv() should have overwritten it:\n");
  showEnvVar(env_var2);  

  /* Indicate normal termination via the 
     EXIT_SUCCESS constant from stdlib.h */

  return EXIT_SUCCESS;
}

		

提交新版本

如果您修改了一个代码片段并且觉得很应该让别人共享,您可以把这作为这个代码片段的最新版本提交上来.


联盟团体会员
合作伙伴
© 共创软件联盟 版权所有
联盟服务条款 | 联盟隐私权规则 | 联系我们
电话: (8610)68313388-5949 | 传真: (8610)88377936
京ICP备05056057号