2 * CDE - Common Desktop Environment
4 * Copyright (c) 1993-2012, The Open Group. All rights reserved.
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)
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
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
23 /* Copyright (c) 1994 FUJITSU LIMITED */
24 /* All Rights Reserved */
27 * $XConsortium: unique_id.c /main/3 1996/06/11 17:40:45 cde-hal $
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
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
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.
47 * HAL COMPUTER SYSTEMS INTERNATIONAL, LTD.
53 #include <sys/types.h>
61 #include <sys/systeminfo.h>
64 /* **************************************************************
65 * unique_id - generate a 15 character NULL terminated id
66 * ************************************************************** */
73 sizeof ("hostid") == 4,
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',
87 static unsigned int mask[] =
88 { 0x00, 0x01, 0x03, 0x07, 0x0f, 0x1f, 0x3f, 0x7f, 0xff };
90 #define COPY_BITS(DEST,DPOS,SRC,SPOS,LEN) \
91 DEST |= ((SRC & (mask[LEN] << SPOS)) >> SPOS) << DPOS;
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'); } \
101 #include <sys/utsname.h>
112 return atoi(u.idnumber);
120 sysinfo (SI_HW_SERIAL, buffer, sizeof (buffer));
121 return (atoi (buffer));
129 static unsigned int hostid;
130 static struct timeval cur_time, old_time;
131 static unsigned short pid;
134 /* -------- First get the information -------- */
136 /* Loop until first char is alpha-numeric. */
140 /* Loop over time until unique. */
143 /* Failure of this call is catastrophic: */
144 if (gettimeofday (&cur_time, NULL) == -1)
146 perror ("unique_id:gettimeofday");
149 /* Truncate microseconds to milliseconds. */
150 cur_time.tv_usec /= 1000;
152 while (cur_time.tv_usec == old_time.tv_usec &&
153 cur_time.tv_sec == old_time.tv_sec);
155 old_time.tv_usec = cur_time.tv_usec;
156 old_time.tv_sec = cur_time.tv_sec;
161 hostid = gethostid();
164 PRINT_BITS ((unsigned int)cur_time.tv_usec);
165 PRINT_BITS ((unsigned int)cur_time.tv_sec);
170 for (i = 0; i < 15; i++)
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);
191 for (i = 0; i < 15; i++)
194 unsigned char ch = buf[i];
195 printf ("%2d 0x%02x ", i, ch);
198 buf[i] = mapping[(int)buf[i]];
201 } while (!isalnum ((unsigned char) buf[0]));
210 main (int argc, char **argv)
218 count = atoi (argv[1]);
222 printf (stderr, "usage: uid [count]");
226 printf ("Generating %d unique ids\n", count);
227 for (i = 0; i < count; i++)