OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [rtos/] [rtems/] [c/] [src/] [lib/] [libc/] [getgrent.c] - Blame information for rev 493

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 30 unneback
/*
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
 *  $Id: getgrent.c,v 1.2 2001-09-27 12:01:15 chris Exp $
9
 */
10
 
11
 
12
#include <stdio.h>
13
#include <sys/types.h>
14
#include <grp.h>
15
#include <errno.h>
16
#include <unistd.h>
17
#include <stdlib.h>
18
#include <string.h>
19
#include <limits.h>
20
 
21
static struct group gr_group;    /* password structure */
22
static FILE *group_fp;
23
 
24
/*
25
 *  The size of these buffers is arbitrary and there is no provision
26
 *  to protect any of them from overflowing.  The scanf patterns
27
 *  need to be changed to prevent overflowing.  In addition,
28
 *  the limits on these needs to be examined.
29
 */
30
 
31
static char groupname[8];
32
static char password[1024];
33
static char groups[1024];
34
static char *gr_mem[16] = { } ;
35
 
36
extern void init_etc_passwd_group(void);
37
 
38
int getgrnam_r(
39
  const char     *name,
40
  struct group   *grp,
41
  char           *buffer,
42
  size_t          bufsize,
43
  struct group  **result
44
)
45
{
46
  FILE *fp;
47
 
48
  init_etc_passwd_group();
49
 
50
  if ((fp = fopen ("/etc/group", "r")) == NULL) {
51
    errno = EINVAL;
52
    return -1;
53
  }
54
 
55
  while (fgets (buffer, bufsize, fp)) {
56
    sscanf (buffer, "%[^:]:%[^:]:%d:%s\n",
57
      groupname, password, (int *) &grp->gr_gid,
58
      groups);
59
    grp->gr_name = groupname;
60
    grp->gr_passwd = password;
61
    grp->gr_mem = gr_mem ;
62
 
63
    if (!strcmp (groupname, name)) {
64
      fclose (fp);
65
      *result = grp;
66
      return 0;
67
    }
68
  }
69
  fclose (fp);
70
  errno = EINVAL;
71
  return -1;
72
}
73
 
74
struct group *getgrnam(
75
  const char *name
76
)
77
{
78
  char   buf[1024];
79
  struct group *g;
80
 
81
  if ( getgrnam_r( name, &gr_group, buf, 1024, &g ) )
82
    return NULL;
83
 
84
  return g;
85
}
86
 
87
int getgrgid_r(
88
  gid_t           gid,
89
  struct group   *grp,
90
  char           *buffer,
91
  size_t          bufsize,
92
  struct group  **result
93
)
94
{
95
  FILE *fp;
96
 
97
  init_etc_passwd_group();
98
 
99
  if ((fp = fopen ("/etc/group", "r")) == NULL) {
100
    errno = EINVAL;
101
    return -1;
102
  }
103
 
104
  while (fgets (buffer, bufsize, fp)) {
105
    sscanf (buffer, "%[^:]:%[^:]:%d:%s\n",
106
      groupname, password, (int *) &gr_group.gr_gid,
107
      groups);
108
    gr_group.gr_name = groupname;
109
    gr_group.gr_passwd = password;
110
    gr_group.gr_mem = gr_mem ;
111
 
112
 
113
    if (gid == gr_group.gr_gid) {
114
      fclose (fp);
115
      *result = grp;
116
      return 0;
117
    }
118
  }
119
  fclose (fp);
120
  errno = EINVAL;
121
  return -1;
122
}
123
 
124
struct group *getgrgid (
125
  gid_t gid
126
)
127
{
128
  char   buf[1024];
129
  struct group *g;
130
 
131
  if ( getgrgid_r( gid, &gr_group, buf, 1024, &g ) )
132
    return NULL;
133
 
134
  return g;
135
}
136
 
137
struct group *getgrent( void )
138
{
139
  char buf[1024];
140
 
141
  if (group_fp == NULL)
142
    return NULL;
143
 
144
  if (fgets (buf, sizeof (buf), group_fp) == NULL)
145
    return NULL;
146
 
147
  sscanf (buf, "%[^:]:%[^:]:%d:%s\n",
148
    groupname, password, (int *) &gr_group.gr_gid,
149
    groups);
150
  gr_group.gr_name = groupname;
151
  gr_group.gr_passwd = password;
152
  gr_group.gr_mem = gr_mem ;
153
 
154
  return &gr_group;
155
}
156
 
157
void
158
setgrent ()
159
{
160
  init_etc_passwd_group();
161
 
162
  if (group_fp != NULL)
163
    fclose (group_fp);
164
 
165
  group_fp = fopen ("/etc/group", "r");
166
}
167
 
168
void
169
endgrent ()
170
{
171
  if (group_fp != NULL)
172
    fclose (group_fp);
173
}

powered by: WebSVN 2.1.0

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