ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Where is the zero? Is that the return of getpwuid or is that the pointer to the user-name string? If it's the return of getpwuid, that normally means the user ID doesn't exist or you don't have permission to read the passwd file. Print strerror(errno) immediately after getpwuid and tell us what it says.
Kevin Barry
PS You need getgrgid to get the group name. Also, you'll sometimes encounter an external file system with unknown IDs because the files were created on other machines. That means you'll have to deal with unknown users. Extracting archives can do this, also.
Where is the zero? Is that the return of getpwuid or is that the pointer to the user-name string? If it's the return of getpwuid, that normally means the user ID doesn't exist or you don't have permission to read the passwd file. Print strerror(errno) immediately after getpwuid and tell us what it says.
Kevin Barry
PS You need getgrgid to get the group name. Also, you'll sometimes encounter an external file system with unknown IDs because the files were created on other machines. That means you'll have to deal with unknown users. Extracting archives can do this, also.
Thanks Ta0kira,
yes you are right, it seems like I dont have permission rights to access passwd file as I tried /etc/passwd and I got a message saying permission denied.
However, those two 0's were the return values.
I have also tried getgrgid and it printing group id but I want to print a group name of the group ID.
like my user id is 34008 and user name is LinuxInfo so I want to print User Name.
Of course, I left off the error checking as an exercise for the OP to complete.
Thanks dwhitne,
I tried the above code and I am getting two warning and 1 error. Dont know how to debug. Below are the warning and error I am getting:
cc: Warning: Lab2b.c, line 14: In the initializer for pwd, "getpwuid(...)" of type "int", is being converted to "pointer to struct passwd".
struct passwd* pwd = getpwuid( getuid() );
------------------------^
cc: Warning: Lab2b.c, line 15: In the initializer for grp, "pwd" is a pointer to an incomplete struct or union and should not be used as the left operand of
mber dereference.
struct group* grp = getgrgid(pwd->pw_gid);
---------------------------------^
cc: Error: Lab2b.c, line 15: In the initializer for grp, "pw_gid" is not a member of "pwd".
struct group* grp = getgrgid(pwd->pw_gid);
---------------------------------^
The program I posted compiles/links on my Linux system that has GCC 4.2.0.
If you are using a different OS (e.g. Solaris, Windoze, etc.), then you may want to check if those library functions are available. Reference the man-page(s) or other documentation for your system.
Btw, I used the following statement to compile the program:
The program I posted compiles/links on my Linux system that has GCC 4.2.0.
If you are using a different OS (e.g. Solaris, Windoze, etc.), then you may want to check if those library functions are available. Reference the man-page(s) or other documentation for your system.
Btw, I used the following statement to compile the program:
I figured out the problem and its perfectly printing my user name but for the group name its giving core dump. So, if I ignore the printf statement for user's group its working fine but when I include printf statement for user's group I am getting core dump.
any idea how to fix this.
printf("user's name = %s\n", pwd->pw_name);
/*printf("user's group = %s\n", grp->gr_name);*/
I suspect you're seeing "0" simply because your system doesn't happen to have a user "1533" or a group "20". Or perhaps your login doesn't have permission to access the user database.
Either way, here's DWhitney67's example With (rudimentary) error checking:
Code:
#include <stdio.h>
#include <unistd.h>
#include <pwd.h>
#include <grp.h>
int main()
{
/* We read these values from "stat()" */
int user_id = 1533;
int group_id = 20;
/* We're going to get these strings from getpwuid() and getgrgid () */
struct passwd* pwd = getpwuid( getuid() );
struct group* grp = getgrgid(pwd->pw_gid);
if ((pwd = getpwuid (1533)) == NULL)
perror ("Unable to read user info");
else
printf("user's name = %s\n", pwd->pw_name);
if ((grp = getgrgid (group_id)) == NULL)
perror ("Unable to read group info");
else
printf("user's group = %s\n", grp->gr_name);
return 0;
}
My output is:
Quote:
Unable to read user info!: No such file or directory
user's group = games
Another problem is if you create a file created on a system where group id 20 happens to be "games", then copy that file to a second system, there's no guarantee that second system will have a group "games", nor that "games" will happen to be group ID 20.
In other words, the group id's and file id's in a file are completely non-portable between different hosts. Unless you "wrap" the files in a container like "tar", "cpio" or "zip" that contains owner and group info as "metadata".
Thanks a lot for all your help. Even I think the problem is with permissions. But, it was really great that I atleast found the way of doing it. I really appreciate your help.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.