home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
ftp.ee.pdx.edu
/
2014.02.ftp.ee.pdx.edu.tar
/
ftp.ee.pdx.edu
/
pub
/
frp
/
util
/
name3
/
names.c
< prev
next >
Wrap
C/C++ Source or Header
|
1991-03-09
|
4KB
|
145 lines
#include <stdio.h>
#define MAXSYLLNUM 1024
#define GETSYLL() fscanf (dfile, "%s", linebuf);
#define USAGE "Usage: names [-f file] [-n number-of-names] [-i]\n"
char *progname;
#ifndef NAMEFILE
# define NAMEFILE "default"
/* "/userpk/etc/req/lib/names/default"
/* default file for names */
#endif
main(argc, argv)
int argc;
char **argv;
{
extern char *optarg;
extern int optind, getopt();
int how_many, /* number of names to generate */
num_mids, /* " " middle syllables (0-2) */
seed = getpid (); /* for random number generator */
int max_start = 0, /* maximum numbers of start, middle and */
max_mid = 0, /* end syllables read (subject to */
max_end = 0; /* ceiling of MAXSYLLNUM) */
char *name_start[MAXSYLLNUM];
char *name_middle[MAXSYLLNUM];
char *name_end[MAXSYLLNUM];
char linebuf[BUFSIZ];
char *name_file = NAMEFILE;
FILE *dfile = NULL; /* file pointer for data file */
int infinity = 0; /* give an infinite number of names if set */
int option;
progname = argv[0];
/* parse args */
while ( (option = getopt(argc, argv, "f:in:") ) != EOF) {
switch(option) {
case '?' : /* no-such-option, getopt has printed a message */
fprintf(stderr, "%s", USAGE);
exit(1);
case 'f': { /* -f file-of-syllables */
name_file = optarg;
break;
}
case 'n': { /* how many names to do */
if ( (how_many = atoi(optarg)) < 0 ) {
err("number of names invalid (-n %d)\n", optarg);
exit(2);
}
break;
}
case 'i': { /* infinite number of names */
infinity = 1;
break;
}
default: {
fprintf(stderr, USAGE);
exit(2);
}
}
}
if (optind != argc) {
fprintf(stderr, USAGE);
exit(2);
}
if ((dfile = fopen (name_file, "r")) == NULL) {
err("cannot read database %s\n", name_file);
exit (2);
}
/* read the data file */
GETSYLL();
while (strcmp(linebuf, "%") != NULL && *linebuf) {
name_start[max_start] = (char *) malloc (strlen (linebuf) + 1);
strcpy (name_start[max_start++], linebuf);
if (max_start == MAXSYLLNUM) {
err("too many initial syllables - but continuing\n");
while (strcmp (linebuf, "%")) GETSYLL();
break;
} else GETSYLL();
}
max_mid = 0; /* initialise medial syllables */
GETSYLL();
while (strcmp (linebuf, "%") != NULL) {
name_middle[max_mid] = (char *) malloc (strlen (linebuf) + 1);
strcpy (name_middle[max_mid++], linebuf);
if (max_mid == MAXSYLLNUM) {
err("too many medial syllables - but continuing\n");
while (strcmp (linebuf, "%"))
GETSYLL();
break;
} else GETSYLL();
}
max_end = 0;
GETSYLL();
while (strcmp (linebuf, "%") != NULL) {
name_end[max_end] = (char *) malloc (strlen (linebuf) + 1);
strcpy (name_end[max_end++], linebuf);
if (max_end == MAXSYLLNUM) {
err("too many final syllables - but continuing\n");
while (strcmp (linebuf, "%"))
GETSYLL();
break;
} else GETSYLL();
}
/* now print the names out */
if (!(max_start && max_mid && max_end)) {
err("zero-length section in datafile\n");
exit (3);
}
srand (seed);
while (infinity || how_many-- > 0) {
printf ("%s", name_start[rand_int () % max_start]);
for (num_mids = (rand_int () % 3) / 2; num_mids > 0; num_mids--)
printf ("%s", name_middle[rand_int () % max_mid]);
printf ("%s\n", name_end[rand_int () % max_end]);
}
}
rand_int() {
return ((int) (rand() / 37));
}
err(s, a1, a2, a3)
char *s;
{
fputs(progname, stderr);
fputs(": ", stderr);
fprintf (stderr, s, a1, a2, a3);
}