Chinaunix首页 | 论坛 | 博客
  • 博客访问: 627017
  • 博文数量: 43
  • 博客积分: 1103
  • 博客等级: 少尉
  • 技术积分: 569
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-15 00:04
个人简介

test

文章分类

全部博文(43)

文章存档

2012年(20)

2011年(23)

分类: C/C++

2011-07-04 02:45:02

用C写的,输出CGI的环境变量.
  1. 路径:/var/www/cgi-bin/
  2. 编译:gcc -o cgi_env cgi_env.c -std=c99
  3. 在浏览器中输入http://localhost/cgi-bin/cgi_env 或者 http://ip/cgi-bin/cgi_env
  1. //cgi_env.c
  2. #include <stdlib.h>
  3. #include <stdio.h>

  4. int main ( int argc, char *argv[] )
  5. {
  6.     char *env_var[ 24 ] = {
  7.     "COMSPEC", "DOCUMENT_ROOT", "GATEWAY_INTERFACE",
  8.     "HTTP_ACCEPT", "HTTP_ACCEPT_ENCODING",
  9.     "HTTP_ACCEPT_LANGUAGE", "HTTP_CONNECTION",
  10.     "HTTP_HOST", "HTTP_USER_AGENT", "PATH",
  11.     "QUERY_STRING", "REMOTE_ADDR", "REMOTE_PORT",
  12.     "REQUEST_METHOD", "REQUEST_URI", "SCRIPT_FILENAME",
  13.     "SCRIPT_NAME", "SERVER_ADDR", "SERVER_ADMIN",
  14.     "SERVER_NAME","SERVER_PORT","SERVER_PROTOCOL",
  15.     "SERVER_SIGNATURE","SERVER_SOFTWARE" };

  16.     printf ( "%s\n","Content-Type: text/html\n" );
  17.     printf ( "%s\n","1.0\"?>" );
  18.     printf ( "%s\n","-//W3C//DTD XHTML 1.1//EN\" " );
  19.     printf ( "%s\n","\"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd\">" );
  20.     printf ( "%s\n","http://www.w3.org/1999/xhtml\">" );
  21.     printf ( "%s\n","CGI环境变量" );
  22.     printf ( "%s\n","0\" cellspacing = \"2\">");

  23.     for ( int i=0;i<24;i++ ) {
  24.         printf ( "%s%s%s\n","
  25. ");
  26.     }
  27.     printf ( "%s\n","
  28. ",env_var[i],"" );
  29.         char *value = getenv( env_var[i]);

  30.         if ( value ) {
  31.             printf ( "%s",value );
  32.         }
  33.         else {
  34.             printf ( "%s","CGI环境变量不存在." );
  35.         }
  36.         printf ( "%s\n","
  37. "
    );
  38.     printf ( "%s\n","C语言编写" );
  39.     return EXIT_SUCCESS;
  40. }
阅读(4231) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~