Reference: http://blog.chinaunix.net/uid-620765-id-2084386.html
Remember the complete declaration of the C language main function?
int main(int argc, char **argv, char **envp);
这个就是c语言中main函数的完整声明, argc表示参数个数,argv表示参数字符String, and this envp represents the environment variable string. There can be more than one parameter and environment variable, so it is represented by a pointer to a string pointer.
比如我们用C语言写了一个程序,运行在命令行下,带有2个参数
C:> cpfile.exe c:\test.txt d:\test. txt
那么这里的
argc=3;
argv[0] = “cpfile.exe”;
argv[1] = “c:\test.txt”;
argv[2] = “d:\test.txt”;
在c语言中的stdlib.h中有一个
extern char **environ; 或者
extern char **_environ ; Statement
The parameter of this envp is actually environ.
好,我们看看在cgi程序(其实就是一个C语言程序)里面怎么枚举这些环境变量
先写一个html页面:(当然也可以不要)
test title> < form id="mainform" method="post" action="/printenv.cgi" enctype="multipart/form-data">
呈现效果如下:
cgi程序源码:
/* env.c */
#include < stdio.h> #include#include int main() {extern char **environ; int nlen = 0; int i; char *psz_content = NULL; char **pp_env; printf( "Content-type: text/html\n\n"); for (pp_env = environ; *pp_env; pp_env++) printf("%s
", *pp_env); if (strcmp("GET", getenv( "REQUEST_METHOD")) == 0 ) {printf("%s
", getenv("QUERY_STRING"));} else {nlen = atoi(getenv("CONTENT_LENGTH")); psz_content = (char * )malloc( nlen + 1 ); memset( psz_content, 0, nlen + 1 ); printf("char value:
"); for (i = 0; i
string value: %s
", psz_content);} return 0;} pre>
If it is fastcgi, you can use the following code to print environment variables:
#include#include #include #include int main() {extern char **environ; int nlen = 0; int i; char *psz_content = NULL; char **pp_env; while(FCGI_Accept() >= 0) {printf("Content-type: text/html\n\n"); for (pp_env = environ; *pp_env; pp_env++) printf("%s
", *pp_env); if (strcmp("GET", getenv("REQUEST_METHOD")) == 0) {printf("%s
", getenv( "QUERY_STRING"));} else {nlen = atoi(getenv("CONTENT_LENGTH")); psz_content = (char * )malloc( nlen + 1 ); memset( psz_content, 0, nlen + 1 ); printf("char value:
"); for (i = 0; i
string value: %s
", psz_content);}} return 0;}Note:
1, here Use the fgetc function to read data from standard input. You cannot use gets instead.
2. In FCGX, the header and environment information are in FCGX_Request->envp, which cannot be obtained with genenv, but FCGX_GetParam is used.
In the address bar http://localhost/testenv.htm
Assuming that the username and password we entered are abcd and efgh, after submitting the form, the results displayed on my machine are as follows: < br>
COMSPEC=C:\WINDOWS\system32\cmd.exe
CONTENT_LENGTH=31
CONTENT_TYPE=application/x-www-form-urlencoded
DOCUMENT_ROOT=d:/chateaurt4/www
HTTP_ACCEPT=text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
HTTP_ACCEPT_CHARSET=gb2312 ,utf-8;q=0.7,*;q=0.7
HTTP_ACCEPT_ENCODING=gzip,deflate
HTTP_ACCEPT_LANGUAGE=zh-cn,zh;q=0.5
HTTP_CONNECTION=keep-alive
HTTP_HOST=localhost
HTTP_KEEP_ALIVE=300
HTTP_REFERER=http://localhost/testenv.htm
HTTP_USER_AGENT=Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.8.1.3) Gecko/20070309 Firefox/2.0.0.3
PATH=C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem;D:\MicrosoftVisual Studio\Common\Tools\WinNT;D:\Microsoft VisualStudio\Common \MSDev98\Bin;D:\Microsoft VisualStudio\Common\Tools;D:\Microsoft VisualStudio\VC98\bin;D:\AppServ\Apache2.2\bin; D:\AppServ\php5;D:\AppServ\MySQL\bin
REMOTE_ADDR=127.0.0.1
REMOTE_PORT=1644
SCRIPT_FILENAME=d:/chateaurt4/www/cgi-bin/env.cgi
SERVER_ADDR=127.0.0.1
SERVER_ADMIN=who@where.net
SERVER_NAME=localhost
SERVER_PORT=80
SERVER_SIGNATURE= Apache/1.3.33 Server at localhost Port 80SERVER_SOFTWARE=Apache/1.3.33 (Win32)
SystemRoot=C:\WINDOWS
WINDIR=C:\WINDOWS
GATEWAY_INTERFACE=CGI/1.1
SERVER_PROTOCOL=HTTP/1.1
REQUEST_METHOD=POST
QUERY_STRING=
REQUEST_URI=/cgi-bin/env.cgi
SCRIPT_NAME=/cgi-bin/env.cgiusr=abcd&pwd=efgh&submit=Submit
Of course, different machines may output some different results. If it is purely to output environment variables, we can do this:
#includeint main (int argc , char **argv, char **envp){ char **var = envp; while (*var) printf ("%s \n",*var++); return 0;} The environment obtained here The variable result is quite different from the result in the CGI program, because one obtains the environment variables related to the web application, and the other obtains the environment variables of the system.