Click here to Skip to main content
15,899,825 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Code to retrieve list of users and do some manipulation

I'm novice to Linux, i need a snippet to

fetch the username from the user system in linux, the username must be the input.

I need to enter the username, if it is listed in the system, then it should display the username.


i got the command to list the existing user from the system

Otherwise it should throw the error.

Please advice me if missed out something.

Thanks in advance.
Posted
Comments
Sergey Alexandrovich Kryukov 29-Jun-12 14:49pm    
Why? especially for a novice...
--SA

Just read the file "/etc/passwd". It only makes sense for the local host passwords stored in this file, not in some external database like LDAP. Normally, to get access to this file you need to have administrative account and run your application using this file via sudo (do you know about sudo, by the way? I mean: http://en.wikipedia.org/wiki/Sudo[^]).

—SA
 
Share this answer
 
Use the getpwnam() or getpwnam_r() functions. They work independently of the method used to store the user information. The manual page[^] has example code.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900