Seth Woolley's Man Viewer

getgrent_r(3) - fgetgrent_r, getgrent_r, fgetgrent_r, getgrent_r - get group file entry reentrantly - man 3 getgrent_r

([section] manual, -k keyword, -K [section] search, -f whatis)
man plain no title

GETGRENT(3)                Linux Programmer's Manual               GETGRENT(3)



NAME
       getgrent_r, fgetgrent_r - get group file(1,n) entry reentrantly

SYNOPSIS
       #define _GNU_SOURCE
       #include <grp.h>

       int getgrent_r(struct group *gbuf, char *buf,
                      size_t buflen, struct group **gbufp);

       int fgetgrent_r(FILE *fp, struct group *gbuf, char *buf,
                       size_t buflen, struct group **gbufp);

DESCRIPTION
       The functions getgrent_r() and fgetgrent_r() are the reentrant versions
       of getgrent(3) and fgetgrent(3).  The former reads the next group entry
       from  the stream initialized by setgrent(3).  The latter reads the next
       group entry from the stream fp given as parameter.

       The group structure is defined in(1,8) <grp.h> as follows:

              struct group {
                    char    *gr_name;      /* group name */
                    char    *gr_passwd;    /* group password */
                    gid_t   gr_gid;        /* group id */
                    char    **gr_mem;      /* group members */
              };

       The non-reentrant functions return a pointer to static  storage,  where
       this  static  storage contains further pointers to group name, password
       and members.  The reentrant functions described here return all of that
       in(1,8)  caller-provided buffers. First of all there is the buffer gbuf that
       can hold a struct group. And next the buffer buf of  size  buflen  that
       can hold additional strings.  The result of these functions, the struct
       group read(2,n,1 builtins) from the stream, is stored in(1,8) the provided buffer *gbuf, and
       a pointer to this struct group is returned in(1,8) *gbufp.

RETURN VALUE
       On  success,  these  functions  return 0 and *gbufp is a pointer to the
       struct group.  On error(8,n), these functions  return  an  error(8,n)  value  and
       *gbufp is NULL.

ERRORS
       ENOENT No more entries.

       ERANGE Insufficient  buffer  space  supplied.  Try  again  with  larger
              buffer.

EXAMPLE
       #define _GNU_SOURCE
       #include <grp.h>
       #include <stdio.h>
       #define BUFLEN 4096

       int main() {
             struct group grp, *grpp;
             char buf[BUFLEN];
             int i;

             setgrent();
             while (1) {
                   i = getgrent_r(&grp, buf, BUFLEN, &grpp);
                   if(3,n) (i)
                         break;
                   printf(1,3,1 builtins)("%s (%d):", grpp->gr_name, grpp->gr_gid);
                   for (i = 0; ; i++) {
                         if(3,n) (grpp->gr_mem[i] == NULL)
                               break;
                         printf(1,3,1 builtins)(" %s", grpp->gr_mem[i]);
                   }
                   printf(1,3,1 builtins)("\n");
             }
             endgrent();
             return 0;
       }

CONFORMING TO
       These functions are GNU extensions, done  in(1,8)  a  style  resembling  the
       POSIX  version(1,3,5) of functions like getpwnam_r(3).  Other systems use pro-
       totype

           struct group *
           getgrent_r(struct group *grp, char *buf, int buflen);

       or, better,

           int
           getgrent_r(struct group *grp, char *buf, int buflen,
                      FILE **gr_fp);


NOTES
       The function getgrent_r() is not really reentrant since it  shares  the
       reading position in(1,8) the stream with all other threads.

SEE ALSO
       fgetgrent(3), getgrent(3), getgrgid(3), getgrnam(3), group(5)



GNU                               2003-11-15                       GETGRENT(3)

References for this manual (incoming links)