Mon Jun 3 21:03:54 1996 Roland McGrath <roland@delasyd.gnu.ai.mit.edu>
[glibc.git] / pwd / pwdread.c
blob12032367fa54bbabab76bf3c0aadf6097319040e
1 /* Copyright (C) 1991, 1992, 1995 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
19 #include <ansidecl.h>
20 #include <errno.h>
21 #include <limits.h>
22 #include <stddef.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <pwd.h>
27 #include <sys/types.h>
30 /* This is the function that all the others are based on.
31 The format of the password file is known only here. */
33 /* Structure containing info kept by each __pwdread caller. */
34 typedef struct
36 char *buf;
37 size_t buflen;
38 struct passwd p;
39 } pwdread_info;
42 /* Return a chunk of memory containing a pre-initialized `pwdread_info'. */
43 PTR
44 DEFUN_VOID(__pwdalloc)
46 pwdread_info *info = (PTR) malloc (sizeof(pwdread_info));
47 if (info == NULL)
48 return NULL;
49 info->buf = NULL;
50 info->buflen = 0;
51 return info;
54 /* Read a password entry from STREAM, filling in P. */
55 struct passwd *
56 DEFUN(__pwdread, (stream, p), FILE *stream AND PTR CONST p)
58 register pwdread_info *CONST info = (pwdread_info *) p;
59 char *start, *end;
61 /* Idiocy checks. */
62 if (stream == NULL)
64 errno = EINVAL;
65 return NULL;
69 if (__getline (&info->buf, &info->buflen, stream) == -1)
70 return NULL;
71 while (info->buf[0] == '#');
73 start = info->buf;
74 end = strchr (start, ':');
75 if (end == NULL)
76 return NULL;
77 *end = '\0';
78 info->p.pw_name = start;
80 start = end + 1;
81 end = strchr (start, ':');
82 if (end == NULL)
83 return NULL;
84 *end = '\0';
85 info->p.pw_passwd = start;
87 info->p.pw_uid = (uid_t) strtol (end + 1, &end, 10);
88 if (*end != ':')
89 return NULL;
90 info->p.pw_gid = (gid_t) strtol (end + 1, &end, 10);
91 if (*end != ':')
92 return NULL;
94 start = end + 1;
95 end = strchr (start, ':');
96 if (end == NULL)
97 return NULL;
98 *end = '\0';
99 info->p.pw_gecos = start;
101 start = end + 1;
102 end = strchr (start, ':');
103 if (end == NULL)
104 return NULL;
105 *end = '\0';
106 info->p.pw_dir = start;
108 start = end + 1;
109 end = strchr (start, '\n');
110 if (end == NULL)
111 return NULL;
112 *end = '\0';
113 info->p.pw_shell = start;
115 return &info->p;
119 struct passwd *
120 __pwdscan (void **info, int (*selector) (struct passwd *))
122 FILE *stream;
123 struct passwd *p;
125 if (*info == NULL)
127 *info = __pwdalloc ();
128 if (info == NULL)
129 return NULL;
132 stream = __pwdopen ();
133 if (stream == NULL)
134 return NULL;
136 p = NULL;
137 while (! feof (stream))
139 p = __pwdread (stream, *info);
140 if (p && (*selector) (p))
141 break;
142 p = NULL;
145 (void) fclose (stream);
146 return p;