利用c語言編製cgi實現搜尋

來源:互聯網
上載者:User

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="">
<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=""> ", cTempName);
printf("Waiting....................... ");
printf("%s", keyword);
return 0;
}

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.