st_gid and I-node with different values inside the stat structure [closed]

1

I'm trying to build a program inside the stat function that shows the value of inode and the value of gid .

Follow the code:

#include <stdio.h>
#include <sys/types.h
#include <sys/stat.h>
#include <unistd.h>

int stat(const char *path, struct stat *buf);
struct stat buf;
printf("st_gid : %d\n", buf.st_gid); 
printf("I-node number:            %ld\n", (long) buf.st_ino);

Only the return is:

  

st_gid: 12779520 I-node number: -1076104514

The program is returning a nonzero number for the gid that should be zero because the file is authored by root and running as root and a negative number for the inode. When after the ls -li command it returns zero for the gid and 263458 for the inode. Could someone clarify where the error is?

    
asked by anonymous 15.05.2016 / 22:59

1 answer

0

You have redeclared the stat method, and did not assign the value to buf .

main.c:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <fcntl.h>

#define PATH "/etc/passwd"

int main(int argc, char **argv){
    struct stat buf;
    //--------------------------------------
    stat(PATH, &buf);
    printf("%d - %ld\n", buf.st_gid, buf.st_ino);
    //--------------------------------------
    int fd = open(PATH, O_RDONLY);

    fstat(fd, &buf);
    printf("%d - %ld\n", buf.st_gid, buf.st_ino);
    close(fd);

    //--------------------------------------
    lstat(PATH, &buf);
    printf("%d - %ld\n", buf.st_gid, buf.st_ino);

    return 0;
}

Output:

0 - 2378206
0 - 2378206
0 - 2378206

Files belonging to the user, have 10 in buf.st_gid (Regarding my tests).

    
16.05.2016 / 20:26