uuid.c (852B)
1 #include <u.h> 2 #include <libc.h> 3 #include <libsec.h> 4 5 /* 6 * simple random uuid generator 7 */ 8 9 struct UUID { 10 u32int tl; // time_low 11 u16int tm; // time_mid 12 u16int th; // time_hi_and_version 13 u8int sh; // clock_seq_hi_and_reserved 14 u8int sl; // clock_seq_low 15 u8int nd[6]; // node 16 } uuid; 17 18 void 19 usage(void) 20 { 21 fprint(2, "usage: %s\n", argv0); 22 exits("usage"); 23 } 24 25 void 26 main(int argc, char **argv) 27 { 28 ARGBEGIN{ 29 default: 30 usage(); 31 } ARGEND 32 if (argc != 0) usage(); 33 34 genrandom((uchar *)(&uuid), sizeof(uuid)); 35 36 // mask 1001 1111, data 0010 0000 37 uuid.sh = uuid.sh & 0x60 | 0x20; 38 39 // mask 0000 1111 1111 1111 40 // data 0100 0000 0000 0000 41 uuid.th = uuid.th & 0x0fff | 0x4000; 42 43 print("%08ux-%04uhx-%04uhx-%02uhhx%02uhhx-", uuid.tl, uuid.tm, uuid.th, uuid.sh, uuid.sl); 44 45 int i; 46 47 for (i = 0; i < 6; i++) print("%02uhhx", uuid.nd[i]); 48 49 print("\n"); 50 }