#include <stdio.h>
extern char **environ;
//int main(int argc, char *argv[], char *envp[])
int main(int argc, char *argv[])
{
int idx = 0;
(void)argc;
(void)argv;
while (environ[idx])
{
printf("environ[%d]: %s\\n", idx, environ[idx]);
idx++;
}
return (0);
}
C에서 환경변수는 크게 2가지 형태로 받아올 수 있다. 위와 같이 extern 변수로 받아오는 방법, 주석과 같이 argc, argv와 함께 main의 인자로 받아오는 방법. gnu.org 레퍼런스에 따르면 3-argument 형태가 posix 표준이 아니라고 되어있다.
POSIX.1 does not allow this three-argument form, so to be portable it is best to write
main
to take two arguments, and use the value ofenviron
.
참고 링크
https://www.gnu.org/software/libc/manual/html_node/Environment-Access.html
https://www.gnu.org/software/libc/manual/html_node/Program-Arguments.html