Disable all code related to libXp
[oweals/cde.git] / cde / programs / dtinfo / DtMmdb / misc / unique_id.c
1 /*
2  * CDE - Common Desktop Environment
3  *
4  * Copyright (c) 1993-2012, The Open Group. All rights reserved.
5  *
6  * These libraries and programs are free software; you can
7  * redistribute them and/or modify them under the terms of the GNU
8  * Lesser General Public License as published by the Free Software
9  * Foundation; either version 2 of the License, or (at your option)
10  * any later version.
11  *
12  * These libraries and programs are distributed in the hope that
13  * they will be useful, but WITHOUT ANY WARRANTY; without even the
14  * implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  * PURPOSE. See the GNU Lesser General Public License for more
16  * details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with these librararies and programs; if not, write
20  * to the Free Software Foundation, Inc., 51 Franklin Street, Fifth
21  * Floor, Boston, MA 02110-1301 USA
22  */
23 /*      Copyright (c) 1994 FUJITSU LIMITED      */
24 /*      All Rights Reserved                     */
25
26 /*
27  * $XConsortium: unique_id.c /main/3 1996/06/11 17:40:45 cde-hal $
28  *
29  * Copyright (c) 1992 HAL Computer Systems International, Ltd.
30  * All rights reserved.  Unpublished -- rights reserved under
31  * the Copyright Laws of the United States.  USE OF A COPYRIGHT
32  * NOTICE IS PRECAUTIONARY ONLY AND DOES NOT IMPLY PUBLICATION
33  * OR DISCLOSURE.
34  * 
35  * THIS SOFTWARE CONTAINS CONFIDENTIAL INFORMATION AND TRADE
36  * SECRETS OF HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.  USE,
37  * DISCLOSURE, OR REPRODUCTION IS PROHIBITED WITHOUT THE
38  * PRIOR EXPRESS WRITTEN PERMISSION OF HAL COMPUTER SYSTEMS
39  * INTERNATIONAL, LTD.
40  * 
41  *                         RESTRICTED RIGHTS LEGEND
42  * Use, duplication, or disclosure by the Government is subject
43  * to the restrictions as set forth in subparagraph (c)(l)(ii)
44  * of the Rights in Technical Data and Computer Software clause
45  * at DFARS 252.227-7013.
46  *
47  *          HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
48  *                  1315 Dell Avenue
49  *                  Campbell, CA  95008
50  * 
51  */
52
53 #include <sys/types.h>
54 #include <sys/time.h>
55 #include <stdio.h>
56 #include <stdlib.h>
57 #include <unistd.h>
58 #include <ctype.h>
59
60 #ifdef SVR4
61 #include <sys/systeminfo.h>
62 #endif
63
64 /* **************************************************************
65  * unique_id - generate a 15 character NULL terminated id
66  * ************************************************************** */
67
68 /* This code assumes:
69
70    sizeof (time_t) == 4,
71    sizeof (int) == 4,
72    sizeof (short) == 2,
73    sizeof ("hostid") == 4,
74    sizeof ("pid") == 2,
75 */
76
77 /* #define DEBUG */
78
79 static char mapping[] =
80   { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
81     'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
82     'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
83     'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
84     'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
85     '.', '_' };
86
87 static unsigned int mask[] =
88   { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
89
90 #define COPY_BITS(DEST,DPOS,SRC,SPOS,LEN) \
91   DEST |= ((SRC & (mask[LEN] << SPOS)) >> SPOS) << DPOS;
92
93 #define PRINT_BITS(BITS) \
94   { int i; unsigned long bits = BITS; printf (#BITS " = 0x%04x", BITS); \
95     for (i = 0; i < sizeof(BITS) * 8; i++, bits <<= 1) { \
96       if (!(i%4)) putchar (' '); \
97       (bits & (1L << (sizeof(BITS) * 8)-1)) ? putchar('1') : putchar('0'); } \
98     putchar ('\n'); } 
99
100 #if defined(hpux)
101 #include <sys/utsname.h>
102 static unsigned int
103 gethostid()
104 {
105   struct utsname u;
106   int i;
107
108   i=uname(&u);
109   if (i==-1)
110     abort();
111   if (u.idnumber[0])
112     return atoi(u.idnumber);
113   abort();
114 }
115 #elif defined(SVR4) && !defined(sun)
116 static unsigned int
117 gethostid()
118 {
119   char buffer[256];
120   sysinfo (SI_HW_SERIAL, buffer, sizeof (buffer));
121   return (atoi (buffer));
122 }
123 #endif
124
125 const char *
126 unique_id (void)
127 {
128   static char buf[16];
129   static unsigned int hostid;
130   static struct timeval cur_time, old_time;
131   static unsigned short pid;
132   static int i;
133
134   /* -------- First get the information -------- */
135
136   /* Loop until first char is alpha-numeric. */
137   do
138     {
139
140       /* Loop over time until unique. */
141       do
142         {
143           /* Failure of this call is catastrophic: */
144           if (gettimeofday (&cur_time, NULL) == -1)
145             {
146               perror ("unique_id:gettimeofday");
147               abort();
148             }
149           /* Truncate microseconds to milliseconds. */
150           cur_time.tv_usec /= 1000;
151         }
152       while (cur_time.tv_usec == old_time.tv_usec &&
153              cur_time.tv_sec == old_time.tv_sec);
154
155       old_time.tv_usec = cur_time.tv_usec;
156       old_time.tv_sec = cur_time.tv_sec;
157
158       if (pid == 0)
159         pid = getpid();
160       if (hostid == 0)
161         hostid = gethostid();
162
163 #ifdef DEBUG
164       PRINT_BITS ((unsigned int)cur_time.tv_usec);
165       PRINT_BITS ((unsigned int)cur_time.tv_sec);
166       PRINT_BITS (pid);
167       PRINT_BITS (hostid);
168 #endif
169
170       for (i = 0; i < 15; i++)
171         buf[i] = 0;
172
173       COPY_BITS (buf[0], 0, cur_time.tv_usec, 0, 6);
174       COPY_BITS (buf[1], 0, cur_time.tv_usec, 6, 4);
175       COPY_BITS (buf[1], 4, cur_time.tv_sec, 0, 2);
176       COPY_BITS (buf[2], 0, cur_time.tv_sec, 2, 6);
177       COPY_BITS (buf[3], 0, cur_time.tv_sec, 8, 6);
178       COPY_BITS (buf[4], 0, cur_time.tv_sec, 14, 6);
179       COPY_BITS (buf[5], 0, cur_time.tv_sec, 20, 6);
180       COPY_BITS (buf[6], 0, cur_time.tv_sec, 26, 6);
181       COPY_BITS (buf[7], 0, pid, 0, 6);
182       COPY_BITS (buf[8], 0, pid, 6, 6);
183       COPY_BITS (buf[9], 0, pid, 12, 4);
184       COPY_BITS (buf[9], 4, hostid, 0, 2);
185       COPY_BITS (buf[10], 0, hostid, 2, 6);
186       COPY_BITS (buf[11], 0, hostid, 8, 6);
187       COPY_BITS (buf[12], 0, hostid, 14, 6);
188       COPY_BITS (buf[13], 0, hostid, 20, 6);
189       COPY_BITS (buf[14], 0, hostid, 26, 6);
190
191       for (i = 0; i < 15; i++)
192         {
193 #ifdef DEBUG
194           unsigned char ch = buf[i];
195           printf ("%2d  0x%02x  ", i, ch);
196           PRINT_BITS (ch);
197 #endif
198           buf[i] = mapping[(int)buf[i]];
199         }
200
201     } while (!isalnum ((unsigned char) buf[0]));
202
203   return (buf);
204 }
205
206
207 #ifdef TEST
208
209 int
210 main (int argc, char **argv)
211 {
212   int count = 0;
213   int i;
214
215   if (argc == 1)
216     count = 1;
217   else if (argc == 2)
218     count = atoi (argv[1]);
219
220   if (count == 0)
221     {
222       printf (stderr, "usage: uid [count]");
223       exit (1);
224     }
225
226   printf ("Generating %d unique ids\n", count);
227   for (i = 0; i < count; i++)
228     puts (unique_id());
229 }
230
231 #endif /* TEST */