Uname is a unique variant of the executable file 'uname'.
The available options are:
-s : To print the kernel name
-v : To print the kernel version
-r : To print the kernel release
-n : To print the network node hostname
-m : To print the machine hardware name
-a : To print all of the above
If no options are provided, uname acts like uname -s. The tool can be accessed through the following sample code:
#include < unistd.h >
#include < sys/utsname.h >
#include < stdio.h >
#include < stdlib.h >
#include < string.h >
void print_usage(void);
int main( int argc, char *argv[] ) {
int c;
extern char *optarg;
extern int optind, optopt;
int a_flag = 0, s_flag = 0, n_flag = 0, r_flag = 0, v_flag = 0, m_flag = 0;
int errflag=0;
char host[256];
struct utsname uts;
if (gethostname(host, 255) != 0 || uname(&uts) < 0) {
fprintf(stderr, "Unable to get host information\n");
exit(1);
}
If no options are provided, the uname system will print the basic system name (sysname). However, when provided with one or more options, it will parse the options and print relevant details. If any erroneous options are passed while executing the program, it will print a usage info before exiting.
if (errflag != 0) {
print_usage();
exit(1);
}
char *sysname, *machine, *nodename, *release, *version;
sysname=uts.sysname;
machine=uts.machine;
nodename=uts.nodename;
release=uts.release;
version=uts.version;
The program will then allocate memory space for all eventualities and execute the corresponding option using strcat, prints it with only valid options.
if (a_flag == 1) {
printf("%s %s %s %s %s\n",sysname, nodename, release, version, machine);
} else {
char *printstring;
/* allocate enough space for all eventualities....*/
printstring = (char *) malloc(strlen(sysname)+strlen(version)+strlen(release)+strlen(nodename)+strlen(machine)+6);
strcpy( printstring , "");
if (s_flag == 1) {
strcat( printstring, sysname );
strcat( printstring, " ");
}
if (n_flag == 1) {
strcat( printstring, nodename );
strcat( printstring, " ");
}
if (r_flag == 1) {
strcat( printstring, release );
strcat( printstring, " ");
}
if (v_flag == 1) {
strcat( printstring, version );
strcat( printstring, " ");
}
if (m_flag == 1) {
strcat( printstring, machine );
strcat
Version 0.1: N/A