source: rtems/c/src/lib/libc/getgrent.c @ 94a86c40

4.104.114.84.95
Last change on this file since 94a86c40 was a0a225f, checked in by Joel Sherrill <joel.sherrill@…>, on 07/02/99 at 18:50:40

Added code to initialize the /etc/group and /etc/passwd files.

  • Property mode set to 100644
File size: 3.1 KB
Line 
1/*
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$
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
21static struct group gr_group;    /* password structure */
22static 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
31static char groupname[8];
32static char password[1024];
33static char groups[1024];
34static char *gr_mem[16] = { } ;
35
36extern void init_etc_passwd_group(void);
37
38int 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
74struct 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
87int 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
124struct 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
137struct 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
157void
158setgrent ()
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
168void
169endgrent ()
170{
171  if (group_fp != NULL)
172    fclose (group_fp);
173}
Note: See TracBrowser for help on using the repository browser.