summaryrefslogtreecommitdiffstats
path: root/c/src/exec/libcsupport/src/getgrent.c
blob: b47ece8283886f03bed67eb37d57b3ae9600cc41 (plain) (blame)
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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
/*
 *  $Id$
 */

#include <stdio.h>
#include <sys/types.h>
#include <grp.h>
#include <errno.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <limits.h>

static struct group gr_group;    /* password structure */
static FILE *group_fp;

static char groupname[8];
static char password[1024];
static char groups[1024];
static char *gr_mem[16] = { } ;

int getgrnam_r(
  const char     *name,
  struct group   *grp,
  char           *buffer,
  size_t          bufsize,
  struct group  **result
)
{
  FILE *fp;

  if ((fp = fopen ("/etc/group", "r")) == NULL) {
    errno = EINVAL;
    return -1;
  }

  while (fgets (buffer, bufsize, fp)) {
    sscanf (buffer, "%[^:]:%[^:]:%d:%s\n",
      groupname, password, (int *) &grp->gr_gid,
      groups);
    grp->gr_name = groupname;
    grp->gr_passwd = password;
    grp->gr_mem = gr_mem ;
    
    if (!strcmp (groupname, name)) {
      fclose (fp);
      *result = grp;
      return 0;
    }
  }
  fclose (fp);
  errno = EINVAL;
  return -1;
}

struct group *getgrnam(
  const char *name
)
{
  char   buf[1024];
  struct group *g;

  if ( getgrnam_r( name, &gr_group, buf, 1024, &g ) )
    return NULL;

  return g;
}

int getgrgid_r(
  gid_t           gid,
  struct group   *grp,
  char           *buffer,
  size_t          bufsize,
  struct group  **result
)
{
  FILE *fp;

  if ((fp = fopen ("/etc/group", "r")) == NULL) {
    errno = EINVAL;
    return -1;
  }

  while (fgets (buffer, bufsize, fp)) {
    sscanf (buffer, "%[^:]:%[^:]:%d:%s\n",
      groupname, password, (int *) &gr_group.gr_gid,
      groups);
    gr_group.gr_name = groupname;
    gr_group.gr_passwd = password;
    gr_group.gr_mem = gr_mem ;


    if (gid == gr_group.gr_gid) {
      fclose (fp);
      *result = grp;
      return 0;
    }
  }
  fclose (fp);
  errno = EINVAL;
  return -1;
}

struct group *getgrgid (
  gid_t gid
)
{
  char   buf[1024];
  struct group *g;

  if ( getgrgid_r( gid, &gr_group, buf, 1024, &g ) )
    return NULL;

  return g;
}

struct group *getgrent( void )
{
  char buf[1024];

  if (group_fp == NULL)
    return NULL;

  if (fgets (buf, sizeof (buf), group_fp) == NULL)
    return NULL;

  sscanf (buf, "%[^:]:%[^:]:%d:%s\n",
    groupname, password, (int *) &gr_group.gr_gid,
    groups);
  gr_group.gr_name = groupname;
  gr_group.gr_passwd = password;
  gr_group.gr_mem = gr_mem ;

  return &gr_group;
}

void
setgrent ()
{
  if (group_fp != NULL)
    fclose (group_fp);

  group_fp = fopen ("/etc/group", "r");
}

void
endgrent ()
{
  if (group_fp != NULL)
    fclose (group_fp);
}