Using C language to compile CGI to realize search

Source: Internet
Author: User
Tags printf sprintf

1. Environmental

/usr/local/apache/htdocs/(Files to be retrieved are in this directory)

/usr/local/apache/temp/(as a transit folder)

/usr/local/apache/cgi-bin/

2. Place the a.out produced by GCC search.c in/usr/local/apache/cgi-bin/and the permissions set to nobody can be performed.

3. Chown-r Nobody.nobody/usr/local/apache/temp

4. Add a file deletetemp permission in/etc/cron.daily to 555

Rm-f/usr/local/apache/temp/*

5. Add a file myupdate permission in/etc/cron.hourly to 555

Updatedb-u/usr/local/apache/htdocs

6. Delete a file in the/etc/cron.daily Slocate.cron

Appendix (total 2 article):

1. Index.htm's source code
<title>untitled document</title>
<meta http-equiv= "Content-type" content= "text/html; charset=gb2312 ">

<body>
<p> Please enter your query string </p>
<form name= "Form1" action= "" >
<p> </p>
<p>
<input name= "KeyName" value= "" type=text>
</p>
<p>
<input type= "Submit" value= "Query" >
</p>
</form>
<p> </p>
</body>

2. SEARCH.C's source code
#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;
}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.