PDA

View Full Version : User Info



DaveHope
02-28-2004, 07:56 AM
Hi All,
Just wondering if anyone could reccomend a way of getting user account into (name, homedir and any plan they may have) in C from a username. kinda like finger I guess. I thought of verifying a users homedir but that's not an option as users without a homedir would be excluded. The info would then be passed onto a series of GTK labels, but i'll worry about that later ;)

Thanks very much!

Dave.

Hammer
02-28-2004, 10:48 AM
Maybe something like http://www.rt.com/man/getpwent.3.html

DaveHope
02-28-2004, 11:40 AM
Thanks, that looks ot be just what I'm looking for! - Now to work out how to use it...

DaveHope
02-28-2004, 03:53 PM
Ok, I've been trying to find out some info on using getpwent for a good few hours now and have found nothing, would someone be kind enough to shove me in the right directtion with a link? - There seems to be little of use in the man pages :(

linuxdude
02-29-2004, 12:39 AM
I found this site I hope it helps
here (http://www.metalshell.com/view/source/107/)
After more searching I have found this also
Here (http://database.sarang.net/study/C/ANSI-C/cref/passwd.html)

DaveHope
02-29-2004, 03:24 AM
Thanks, just what I was looking for.

DaveHope
02-29-2004, 04:02 AM
I've just been toying with the code on metashell and it works fine if I just print the results straight away, but if I run a simple if statement nothing is returned...



ViewUserInfo() {
struct passwd *user;

/* open the user database */
setpwent();

/* read the user data one by one and print their names to stdout */
while( (user = getpwent())!=NULL )
if (user->pw_name == "root") {
printf("YAY\n");
}
// printf("%s\n", user->pw_dir);
// printf("%s\n", user->pw_name);

/* close the database */
endpwent();
}


Anyone happen to have an idea as to why?

-=SoKrA=-
02-29-2004, 07:44 AM
You can't compare strings like that.
You should use strcmp from <string.h>, assuming you're using C.


ViewUserInfo() {
struct passwd *user;

/* open the user database */
setpwent();

/* read the user data one by one and print their names to stdout */
while( (user = getpwent())!=NULL )
if (!strcmp(user->pw_name, "root")) {
printf("YAY\n");
}
// printf("%s\n", user->pw_dir);
// printf("%s\n", user->pw_name);

/* close the database */
endpwent();
}

The ! is there because it will return -1 if the first one is "bigger", 0 if they're equal and 1 if the second one is "bigger". I can't remember what is the comparison it does right now, but you're looking for it to return a 0.
EDIT: Fixed a typo.
Also, with the if statement you had, it was comparing one pointer to another, and they'll never be equal in this function.

DaveHope
02-29-2004, 07:51 AM
Thanks! - It gets a bit much at times, learning PHP, C, Javascript and being told I may have to learn ASP (again) and .Net :/

linuxdude
02-29-2004, 05:33 PM
You'll get it;)

kwm32
03-26-2004, 05:01 PM
you do not need to put yourself through all that bull$$$$ just use echo and the $user but that is only if you are logged on to that account so you might have to put yourself through all that bull$$$$ and also the most common working directory is /home/user just to let you know hope that helped

XSquared
03-26-2004, 07:28 PM
a) Watch the language.
b) Don't bump month-old posts.
c) He wanted to be able to get info about any user, not just the currently logged in one.