xref: /illumos-gate/usr/src/tools/cscope-fast/logdir.c (revision e3ae4b35c024af1196582063ecee3ab79367227d)
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 /*
32  *	logdir()
33  *
34  *	This routine does not use the getpwent(3) library routine
35  *	because the latter uses the stdio package.  The allocation of
36  *	storage in this package destroys the integrity of the shell's
37  *	storage allocation.
38  */
39 
40 #include <fcntl.h>	/* O_RDONLY */
41 #include <stdlib.h>
42 #include <unistd.h>
43 #include <string.h>
44 
45 #define	BUFSIZ	160
46 
47 static char line[BUFSIZ+1];
48 
49 static char *
50 passwdfield(char *p)
51 {
52 	while (*p && *p != ':')
53 		++p;
54 	if (*p)
55 		*p++ = 0;
56 	return (p);
57 }
58 
59 char *
60 logdir(char *name)
61 {
62 	char	*p;
63 	int	i, j;
64 	int	pwf;
65 
66 	/* attempt to open the password file */
67 	if ((pwf = open("/etc/passwd", O_RDONLY)) == -1)
68 		return (0);
69 
70 	/* find the matching password entry */
71 	do {
72 		/* get the next line in the password file */
73 		i = read(pwf, line, BUFSIZ);
74 		for (j = 0; j < i; j++)
75 			if (line[j] == '\n')
76 				break;
77 		/* return a null pointer if the whole file has been read */
78 		if (j >= i)
79 			return (0);
80 		line[++j] = 0;			/* terminate the line */
81 		/* point at the next line */
82 		(void) lseek(pwf, (long)(j - i), 1);
83 		p = passwdfield(line);		/* get the logname */
84 	} while (*name != *line ||	/* fast pretest */
85 	    strcmp(name, line) != 0);
86 	(void) close(pwf);
87 
88 	/* skip the intervening fields */
89 	p = passwdfield(p);
90 	p = passwdfield(p);
91 	p = passwdfield(p);
92 	p = passwdfield(p);
93 
94 	/* return the login directory */
95 	(void) passwdfield(p);
96 	return (p);
97 }
98