Updated: April 19, 2023 |
Get the next entry from the password database
#include <sys/types.h> #include <pwd.h> struct passwd* getpwent( void );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The getpwent() function returns the next entry from the password database. This function uses a static buffer that's overwritten by each call.
When getpwent() is first called, the password database is opened, and remains open until either a NULL is returned to signify end-of-file, or endpwent() is called.
A pointer to an object of type struct passwd containing the next entry from the password database. When getpwent() reaches the end of the database, it returns NULL but doesn't change the value of errno; on error, it returns NULL and sets errno.
The getpwent() function uses the following functions, and as a result, errno can be set to an error for any of these calls:
/* * This program loops, reading a login name from standard * input and checking to see if it is a valid name. If it * is not valid, the entire contents of the name in the * password database are printed. */ #include <stdio.h> #include <stdlib.h> #include <sys/types.h> #include <pwd.h> int main( void ) { struct passwd* pw; char buf[80]; setpwent( ); while( gets( buf ) != NULL ) { if( ( pw = getpwnam( buf ) ) != ( struct passwd * )0 ) { printf( "Valid login name is: %s\n", pw->pw_name ); } else { setpwent( ); while( ( pw=getpwent( ) ) != ( struct passwd * )0 ) printf( "%s\n", pw->pw_name ); } } endpwent(); return( EXIT_SUCCESS ); }
Safety: | |
---|---|
Cancellation point | Yes |
Interrupt handler | No |
Signal handler | No |
Thread | No |