More Explanation of the Web Database


Have a look at db.c. It is quite long, but over half of the code is the form manipulation functions that were introduced in echoer.c.

An overview of its execution is shown in the diagram:

db action [GIF]


Coding

A key piece of code in db.c is in main():

  patno = get_pats(patterns, entries, etnum);
  if (patno > 0) {
    print_pats(patterns, patno);
    process_pats(patterns, patno);
  }
  else
    printf("Must specify at least 1 pattern\n");
The non-empty strings (patterns) in the entries[] array are copied into the patterns[] array by get_pats(). If there are some patterns, they are printed out by print_pats() and processed by process_pats().

Process_pats() uses the UNIX fgrep command to search the database (which is really just a large text file). For instance, if there is one pattern in pats[0], then the fgrep call is:

    fgrep 'pats[0]' database-file > results-file
The results file will contain the matching lines from the database file.

If there are several patterns then several fgrep calls must be piped together:

  fgrep 'pats[0]' database-file | fgrep 'pats[1]' > results-file

The database file (called dir.alp) is defined in FNM in db.c, and you can view it.

An unusual function in db.c is num_matches() which invokes the UNIX command wc -l by using popen(). The output of popen() is accessed via the file pointer, fp, which allows the number of lines to be read. wc is more efficient than counting the number of lines in the file using C directly.

Another function of note is record_details(), which records details about users of the script in the RFNM file (people.txt). It does this by reading several environment variables (e.g. REMOTE_USER, REMOTE_IDENT). There are many more Web environment variables, and further information on them can be found in the "Interactive Forms in HTML" article.


Back to the Main Web Database Page