]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/opie/libopie/newseed.c
unfinished sblive driver, playback/mixer only for now - not enabled in
[FreeBSD/FreeBSD.git] / contrib / opie / libopie / newseed.c
1 /* newseed.c: The opienewseed() library function.
2
3 %%% copyright-cmetz-96
4 This software is Copyright 1996-1997 by Craig Metz, All Rights Reserved.
5 The Inner Net License Version 2 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.31. Added time.h.
12         Created by cmetz for OPIE 2.22.
13 */
14
15 #include "opie_cfg.h"
16 #if HAVE_TIME_H
17 #include <time.h>
18 #endif /* HAVE_TIME_H */
19 #if HAVE_STRING_H
20 #include <string.h>
21 #endif /* HAVE_STRING_H */
22 #include <ctype.h>
23 #if HAVE_UNISTD_H
24 #include <unistd.h>
25 #endif /* HAVE_UNISTD_H */
26 #if HAVE_SYS_UTSNAME_H
27 #include <sys/utsname.h>
28 #endif /* HAVE_SYS_UTSNAME_H */
29 #include <errno.h>
30 #include "opie.h"
31
32 int 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