freebsd-nq/contrib/opie/libopie/newseed.c

93 lines
1.9 KiB
C
Raw Normal View History

/* newseed.c: The opienewseed() library function.
1997-09-29 08:53:38 +00:00
%%% copyright-cmetz-96
2002-03-21 23:42:52 +00:00
This software is Copyright 1996-2001 by Craig Metz, All Rights Reserved.
The Inner Net License Version 3 applies to this software.
You should have received a copy of the license with this software. If
you didn't get a copy, you may request one from <license@inner.net>.
History:
2002-03-21 23:42:52 +00:00
Modified by cmetz for OPIE 2.4. Greatly simplified increment. Now does
not add digits. Reformatted the code.
2000-04-10 11:18:54 +00:00
Modified by cmetz for OPIE 2.32. Added syslog.h if DEBUG.
1997-09-29 08:53:38 +00:00
Modified by cmetz for OPIE 2.31. Added time.h.
Created by cmetz for OPIE 2.22.
2000-04-10 11:18:54 +00:00
$FreeBSD$
*/
#include "opie_cfg.h"
1997-09-29 08:53:38 +00:00
#if HAVE_TIME_H
#include <time.h>
#endif /* HAVE_TIME_H */
#if HAVE_STRING_H
#include <string.h>
#endif /* HAVE_STRING_H */
#include <ctype.h>
#if HAVE_UNISTD_H
#include <unistd.h>
#endif /* HAVE_UNISTD_H */
#if HAVE_SYS_UTSNAME_H
#include <sys/utsname.h>
#endif /* HAVE_SYS_UTSNAME_H */
#include <errno.h>
2000-04-10 11:18:54 +00:00
#if DEBUG
#include <syslog.h>
#endif /* DEBUG */
#include "opie.h"
int opienewseed FUNCTION((seed), char *seed)
{
2002-03-21 23:42:52 +00:00
if (!seed)
return -1;
if (seed[0]) {
char *c, *end;
unsigned int i, max;
if ((i = strlen(seed)) > OPIE_SEED_MAX)
i = OPIE_SEED_MAX;
for (c = end = seed + i - 1, max = 1;
(c > seed) && isdigit(*c); c--)
max *= 10;
if ((i = strtoul(++c, (char **)0, 10)) < max) {
if (++i >= max)
i = 1;
snprintf(c, end - c, "%d", i);
seed[OPIE_SEED_MAX] = 0;
return 0;
}
}
2002-03-21 23:42:52 +00:00
{
time_t now;
2002-03-21 23:42:52 +00:00
time(&now);
srand(now);
}
2002-03-21 23:42:52 +00:00
{
struct utsname utsname;
2002-03-21 23:42:52 +00:00
if (uname(&utsname) < 0) {
2000-04-10 11:18:54 +00:00
#if DEBUG
2002-03-21 23:42:52 +00:00
syslog(LOG_DEBUG, "uname: %s(%d)", strerror(errno),
errno);
2000-04-10 11:18:54 +00:00
#endif /* DEBUG */
2002-03-21 23:42:52 +00:00
utsname.nodename[0] = 'k';
utsname.nodename[1] = 'e';
}
utsname.nodename[2] = 0;
if (snprintf(seed, OPIE_SEED_MAX+1, "%s%04d", utsname.nodename,
(rand() % 9999) + 1) >= OPIE_SEED_MAX+1)
return -1;
return 0;
}
}