User Info

This is a discussion on User Info within the Linux Programming forums, part of the Platform Specific Boards category; Hi All, Just wondering if anyone could reccomend a way of getting user account into (name, homedir and any plan ...

  1. #1
    Registered User
    Join Date
    Dec 2003
    Posts
    68

    User Info

    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.

  2. #2
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  3. #3
    Registered User
    Join Date
    Dec 2003
    Posts
    68
    Thanks, that looks ot be just what I'm looking for! - Now to work out how to use it...

  4. #4
    Registered User
    Join Date
    Dec 2003
    Posts
    68
    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

  5. #5
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    I found this site I hope it helps
    here
    [EDIT]After more searching I have found this also[/EDIT]
    Here
    Last edited by linuxdude; 02-29-2004 at 12:46 AM.

  6. #6
    Registered User
    Join Date
    Dec 2003
    Posts
    68
    Thanks, just what I was looking for.

  7. #7
    Registered User
    Join Date
    Dec 2003
    Posts
    68
    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...

    Code:
    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?

  8. #8
    Me -=SoKrA=-'s Avatar
    Join Date
    Oct 2002
    Location
    Europe
    Posts
    448
    You can't compare strings like that.
    You should use strcmp from <string.h>, assuming you're using C.
    Code:
    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.
    Last edited by -=SoKrA=-; 02-29-2004 at 07:51 AM.
    SoKrA-BTS "Judge not the program I made, but the one I've yet to code"
    I say what I say, I mean what I mean.
    IDE: emacs + make + gcc and proud of it.

  9. #9
    Registered User
    Join Date
    Dec 2003
    Posts
    68
    Thanks! - It gets a bit much at times, learning PHP, C, Javascript and being told I may have to learn ASP (again) and .Net :/

  10. #10
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    You'll get it

  11. #11
    Registered User
    Join Date
    Mar 2004
    Posts
    44

    fj

    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

  12. #12
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    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.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Matching user info with data in file
    By RVDFan85 in forum C++ Programming
    Replies: 2
    Last Post: 12-12-2006, 12:40 AM
  2. pass error before char
    By bazzano in forum C Programming
    Replies: 3
    Last Post: 03-26-2006, 12:00 PM
  3. ~ User Input script help~
    By indy in forum C Programming
    Replies: 4
    Last Post: 12-02-2003, 06:01 AM
  4. strings making sure user has entered some info
    By marcus430 in forum C++ Programming
    Replies: 3
    Last Post: 11-22-2003, 07:58 AM
  5. Stopping a user from typeing.
    By knave in forum C++ Programming
    Replies: 4
    Last Post: 09-10-2001, 01:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21