1 /* newseed.c: The opienewseed() library function.
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>.
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.
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 $
24 #endif /* HAVE_TIME_H */
27 #endif /* HAVE_STRING_H */
31 #endif /* HAVE_UNISTD_H */
32 #if HAVE_SYS_UTSNAME_H
33 #include <sys/utsname.h>
34 #endif /* HAVE_SYS_UTSNAME_H */
41 int opienewseed
FUNCTION((seed
), char *seed
)
51 if ((slen
= strlen(seed
)) > OPIE_SEED_MAX
)
54 for (c
= end
= seed
+ slen
- 1, max
= 1;
55 (c
>= seed
) && isdigit(*c
); c
--)
58 /* c either points before seed or to an alpha, so skip */
61 /* keep alphas, only look at numbers */
64 if ((i
= strtoul(c
, (char **)0, 10)) < max
) {
69 * If we roll over, we will have to generate a
70 * seed which is at least as long as the previous one
71 * was. snprintf() will add a NUL character as well.
73 snprintf(c
, slen
+ 1, "%0*d", slen
, i
);
74 seed
[OPIE_SEED_MAX
] = 0;
87 struct utsname utsname
;
89 if (uname(&utsname
) < 0) {
91 syslog(LOG_DEBUG
, "uname: %s(%d)", strerror(errno
),
94 utsname
.nodename
[0] = 'k';
95 utsname
.nodename
[1] = 'e';
97 utsname
.nodename
[2] = 0;
99 if (snprintf(seed
, OPIE_SEED_MAX
+1, "%s%04d", utsname
.nodename
,
100 (rand() % 9999) + 1) >= OPIE_SEED_MAX
+1)