VMS Help  —  CRTL  getpwnam  Example
      When building a sample program with /def=_USE_STD_STAT, you can
      observe the following:

      o  When the DECC$POSIX_STYLE_UID logical is enabled:

         -  For a system, that supports POSIX style identifiers:

            - getpwnam_r API reads information from the TCP/IP proxy
            database and fills UID and GID with values from the
            TCP/IP proxy database.

            - getgrgid_r API returns gr_name and gr_mem from
            the right's database associated with GID returned by
            getpwnam_r API.

         -  System with no support for POSIX style identifiers,
            getpwnam_r fills GID and UID with SYSGEN parameters as
            "DEFUID" and "DEFGID".

      o  When the DECC$POSIX_STYLE_UID logical is not defined:

         getpwnam function returns information about a user database
         entry for the specified name, which is specified in
         SYSUAF.DAT

        #include <unistd>  // getuid()
        #include <pwd>     // getpwuid_r()
        #include <grp>
        #include <errno.h>
        #include <stdio.h>
        #include <string.h>

        main()
        {

        struct passwd pwd2;
        const unsigned int PWD_BUFF_SIZE = 1024;
        const unsigned int GRP_BUFF_SIZE = 1024;

        struct passwd *p_passwd;
        struct passwd *result;
        struct group *grpresult;
        struct group grp;
        char pwdBuffer[PWD_BUFF_SIZE],*name;
        char grpBuffer[GRP_BUFF_SIZE];
        char buf[PWD_BUFF_SIZE];

        gid_t gid;
        uid_t uid;

        int status;
        p_passwd = getpwnam("user1");
        uid=p_passwd->pw_uid;
        gid=p_passwd->pw_gid;

        printf("User id is %u\n", uid);
        printf("Group id is %u\n", gid);

        status = getpwnam_r("user1", &pwd2, pwdBuffer, PWD_BUFF_SIZE, &result);

        gid = pwd2.pw_gid;

        status = getgrgid_r(gid, &grp, grpBuffer, GRP_BUFF_SIZE, &grpresult);

        gid=grp.gr_gid; name=grp.gr_name;

        strcpy(name,grp.gr_name);

        printf("Group id is %u\n", gid);
        printf("Group name is %s\n", name);

        }

      Running the example program with /def=_USE_STD_STAT produces
      the following result:

      o  When the DECC$POSIX_STYLE_UID logical is NOT enabled, prints
         uid as 11010118 (result of 65536*168+ 70) and gid as 168
         with group name as RTL.

      o  When the DECC$POSIX_STYLE_UID logical is enabled and POSIX
         style identifiers are supported, prints uid as 70, gid as
         168 with group name as FOR_POSIX_TEST (retrieved from TCP/IP
         proxy database).

      o  When the DECC$POSIX_STYLE_UID logical is enabled, but POSIX
         style identifiers are not supported, prints uid as DEFUID,
         gid as DEFGID with group name as invalid buffer.
Close Help