
/* randurl.c */
/* Andrew Davison, July 1996 (ad@cs.mu.oz.au) */

/* Usage:
     randurl

   Randomly choose a name from the RANDS file, and return
   it as the new location.

   If randurl is invoked from a Web page, the randomly
   chosen name URL will be loaded in its place.

   Compiled code placed in:
	/var/web/cgi-bin/students
*/

#include <stdio.h>
#include <stdlib.h>   /* for rand() */
#include <time.h>     /* for time() */

#define ADDR   "fivedots.coe.psu.ac.th"
#define RANDS  "/export/home/ad/public_html/code/rand/names.txt"
#define HOME   "http://fivedots.coe.psu.ac.th/~ad/"
#define MAXLEN 300   /* max length of a string */

int main()
{
  FILE *fp;
  int rnum, posn;
  char name[MAXLEN], url[MAXLEN];

  if ((fp = fopen(RANDS, "r")) == NULL)
    printf("Location: %s\n\n", HOME);
  else {
    fscanf(fp, "%d", &rnum);
    srand(time(NULL));
    posn = rand() % rnum;
    while (posn >= 0) {
      fscanf(fp, "%s", name);
      posn--;
    }
    fclose(fp);
    sprintf(url, "http://%s/~%s/", ADDR, name);   
    printf("Location: %s\n\n", url);
  }
  return 0;
}

