当前位置: 首页 > 图文教程 > 操作系统 > Unix/Linux > 小技巧:自制c语言编制cgi实现搜索

Unix/Linux
Creating a OpenBSD 3.5 bootable CD
解决Fatal error: Call to undefined function: preg_match()
[工具 使用 原创] ls可以是彩色的,df也可以是彩色的
ipfilter+squid配置实例
UNIX痛恨者手册
postfix+ldap+sasl2+courier-imap for freebsd完全ports
硬盘、分区引导和操作系统加载程序
Unix/Linux下的“魔法兔仔”, webmin
solaris10 apache2 的问题
解决启动DHCP客户端时,出现unknown 主机名称的方法
A1000的电池用完了会有什么严重后果
Solaris 2.6+Ultra5内置扬声器故障诊断实例
各种shell下文件名补全功能的使用
民工日记--- We sell dream
让你的终端同时支持中文和 PP 的背景(转载收藏)
民工日记 --不变的变化
配置Solaris上网
民工日记--不鼓励也不反对
Solaris Security Toolkit 4.2
民工日记--无奈

Unix/Linux 中的 小技巧:自制c语言编制cgi实现搜索


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-01   浏览: 136 ::
收藏到网摘: n/a


  1. 环境
  /usr/local/apache/htdocs/ (待检索的文件在该目录下)
  /usr/local/apache/temp/ (作为中转的文件夹)
  /usr/local/apache/cgi-bin/
  2. 将gcc search.c产生的a.out放置在/usr/local/apache/cgi-bin/中,权限设置为nobody可
  以执行。
  3. chown -R nobody.nobody /usr/local/apache/temp
  4. 在/etc/cron.daily中增加一个文件deletetemp 权限为555
  rm -f /usr/local/apache/temp/*
  5. 在/etc/cron.hourly中增加一个文件myupdate 权限为555
  updatedb -U /usr/local/apache/htdocs
  6. 删除/etc/cron.daily中的一个文件slocate.cron
  
  附录(共2条):
  1. index.htm的源码
  
  
  Untitled Document
  
  
  
  
  
请输入你的查询字符串
  

  
  
  
  
  
  
  
  


  
 
  
  
  
  2. search.c的源码
  #include
  #include
  #include
  #include
  #include
  
  int main(int argc, char* argv[])
  {
  int fd;
  int status;
  time_t i;
  char cFileName[64];
  char cTempName[64];
  char cBuffer[1024];
  char *p = cBuffer;
  char cContent[10240];
  
  char *data;
  char keyword[1024];
  data = getenv("QUERY_STRING");
  if(data==NULL)
  {
  printf("Content-Type:text/html ");
  printf("not found!");
  exit(1);
  }
  
  sscanf(data, "keyname=%s/DownloadFiles\a\2003-09-01\&", keyword);
  
  p += sprintf(p, "locate '%s' | sed -e 's#^/usr/local/apache/htdocs#http://129.158.217.223#' | sed -e 's#^.*$#/DownloadFiles\a\2003-09-01\&
  #' > ", keyword);
  i = time(NULL);
  
  sprintf(cTempName, "%d.html", i);
  sprintf(cFileName, "/usr/local/apache/temp/%d.html", i);
  strcat(cBuffer, cFileName);
  cBuffer[1024-1]=0;
  
  system(cBuffer);
  fd = open(cFileName, O_RDWR);
  status = read(fd, cContent, sizeof(cContent)-1);
  close(fd);
  
  printf("Content-Type:text/html ");
  printf(" ", cTempName);
  printf("Waiting....................... ");
  printf("%s", keyword);
  return 0;
  }