xref: /original-bsd/lib/libcompat/4.1/getpw.c (revision c3e32dec)
1 /*-
2  * Copyright (c) 1980, 1993
3  *	The Regents of the University of California.  All rights reserved.
4  *
5  * %sccs.include.proprietary.c%
6  */
7 
8 #if defined(LIBC_SCCS) && !defined(lint)
9 static char sccsid[] = "@(#)getpw.c	8.1 (Berkeley) 06/04/93";
10 #endif /* LIBC_SCCS and not lint */
11 
12 #include	<stdio.h>
13 
14 getpw(uid, buf)
15 int uid;
16 char buf[];
17 {
18 	static FILE *pwf;
19 	register n, c;
20 	register char *bp;
21 
22 	if(pwf == 0)
23 		pwf = fopen("/etc/passwd", "r");
24 	if(pwf == NULL)
25 		return(1);
26 	rewind(pwf);
27 
28 	for (;;) {
29 		bp = buf;
30 		while((c=getc(pwf)) != '\n') {
31 			if(c == EOF)
32 				return(1);
33 			*bp++ = c;
34 		}
35 		*bp++ = '\0';
36 		bp = buf;
37 		n = 3;
38 		while(--n)
39 		while((c = *bp++) != ':')
40 			if(c == '\n')
41 				return(1);
42 		while((c = *bp++) != ':') {
43 			if(c<'0' || c>'9')
44 				continue;
45 			n = n*10+c-'0';
46 		}
47 		if(n == uid)
48 			return(0);
49 	}
50 }
51