Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / contrib / opie / libopie / newseed.c
CommitLineData
984263bc
MD
1/* newseed.c: The opienewseed() library function.
2
3%%% copyright-cmetz-96
4This software is Copyright 1996-2001 by Craig Metz, All Rights Reserved.
5The Inner Net License Version 3 applies to this software.
6You should have received a copy of the license with this software. If
7you didn't get a copy, you may request one from <license@inner.net>.
8
9 History:
10
11 Modified by cmetz for OPIE 2.4. Greatly simplified increment. Now does
12 not add digits. Reformatted the code.
13 Modified by cmetz for OPIE 2.32. Added syslog.h if DEBUG.
14 Modified by cmetz for OPIE 2.31. Added time.h.
15 Created by cmetz for OPIE 2.22.
16
17$FreeBSD: src/contrib/opie/libopie/newseed.c,v 1.2.6.2 2002/07/15 14:48:47 des Exp $
1de703da 18$DragonFly: src/contrib/opie/libopie/newseed.c,v 1.2 2003/06/17 04:24:05 dillon Exp $
984263bc
MD
19*/
20
21#include "opie_cfg.h"
22#if HAVE_TIME_H
23#include <time.h>
24#endif /* HAVE_TIME_H */
25#if HAVE_STRING_H
26#include <string.h>
27#endif /* HAVE_STRING_H */
28#include <ctype.h>
29#if HAVE_UNISTD_H
30#include <unistd.h>
31#endif /* HAVE_UNISTD_H */
32#if HAVE_SYS_UTSNAME_H
33#include <sys/utsname.h>
34#endif /* HAVE_SYS_UTSNAME_H */
35#include <errno.h>
36#if DEBUG
37#include <syslog.h>
38#endif /* DEBUG */
39#include "opie.h"
40
41int opienewseed FUNCTION((seed), char *seed)
42{
43 if (!seed)
44 return -1;
45
46 if (seed[0]) {
47 char *c, *end;
48 unsigned int i, max;
49
50 if ((i = strlen(seed)) > OPIE_SEED_MAX)
51 i = OPIE_SEED_MAX;
52
53 for (c = end = seed + i - 1, max = 1;
54 (c > seed) && isdigit(*c); c--)
55 max *= 10;
56
57 if ((i = strtoul(++c, (char **)0, 10)) < max) {
58 if (++i >= max)
59 i = 1;
60
61 snprintf(c, end - c, "%d", i);
62 seed[OPIE_SEED_MAX] = 0;
63 return 0;
64 }
65 }
66
67 {
68 time_t now;
69
70 time(&now);
71 srand(now);
72 }
73
74 {
75 struct utsname utsname;
76
77 if (uname(&utsname) < 0) {
78#if DEBUG
79 syslog(LOG_DEBUG, "uname: %s(%d)", strerror(errno),
80 errno);
81#endif /* DEBUG */
82 utsname.nodename[0] = 'k';
83 utsname.nodename[1] = 'e';
84 }
85 utsname.nodename[2] = 0;
86
87 if (snprintf(seed, OPIE_SEED_MAX+1, "%s%04d", utsname.nodename,
88 (rand() % 9999) + 1) >= OPIE_SEED_MAX+1)
89 return -1;
90 return 0;
91 }
92}
93