source: rtems/c/src/lib/libc/getgrent.c @ c51917f3

4.104.114.84.95
Last change on this file since c51917f3 was c51917f3, checked in by Joel Sherrill <joel.sherrill@…>, on 07/02/99 at 18:09:03

Fixed format strings and warnings.

  • Property mode set to 100644
File size: 2.5 KB
Line 
1/*
2 *  $Id$
3 */
4
5#include <stdio.h>
6#include <sys/types.h>
7#include <grp.h>
8#include <errno.h>
9#include <unistd.h>
10#include <stdlib.h>
11#include <string.h>
12#include <limits.h>
13
14static struct group gr_group;    /* password structure */
15static FILE *group_fp;
16
17static char groupname[8];
18static char password[1024];
19static char groups[1024];
20static char *gr_mem[16] = { } ;
21
22int getgrnam_r(
23  const char     *name,
24  struct group   *grp,
25  char           *buffer,
26  size_t          bufsize,
27  struct group  **result
28)
29{
30  FILE *fp;
31
32  if ((fp = fopen ("/etc/group", "r")) == NULL) {
33    errno = EINVAL;
34    return -1;
35  }
36
37  while (fgets (buffer, bufsize, fp)) {
38    sscanf (buffer, "%[^:]:%[^:]:%d:%s\n",
39      groupname, password, (int *) &grp->gr_gid,
40      groups);
41    grp->gr_name = groupname;
42    grp->gr_passwd = password;
43    grp->gr_mem = gr_mem ;
44   
45    if (!strcmp (groupname, name)) {
46      fclose (fp);
47      *result = grp;
48      return 0;
49    }
50  }
51  fclose (fp);
52  errno = EINVAL;
53  return -1;
54}
55
56struct group *getgrnam(
57  const char *name
58)
59{
60  char   buf[1024];
61  struct group *g;
62
63  if ( getgrnam_r( name, &gr_group, buf, 1024, &g ) )
64    return NULL;
65
66  return g;
67}
68
69int getgrgid_r(
70  gid_t           gid,
71  struct group   *grp,
72  char           *buffer,
73  size_t          bufsize,
74  struct group  **result
75)
76{
77  FILE *fp;
78
79  if ((fp = fopen ("/etc/group", "r")) == NULL) {
80    errno = EINVAL;
81    return -1;
82  }
83
84  while (fgets (buffer, bufsize, fp)) {
85    sscanf (buffer, "%[^:]:%[^:]:%d:%s\n",
86      groupname, password, (int *) &gr_group.gr_gid,
87      groups);
88    gr_group.gr_name = groupname;
89    gr_group.gr_passwd = password;
90    gr_group.gr_mem = gr_mem ;
91
92
93    if (gid == gr_group.gr_gid) {
94      fclose (fp);
95      *result = grp;
96      return 0;
97    }
98  }
99  fclose (fp);
100  errno = EINVAL;
101  return -1;
102}
103
104struct group *getgrgid (
105  gid_t gid
106)
107{
108  char   buf[1024];
109  struct group *g;
110
111  if ( getgrgid_r( gid, &gr_group, buf, 1024, &g ) )
112    return NULL;
113
114  return g;
115}
116
117struct group *getgrent( void )
118{
119  char buf[1024];
120
121  if (group_fp == NULL)
122    return NULL;
123
124  if (fgets (buf, sizeof (buf), group_fp) == NULL)
125    return NULL;
126
127  sscanf (buf, "%[^:]:%[^:]:%d:%s\n",
128    groupname, password, (int *) &gr_group.gr_gid,
129    groups);
130  gr_group.gr_name = groupname;
131  gr_group.gr_passwd = password;
132  gr_group.gr_mem = gr_mem ;
133
134  return &gr_group;
135}
136
137void
138setgrent ()
139{
140  if (group_fp != NULL)
141    fclose (group_fp);
142
143  group_fp = fopen ("/etc/group", "r");
144}
145
146void
147endgrent ()
148{
149  if (group_fp != NULL)
150    fclose (group_fp);
151}
Note: See TracBrowser for help on using the repository browser.