#define _GNU_SOURCE

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <pwd.h>
#include <err.h>
#include <errno.h>

struct passwd *getpwnam2(const char *name);

int
main(int argc, char *argv[])
{
    struct passwd *pwd;

    if (argc != 2)
        errx(EXIT_FAILURE, "usage: %s username", program_invocation_name);

    if ((pwd = getpwnam2(argv[1])) == NULL)
        exit(EXIT_FAILURE);

    printf("%ld %s\n", (long) pwd->pw_uid, pwd->pw_name);
    exit(EXIT_SUCCESS);
}

struct passwd *
getpwnam2(const char *name)
{
    struct passwd *pwd;

    pwd = NULL;
    setpwent();
    while ((pwd = getpwent()) != NULL)
        if (strcmp(pwd->pw_name, name) == 0)
            break;
    endpwent();
    return pwd;
}
