Skip to content

Commit 3771088

Browse files
committed
Add xy.os_family
1 parent 066ac19 commit 3771088

File tree

1 file changed

+43
-33
lines changed

1 file changed

+43
-33
lines changed

lib/xy.h

Lines changed: 43 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,8 @@ struct
7272
bool on_bsd;
7373
bool on_android;
7474

75+
/* @flavor 同 just 中的 os_family(),只区分 windows, unix */
76+
char *os_family;
7577
char *os_devnull;
7678
}
7779
xy =
@@ -88,6 +90,7 @@ xy =
8890
.on_bsd = false,
8991
.on_android = false,
9092

93+
.os_family = NULL,
9194
.os_devnull = NULL
9295
};
9396

@@ -1051,21 +1054,6 @@ xy_run_get_stdout (const char *cmd, char **output)
10511054
}
10521055

10531056

1054-
/**
1055-
* @flavor 该函数同 just 中的 os_family(),只区分 windows, unix
1056-
*
1057-
* @return 返回 "windows" 或 "unix"
1058-
*/
1059-
#define xy_os_family _xy_os_family ()
1060-
static char *
1061-
_xy_os_family ()
1062-
{
1063-
if (xy.on_windows)
1064-
return "windows";
1065-
else
1066-
return "unix";
1067-
}
1068-
10691057

10701058
/**
10711059
* @brief 该函数返回所在 os family 的对应字符串
@@ -1312,11 +1300,17 @@ xy_parent_dir (const char *path)
13121300
}
13131301

13141302

1315-
1303+
/**
1304+
* @internal 仅由 xy_init () 调用
1305+
*/
13161306
void
1317-
xy_detect_os ()
1307+
_xy_detect_os ()
13181308
{
1319-
// C:
1309+
/**
1310+
* 首先判断是否为 Windows
1311+
*
1312+
* SystemDrive 为 C:
1313+
*/
13201314
char *drive = getenv ("SystemDrive");
13211315
if (drive)
13221316
{
@@ -1326,11 +1320,13 @@ xy_detect_os ()
13261320
if (d)
13271321
{
13281322
xy.on_windows = true;
1323+
xy.os_family = "windows";
13291324
closedir (d);
13301325
return;
13311326
}
13321327
}
13331328

1329+
/* 判断 Linux */
13341330
FILE *fp = fopen ("/proc/version", "r");
13351331
if (fp)
13361332
{
@@ -1340,16 +1336,25 @@ xy_detect_os ()
13401336
if (strstr (buf, "Linux"))
13411337
{
13421338
xy.on_linux = true;
1339+
xy.os_family = "unix";
13431340
return;
13441341
}
13451342
}
13461343

1347-
// @consult https://android.googlesource.com/platform/system/core/+/refs/heads/main/rootdir/init.environ.rc.in
1344+
/**
1345+
* 判断 Android
1346+
*
1347+
* @consult https://android.googlesource.com/platform/system/core/+/refs/heads/main/rootdir/init.environ.rc.in
1348+
*/
13481349
char *android_env = getenv ("ANDROID_ROOT");
1349-
if (xy_str_find (android_env, "/system").found)
1350+
if (android_env)
13501351
{
1351-
xy.on_android = true;
1352-
return;
1352+
if (xy_str_find (android_env, "/system").found)
1353+
{
1354+
xy.on_android = true;
1355+
xy.os_family = "unix";
1356+
return;
1357+
}
13531358
}
13541359

13551360
/* 判断 macOS */
@@ -1361,31 +1366,36 @@ xy_detect_os ()
13611366
if (d)
13621367
{
13631368
xy.on_macos = true;
1369+
xy.os_family = "unix";
13641370
closedir (d);
13651371
return;
13661372
}
13671373
}
13681374

13691375
/* 最后判断 BSD */
13701376
fp = popen ("uname -s", "r");
1371-
if (!fp)
1377+
if (fp)
13721378
{
1373-
DIR *bsd_dir = opendir ("/etc/rc.d");
1374-
if (bsd_dir)
1379+
char buf[256];
1380+
fgets (buf, sizeof (buf), fp);
1381+
pclose (fp);
1382+
if (strstr (buf, "BSD") != NULL)
13751383
{
1376-
closedir (bsd_dir);
13771384
xy.on_bsd = true;
1385+
xy.os_family = "unix";
13781386
return;
13791387
}
13801388
}
13811389
else
13821390
{
1383-
char buf[256];
1384-
fgets (buf, sizeof (buf), fp);
1385-
pclose (fp);
1386-
if (strstr (buf, "BSD") != NULL)
1387-
xy.on_bsd = true;
1388-
return;
1391+
DIR *bsd_dir = opendir ("/etc/rc.d");
1392+
if (bsd_dir)
1393+
{
1394+
closedir (bsd_dir);
1395+
xy.on_bsd = true;
1396+
xy.os_family = "unix";
1397+
return;
1398+
}
13891399
}
13901400

13911401
if (!(xy.on_windows || xy.on_linux || xy.on_android || xy.on_macos || xy.on_bsd))
@@ -1408,7 +1418,7 @@ xy_use_utf8 ()
14081418
void
14091419
xy_init ()
14101420
{
1411-
xy_detect_os ();
1421+
_xy_detect_os ();
14121422

14131423
if (xy.on_windows)
14141424
xy.os_devnull = "nul";

0 commit comments

Comments
 (0)