xref: /titanic_44/usr/src/tools/cscope-fast/logdir.c (revision 843e19887f64dde75055cf8842fc4db2171eff45)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*	Copyright (c) 1988 AT&T	*/
23 /*	  All Rights Reserved  	*/
24 
25 
26 /*
27  * Copyright (c) 1999 by Sun Microsystems, Inc.
28  * All rights reserved.
29  */
30 
31 #pragma ident	"%Z%%M%	%I%	%E% SMI"
32 
33 /*
34  *	logdir()
35  *
36  *	This routine does not use the getpwent(3) library routine
37  *	because the latter uses the stdio package.  The allocation of
38  *	storage in this package destroys the integrity of the shell's
39  *	storage allocation.
40  */
41 
42 #include <fcntl.h>	/* O_RDONLY */
43 #include <stdlib.h>
44 #include <unistd.h>
45 #include <string.h>
46 
47 #define	BUFSIZ	160
48 
49 static char line[BUFSIZ+1];
50 
51 static char *
52 passwdfield(char *p)
53 {
54 	while (*p && *p != ':')
55 		++p;
56 	if (*p)
57 		*p++ = 0;
58 	return (p);
59 }
60 
61 char *
62 logdir(char *name)
63 {
64 	char	*p;
65 	int	i, j;
66 	int	pwf;
67 
68 	/* attempt to open the password file */
69 	if ((pwf = open("/etc/passwd", O_RDONLY)) == -1)
70 		return (0);
71 
72 	/* find the matching password entry */
73 	do {
74 		/* get the next line in the password file */
75 		i = read(pwf, line, BUFSIZ);
76 		for (j = 0; j < i; j++)
77 			if (line[j] == '\n')
78 				break;
79 		/* return a null pointer if the whole file has been read */
80 		if (j >= i)
81 			return (0);
82 		line[++j] = 0;			/* terminate the line */
83 		/* point at the next line */
84 		(void) lseek(pwf, (long)(j - i), 1);
85 		p = passwdfield(line);		/* get the logname */
86 	} while (*name != *line ||	/* fast pretest */
87 	    strcmp(name, line) != 0);
88 	(void) close(pwf);
89 
90 	/* skip the intervening fields */
91 	p = passwdfield(p);
92 	p = passwdfield(p);
93 	p = passwdfield(p);
94 	p = passwdfield(p);
95 
96 	/* return the login directory */
97 	(void) passwdfield(p);
98 	return (p);
99 }
100