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