AMD64 - AUDIT RUN - Fix format strings, size_t, and other issues
[dragonfly.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: src/contrib/opie/libopie/newseed.c,v 1.2.6.2 2002/07/15 14:48:47 des Exp $
18 $DragonFly: src/contrib/opie/libopie/newseed.c,v 1.2 2003/06/17 04:24:05 dillon Exp $
19 */
20
21 #include "opie_cfg.h"
22 #include <stdio.h>
23 #if HAVE_TIME_H
24 #include <time.h>
25 #endif /* HAVE_TIME_H */
26 #if HAVE_STRING_H
27 #include <string.h>
28 #endif /* HAVE_STRING_H */
29 #include <ctype.h>
30 #if HAVE_UNISTD_H
31 #include <unistd.h>
32 #endif /* HAVE_UNISTD_H */
33 #if HAVE_SYS_UTSNAME_H
34 #include <sys/utsname.h>
35 #endif /* HAVE_SYS_UTSNAME_H */
36 #include <errno.h>
37 #if DEBUG
38 #include <syslog.h>
39 #endif /* DEBUG */
40 #include "opie.h"
41
42 int opienewseed FUNCTION((seed), char *seed)
43 {
44         if (!seed)
45                 return -1;
46
47         if (seed[0]) {
48                 char *c, *end;
49                 unsigned int i, max;
50                 size_t slen;
51
52                 if ((slen = strlen(seed)) > OPIE_SEED_MAX)
53                         slen = OPIE_SEED_MAX;
54
55                 for (c = end = seed + slen - 1, max = 1;
56                                 (c >= seed) && isdigit(*c); c--)
57                         max *= 10;
58
59                 /* c either points before seed or to an alpha, so skip */
60                 ++c;
61
62                 /* keep alphas, only look at numbers */
63                 slen -= c - seed;
64
65                 if ((i = strtoul(c, (char **)0, 10)) < max) {
66                         if (++i >= max)
67                                 i = 1;
68
69                         /*
70                          * If we roll over, we will have to generate a
71                          * seed which is at least as long as the previous one
72                          * was.  snprintf() will add a NUL character as well.
73                          */
74                         snprintf(c, slen + 1, "%0*d", slen, i);
75                         seed[OPIE_SEED_MAX] = 0;
76                         return 0;
77                 }
78         }
79
80         {
81                 time_t now;
82
83                 time(&now);
84                 srand(now);
85         }
86
87         {
88                 struct utsname utsname;
89
90                 if (uname(&utsname) < 0) {
91 #if DEBUG
92                         syslog(LOG_DEBUG, "uname: %s(%d)", strerror(errno),
93                                 errno);
94 #endif /* DEBUG */
95                         utsname.nodename[0] = 'k';
96                         utsname.nodename[1] = 'e';
97                 }
98                 utsname.nodename[2] = 0;
99
100                 if (snprintf(seed, OPIE_SEED_MAX+1, "%s%04d", utsname.nodename,
101                                 (rand() % 9999) + 1) >= OPIE_SEED_MAX+1)
102                         return -1;
103                 return 0;
104         }
105 }
106