8990 /opt/onbld/gk is useless
[unleashed.git] / usr / src / tools / cscope-fast / vpinit.c
blob2b738e20451ce02a7b00fa8afc8882c44f55d76a
1 /*
2 * CDDL HEADER START
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.
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.
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]
20 * CDDL HEADER END
22 /* Copyright (c) 1988 AT&T */
23 /* All Rights Reserved */
27 * Copyright (c) 1999 by Sun Microsystems, Inc.
28 * All rights reserved.
31 #pragma ident "%Z%%M% %I% %E% SMI"
33 /* vpinit - initialize vpdirs or update vpdirs based on currentdir */
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"
42 char **vpdirs; /* directories (including current) in view path */
43 int vpndirs; /* number of directories in view path */
45 char *argv0 = "libvp"; /* command name default for messages */
47 void
48 vpinit(char *currentdir)
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;
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]);
61 free(vpdirs);
62 vpndirs = 0;
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;
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;
76 /* see if this directory is in the first view path node */
77 for (i = 0; vpath[i] == currentdir[i] && vpath[i] != '\0'; ++i) {
80 if (i == 0 || (vpath[i] != ':' && vpath[i] != '\0') ||
81 (currentdir[i] != '/' && currentdir[i] != '\0')) {
82 return;
84 suffix = &currentdir[i];
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;
93 /* create the source directory list */
94 vpdirs = (char **)mymalloc(vpndirs * sizeof (char *));
96 /* don't change VPATH in the environment */
97 vpath = stralloc(vpath);
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;
104 vpdirs[i] = s;
105 while (*s != '\0' && *++s != ':') {
106 if (*s == '\n') {
107 *s = '\0';
110 if (*s != '\0') {
111 *s++ = '\0';
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;
121 free(vpath);