Skip to content

Commit 98b2e14

Browse files
committed
动态检测 OS 以及添加 xy_init()
1 parent e6d512e commit 98b2e14

File tree

1 file changed

+114
-32
lines changed

1 file changed

+114
-32
lines changed

lib/xy.h

Lines changed: 114 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
* | Mikachu2333 <[email protected]>
99
* |
1010
* Created On : <2023-08-28>
11-
* Last Modified : <2025-08-20>
11+
* Last Modified : <2025-08-21>
1212
*
1313
*
1414
* xy: 襄阳、咸阳
@@ -22,7 +22,7 @@
2222
#ifndef XY_H
2323
#define XY_H
2424

25-
#define _XY_Version "v0.1.7.0-2025/08/20"
25+
#define _XY_Version "v0.1.7.0-2025/08/21"
2626
#define _XY_Maintain_URL "https://github.com/RubyMetric/chsrc/blob/dev/lib/xy.h"
2727
#define _XY_Maintain_URL2 "https://gitee.com/RubyMetric/chsrc/blob/dev/lib/xy.h"
2828

@@ -44,6 +44,7 @@
4444
#include <stdlib.h>
4545
#include <string.h>
4646
#include <unistd.h>
47+
#include <dirent.h> // opendir() closedir()
4748

4849
#if defined(__STDC__) && __STDC_VERSION__ >= 202311
4950
#define XY_Deprecate_This(msg) [[deprecated(msg)]]
@@ -56,55 +57,37 @@
5657
#endif
5758

5859

59-
/* Global */
60+
/* 全局变量 */
6061
bool xy_enable_color = true;
6162

63+
/* 由 xy_init() 赋值 */
64+
bool xy_on_windows = false;
65+
bool xy_on_linux = false;
66+
bool xy_on_macos = false;
67+
bool xy_on_bsd = false;
68+
bool xy_on_android = false;
69+
70+
char *xy_os_devnull = NULL;
71+
6272
// #define NDEBUG
6373

6474
#ifdef _WIN32
6575
#define XY_Build_On_Windows 1
6676

67-
#define xy_on_windows true
68-
#define xy_on_linux false
69-
#define xy_on_macos false
70-
#define xy_on_bsd false
71-
#define xy_os_devnull "nul"
7277
#include <windows.h>
7378
#include <shlobj.h>
74-
#define xy_use_utf8() SetConsoleOutputCP (65001)
7579

7680
#elif defined(__linux__) || defined(__linux)
7781
#define XY_Build_On_Linux 1
7882
#define XY_Build_On_Unix 1
7983

80-
#define xy_on_windows false
81-
#define xy_on_linux true
82-
#define xy_on_macos false
83-
#define xy_on_bsd false
84-
#define xy_os_devnull "/dev/null"
85-
#define xy_use_utf8()
86-
8784
#elif defined(__APPLE__)
8885
#define XY_Build_On_macOS 1
8986
#define XY_Build_On_Unix 1
9087

91-
#define xy_on_windows false
92-
#define xy_on_linux false
93-
#define xy_on_macos true
94-
#define xy_on_bsd false
95-
#define xy_os_devnull "/dev/null"
96-
#define xy_use_utf8()
97-
9888
#elif defined(__OpenBSD__) || defined(__NetBSD__) || defined(__FreeBSD__)
9989
#define XY_Build_On_BSD 1
10090
#define XY_Build_On_Unix 1
101-
102-
#define xy_on_windows false
103-
#define xy_on_linux false
104-
#define xy_on_macos false
105-
#define xy_on_bsd true
106-
#define xy_os_devnull "/dev/null"
107-
#define xy_use_utf8()
10891
#endif
10992

11093
#define assert_str(a, b) assert (xy_streql ((a), (b)))
@@ -661,7 +644,7 @@ _xy_log (int level, const char *prompt, const char *content)
661644
}
662645
else
663646
{
664-
// xy_assert ("CAN'T REACH!");
647+
xy_unreached();
665648
}
666649

667650
if (to_stderr)
@@ -747,7 +730,7 @@ _xy_log_brkt (int level, const char *prompt1, const char *prompt2, const char *c
747730
}
748731
else
749732
{
750-
// xy_assert ("CAN'T REACH!");
733+
xy_unreached();
751734
}
752735

753736
if (to_stderr)
@@ -1110,6 +1093,105 @@ xy_parent_dir (const char *path)
11101093

11111094

11121095

1096+
void
1097+
xy_detect_os ()
1098+
{
1099+
// C:
1100+
char *drive = getenv ("SystemDrive");
1101+
if (drive)
1102+
{
1103+
char path[256];
1104+
snprintf (path, sizeof (path), "%s\\Users", drive);
1105+
DIR *d = opendir (path);
1106+
if (d)
1107+
{
1108+
xy_on_windows = true;
1109+
closedir (d);
1110+
return;
1111+
}
1112+
}
1113+
1114+
FILE *fp = fopen ("/proc/version", "r");
1115+
if (fp)
1116+
{
1117+
char buf[256] = {0};
1118+
fread (buf, 1, sizeof(buf) - 1, fp);
1119+
fclose (fp);
1120+
if (strstr (buf, "Android"))
1121+
{
1122+
xy_on_android = true;
1123+
return;
1124+
}
1125+
else if (strstr (buf, "Linux"))
1126+
{
1127+
xy_on_linux = true;
1128+
return;
1129+
}
1130+
}
1131+
1132+
/* 判断 macOS */
1133+
DIR *d = opendir ("/System/Applications");
1134+
if (d)
1135+
{
1136+
closedir (d);
1137+
d = opendir ("/Library/Apple");
1138+
if (d)
1139+
{
1140+
xy_on_macos = true;
1141+
closedir (d);
1142+
}
1143+
}
1144+
1145+
/* 最后判断 BSD */
1146+
fp = popen ("uname -s", "r");
1147+
if (!fp)
1148+
{
1149+
if (opendir ("/etc/rc.d"))
1150+
{
1151+
closedir (d);
1152+
xy_on_bsd = true;
1153+
return;
1154+
}
1155+
}
1156+
else
1157+
{
1158+
char buf[256];
1159+
fgets (buf, sizeof (buf), fp);
1160+
pclose (fp);
1161+
if (strstr (buf, "BSD") != NULL)
1162+
xy_on_bsd = true;
1163+
}
1164+
1165+
assert (xy_on_windows || xy_on_linux || xy_on_android || xy_on_macos || xy_on_bsd);
1166+
}
1167+
1168+
1169+
void
1170+
xy_use_utf8 ()
1171+
{
1172+
#ifdef XY_Build_On_Windows
1173+
SetConsoleOutputCP (65001);
1174+
#endif
1175+
}
1176+
1177+
1178+
/**
1179+
* @note 该函数必须被首先调用,方能使用各个跨操作系统的函数
1180+
*/
1181+
void
1182+
xy_init ()
1183+
{
1184+
xy_detect_os ();
1185+
1186+
if (xy_on_windows)
1187+
xy_os_devnull = "nul";
1188+
else
1189+
xy_os_devnull = "/dev/null";
1190+
1191+
xy_use_utf8 ();
1192+
}
1193+
1194+
11131195
/******************************************************
11141196
* Container
11151197
******************************************************/

0 commit comments

Comments
 (0)