newseed.c revision 37304
1252190Srpaulo/* newseed.c: The opienewseed() library function.
2252190Srpaulo
3252190Srpaulo%%% copyright-cmetz-96
4252190SrpauloThis software is Copyright 1996-1997 by Craig Metz, All Rights Reserved.
5252190SrpauloThe Inner Net License Version 2 applies to this software.
6252190SrpauloYou should have received a copy of the license with this software. If
7252190Srpauloyou didn't get a copy, you may request one from <license@inner.net>.
8252190Srpaulo
9252190Srpaulo	History:
10252190Srpaulo
11252190Srpaulo	Modified by cmetz for OPIE 2.31. Added time.h.
12252190Srpaulo	Created by cmetz for OPIE 2.22.
13252190Srpaulo*/
14252190Srpaulo
15252190Srpaulo#include "opie_cfg.h"
16252190Srpaulo#if HAVE_TIME_H
17252190Srpaulo#include <time.h>
18252190Srpaulo#endif /* HAVE_TIME_H */
19252190Srpaulo#if HAVE_STRING_H
20252190Srpaulo#include <string.h>
21252190Srpaulo#endif /* HAVE_STRING_H */
22252190Srpaulo#include <ctype.h>
23252190Srpaulo#if HAVE_UNISTD_H
24252190Srpaulo#include <unistd.h>
25252190Srpaulo#endif /* HAVE_UNISTD_H */
26252190Srpaulo#if HAVE_SYS_UTSNAME_H
27252190Srpaulo#include <sys/utsname.h>
28252190Srpaulo#endif /* HAVE_SYS_UTSNAME_H */
29#include <errno.h>
30#include "opie.h"
31
32int opienewseed FUNCTION((seed), char *seed)
33{
34  if (!seed)
35    return -1;
36
37  if (seed[0]) {
38    int i;
39
40    if ((i = strlen(seed)) >= OPIE_SEED_MIN) {
41      long j;
42      char *c;
43
44      if (i > OPIE_SEED_MAX)
45	i = OPIE_SEED_MAX;
46
47      c = seed + i - 1;
48
49      while(c != seed) {
50	if (!isdigit(*c))
51	  break;
52	c--;
53      }
54
55      c++;
56
57      if (j = strtol(c, (char **)0, 10)) {
58	char buf[OPIE_SEED_MAX];
59
60	*c = 0;
61	strcpy(buf, seed);
62
63	if (errno == ERANGE) {
64	  j = 1;
65	} else {
66	  int k = 1, l = OPIE_SEED_MAX - strlen(buf);
67	  while(l--) k *= 10;
68
69	  if (++j >= k)
70	    j = 1;
71	}
72
73	sprintf(seed, "%s%04ld", buf, j);
74	return 0;
75      }
76    }
77  }
78
79  {
80    {
81    time_t now;
82    time(&now);
83    srand(now);
84    }
85
86    {
87    struct utsname utsname;
88
89    if (uname(&utsname) < 0) {
90#if 0
91      perror("uname");
92#endif /* 0 */
93      utsname.nodename[0] = 'k';
94      utsname.nodename[1] = 'e';
95    }
96    utsname.nodename[2] = 0;
97
98    sprintf(seed, "%s%04d", utsname.nodename, (rand() % 9999) + 1);
99    return 0;
100    }
101  }
102}
103
104