source: rtems/c/src/lib/libc/getgrent.c @ 5c27c80

4.104.114.84.95
Last change on this file since 5c27c80 was 9c49db4, checked in by Joel Sherrill <joel.sherrill@…>, on 01/08/01 at 18:26:44

2001-01-08 Ralf Corsepius <corsepiu@…>

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