/* hostname.c — 随机主机名生成模块 * * 基于当前时间戳的纳秒部分,从 78 张塔罗牌中随机选取一张作为主机名。 * 格式:<5位时间戳十六进制>-<塔罗牌名> * 例如:1204f86c-king-of-cups * * 78 张牌 = 22 张大阿卡纳 + 56 张小阿卡纳(4花色×14数字) */ #include #include #include "hostname.h" int choose_hostname(char *buff, size_t len) { /* 小阿卡纳四花色 */ static const char *suits[] = { "swords", "wands", "pentacles", "cups" }; /* 小阿卡纳 14 张数字牌 */ static const char *minor[] = { "ace", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten", "page", "knight", "queen", "king" }; /* 大阿卡纳 22 张 */ static const char *major[] = { "fool", "magician", "high-priestess", "empress", "emperor", "hierophant", "lovers", "chariot", "strength", "hermit", "wheel", "justice", "hanged-man", "death", "temperance", "devil", "tower", "star", "moon", "sun", "judgment", "world" }; /* 用单调时钟的纳秒部分做随机种子 */ struct timespec now = {0}; clock_gettime(CLOCK_MONOTONIC, &now); size_t ix = now.tv_nsec % 78; /* 78 张牌 */ if (ix < sizeof(major) / sizeof(*major)) { /* 0~21:大阿卡纳 */ snprintf(buff, len, "%05lx-%s", now.tv_sec, major[ix]); } else { /* 22~77:小阿卡纳(数字 + 花色) */ ix -= sizeof(major) / sizeof(*major); snprintf(buff, len, "%05lxc-%s-of-%s", now.tv_sec, minor[ix % (sizeof(minor) / sizeof(*minor))], suits[ix / (sizeof(minor) / sizeof(*minor))]); } return 0; }