source: rtems/cpukit/libcsupport/src/getgroups.c @ 1c6926c1

5
Last change on this file since 1c6926c1 was ffa71f1, checked in by Sebastian Huber <sebastian.huber@…>, on 11/17/14 at 06:53:01

libcsupport: Implement getgroups()

  • Property mode set to 100644
File size: 1.1 KB
Line 
1/**
2 *  @file
3 *
4 *  @brief Get Supplementary IDs
5 *  @ingroup libcsupport
6 */
7
8#if HAVE_CONFIG_H
9#include "config.h"
10#endif
11
12#include <sys/types.h>
13#include <grp.h>
14#include <pwd.h>
15#include <string.h>
16#include <unistd.h>
17
18#include <rtems/seterr.h>
19
20/**
21 *  4.2.3 Get Supplementary IDs, P1003.1b-1993, p. 86
22 */
23int getgroups(
24  int    gidsetsize,
25  gid_t  grouplist[]
26)
27{
28  int rv;
29  struct passwd pwd;
30  struct passwd *pwd_res;
31  char buf[256];
32  gid_t gid;
33  const char *user;
34  struct group *grp;
35
36  rv = getpwuid_r(getuid(), &pwd, &buf[0], sizeof(buf), &pwd_res);
37  if (rv != 0) {
38    return rv;
39  }
40
41  gid = pwd.pw_gid;
42  user = pwd.pw_name;
43
44  setgrent();
45
46  while ((grp = getgrent()) != NULL) {
47    char **mem = &grp->gr_mem[0];
48
49    if (grp->gr_gid == gid) {
50      continue;
51    }
52
53    while (*mem != NULL) {
54      if (strcmp(*mem, user) == 0) {
55        if (rv < gidsetsize) {
56          grouplist[rv] = grp->gr_gid;
57        }
58
59        ++rv;
60
61        break;
62      }
63
64      ++mem;
65    }
66  }
67
68  endgrent();
69
70  if (gidsetsize == 0 || rv <= gidsetsize) {
71    return rv;
72  } else {
73    rtems_set_errno_and_return_minus_one(EINVAL);
74  }
75}
Note: See TracBrowser for help on using the repository browser.