Arthur de Jong

Open Source / Free Software developer

summaryrefslogtreecommitdiffstats
path: root/tests/lookup_groupbyuser.c
blob: a9796ea426d4824550155bcd2a2d0230da35d975 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
/*
   lookup_groupbyuser.c - simple lookup for groups by user

   Copyright (C) 2014 Arthur de Jong

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 2.1 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General Public
   License along with this library; if not, write to the Free Software
   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
   02110-1301 USA
*/

#include "config.h"

#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <grp.h>

#ifndef HAVE_GETGROUPLIST
/* dummy implementation that for systems without getgrouplist() */
int main(int argc,char *argv[])
{
  fprintf(stderr, "%s: getgrouplist() not available\n", argv[0]);
  return 1;
}
#else /* HAVE_GETGROUPLIST */

/* the main program... */
int main(int argc,char *argv[])
{
  gid_t groups[1024];
  int ngroups = sizeof(groups);
  int i;
  /* check arguments */
  if ((argc != 1) && (argc != 2))
  {
    fprintf(stderr, "Usage: %s [USERNAME]\n", argv[0]);
    exit(EXIT_FAILURE);
  }
  /* start lookup */
  if (getgrouplist(argv[1], (gid_t)-1, groups, &ngroups) < 0)
  {
    fprintf(stderr, "getgrouplist() failed (%d entries would be returned)\n",
            ngroups);
    exit(EXIT_FAILURE);
  }
  /* print results */
  printf("user=%s groups=", argv[1]);
  for (i = 0; i < ngroups; i++)
  {
    if (groups[i] != (gid_t)-1)
    {
      if (i > 0)
        printf(",");
      printf("%d", groups[i]);
    }
  }
  printf("\n");
  return 0;
}

#endif /* HAVE_GETGROUPLIST */