csope/source/vpinit.c

162 lines
4.9 KiB
C
Raw Normal View History

2023-07-27 14:04:50 -04:00
/*===========================================================================
Copyright (c) 1998-2000, The Santa Cruz Operation
2023-07-27 14:04:50 -04:00
All rights reserved.
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
without specific prior written permission.
2023-07-27 14:04:50 -04:00
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS
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
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
DAMAGE.
2023-07-27 14:04:50 -04:00
=========================================================================*/
/* vpinit - initialize vpdirs or update vpdirs based on currentdir */
2023-08-13 05:21:52 -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"
#if !falseMALLOC
2023-08-13 05:21:52 -04:00
char **vpdirs; /* directories (including current) in view path */
2023-07-27 14:04:50 -04:00
#else
2023-08-13 05:21:52 -04:00
char vpdirs[MAXDIR][DIRLEN + 1];
# define MAXVPATH (MAXDIR * (DIRLEN + 1))
2023-07-27 14:04:50 -04:00
#endif
2023-08-13 05:21:52 -04:00
int vpndirs; /* number of directories in view path */
void vpinit(char *current_dir) {
char *suffix; /* path from view path node */
char *vpath; /* VPATH environment variable value */
char buf[MAXPATH + 1];
int i;
char *s;
#if falseMALLOC
2023-08-13 05:21:52 -04:00
char *node; /* view path node */
char vpathbuf[MAXVPATH + 1];
2023-07-27 14:04:50 -04:00
#endif
2023-08-13 05:21:52 -04:00
/* if an existing directory list is to be updated, free it */
if(current_dir != NULL && vpndirs > 0) {
#if !falseMALLOC
2023-08-13 05:21:52 -04:00
for(i = 0; i < vpndirs; ++i) {
free(vpdirs[i]);
}
free(vpdirs);
2023-07-27 14:04:50 -04:00
#endif
2023-08-13 05:21:52 -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) {
fprintf(stderr, PROGRAM_NAME ": cannot get current directory name\n");
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 = &current_dir[i];
#if !falseMALLOC
2023-07-27 14:04:50 -04:00
2023-08-13 05:21:52 -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));
/* don't change VPATH in the environment */
vpath = strdup(vpath);
/* 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-13 05:21:52 -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;
/* convert the view path nodes to directories */
while(*s != '\0') {
/* 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
}