passwd_info is a simple program that can query the /etc/passwd file for current user or specified user.
USAGE: passwd_info [username]
Sample:
#include < unistd.h >
#include < stdlib.h >
#include < stdio.h >
#include < sys/types.h >
#include < pwd.h >
#include < string.h >
struct passwd *pw;
void print_usage(void);
int main( int argc, char *argv[] )
{
if ( argc > 2 ) {
print_usage();
exit(1);
}
char *name;
uid_t uid;
gid_t gid;
char *gecos;
char *dir;
char *shell;
char *user_arg;
if ( argv[1] != NULL ) {
user_arg=(char *)malloc(strlen(argv[1]));
strcpy( user_arg, argv[1] );
if ( (pw = getpwnam(user_arg) ) == NULL ) {
fprintf( stderr, "Error: user %s does not existn", user_arg );
exit(1);
}
uid = pw->pw_uid;
free(user_arg);
}
else /* get for current user */
{
uid = getuid();
pw = getpwuid(uid);
}
name = pw->pw_name;
gid = pw->pw_gid;
gecos = pw->pw_gecos;
dir = pw->pw_dir;
shell = pw->pw_shell;
printf( "/etc/passwd file information for %sn", name );
printf( "Username = %stUID = %dtGID = %dn", name, (int)uid, (int)gid );
printf( "GECOS = %snHome = %stShell = %sn", gecos, dir, shell );
exit(0);
}
void print_usage( void )
{
fprintf( stderr, "USAGE: passwd_info [username]n" );
}
Product's homepage