stat() get group name is root

Posted by mengmenger on Stack Overflow See other posts from Stack Overflow or by mengmenger
Published on 2010-04-09T00:54:24Z Indexed on 2010/04/09 1:03 UTC
Read the original article Hit count: 360

Filed under:

I have a file src.tar.gz whoes owner and group are named "src". When I run test.c compiled with name "test" (permission: -rwsr-xr-x owner:root group:staff)

The way I run it: I am running it as group member under "src" group. But I run "test" as root since "test" permission is -rwsr-xr-x

Question: Why did result come out like this? is the src.tar.gz group should be "src"?

Output:

Error: my group: src
Error: src.tar.gz group is root

test.c

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

void main(int ac, char **args) {

        const char *ERR_MSG_FORMAT = "%s: %s %s.\n";
        char *ptr_source_file = "src.tar.gz";
        struct stat src_stat;
        gid_t  src_gid, my_gid;

        int i = stat(ptr_source_file, &src_stat);
                my_gid = getgid();
                struct group *cur_gr = getgrgid(my_gid);
                fprintf(stderr, ERR_MSG_FORMAT, "Error", "my group: ", cur_gr->gr_name);

                src_gid = src_stat.st_gid;
                struct group *src_gr = getgrgid(src_gid);
                fprintf(stderr, ERR_MSG_FORMAT, "Error","src.tar.gz group is ", src_gr->gr_name);

}

© Stack Overflow or respective owner

Related posts about c