diff options
Diffstat (limited to 'contrib/opie/libopie/newseed.c')
-rw-r--r-- | contrib/opie/libopie/newseed.c | 109 |
1 files changed, 46 insertions, 63 deletions
diff --git a/contrib/opie/libopie/newseed.c b/contrib/opie/libopie/newseed.c index b351798..0455857 100644 --- a/contrib/opie/libopie/newseed.c +++ b/contrib/opie/libopie/newseed.c @@ -1,13 +1,15 @@ /* newseed.c: The opienewseed() library function. %%% copyright-cmetz-96 -This software is Copyright 1996-1998 by Craig Metz, All Rights Reserved. -The Inner Net License Version 2 applies to this software. +This software is Copyright 1996-2001 by Craig Metz, All Rights Reserved. +The Inner Net License Version 3 applies to this software. You should have received a copy of the license with this software. If you didn't get a copy, you may request one from <license@inner.net>. History: + Modified by cmetz for OPIE 2.4. Greatly simplified increment. Now does + not add digits. Reformatted the code. Modified by cmetz for OPIE 2.32. Added syslog.h if DEBUG. Modified by cmetz for OPIE 2.31. Added time.h. Created by cmetz for OPIE 2.22. @@ -35,73 +37,54 @@ you didn't get a copy, you may request one from <license@inner.net>. int opienewseed FUNCTION((seed), char *seed) { - if (!seed) - return -1; - - if (seed[0]) { - int i; - - if ((i = strlen(seed)) >= OPIE_SEED_MIN) { - long j; - char *c; - - if (i > OPIE_SEED_MAX) - i = OPIE_SEED_MAX; - - c = seed + i - 1; - - while(c != seed) { - if (!isdigit(*c)) - break; - c--; - } - - c++; - - if (j = strtol(c, (char **)0, 10)) { - char buf[OPIE_SEED_MAX]; - - *c = 0; - strcpy(buf, seed); - - if (errno == ERANGE) { - j = 1; - } else { - int k = 1, l = OPIE_SEED_MAX - strlen(buf); - while(l--) k *= 10; - - if (++j >= k) - j = 1; + if (!seed) + return -1; + + if (seed[0]) { + char *c, *end; + unsigned int i, max; + + if ((i = strlen(seed)) > OPIE_SEED_MAX) + i = OPIE_SEED_MAX; + + for (c = end = seed + i - 1, max = 1; + (c > seed) && isdigit(*c); c--) + max *= 10; + + if ((i = strtoul(++c, (char **)0, 10)) < max) { + if (++i >= max) + i = 1; + + snprintf(c, end - c, "%d", i); + seed[OPIE_SEED_MAX] = 0; + return 0; + } } - sprintf(seed, "%s%04d", buf, j); - return 0; - } - } - } + { + time_t now; - { - { - time_t now; - time(&now); - srand(now); - } + time(&now); + srand(now); + } - { - struct utsname utsname; + { + struct utsname utsname; - if (uname(&utsname) < 0) { + if (uname(&utsname) < 0) { #if DEBUG - syslog(LOG_DEBUG, "uname: %s(%d)", strerror(errno), errno); + syslog(LOG_DEBUG, "uname: %s(%d)", strerror(errno), + errno); #endif /* DEBUG */ - utsname.nodename[0] = 'k'; - utsname.nodename[1] = 'e'; - } - utsname.nodename[2] = 0; - - sprintf(seed, "%s%04d", utsname.nodename, (rand() % 9999) + 1); - return 0; - } - } + utsname.nodename[0] = 'k'; + utsname.nodename[1] = 'e'; + } + utsname.nodename[2] = 0; + + if (snprintf(seed, OPIE_SEED_MAX+1, "%s%04d", utsname.nodename, + (rand() % 9999) + 1) >= OPIE_SEED_MAX+1) + return -1; + return 0; + } } |