自制c语言编制cgi实现搜索功能

王朝c/c++·作者佚名  2008-05-18
窄屏简体版  字體: |||超大  

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的源码

<html>

<head>

<title>Untitled Document</title>

<meta http-equiv="Content-Type" content="text/html; charset=gb2312">

</head>

<body>

<p>请输入你的查询字符串</p>

<form name="form1" action="http://129.158.217.223/cgi-bin/a.out">

<p></p>

<p>

<input name="keyname" value="" type=text>

</p>

<p>

<input type="submit" value="查询">

</p>

</form>

<p> </p>

</body>

</html>

2. search.c的源码

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <sys/types.h>

#include <fcntl.h>

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&", keyword);

p += sprintf(p, "locate '%s' | sed -e 's#^/usr/local/apache/htdocs#http://129.158.217.223#' | sed -e 's#^.*$#<a href=&>&</a>#' > ", 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("<meta http-equiv=refresh content=0;url="http://129.158.217.223/temp/%s">

", cTempName);

printf("Waiting.......................

");

printf("%s", keyword);

return 0;

}

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
 
 
© 2005- 王朝網路 版權所有 導航