Sponsored Content
Full Discussion: Help with getuid
Top Forums Programming Help with getuid Post 302893897 by Corona688 on Friday 21st of March 2014 02:06:53 PM
Old 03-21-2014
"Has errors" is not really a useful thing to say. What errors it had would've been much more helpful. If you'd posted those errors, someone else could tell I'd misspelled some function names -- not to mention some other errors in my code, which was written in a real hurry. Sorry about that, this one actually compiles:

Try this:
Code:
#include <sys/types.h>
#include <pwd.h>
#include <stdio.h>

int main(void) {
        const char *user="user";
        struct passwd *p=getpwnam(user);

        if(p != NULL)
                printf("name %s UID %d GID %d\n", user, (int)p->pw_name, p->pw_uid, (int)p->pw_gid);
        else fprintf(stderr, "No such user '%s'\n", user);
}


Last edited by Corona688; 03-21-2014 at 03:21 PM..
This User Gave Thanks to Corona688 For This Post:
 

We Also Found This Discussion For You

1. Programming

how to write a wrapper c code to return uid using getuid() function

And how to use setuid() ? thanks (4 Replies)
Discussion started by: pwd
4 Replies
GETPWNAM(3)						     Linux Programmer's Manual						       GETPWNAM(3)

NAME
getpwnam, getpwuid - get password file entry SYNOPSIS
#include <pwd.h> #include <sys/types.h> struct passwd *getpwnam(const char *name); struct passwd *getpwuid(uid_t uid); DESCRIPTION
The getpwnam() function returns a pointer to a structure containing the broken out fields of a line from /etc/passwd for the entry that matches the user name name. The getpwuid() function returns a pointer to a structure containing the broken out fields of a line from /etc/passwd for the entry that matches the user uid uid. The passwd structure is defined in <pwd.h> as follows: struct passwd { char *pw_name; /* user name */ char *pw_passwd; /* user password */ uid_t pw_uid; /* user id */ gid_t pw_gid; /* group id */ char *pw_gecos; /* real name */ char *pw_dir; /* home directory */ char *pw_shell; /* shell program */ }; RETURN VALUE
The getpwnam() and getpwuid() functions return a pointer to the passwd structure, or NULL if the matching entry is not found or an error occurs. ERRORS
ENOMEM Insufficient memory to allocate passwd structure. FILES
/etc/passwd password database file CONFORMING TO
SVID 3, POSIX, BSD 4.3 SEE ALSO
fgetpwent(3), getgrnam(3), getpwent(3), setpwent(3), endpwent(3), getpw(3), putpwent(3), passwd(5) GNU
1996-05-27 GETPWNAM(3)
All times are GMT -4. The time now is 07:02 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy