2023-07-27 14:04:50 -04:00
|
|
|
/*===========================================================================
|
2023-08-04 14:34:51 -04:00
|
|
|
Copyright (c) 1998-2000, The Santa Cruz Operation
|
2023-07-27 14:04:50 -04:00
|
|
|
All rights reserved.
|
2023-08-04 14:34:51 -04:00
|
|
|
|
2023-07-27 14:04:50 -04:00
|
|
|
Redistribution and use in source and binary forms, with or without
|
|
|
|
modification, are permitted provided that the following conditions are met:
|
|
|
|
|
|
|
|
*Redistributions of source code must retain the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer.
|
|
|
|
|
|
|
|
*Redistributions in binary form must reproduce the above copyright notice,
|
|
|
|
this list of conditions and the following disclaimer in the documentation
|
|
|
|
and/or other materials provided with the distribution.
|
|
|
|
|
|
|
|
*Neither name of The Santa Cruz Operation nor the names of its contributors
|
|
|
|
may be used to endorse or promote products derived from this software
|
2023-08-04 14:34:51 -04:00
|
|
|
without specific prior written permission.
|
2023-07-27 14:04:50 -04:00
|
|
|
|
|
|
|
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
|
2023-08-04 15:09:58 -04:00
|
|
|
IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT falseT LIMITED TO,
|
2023-07-27 14:04:50 -04:00
|
|
|
THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
2023-08-04 15:19:25 -04:00
|
|
|
PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
|
2023-07-27 14:04:50 -04:00
|
|
|
LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
2023-08-04 15:09:58 -04:00
|
|
|
CONSEQUENTIAL DAMAGES (INCLUDING, BUT falseT LIMITED TO, PROCUREMENT OF
|
2023-07-27 14:04:50 -04:00
|
|
|
SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
|
|
INTERRUPTION)
|
|
|
|
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
|
2023-08-04 14:34:51 -04:00
|
|
|
DAMAGE.
|
2023-07-27 14:04:50 -04:00
|
|
|
=========================================================================*/
|
|
|
|
|
|
|
|
/* vpinit - initialize vpdirs or update vpdirs based on currentdir */
|
|
|
|
|
2023-08-04 13:49:03 -04:00
|
|
|
#include <stdio.h> /* stderr */
|
2023-07-27 14:04:50 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include "vp.h"
|
|
|
|
|
|
|
|
#include "library.h"
|
|
|
|
#include "global.h"
|
|
|
|
#include "constants.h"
|
|
|
|
|
2023-08-04 15:09:58 -04:00
|
|
|
#if !falseMALLOC
|
2023-08-04 13:49:03 -04:00
|
|
|
char **vpdirs; /* directories (including current) in view path */
|
2023-07-27 14:04:50 -04:00
|
|
|
#else
|
2023-08-04 13:49:03 -04:00
|
|
|
char vpdirs[MAXDIR][DIRLEN + 1];
|
|
|
|
#define MAXVPATH (MAXDIR * (DIRLEN + 1))
|
2023-07-27 14:04:50 -04:00
|
|
|
#endif
|
2023-08-04 13:49:03 -04:00
|
|
|
int vpndirs; /* number of directories in view path */
|
2023-07-27 14:04:50 -04:00
|
|
|
|
|
|
|
void
|
|
|
|
vpinit(char *current_dir)
|
|
|
|
{
|
2023-08-04 13:49:03 -04:00
|
|
|
char *suffix; /* path from view path node */
|
|
|
|
char *vpath; /* VPATH environment variable value */
|
|
|
|
char buf[MAXPATH + 1];
|
|
|
|
int i;
|
|
|
|
char *s;
|
2023-08-04 15:09:58 -04:00
|
|
|
#if falseMALLOC
|
2023-08-04 13:49:03 -04:00
|
|
|
char *node; /* view path node */
|
|
|
|
char vpathbuf[MAXVPATH + 1];
|
2023-07-27 14:04:50 -04:00
|
|
|
#endif
|
2023-08-04 14:34:51 -04:00
|
|
|
|
2023-08-04 13:49:03 -04:00
|
|
|
/* if an existing directory list is to be updated, free it */
|
|
|
|
if (current_dir != NULL && vpndirs > 0) {
|
2023-08-04 15:09:58 -04:00
|
|
|
#if !falseMALLOC
|
2023-08-04 13:49:03 -04:00
|
|
|
for (i = 0; i < vpndirs; ++i) {
|
|
|
|
free(vpdirs[i]);
|
|
|
|
}
|
|
|
|
free(vpdirs);
|
2023-07-27 14:04:50 -04:00
|
|
|
#endif
|
2023-08-04 13:49:03 -04:00
|
|
|
vpndirs = 0;
|
|
|
|
}
|
|
|
|
/* return if the directory list has been computed */
|
|
|
|
/* or there isn't a view path environment variable */
|
|
|
|
if (vpndirs > 0 || (vpath = getenv("VPATH")) == NULL ||
|
|
|
|
*vpath == '\0') {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* if not given, get the current directory name */
|
|
|
|
if (current_dir == NULL && (current_dir = getcwd(buf, MAXPATH)) == NULL) {
|
2023-08-11 17:31:25 -04:00
|
|
|
fprintf(stderr, PROGRAM_NAME ": cannot get current directory name\n");
|
2023-08-04 13:49:03 -04:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
/* see if this directory is in the first view path node */
|
|
|
|
for (i = 0; vpath[i] == current_dir[i] && vpath[i] != '\0'; ++i) {
|
|
|
|
;
|
|
|
|
}
|
|
|
|
if ((vpath[i] != ':' && vpath[i] != '\0') ||
|
|
|
|
(current_dir[i] != '/' && current_dir[i] != '\0')) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
suffix = ¤t_dir[i];
|
2023-08-04 15:09:58 -04:00
|
|
|
#if !falseMALLOC
|
2023-07-27 14:04:50 -04:00
|
|
|
|
2023-08-04 13:49:03 -04:00
|
|
|
/* count the nodes in the view path */
|
|
|
|
vpndirs = 1;
|
|
|
|
for (i = 0; vpath[i] != '\0'; ++i) {
|
|
|
|
if (vpath[i] == ':' && vpath[i + 1]) {
|
|
|
|
++vpndirs;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* create the source directory list */
|
|
|
|
vpdirs = malloc(vpndirs * sizeof(*vpdirs));
|
2023-07-27 14:04:50 -04:00
|
|
|
|
2023-08-04 13:49:03 -04:00
|
|
|
/* don't change VPATH in the environment */
|
|
|
|
vpath = strdup(vpath);
|
2023-08-04 14:34:51 -04:00
|
|
|
|
2023-08-04 13:49:03 -04:00
|
|
|
/* split the view path into nodes */
|
|
|
|
for (i = 0, s = vpath; *s != '\0'; ++i) {
|
|
|
|
vpdirs[i] = s;
|
|
|
|
while (*s != '\0' && *++s != ':') {
|
|
|
|
if (*s == '\n') {
|
|
|
|
*s = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*s != '\0') {
|
|
|
|
*s++ = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* convert the view path nodes to directories */
|
|
|
|
for (i = 0; i < vpndirs; ++i) {
|
|
|
|
s = malloc(strlen(vpdirs[i]) + strlen(suffix) + 1);
|
|
|
|
(void) strcpy(s, vpdirs[i]);
|
|
|
|
(void) strcat(s, suffix);
|
|
|
|
vpdirs[i] = s;
|
|
|
|
}
|
|
|
|
free(vpath);
|
2023-07-27 14:04:50 -04:00
|
|
|
#else
|
2023-08-04 13:49:03 -04:00
|
|
|
/* don't change VPATH in the environment */
|
|
|
|
if (strlen(vpath) > MAXVPATH) {
|
|
|
|
(void) fprintf(stderr, "%s: VPATH is longer than %d characters: %s\n", argv0, MAXVPATH, vpath);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
(void) strcpy(vpathbuf, vpath);
|
|
|
|
s = vpathbuf;
|
2023-08-04 14:34:51 -04:00
|
|
|
|
2023-08-04 13:49:03 -04:00
|
|
|
/* convert the view path nodes to directories */
|
|
|
|
while (*s != '\0') {
|
2023-08-04 14:34:51 -04:00
|
|
|
|
2023-08-04 13:49:03 -04:00
|
|
|
/* get the next node */
|
|
|
|
node = s;
|
|
|
|
while (*s != '\0' && *++s != ':') {
|
|
|
|
if (*s == '\n') {
|
|
|
|
*s = '\0';
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (*s != '\0') {
|
|
|
|
*s++ = '\0';
|
|
|
|
}
|
|
|
|
/* ignore a directory that is too long */
|
|
|
|
if (strlen(node) + strlen(suffix) > DIRLEN) {
|
|
|
|
(void) fprintf(stderr, "%s: VPATH directory is longer than %d characters: %s%s\n", argv0, DIRLEN, node, suffix);
|
|
|
|
}
|
|
|
|
else if (vpndirs >= MAXDIR) {
|
|
|
|
(void) fprintf(stderr, "%s: VPATH has more than %d nodes\n", argv0, vpndirs);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
/* create the view path directory */
|
|
|
|
(void) strcpy(vpdirs[vpndirs], node);
|
|
|
|
(void) strcat(vpdirs[vpndirs], suffix);
|
|
|
|
++vpndirs;
|
|
|
|
}
|
|
|
|
}
|
2023-07-27 14:04:50 -04:00
|
|
|
#endif
|
|
|
|
}
|