st_gid and I-Node with different values within the stat structure

Asked

Viewed 34 times

1

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

Follows 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 that the return is :

st_gid : 12779520 I-Node number: -1076104514

The program is returning a non-zero number to gid which should be zero because the file is root authored and is running as root and a negative number for inode. When after the ls -li command it returns zero for gid and 263458 for inode. Someone could clarify where the error is?

1 answer

0

You redeclared the method stat, and did not assign the value in 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;
}

Exit:

0 - 2378206
0 - 2378206
0 - 2378206

The files belonging to the user, have the value 10 in the buf.st_gid (Relating to my tests).

  • Okay. I’ll test here to see if it works and return. Thank you.

  • really is right missing assign value in buf. Thank you and can call it off.

  • @uthacod, if the answer solved your problem, you can click on V next to the answer to mark your question as answered

Browser other questions tagged

You are not signed in. Login or sign up in order to post.