Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I access uname struct's domainname member even if I defined _GNU_SOURCE

Tags:

c

linux

macros

I am trying to some get Linux kernel version information by calling uname system call, but I am getting a compiler error saying ‘struct utsname’ has no member named ‘domainname’

#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/utsname.h>

#define _GNU_SOURCE

int main(void) {

   struct utsname buffer;

   errno = 0;
   if (uname(&buffer) != 0) {
      perror("uname");
      exit(EXIT_FAILURE);
   }

   printf("system name = %s\n", buffer.sysname);
   printf("node name   = %s\n", buffer.nodename);
   printf("release     = %s\n", buffer.release);
   printf("version     = %s\n", buffer.version);
   printf("machine     = %s\n", buffer.machine);

   #ifdef _GNU_SOURCE
      printf("domain name = %s\n", buffer.domainname);
   #endif

   return EXIT_SUCCESS;
}

according to https://linux.die.net/man/2/uname struct utsname is

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
};

I am not sure what I missed here

like image 866
Melan Avatar asked Dec 09 '19 10:12

Melan


People also ask

What is Uname function write a suitable C program?

The uname() function takes a pointer to the utsname structure that will store the result as input. Therefore, just make a temporary utsname instance, pass the address of it to uname , and read the content of this struct after the function succeed.


1 Answers

From man feature_test_macros:

NOTE: In order to be effective, a feature test macro must be defined before including any header files

It's:

#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <sys/utsname.h>
like image 74
KamilCuk Avatar answered Nov 15 '22 05:11

KamilCuk