2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

getenv

Last updated at Posted at 2015-02-03

特定の環境変数


# defing _GNU_SOURCE
# include <stdio.h>
# include <stdlib.h>

# if _GNU_SOURCE 1  /* 特権ないと実行できない */
  const* char path = secure_getenv("PATH");
  puts(path);
# else             /* 特権なくてもおk */
  const* char path = getenv("PATH");
  puts(path);
# endif

  #define OVER_WRITE 0
  #define YES 0
  #define NO 1

  const char* name = "hoge";
  const char* value = "1"

  /* 同名の環境変数は上書き */
  /* name=value 形式      */
  int ret = setenv(name, value, OVER_WRITE+YES); 

  /* 上書きしない */
  setenv(name, value, OVER_WRITE+NO); 

  switch (ret) {
     case -1:
       perror("setenv");
       break;
     case 0:
       puts("Success");
       break;
  }

環境変数すべて

  
 {  
    extern char** environ;
    int i;
    for(i=0; environ[i] != NULL; ++i) {
        puts(environ[i]);
    }
 }

2
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?