Updated: April 19, 2023 |
Get information about a user with a given name
#include <sys/types.h> #include <shadow.h> struct spwd* getspnam( char* name ); struct spwd* getspnam_r( const char* name, struct spwd* result, char* buffer, size_t bufsize );
libc
Use the -l c option to qcc to link against this library. This library is usually included automatically.
The getspnam() and getspnam_r() functions allow a process to gain more knowledge about a user name. The getspnam() function uses a static buffer that's overwritten by each call.
A pointer to an object of type struct spwd containing an entry from the shadow file with a matching name, or NULL if an error occurred or the function couldn't find a matching entry.
#include <stdio.h> #include <stdlib.h> #include <pwd.h> #include <shadow.h> /* * Print information from the password entry * about the user name given as argv[1]. */ int main( int argc, char** argv ) { struct spwd* sp; if (argc < 2) { printf("%s username \n", argv[0]); return(EXIT_FAILURE); } if( ( sp = getspnam( argv[1] ) ) == (struct spwd*)0) { fprintf( stderr, "getspnam: unknown %s\n", argv[1] ); return( EXIT_FAILURE ); } printf( "login name %s\n", sp->sp_namp ); printf( "password %s\n", sp->sp_pwdp ); return( EXIT_SUCCESS ); }
Safety: | |
---|---|
Cancellation point | Yes |
Interrupt handler | No |
Signal handler | No |
Thread | No |
Safety: | |
---|---|
Cancellation point | Yes |
Interrupt handler | No |
Signal handler | No |
Thread | Yes |