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

Unix/Linux
Linux教程:locate用法
wordpress 安装心得
gentoo的一些杂记
用VIM编写C/C++程序
Apache API notes
Apache API note(2)
FBI 供应商开发安全版 Linux 成本优势获青睐
来自HP的真实Linux体验的成功故事
2004全球20大安全隐患排行出炉 专家建议停用IE
启动“gnome-run”对话框
最近郁闷啊
有矢而发,触类旁通[内核学习的方法论]-- 转载自黄嘴企鹅Linux内核研究小组
linux内核阅读计划 -- based on kernel 2.6.8
IBM:大型机使用 Linux 的黄金时代即将到来
windows 环境下配置apache+mysql+php
利用crontab,系统每天定时备份mysql数据库
Linux 操作系统跑Linux慢的分析
php_mysql.dll不能注册的解决方法
距离rhce考试还有17天
决定你一生成就的二十一个信念

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


出处:互联网   整理: 软晨网(RuanChen.com)   发布: 2009-11-01   浏览: 78 ::
收藏到网摘: 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;
  }