Sponsored Content
Full Discussion: Help with getuid
Top Forums Programming Help with getuid Post 302893878 by Corona688 on Friday 21st of March 2014 12:07:27 PM
Old 03-21-2014
getuid is a kernel call which returns a number the process already had -- every process has a user. Looking up other users means opening /etc/passwd, which means using a library call like getpname, which searches the name, or getpwent, which loops over the entire /etc/passwd file.

Note that some of the fields in the structure it returns are obsolete -- especially password, which is now stored elsewhere.

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

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

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

 

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
FGETPWENT(3)						     Linux Programmer's Manual						      FGETPWENT(3)

NAME
fgetpwent - get password file entry SYNOPSIS
#include <pwd.h> #include <stdio.h> #include <sys/types.h> struct passwd *fgetpwent(FILE *stream); DESCRIPTION
The fgetpwent() function returns a pointer to a structure containing the broken out fields of a line in the file stream. The first time it is called it returns the first entry; thereafter, it returns successive entries. The file stream must have the same format as /etc/passwd. 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 fgetpwent() function returns the passwd structure, or NULL if there are no more entries or an error occurs. ERRORS
ENOMEM Insufficient memory to allocate passwd structure. FILES
/etc/passwd password database file CONFORMING TO
SVID 3 SEE ALSO
getpwnam(3), getpwuid(3), getpwent(3), setpwent(3), endpwent(3), getpw(3), putpwent(3), passwd(5) GNU
1996-05-17 FGETPWENT(3)
All times are GMT -4. The time now is 07:47 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy