tags:

views:

62

answers:

2

Hi all!

I am having difficulty figuring out how to implement grep into my CGI script. Basically I will receive a value of eg. 1500 from a HTML page. The CGI script then runs and compares 1500 to a text file. When it finds 1500 it prints the entire line and displays it on the webpage. I would like some tips and pointers on how to do this please. I understand that this involves grep but I don't really know how to put it in.

#include <stdio.h>
#include <stdlib.h>
int main(void)
{
char *data;
long m,n;
printf("%s%c%c\n",
"Content-Type:text/html;charset=iso-8859-1",13,10);
printf("<TITLE>Webpage of Results</TITLE>\n");
printf("<H1>Temperatures</H1>\n");
data = getenv("QUERY_STRING");

The HTML passes the variable time=1500. I understand (correct me if I am wrong) that QUERY_STRING will contain 1500?

A: 

For a URL of the form whatever?query-string, the enviroment variable QUERY_STRING contains the full query-string - in you case time=1500 or similar.

You need to remember that there may be more then one field/value getting passed, seperated by &. By using strtok() you can retrieve the parts of the query string one by one, strcmp() lets you compare them to other strings.

If you found the right token, you need to work with a file:

  • fopen() is used for opening a file
  • fgets() is used to retrieve a line
  • use strstr() to check if a line contains a certain substring
  • use fclose() to close the file when you're done
Georg Fritzsche
Hi gf, thanks for the reply, well I know I won't have any other variables (for now) as I exclusively designed the HTML form like that. Need to get one variable working first before adding more heh. From what you're saying, I shouldn't need to use grep at all?
Joey jie
Never trust anything to send you exactly what you want :) With `grep` do you mean invoking an external process? I would recommend doing this yourself in C - invoking grep and retrieving its output isn't really easier.
Georg Fritzsche
Well...I'd like to hope it sends me what I want haha. Well basically I'll explain what I'd like to do:When receive TIME (1500)Open Text File and Find string(1500)Print string to HTML.Really it's that simple but I don't know how to do it >< hence why I'd like a few pointers.
Joey jie
@Joey: Updated with the basic outline, that should get you going (and reading).
Georg Fritzsche
Many thanks gf! Will get reading this!
Joey jie
A: 

some very straightforward analog of the grep in C is strstr() function

oraz
I've had a quick browse on strstr(). From what I understand, it is used in PHP?
Joey jie
@Joey jie: See http://www.cplusplus.com/reference/clibrary/cstring/strstr/
Lucas Jones