Help with getuid


 
Thread Tools Search this Thread
Top Forums Programming Help with getuid
# 1  
Old 03-21-2014
Help with getuid

I'm not that acquainted to C programming and would like to know how to obtain the internal Unix userid of a user (I'm on HP UX) and stro it in a variable.

I found the getuid() fonction returns the current user's internal ID. But I would like to find it for a different user. I was hoping something like this would work, but it doesn't: UserID = getuid("jsmith").

Please help.

Thanks.
# 2  
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);
}

# 3  
Old 03-21-2014
Thank you Cororona688. I tried compiling your code snipit but I get errors. My C compiler (ie: I'm running HP-UX) is probably incompatible.

How about this:
- I know the Unix command to obtain a user's internal userid on HP-UX is: id -u <userid> . This command returns an integer representing the internal userid.
- I know the C system() function allows running OS commands.
- I don't know how to run the system() function and save the output in a variable. I wish I could just do this: UserId = system("id -u jsmith");
- Can someone show me the correct C syntax for storing the output of this Unix command into a variable in my C program?
Thanks
# 4  
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:
# 5  
Old 03-21-2014
Google for getent.c download
Look at the passwd section.
Especially the BSD sources are quite portable.
# 6  
Old 03-21-2014
Coronna688 - Sorry for not posting the errors. This actually works now!
The GID is the value I was looking for.
Thanks a bunch :-)

MadeInGermany - I didn't know how to read Unix environment variables. Now I do. Thanks!

Let me push my luck on that last question I had:
What's a simple way to run a Unix command and save the echoed value in a variable inside my C program ?
# 7  
Old 03-21-2014
Do you want the program to be run before the C program, or from inside the C program?

From outside:
Code:
cat <<EOF >arg.c
#include <stdio.h>
int main(int argc, char *argv[])
{
        printf("argv[1]=%s\n", argv[1]);
        return(0);
}
EOF

gcc arg.c -o arg

./arg "$(command which prints something)"

From inside:

Code:
#include <stdio.h>
#include <stdlib.h>

int main()
{
       char buf[512];
        FILE *fp=popen("ls", "r");
        while(fgets(buf, 512, fp)) printf("Got line '%s'\n", buf);

        pclose(fp);
}


Last edited by Corona688; 03-21-2014 at 03:43 PM..
Login or Register to Ask a Question

Previous Thread | Next Thread

1 More Discussions You Might Find Interesting

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
Login or Register to Ask a Question