|
|
DataMuseum.dkPresents historical artifacts from the history of: DKUUG/EUUG Conference tapes |
This is an automatic "excavation" of a thematic subset of
See our Wiki for more about DKUUG/EUUG Conference tapes Excavated with: AutoArchaeologist - Free & Open Source Software. |
top - metrics - downloadIndex: T m
Length: 3417 (0xd59)
Types: TextFile
Names: »moniker.c«
└─⟦b20c6495f⟧ Bits:30007238 EUUGD18: Wien-båndet, efterår 1987
└─⟦this⟧ »EUUGD18/General/Cubes/moniker.c«
/* vi:set sw=4 ts=4: */
#ifndef lint
static char sccsid[] = "@(#)moniker.c 5.1 (G.M. Paris) 89/01/22";
#endif lint
/*
**
** cubes 5.1 Copyright 1989 Gregory M. Paris
** Permission granted to redistribute on a no charge basis.
** All other rights are reserved.
**
*/
#include <stdio.h>
#include <syslog.h>
#include <strings.h>
#include "cubes.h"
#ifdef lint
#ifndef MONFILE
#define MONFILE "dummy-monfile"
#endif MONFILE
#endif lint
struct mon {
char m_name[NAMELEN];
struct mon *m_next;
};
/*
** seed: names to seed the moniker list
*/
static struct mon seed[] = {
{ "Ronald Reagan", seed+ 1 },
{ "Nancy Reagan", seed+ 2 },
{ "George Bush", seed+ 3 },
{ "Barbara Bush", seed+ 4 },
{ "Mikhail Gorbachev", seed+ 5 },
{ "Raisa Gorbachev", seed+ 6 },
{ "Benny Hill", seed+ 7 },
{ "Margaret Thatcher", seed+ 8 },
{ "Piotr Chekov", seed+ 9 },
{ "Jamie Finney", seed+10 },
{ "Touche Turtle", seed+11 },
{ "Judy Jetson", seed+12 },
{ "Cobb Anderson", seed+13 },
{ "Della Taze", seed+14 },
{ "Mickey Mouse", seed+15 },
{ "Minnie Mouse", seed+16 },
{ "Edison Carter", seed+17 },
{ "Theora Jones", 0 },
};
static int seedcnt = sizeof seed / sizeof seed[0];
static struct mon *monlist = 0;
static int moncnt = 0;
extern char *malloc();
extern char *fgets();
extern computer *compbyname();
/*
** savename: add a name to the moniker list and save the new list
*/
savename(name)
char *name;
{
register struct mon *m;
FILE *fp;
if(monlist == 0)
readmonfile();
if(strlen(name) < 5) /* small names are boring */
return;
if(compbyname(name) != 0) /* don't save computer names */
return;
/*
** Check for duplicate while finding the end of the list.
*/
for(m = monlist;;m = m->m_next) {
if(strncmp(name, m->m_name, NAMELEN) == 0)
return;
if(m->m_next == 0)
break;
}
/*
** Add the new name to the end of the list.
*/
if((m->m_next = (struct mon *)malloc(sizeof *m)) == 0) {
syslog(LOG_WARNING, "savename: out of memory");
return;
}
m = m->m_next;
m->m_next = 0;
strncpy(m->m_name, name, NAMELEN);
m->m_name[NAMELEN-1] = '\0';
++moncnt;
/*
** Append the new name to the monikers file.
*/
(void) umask(022);
if((fp = fopen(MONFILE, "a")) == 0) {
syslog(LOG_WARNING, "savename: fopen(%s,a): %m", MONFILE);
return;
}
fprintf(fp, "%s\n", m->m_name);
(void) fclose(fp);
}
/*
** moniker: randomly select a name from the moniker list
*/
char *
moniker()
{
register struct mon *m;
register int n;
if(monlist == 0)
readmonfile();
n = randint(moncnt) - 1;
for(m = monlist;n > 0 && m->m_next != 0;--n, m = m->m_next)
;
return m->m_name;
}
/*
** readmonfile: seed the monikers list then read in the monikers file
*/
readmonfile()
{
register struct mon *m;
register FILE *fp;
char *s, line[2*NAMELEN];
monlist = &seed[0];
moncnt = seedcnt;
if((fp = fopen(MONFILE, "r")) == 0) {
syslog(LOG_WARNING, "readmonfile: fopen(%s,r): %m", MONFILE);
return;
}
for(m = monlist;m->m_next != 0;++m)
;
while(fgets(line, sizeof line, fp) != 0) {
if(*(s = &line[strlen(line)-1]) == '\n')
*s = '\0';
if((m->m_next = (struct mon *)malloc(sizeof *m)) == 0) {
syslog(LOG_WARNING, "readmonfile: out of memory");
return;
}
m = m->m_next;
m->m_next = 0;
strncpy(m->m_name, line, NAMELEN);
m->m_name[NAMELEN-1] = '\0';
++moncnt;
}
(void) fclose(fp);
}