1 /*===========================================================================
2 Copyright (c) 1998-2000, The Santa Cruz Operation
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
8 *Redistributions of source code must retain the above copyright notice,
9 this list of conditions and the following disclaimer.
11 *Redistributions in binary form must reproduce the above copyright notice,
12 this list of conditions and the following disclaimer in the documentation
13 and/or other materials provided with the distribution.
15 *Neither name of The Santa Cruz Operation nor the names of its contributors
16 may be used to endorse or promote products derived from this software
17 without specific prior written permission.
19 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
20 IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
21 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
23 LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31 =========================================================================*/
33 /* vpinit - initialize vpdirs or update vpdirs based on currentdir */
35 #include <stdio.h> /* stderr */
43 #include "constants.h"
45 static char const rcsid[] = "$Id: vpinit.c,v 1.8 2006/07/23 20:59:20 broeker Exp $";
48 char **vpdirs; /* directories (including current) in view path */
50 char vpdirs[MAXDIR][DIRLEN + 1];
51 #define MAXVPATH (MAXDIR * (DIRLEN + 1))
53 int vpndirs; /* number of directories in view path */
56 vpinit(char *current_dir)
58 char *suffix; /* path from view path node */
59 char *vpath; /* VPATH environment variable value */
60 char buf[MAXPATH + 1];
64 char *node; /* view path node */
65 char vpathbuf[MAXVPATH + 1];
68 /* if an existing directory list is to be updated, free it */
69 if (current_dir != NULL && vpndirs > 0) {
71 for (i = 0; i < vpndirs; ++i) {
78 /* return if the directory list has been computed */
79 /* or there isn't a view path environment variable */
80 if (vpndirs > 0 || (vpath = getenv("VPATH")) == NULL ||
84 /* if not given, get the current directory name */
85 if (current_dir == NULL && (current_dir = getcwd(buf, MAXPATH)) == NULL) {
86 (void) fprintf(stderr, "%s: cannot get current directory name\n", argv0);
89 /* see if this directory is in the first view path node */
90 for (i = 0; vpath[i] == current_dir[i] && vpath[i] != '\0'; ++i) {
93 if ((vpath[i] != ':' && vpath[i] != '\0') ||
94 (current_dir[i] != '/' && current_dir[i] != '\0')) {
97 suffix = ¤t_dir[i];
100 /* count the nodes in the view path */
102 for (i = 0; vpath[i] != '\0'; ++i) {
103 if (vpath[i] == ':' && vpath[i + 1]) {
107 /* create the source directory list */
108 vpdirs = mymalloc(vpndirs * sizeof(char *));
110 /* don't change VPATH in the environment */
111 vpath = my_strdup(vpath);
113 /* split the view path into nodes */
114 for (i = 0, s = vpath; *s != '\0'; ++i) {
116 while (*s != '\0' && *++s != ':') {
125 /* convert the view path nodes to directories */
126 for (i = 0; i < vpndirs; ++i) {
127 s = mymalloc((strlen(vpdirs[i]) + strlen(suffix) + 1));
128 (void) strcpy(s, vpdirs[i]);
129 (void) strcat(s, suffix);
134 /* don't change VPATH in the environment */
135 if (strlen(vpath) > MAXVPATH) {
136 (void) fprintf(stderr, "%s: VPATH is longer than %d characters: %s\n", argv0, MAXVPATH, vpath);
139 (void) strcpy(vpathbuf, vpath);
142 /* convert the view path nodes to directories */
145 /* get the next node */
147 while (*s != '\0' && *++s != ':') {
155 /* ignore a directory that is too long */
156 if (strlen(node) + strlen(suffix) > DIRLEN) {
157 (void) fprintf(stderr, "%s: VPATH directory is longer than %d characters: %s%s\n", argv0, DIRLEN, node, suffix);
159 else if (vpndirs >= MAXDIR) {
160 (void) fprintf(stderr, "%s: VPATH has more than %d nodes\n", argv0, vpndirs);
164 /* create the view path directory */
165 (void) strcpy(vpdirs[vpndirs], node);
166 (void) strcat(vpdirs[vpndirs], suffix);