xref: /titanic_44/usr/src/tools/cscope-fast/vpinit.c (revision c28749e97052f09388969427adf7df641cdcdc22)
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 /* vpinit - initialize vpdirs or update vpdirs based on currentdir */
34 
35 #include <sys/types.h>
36 #include <string.h>	/* string functions */
37 #include <stdlib.h>
38 #include <stdio.h>	/* stderr */
39 #include "vp.h"
40 #include "library.h"
41 
42 char	**vpdirs;	/* directories (including current) in view path */
43 int	vpndirs;	/* number of directories in view path */
44 
45 char	*argv0 = "libvp";	/* command name default for messages */
46 
47 void
48 vpinit(char *currentdir)
49 {
50 	char	*suffix;	/* path from view path node */
51 	char	*vpath;		/* VPATH environment variable value */
52 	char	buf[MAXPATH + 1];
53 	int	i;
54 	char	*s;
55 
56 	/* if an existing directory list is to be updated, free it */
57 	if (currentdir != NULL && vpndirs > 0) {
58 		for (i = 0; i < vpndirs; ++i) {
59 			free(vpdirs[i]);
60 		}
61 		free(vpdirs);
62 		vpndirs = 0;
63 	}
64 	/* return if the directory list has been computed */
65 	/* or there isn't a view path environment variable */
66 	if (vpndirs > 0 || (vpath = getenv("VPATH")) == NULL ||
67 	    *vpath == '\0') {
68 		return;
69 	}
70 	/* if not given, get the current directory name */
71 	if (currentdir == NULL && (currentdir = mygetwd(buf)) == NULL) {
72 		(void) fprintf(stderr,
73 		    "%s: cannot get current directory name\n", argv0);
74 		return;
75 	}
76 	/* see if this directory is in the first view path node */
77 	for (i = 0; vpath[i] == currentdir[i] && vpath[i] != '\0'; ++i) {
78 		;
79 	}
80 	if (i == 0 || (vpath[i] != ':' && vpath[i] != '\0') ||
81 	    (currentdir[i] != '/' && currentdir[i] != '\0')) {
82 		return;
83 	}
84 	suffix = &currentdir[i];
85 
86 	/* count the nodes in the view path */
87 	vpndirs = 1;
88 	for (s = vpath; *s != '\0'; ++s) {
89 		if (*s == ':' && *(s + 1) != ':' && *(s + 1) != '\0') {
90 			++vpndirs;
91 		}
92 	}
93 	/* create the source directory list */
94 	vpdirs = (char **)mymalloc(vpndirs * sizeof (char *));
95 
96 	/* don't change VPATH in the environment */
97 	vpath = stralloc(vpath);
98 
99 	/* split the view path into nodes */
100 	for (i = 0, s = vpath; i < vpndirs && *s != '\0'; ++i) {
101 		while (*s == ':') {	/* ignore null nodes */
102 			++s;
103 		}
104 		vpdirs[i] = s;
105 		while (*s != '\0' && *++s != ':') {
106 			if (*s == '\n') {
107 				*s = '\0';
108 			}
109 		}
110 		if (*s != '\0') {
111 			*s++ = '\0';
112 		}
113 	}
114 	/* convert the view path nodes to directories */
115 	for (i = 0; i < vpndirs; ++i) {
116 		s = mymalloc((strlen(vpdirs[i]) + strlen(suffix) + 1));
117 		(void) strcpy(s, vpdirs[i]);
118 		(void) strcat(s, suffix);
119 		vpdirs[i] = s;
120 	}
121 	free(vpath);
122 }
123