]> CyberLeo.Net >> Repos - FreeBSD/releng/10.0.git/blob - contrib/opie/libopie/newseed.c
- Copy stable/10 (r259064) to releng/10.0 as part of the
[FreeBSD/releng/10.0.git] / contrib / opie / libopie / newseed.c
1 /* newseed.c: The opienewseed() library function.
2
3 %%% copyright-cmetz-96
4 This software is Copyright 1996-2001 by Craig Metz, All Rights Reserved.
5 The Inner Net License Version 3 applies to this software.
6 You should have received a copy of the license with this software. If
7 you 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$
18 */
19
20 #include "opie_cfg.h"
21 #if HAVE_TIME_H
22 #include <time.h>
23 #endif /* HAVE_TIME_H */
24 #if HAVE_STRING_H
25 #include <string.h>
26 #endif /* HAVE_STRING_H */
27 #include <ctype.h>
28 #if HAVE_UNISTD_H
29 #include <unistd.h>
30 #endif /* HAVE_UNISTD_H */
31 #if HAVE_SYS_UTSNAME_H
32 #include <sys/utsname.h>
33 #endif /* HAVE_SYS_UTSNAME_H */
34 #include <errno.h>
35 #if DEBUG
36 #include <syslog.h>
37 #endif /* DEBUG */
38 #include "opie.h"
39
40 int opienewseed FUNCTION((seed), char *seed)
41 {
42         if (!seed)
43                 return -1;
44
45         if (seed[0]) {
46                 char *c;
47                 unsigned int i, max;
48
49                 if ((i = strlen(seed)) > OPIE_SEED_MAX)
50                         i = OPIE_SEED_MAX;
51
52                 for (c = seed + i - 1, max = 1;
53                                 (c >= seed) && isdigit(*c); c--)
54                         max *= 10;
55
56                 if ((i = strtoul(++c, (char **)0, 10)) < max) {
57                         if (++i >= max)
58                                 i = 1;
59
60                         sprintf(c, "%d", i);
61                         return 0;
62                 }
63         }
64
65         {
66                 time_t now;
67
68                 time(&now);
69                 srand(now);
70         }
71
72         {
73                 struct utsname utsname;
74
75                 if (uname(&utsname) < 0) {
76 #if DEBUG
77                         syslog(LOG_DEBUG, "uname: %s(%d)", strerror(errno),
78                                 errno);
79 #endif /* DEBUG */
80                         utsname.nodename[0] = 'k';
81                         utsname.nodename[1] = 'e';
82                 }
83                 utsname.nodename[2] = 0;
84
85                 if (snprintf(seed, OPIE_SEED_MAX+1, "%s%04d", utsname.nodename,
86                                 (rand() % 9999) + 1) >= OPIE_SEED_MAX+1)
87                         return -1;
88                 return 0;
89         }
90 }
91