]>
git.saurik.com Git - apple/libinfo.git/blob - gen.subproj/getpwent.c
2 * Copyright (c) 1999 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Portions Copyright (c) 1999 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.1 (the "License"). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
14 * The Original Code and all software distributed under the License are
15 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
16 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
17 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE OR NON- INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 * Copyright 1997 Apple Computer, Inc. (unpublished)
27 * /etc/passwd file access routines.
28 * Just read from the /etc/passwd file and skip the dbm database, since
29 * lookupd does all flat file lookups when the system is multi-user.
30 * These routines are only used in single-user mode.
32 * 17 Apr 1997 file created - Marc Majka
41 #define forever for (;;)
47 static struct passwd _pw
= { 0 };
49 static int _pwStayOpen
;
50 static int _pwFileFormat
= 1;
55 if (_pw
.pw_name
!= NULL
) free(_pw
.pw_name
);
56 if (_pw
.pw_passwd
!= NULL
) free(_pw
.pw_passwd
);
57 if (_pw
.pw_class
!= NULL
) free(_pw
.pw_class
);
58 if (_pw
.pw_gecos
!= NULL
) free(_pw
.pw_gecos
);
59 if (_pw
.pw_dir
!= NULL
) free(_pw
.pw_dir
);
60 if (_pw
.pw_shell
!= NULL
) free(_pw
.pw_shell
);
75 if (l
== NULL
) return;
76 for (i
= 0; l
[i
] != NULL
; i
++)
78 if (l
[i
] != NULL
) free(l
[i
]);
81 if (l
!= NULL
) free(l
);
89 if (l
== NULL
) return 0;
90 for (i
= 0; l
[i
] != NULL
; i
++);
100 if (s
== NULL
) return NULL
;
110 insertString(char *s
, char **l
, unsigned int x
)
114 if (s
== NULL
) return l
;
117 l
= (char **)malloc(2 * sizeof(char *));
118 l
[0] = copyString(s
);
123 for (i
= 0; l
[i
] != NULL
; i
++);
124 len
= i
+ 1; /* count the NULL on the end of the list too! */
126 l
= (char **)realloc(l
, (len
+ 1) * sizeof(char *));
128 if ((x
>= (len
- 1)) || (x
== (unsigned int)-1))
130 l
[len
- 1] = copyString(s
);
135 for (i
= len
; i
> x
; i
--) l
[i
] = l
[i
- 1];
136 l
[x
] = copyString(s
);
141 appendString(char *s
, char **l
)
143 return insertString(s
, l
, (unsigned int)-1);
148 tokenize(const char *data
, const char *sep
)
150 char **tokens
= NULL
;
156 if (data
== NULL
) return NULL
;
159 tokens
= appendString((char *)data
, tokens
);
169 /* skip leading white space */
170 while ((p
[0] == ' ') || (p
[0] == '\t') || (p
[0] == '\n')) p
++;
172 /* check for end of line */
173 if (p
[0] == '\0') break;
178 for (j
= 0; (j
< len
) && (scanning
== 1); j
++)
180 if (p
[0] == sep
[j
] || (p
[0] == '\0')) scanning
= 0;
183 while (scanning
== 1)
187 for (j
= 0; (j
< len
) && (scanning
== 1); j
++)
189 if (p
[0] == sep
[j
] || (p
[0] == '\0')) scanning
= 0;
193 /* back over trailing whitespace */
195 if (i
> -1) { /* did we actually copy anything? */
196 while ((buf
[i
] == ' ') || (buf
[i
] == '\t') || (buf
[i
] == '\n')) i
--;
200 tokens
= appendString(buf
, tokens
);
202 /* check for end of line */
203 if (p
[0] == '\0') break;
207 for (j
= 0; (j
< len
) && (scanning
== 1); j
++)
216 if ((scanning
== 0) && p
[0] == '\0')
218 /* line ended at a separator - add a null member */
219 tokens
= appendString("", tokens
);
227 parseUser(char *data
)
232 if (data
== NULL
) return NULL
;
234 tokens
= tokenize(data
, ":");
235 ntokens
= listLength(tokens
);
236 if (( _pwFileFormat
&& (ntokens
!= 10)) ||
237 (!_pwFileFormat
&& (ntokens
!= 7)))
245 _pw
.pw_name
= tokens
[0];
246 _pw
.pw_passwd
= tokens
[1];
247 _pw
.pw_uid
= atoi(tokens
[2]);
249 _pw
.pw_gid
= atoi(tokens
[3]);
254 _pw
.pw_class
= tokens
[4];
255 _pw
.pw_change
= atoi(tokens
[5]);
257 _pw
.pw_expire
= atoi(tokens
[6]);
259 _pw
.pw_gecos
= tokens
[7];
260 _pw
.pw_dir
= tokens
[8];
261 _pw
.pw_shell
= tokens
[9];
265 _pw
.pw_class
= copyString("");
268 _pw
.pw_gecos
= tokens
[4];
269 _pw
.pw_dir
= tokens
[5];
270 _pw
.pw_shell
= tokens
[6];
287 if (s
== NULL
|| s
[0] == '\0') return NULL
;
289 if (s
[0] != '#') s
[strlen(s
) - 1] = '\0';
296 setpassent(int stayopen
)
298 _pwStayOpen
= stayopen
;
310 pwFile
= _PATH_MASTERPASSWD
;
314 pwFile
= _PATH_PASSWD
;
317 _pfp
= fopen(pwFile
, "r");
339 static struct passwd
*
340 getpw(const char *nam
, uid_t uid
, int which
)
347 if (setpwent() == 0) return NULL
;
352 line
= getLine(_pfp
);
353 if (line
== NULL
) break;
362 pw
= parseUser(line
);
366 if ((pw
== NULL
) || (which
== _PWENT_
))
368 if (_pwStayOpen
== 0) endpwent();
372 if (((which
== _PWNAM_
) && (!strcmp(nam
, pw
->pw_name
))) ||
373 ((which
== _PWUID_
) && (uid
== pw
->pw_uid
)))
375 if (_pwStayOpen
== 0) endpwent();
380 if (_pwStayOpen
== 0) endpwent();
387 return getpw(NULL
, 0, _PWENT_
);
391 getpwnam(const char *nam
)
393 return getpw(nam
, 0, _PWNAM_
);
399 return getpw(NULL
, uid
, _PWUID_
);