]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/opie/libopie/newseed.c
nfs_clvnops.c: Fix access to v_mount when vnode unlocked
[FreeBSD/FreeBSD.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 #ifndef HAVE_TIME_H
22 #define HAVE_TIME_H 1
23 #endif
24 #if HAVE_TIME_H
25 #include <time.h>
26 #endif /* HAVE_TIME_H */
27 #if HAVE_STRING_H
28 #include <string.h>
29 #endif /* HAVE_STRING_H */
30 #include <ctype.h>
31 #if HAVE_UNISTD_H
32 #include <unistd.h>
33 #endif /* HAVE_UNISTD_H */
34 #if HAVE_SYS_UTSNAME_H
35 #include <sys/utsname.h>
36 #endif /* HAVE_SYS_UTSNAME_H */
37 #include <errno.h>
38 #if DEBUG
39 #include <syslog.h>
40 #endif /* DEBUG */
41 #include <stdio.h>
42 #include <stdlib.h>
43 #include "opie.h"
44
45 int opienewseed FUNCTION((seed), char *seed)
46 {
47         if (!seed)
48                 return -1;
49
50         if (seed[0]) {
51                 char *c;
52                 unsigned int i, max;
53
54                 if ((i = strlen(seed)) > OPIE_SEED_MAX)
55                         i = OPIE_SEED_MAX;
56
57                 for (c = seed + i - 1, max = 1;
58                                 (c >= seed) && isdigit(*c); c--)
59                         max *= 10;
60
61                 if ((i = strtoul(++c, (char **)0, 10)) < max) {
62                         if (++i >= max)
63                                 i = 1;
64
65                         sprintf(c, "%d", i);
66                         return 0;
67                 }
68         }
69
70         {
71                 time_t now;
72
73                 time(&now);
74                 srand(now);
75         }
76
77         {
78                 struct utsname utsname;
79
80                 if (uname(&utsname) < 0) {
81 #if DEBUG
82                         syslog(LOG_DEBUG, "uname: %s(%d)", strerror(errno),
83                                 errno);
84 #endif /* DEBUG */
85                         utsname.nodename[0] = 'k';
86                         utsname.nodename[1] = 'e';
87                 }
88                 utsname.nodename[2] = 0;
89
90                 if (snprintf(seed, OPIE_SEED_MAX+1, "%s%04d", utsname.nodename,
91                                 (rand() % 9999) + 1) >= OPIE_SEED_MAX+1)
92                         return -1;
93                 return 0;
94         }
95 }
96