Okay, Where's the source for those programs?

One of these days I will comment this stuff. Thanks to my friend Michael Kane for the Perl script that does the animations.

The Animation script (in Perl)

#!/usr/bin/perl
# perl script to animate some gifs. Michael Kane 6/3/95.

o
pen (ListFile, $ARGV[0]);
@List = ();
chop @List;
close ListFile;


print "Content-Type: multipart/x-mixed-replace;boundary=BoundsString\n";
print "\n--BoundsString\n";

for (0 .. $#List) { 
    print "Content-Type: image/gif";
    print "\n\n";
    open (ImageFile, $List[$_]);
    print ();
    close(ImageFile);
    print "\n--BoundsString\n";
}

The Email Voting Booth Form Source (voter.c)

#include <stdio.h>
#include <stdlib.h&g
t
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>

#define	LF		10

void getword(char *word, char *line, char stop);
char x2c(char *what);
void unescape_url(char *url);
void plustospace(char *str);

main(int argc, char *argv[]) {
  register int x, m=0;
  int i=0;
  char *cl;
  char w[256];
  char name[20];
  char vote[10];
  char vfname[256];
  char command[256];
  FILE *vfp;

  printf("Content-type: text/html%c%c",LF,LF);

  cl=getenv("QUERY_STRING");

  vote[0]='\0'; name[0]='\0';


  for (x=0; cl[0] != '\0'; x++) {
    m=x;
    getword(w, cl, '=');
    plustospace(w);
    unescape_url(w);

    if (!strcmp(w, "vote")) {
      getword(w,cl,'&');
      plustospace(w);
      unescape_url(w);
      strcpy(vote,w);
    }
  }

  sprintf(command,"mailx -s \"%s\" rbarrett < /dev/null",vote);
  system(command);

  printf("<TITLE>Thanks!</TITLE>%c",LF);
  printf("<BODY>%c",LF);
  printf("<META HTTP-EQUIV=REFRESH CONTENT=\"2;URL=/~rbarrett/webaholics/ver2/\">%c",LF);
  printf("&
ltCENTER>%c",LF);
  printf("<H1>Thanks for your vote!</H1>%c",LF);
  printf("</CENTER>%c",LF);
  printf("<HR>%c",LF);
  printf("</BODY>%c",LF);
}

The Guestbook source

If for some reason you can't use the source code provided here, (it is almost identical to what I use for the support group pages), here is a link to a public guestbook that you can use if you want one for your pages.

I am avail able for help on your own personal guestbook ventures, and I would suggest giving it a shot. You can base the hard stuff on the source code given below, the rest is just a little file manipulation and string fun. Give it a try and if you need help debugging it I can try to help you out, just send me email.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#define	LF		10
#define END_OF_FILE	"</UL>\n"
#define MAX_ENTRIES	20

typed
ef struct {
  char *name;
  char *val;
} entry;

char *getenv();
char *makeword(char *line, char stop);
char *fmakeword(FILE *f, char stop, int *len);
char x2c(char *what);
void unescape_url(char *url);
void plustospace(char *str);

char *p_time() {
  struct timeval t;
  struct timezone tz;

  gettimeofday(&t, &tz);
  return(ctime(&t.tv_sec));
}

main(int argc, char *argv[]) {
  entry entries[MAX_ENTRIES];
  register int x, m=0;
  int cl;

  char name[30] = "\0";
  char email[80] = "\0";
  char url[100] = "
\0";
  char urlname[100] = "\0";
  char comment[2048] = "\0";
  char *gbfname = "yourfile.html";
  FILE *gbf;

  printf("Content-type: text/html%c%c",LF,LF);

  if(strcmp(getenv("REQUEST_METHOD"),"POST")) {
    exit(1);
  }
  if(strcmp(getenv("CONTENT_TYPE"),"application/x-www-form-urlencoded")) {
    exit(1);
  }
  cl = atoi(getenv("CONTENT_LENGTH"));

  for(x=0;cl && (!feof(stdin));x++) {
    m=x;
    entries[x].val = fmakeword(stdin,'&',&cl);
    plustospace(entries[x].val);
    unescape_url(entries[x].v
al);
    entries[x].name = makeword(entries[x].val,'=');
  }

  if (!strcmp(entries[0].name,"name")) { strcpy(name,entries[0].val); }
  if (!strcmp(entries[1].name,"email")) { strcpy(email,entries[1].val); }
  if (!strcmp(entries[2].name,"url")) { strcpy(url,entries[2].val); }
  if (!strcmp(entries[3].name,"urlname")) { strcpy(urlname,entries[3].val); }
  if (!strcmp(entries[4].name,"comment")) { strcpy(comment,entries[4].val); }

  if (!(gbf=fopen(gbfname,"r+"))) {
    printf("<TITLE>Server Error</TI
TLE>%c",LF);
    printf("<BODY>%c",LF);
    printf("<H1>Server Error</H1>%c",LF);
    printf("Server is unable to open the support group entry file, Please try again later.<P>%c",LF);
    exit(1);
  }

  fseek(gbf,-strlen(END_OF_FILE),SEEK_END);
  if (name[0]) { fprintf(gbf,"<LI><B>%s</B> signed in on %s",name,p_time()); }
  if (email[0]) { fprintf(gbf,"with an email address of: <I><A HREF=\"mailto:%s\">%s</A></I>\n",email,email); }
  if (url[0]) { 
    if (urln
ame[0]) {
      fprintf(gbf,"has a URL: <A HREF=\"%s\">%s</A>.  \n",url,urlname);
    }
    else 
      fprintf(gbf,"has a URL: <A HREF=\"%s\">%s's homepage</A>.  \n",url,name);
  }
  if (comment[0]) { fprintf(gbf,"%s says: %s\n",name,comment); }
  fprintf(gbf,END_OF_FILE);
  fclose(gbf);

  printf("<TITLE>Thanks!</TITLE>%c",LF);
  printf("<META HTTP-EQUIV=REFRESH CONTENT=\"2;URL=/~rbarrett/webaholics/guestbook/entries.html\">%c",LF);
  printf("<BODY>%c",LF);
  printf("<CEN
TER>%c",LF);
  printf("<H1>Thanks for your entry in the Webaholics Support Group page!</H1>%c",LF);
  printf("<B>Please remember you must RELOAD the support group page for you entry to show up, don't resubmit it multiple times!</B>%c",LF);
  printf("<HR>%c",LF);
  printf("Back to the <A HREF=\"/~rbarrett/webaholics/ver2/\">Webaholics</A> page.<BR>%c",LF);
  printf("Or you can read the entries of other <A HREF=\"/~rbarrett/webaholics/guestbook/entries.html\">Webaholics&l
t/A>.%c",LF);
  printf("</CENTER>%c",LF);
  printf("<HR>%c",LF);
}

util.c A useful set of utilities

Note: This file is distributed with the NCSA source code and it is necessary to compile and run the email program above.
#include <stdio.h>

#define LF 10
#define CR 13

void getword(char *word, char *line, char stop) {
    int x = 0,y;

    for(x=0;((line[x]) && (line[x] != stop));x++)
        word[x] = line[x];

    word[x] = '\0';
    
if(line[x]) ++x;
    y=0;

    while(line[y++] = line[x++]);
}

char *makeword(char *line, char stop) {
    int x = 0,y;
    char *word = (char *) malloc(sizeof(char) * (strlen(line) + 1));

    for(x=0;((line[x]) && (line[x] != stop));x++)
        word[x] = line[x];

    word[x] = '\0';
    if(line[x]) ++x;
    y=0;

    while(line[y++] = line[x++]);
    return word;
}

char *fmakeword(FILE *f, char stop, int *cl) {
    int wsize;
    char *word;
    int ll;

    wsize = 102400;
    ll=0;
    word = (char 
*) malloc(sizeof(char) * (wsize + 1));

    while(1) {
        word[ll] = (char)fgetc(f);
        if(ll==wsize) {
            word[ll+1] = '\0';
            wsize+=102400;
            word = (char *)realloc(word,sizeof(char)*(wsize+1));
        }
        --(*cl);
        if((word[ll] == stop) || (feof(f)) || (!(*cl))) {
            if(word[ll] != stop) ll++;
            word[ll] = '\0';
            return word;
        }
        ++ll;
    }
}

char x2c(char *what) {
    register char digit;

    digit = (wh
at[0] >= 'A' ? ((what[0] & 0xdf) - 'A')+10 : (what[0] - '0'));
    digit *= 16;
    digit += (what[1] >= 'A' ? ((what[1] & 0xdf) - 'A')+10 : (what[1] - '0'));
    return(digit);
}

void unescape_url(char *url) {
    register int x,y;

    for(x=0,y=0;url[y];++x,++y) {
        if((url[x] = url[y]) == '%') {
            url[x] = x2c(&url[y+1]);
            y+=2;
        }
    }
    url[x] = '\0';
}

void plustospace(char *str) {
    register int x;

    for(x=0;str[x];x++) if(str[x] == '+') str[x] = ' ';
}

i
nt rind(char *s, char c) {
    register int x;
    for(x=strlen(s) - 1;x != -1; x--)
        if(s[x] == c) return x;
    return -1;
}

int getline(char *s, int n, FILE *f) {
    register int i=0;

    while(1) {
        s[i] = (char)fgetc(f);

        if(s[i] == CR)
            s[i] = fgetc(f);

        if((s[i] == 0x4) || (s[i] == LF) || (i == (n-1))) {
            s[i] = '\0';
            return (feof(f) ? 1 : 0);
        }
        ++i;
    }
}

void send_fd(FILE *f, FILE *fd)
{
    int num_chars=0;
    c
har c;

    while (1) {
        c = fgetc(f);
        if(feof(f))
            return;
        fputc(c,fd);
    }
}

int ind(char *s, char c) {
    register int x;

    for(x=0;s[x];x++)
        if(s[x] == c) return x;

    return -1;
}

void escape_shell_cmd(char *cmd) {
    register int x,y,l;

    l=strlen(cmd);
    for(x=0;cmd[x];x++) {
        if(ind("&;`'\"|*?~<>^()[]{}$\\",cmd[x]) != -1){
            for(y=l+1;y>x;y--)
                cmd[y] = cmd[y-1];
            l++; /* length has been increased */

            cmd[x] = '\\';
            x++; /* skip the character */
        }
    }
}

They are in C and Perl!

It is way, way, WAY beyond the scope of this class for me to teach C. In class I go over the source code for the email program line by line, so hopefully that will help out somewhat. However, if you really want to write some great CGI programs it is time to learn a programming or scripting language. There are tons of great books available and I w ould recommend picking up something like "learning C in 21 days". You really only need the fundamentals to do some neat stuff, and I think that just about anyone with a little perserverence can do it.

Good Luck!

[ Back | Topics ]

© 1995 Rich Barrette