source: rtems/cpukit/libcsupport/src/getgrent.c @ 258fd79

4.104.114.84.95
Last change on this file since 258fd79 was 258fd79, checked in by Joel Sherrill <joel.sherrill@…>, on 07/02/99 at 17:13:27

Password and group routines added by Ralf Corsepius <corsepiu@…>.

  • Property mode set to 100644
File size: 1.9 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
22struct group *
23getgrnam (name)
24     const char *name;
25{
26  FILE *fp;
27  char buf[1024];
28
29  if ((fp = fopen ("/etc/group", "r")) == NULL)
30    {
31      return NULL;
32    }
33
34  while (fgets (buf, sizeof (buf), fp))
35    {
36      sscanf (buf, "%[^:]:%[^:]:%d:%s\n",
37              groupname, password, &gr_group.gr_gid,
38              groups);
39      gr_group.gr_name = groupname;
40      gr_group.gr_passwd = password;
41      gr_group.gr_mem = gr_mem ;
42     
43      if (!strcmp (groupname, name))
44        {
45          fclose (fp);
46          return &gr_group;
47        }
48    }
49  fclose (fp);
50  return NULL;
51}
52
53struct group *
54getgrgid (gid_t gid)
55{
56  FILE *fp;
57  char buf[1024];
58
59  if ((fp = fopen ("/etc/group", "r")) == NULL)
60    {
61      return NULL;
62    }
63
64  while (fgets (buf, sizeof (buf), fp))
65    {
66      sscanf (buf, "%[^:]:%[^:]:%d:%s\n",
67              groupname, password, &gr_group.gr_gid,
68              groups);
69      gr_group.gr_name = groupname;
70      gr_group.gr_passwd = password;
71      gr_group.gr_mem = gr_mem ;
72
73
74      if (gid == gr_group.gr_gid)
75        {
76          fclose (fp);
77          return &gr_group;
78        }
79    }
80  fclose (fp);
81  return NULL;
82}
83
84struct group *
85getgrent ()
86{
87  char buf[1024];
88
89  if (group_fp == NULL)
90    return NULL;
91
92  if (fgets (buf, sizeof (buf), group_fp) == NULL)
93    return NULL;
94
95  sscanf (buf, "%[^:]:%[^:]:%d:%s\n",
96          groupname, password, &gr_group.gr_gid,
97          groups);
98  gr_group.gr_name = groupname;
99  gr_group.gr_passwd = password;
100  gr_group.gr_mem = gr_mem ;
101
102  return &gr_group;
103}
104
105void
106setgrent ()
107{
108  if (group_fp != NULL)
109    fclose (group_fp);
110
111  group_fp = fopen ("/etc/group", "r");
112}
113
114void
115endgrent ()
116{
117  if (group_fp != NULL)
118    fclose (group_fp);
119}
Note: See TracBrowser for help on using the repository browser.