When I run uname -a on the command line, I get the following output:
Linux raspberrypi 5.10.63-v7l+ #1459 SMP Wed Oct 6 16:41:57 BST 2021 armv7l GNU/Linux
This is achieved by the -a parameter which is equivalent to using these parameters (there are 6) -snrvmo.
I am trying to replicate this using the uname() syscall in C. The manpage says the following about my uname() struct that is returned:
DESCRIPTION
uname() returns system information in the structure pointed to by buf. The utsname struct is de‐
fined in <sys/utsname.h>:
struct utsname {
char sysname[]; /* Operating system name (e.g., "Linux") */
char nodename[]; /* Name within "some implementation-defined
network" */
char release[]; /* Operating system release (e.g., "2.6.28") */
char version[]; /* Operating system version */
char machine[]; /* Hardware identifier */
#ifdef _GNU_SOURCE
char domainname[]; /* NIS or YP domain name */
#endif
};
You'll notice there is no operating system string corresponding to the the command line uname -o option. uname --help shows there is a -o parameter to display the OS and that doesn't seem to be available in the struct returned by the uname() syscall.
-o, --operating-system
print the operating system
So the best I can seem to do is get the following information using the syscall noting that "GNU/Linux" isn't at the end like what is given by uname -a:
Linux raspberrypi 5.10.63-v7l+ #1459 SMP Wed Oct 6 16:41:57 BST 2021 armv7l
Is there a way I can get the OS name (in this case, "GNU/Linux") in my C program like I can using uname -o?
My source code is essentially this
You can read the
unamecode here: https://github.com/MaiZure/coreutils-8.3/blob/master/src/uname.cIn that code, is written:
We can understand that in that word: "If have to print kernel info, or node or machine, use
unamesyscall".The Operating system is printed latter:
The
HOST_OPERATING_SYSTEMis defined ingnulibSince one compiled software can only be used by one OS, you can imagine to set it at build time.