newseed.c revision 22347
1/* newseed.c: The opienewseed() library function.
2
3%%% copyright-cmetz
4This software is Copyright 1996 by Craig Metz, All Rights Reserved.
5The Inner Net License Version 2 applies to this software.
6You should have received a copy of the license with this software. If
7you didn't get a copy, you may request one from <license@inner.net>.
8
9	History:
10
11	Created by cmetz for OPIE 2.22.
12*/
13
14#include "opie_cfg.h"
15#if HAVE_STRING_H
16#include <string.h>
17#endif /* HAVE_STRING_H */
18#include <ctype.h>
19#if HAVE_UNISTD_H
20#include <unistd.h>
21#endif /* HAVE_UNISTD_H */
22#if HAVE_SYS_UTSNAME_H
23#include <sys/utsname.h>
24#endif /* HAVE_SYS_UTSNAME_H */
25#include <errno.h>
26#include "opie.h"
27
28int opienewseed FUNCTION((seed), char *seed)
29{
30  if (!seed)
31    return -1;
32
33  if (seed[0]) {
34    int i;
35
36    if ((i = strlen(seed)) >= OPIE_SEED_MIN) {
37      long j;
38      char *c;
39
40      if (i > OPIE_SEED_MAX)
41	i = OPIE_SEED_MAX;
42
43      c = seed + i - 1;
44
45      while(c != seed) {
46	if (!isdigit(*c))
47	  break;
48	c--;
49      }
50
51      c++;
52
53      if (j = strtol(c, (char **)0, 10)) {
54	char buf[OPIE_SEED_MAX];
55
56	*c = 0;
57	strcpy(buf, seed);
58
59	if (errno == ERANGE) {
60	  j = 1;
61	} else {
62	  int k = 1, l = OPIE_SEED_MAX - strlen(buf);
63	  while(l--) k *= 10;
64
65	  if (++j >= k)
66	    j = 1;
67	}
68
69	sprintf(seed, "%s%04d", buf, j);
70	return 0;
71      }
72    }
73  }
74
75  {
76    {
77    time_t now;
78    time(&now);
79    srand(now);
80    }
81
82    {
83    struct utsname utsname;
84
85    if (uname(&utsname) < 0) {
86#if 0
87      perror("uname");
88#endif /* 0 */
89      utsname.nodename[0] = 'k';
90      utsname.nodename[1] = 'e';
91    }
92    utsname.nodename[2] = 0;
93
94    sprintf(seed, "%s%04d", utsname.nodename, (rand() % 9999) + 1);
95    return 0;
96    }
97  }
98}
99
100