Powered by Linux
[PATCH] pre-proc: use uname() syscall instead of invoking uname — Semantic Matching Tool

[PATCH] pre-proc: use uname() syscall instead of invoking uname

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Not all systems have uname at /bin/uname and using uname() directly
seems to be a better case than spawning shell.

The utsname.sysname seems to be returning different name ("Linux"
instead of "GNU/Linux") but this doesn't seem to be a problem.
"uname -o" does seem to be returning HOST_OPERATING_SYSTEM and
utsname.sysname being defined as KERNEL_NAME.

Signed-off-by: Andrey Albersteyn <aalbersh@xxxxxxxxxx>
---
 pre-process.c | 24 ++++++++----------------
 1 file changed, 8 insertions(+), 16 deletions(-)

diff --git a/pre-process.c b/pre-process.c
index 457685c0f585..e34fd6a8cf99 100644
--- a/pre-process.c
+++ b/pre-process.c
@@ -37,6 +37,7 @@
 #include <time.h>
 #include <dirent.h>
 #include <sys/stat.h>
+#include <sys/utsname.h>
 
 #include "lib.h"
 #include "allocate.h"
@@ -2412,29 +2413,20 @@ void init_include_path(void)
 {
 	FILE *fp;
 	char path[256];
-	char arch[32];
 	char os[32];
+	int error;
+	struct utsname name;
 
-	fp = popen("/bin/uname -m", "r");
-	if (!fp)
+	error = uname(&name);
+	if (error)
 		return;
-	if (!fgets(arch, sizeof(arch) - 1, fp))
-		return;
-	pclose(fp);
-	if (arch[strlen(arch) - 1] == '\n')
-		arch[strlen(arch) - 1] = '\0';
-
-	fp = popen("/bin/uname -o", "r");
-	if (!fp)
-		return;
-	fgets(os, sizeof(os) - 1, fp);
-	pclose(fp);
 
-	if (strcmp(os, "GNU/Linux\n") != 0)
+	if (strcmp(name.sysname, "Linux") != 0)
 		return;
 	strcpy(os, "linux-gnu");
 
-	snprintf(path, sizeof(path), "/usr/include/%s-%s/", arch, os);
+	snprintf(path, sizeof(path), "/usr/include/%s-%s/",
+			name.machine, os);
 	add_pre_buffer("#add_system \"%s/\"\n", path);
 }
 
-- 
2.42.0





[Index of Archives]     [Linux USB Devel]     [Linux Audio Users]     [Yosemite News]     [Linux Kernel]     [Linux SCSI]     [Big List of Linux Books]

  Powered by Linux