/* Copyright (C) 1992-1998 The Geometry Center * Copyright (C) 1998-2000 Stuart Levy, Tamara Munzner, Mark Phillips * * This file is part of Geomview. * * Geomview is free software; you can redistribute it and/or modify it * under the terms of the GNU Lesser General Public License as published * by the Free Software Foundation; either version 2, or (at your option) * any later version. * * Geomview is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with Geomview; see the file COPYING. If not, write * to the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, * USA, or visit http://www.gnu.org. */ #if defined(HAVE_CONFIG_H) && !defined(CONFIG_H_INCLUDED) #include "config.h" #endif static char copyright[] = "Copyright (C) 1992-1998 The Geometry Center\n\ Copyright (C) 1998-2000 Stuart Levy, Tamara Munzner, Mark Phillips"; /* Authors: Charlie Gunn, Stuart Levy, Tamara Munzner, Mark Phillips */ /* $Header: /cvsroot/geomview/geomview/src/lib/oogl/util/findfile.c,v 1.2 2000/09/01 22:38:16 mphillips Exp $ */ #include #include #include #include "ooglutil.h" #if defined(unix) || defined(__unix) # ifdef NeXT # include # else # include /* needed for access() */ # endif #else /* Win32 */ # include # define R_OK 4 /* No Windows include file defines this?! */ #endif #include static char **dirlist = NULL; static void dirprefix(char *file, char *dir); char *envexpand(char *s); /*----------------------------------------------------------------------- * Function: filedirs * Description: set the list of directories to search for files * Args: dirs: NULL-terminated array of pointers to directory * strings * Author: mbp * Date: Wed Feb 12 14:09:48 1992 * Notes: This function sets the list of directories searched by * findfile(). It makes an internal copy of these directories * and expands all environment variables in them. */ void filedirs(char *dirs[]) { char buf[1024], **p; int i,ndirs; if (dirlist) { for (p=dirlist; *p!=NULL; ++p) free(*p); OOGLFree(dirlist); } for (ndirs=0; dirs[ndirs]!=NULL; ++ndirs); dirlist = OOGLNewNE(char *,ndirs+1, "filedirs: dirlist"); for (i=0; i= dir && *end != '/') --end; if (end >= dir) *(end+1) = '\0'; else dir[0] = '\0'; } /*----------------------------------------------------------------------- * Function: envexpand * Description: expand environment variables in a string * Args: *s: the string * Returns: s * Author: mbp * Date: Fri Feb 14 09:46:22 1992 * Notes: expansion is done inplace; there better be enough room! */ char * envexpand(char *s) { char *c, *env, *envend, *tail; c = s; if (*c == '~' && (env = getenv("HOME"))) { tail = strdup(c+1); strcpy(c, env); strcat(c, tail); c += strlen(env); free(tail); } while (*c != '\0') { if (*c == '$') { for(envend = c; isalnum(*++envend) || *envend == '_'; ) ; tail = strdup(envend); *envend = '\0'; if((env = getenv(c+1)) == NULL) { OOGLError(1, "%s : No %s environment variable",s,c+1); strcpy(c,tail); } else { strcpy(c,env); strcat(c,tail); c += strlen(env); } free(tail); } else ++c; } return s; }