OpenCores
URL https://opencores.org/ocsvn/or1k/or1k/trunk

Subversion Repositories or1k

[/] [or1k/] [trunk/] [rtems-20020807/] [cpukit/] [libcsupport/] [src/] [getgrent.c] - Blame information for rev 1765

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 1026 ivang
/*
2
 *  POSIX 1003.1b - 9.2.1 - Group Database Access Routines
3
 *
4
 *  The license and distribution terms for this file may be
5
 *  found in the file LICENSE in this distribution or at
6
 *  http://www.OARcorp.com/rtems/license.html.
7
 *
8
 *  getgrent.c,v 1.6 2001/08/09 22:11:19 joel Exp
9
 */
10
 
11
#if HAVE_CONFIG_H
12
#include "config.h"
13
#endif
14
 
15
#include <stdio.h>
16
#include <sys/types.h>
17
#include <grp.h>
18
#include <errno.h>
19
#include <unistd.h>
20
#include <stdlib.h>
21
#include <string.h>
22
#include <limits.h>
23
 
24
#include <rtems/libio_.h>
25
 
26
 
27
static struct group gr_group;    /* password structure */
28
static FILE *group_fp;
29
 
30
/*
31
 *  The size of these buffers is arbitrary and there is no provision
32
 *  to protect any of them from overflowing.  The scanf patterns
33
 *  need to be changed to prevent overflowing.  In addition,
34
 *  the limits on these needs to be examined.
35
 */
36
 
37
static char groupname[8];
38
static char password[1024];
39
static char groups[1024];
40
static char *gr_mem[16] = { } ;
41
 
42
extern void init_etc_passwd_group(void);
43
 
44
int getgrnam_r(
45
  const char     *name,
46
  struct group   *grp,
47
  char           *buffer,
48
  size_t          bufsize,
49
  struct group  **result
50
)
51
{
52
  FILE *fp;
53
  rtems_user_env_t * aux=rtems_current_user_env; /* save */
54
 
55
  init_etc_passwd_group();
56
  rtems_current_user_env=&rtems_global_user_env; /* set root */
57
 
58
  if ((fp = fopen ("/etc/group", "r")) == NULL) {
59
    errno = EINVAL;
60
    rtems_current_user_env=aux; /* restore */
61
    return -1;
62
  }
63
 
64
  while (fgets (buffer, bufsize, fp)) {
65
    sscanf (buffer, "%[^:]:%[^:]:%d:%s\n",
66
      groupname, password, (int *) &grp->gr_gid,
67
      groups);
68
    grp->gr_name = groupname;
69
    grp->gr_passwd = password;
70
    grp->gr_mem = gr_mem ;
71
 
72
    if (!strcmp (groupname, name)) {
73
      fclose (fp);
74
      *result = grp;
75
      rtems_current_user_env=aux; /* restore */
76
      return 0;
77
    }
78
  }
79
  fclose (fp);
80
  errno = EINVAL;
81
  rtems_current_user_env=aux; /* restore */
82
  return -1;
83
}
84
 
85
struct group *getgrnam(
86
  const char *name
87
)
88
{
89
  char   buf[1024];
90
  struct group *g;
91
 
92
  if ( getgrnam_r( name, &gr_group, buf, 1024, &g ) )
93
    return NULL;
94
 
95
  return g;
96
}
97
 
98
int getgrgid_r(
99
  gid_t           gid,
100
  struct group   *grp,
101
  char           *buffer,
102
  size_t          bufsize,
103
  struct group  **result
104
)
105
{
106
  FILE *fp;
107
  rtems_user_env_t * aux=rtems_current_user_env; /* save */
108
 
109
 
110
  init_etc_passwd_group();
111
  rtems_current_user_env=&rtems_global_user_env; /* set root */
112
 
113
  if ((fp = fopen ("/etc/group", "r")) == NULL) {
114
    errno = EINVAL;
115
    rtems_current_user_env=aux; /* restore */
116
    return -1;
117
  }
118
 
119
  while (fgets (buffer, bufsize, fp)) {
120
    sscanf (buffer, "%[^:]:%[^:]:%d:%s\n",
121
      groupname, password, (int *) &gr_group.gr_gid,
122
      groups);
123
    gr_group.gr_name = groupname;
124
    gr_group.gr_passwd = password;
125
    gr_group.gr_mem = gr_mem ;
126
 
127
 
128
    if (gid == gr_group.gr_gid) {
129
      fclose (fp);
130
      *result = grp;
131
      rtems_current_user_env=aux; /* restore */
132
      return 0;
133
    }
134
  }
135
  fclose (fp);
136
  errno = EINVAL;
137
  rtems_current_user_env=aux; /* restore */
138
  return -1;
139
}
140
 
141
struct group *getgrgid (
142
  gid_t gid
143
)
144
{
145
  char   buf[1024];
146
  struct group *g;
147
 
148
  if ( getgrgid_r( gid, &gr_group, buf, 1024, &g ) )
149
    return NULL;
150
 
151
  return g;
152
}
153
 
154
struct group *getgrent( void )
155
{
156
  char buf[1024];
157
 
158
  if (group_fp == NULL)
159
    return NULL;
160
 
161
  if (fgets (buf, sizeof (buf), group_fp) == NULL)
162
    return NULL;
163
 
164
  sscanf (buf, "%[^:]:%[^:]:%d:%s\n",
165
    groupname, password, (int *) &gr_group.gr_gid,
166
    groups);
167
  gr_group.gr_name = groupname;
168
  gr_group.gr_passwd = password;
169
  gr_group.gr_mem = gr_mem ;
170
 
171
  return &gr_group;
172
}
173
 
174
void
175
setgrent ()
176
{
177
  rtems_user_env_t * aux=rtems_current_user_env; /* save */
178
  init_etc_passwd_group();
179
  rtems_current_user_env=&rtems_global_user_env; /* set root */
180
 
181
  if (group_fp != NULL)
182
    fclose (group_fp);
183
 
184
  group_fp = fopen ("/etc/group", "r");
185
  rtems_current_user_env=aux; /* restore */
186
}
187
 
188
void
189
endgrent ()
190
{
191
  if (group_fp != NULL)
192
    fclose (group_fp);
193
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.