Back to index

radiance  4R0+20100331
Functions | Variables
getpath.c File Reference
#include "copyright.h"
#include <string.h>
#include <ctype.h>
#include "rtio.h"
#include "paths.h"

Go to the source code of this file.

Functions

char * getpath (register char *fname, register char *searchpath, int mode)

Variables

static const char RCSid [] = "$Id: getpath.c,v 2.17 2004/02/12 18:55:50 greg Exp $"

Function Documentation

char* getpath ( register char *  fname,
register char *  searchpath,
int  mode 
)

Definition at line 26 of file getpath.c.

{
       static char  pname[PATH_MAX];
       char uname[512];
       register char  *cp;
       int i;

       if (fname == NULL) { return(NULL); }

       pname[0] = '\0';            /* check for full specification */

       if (ISABS(fname)) { /* absolute path */
              strncpy(pname, fname, sizeof(pname)-1);
       } else {
              switch (*fname) {
                     case '.':                          /* relative to cwd */
                            strncpy(pname, fname, sizeof(pname)-1);
                            break;
                     case '~':                          /* relative to home directory */
                            fname++;
                            cp = uname;
                            for (i=0;i<sizeof(uname)&&*fname!='\0'&&!ISDIRSEP(*fname);i++)
                                   *cp++ = *fname++;
                            *cp = '\0';
                            cp = gethomedir(uname, pname, sizeof(pname));
                            if(cp == NULL) return NULL;
                            strncat(pname, fname, sizeof(pname)-strlen(pname)-1);
                            break;
              }
       }
       if (pname[0])        /* got it, check access if search requested */
              return(searchpath==NULL||access(pname,mode)==0 ? pname : NULL);

       if (searchpath == NULL) {                 /* don't search */
              strncpy(pname, fname, sizeof(pname)-1);
              return(pname);
       }
       /* check search path */
       do {
              cp = pname;
              while (*searchpath && (*cp = *searchpath++) != PATHSEP) {
                     cp++;
              }
              if (cp > pname && !ISDIRSEP(cp[-1])) {
                     *cp++ = DIRSEP;
              }
              strncpy(cp, fname, sizeof(pname)-strlen(pname)-1);
              if (access(pname, mode) == 0)             /* file accessable? */
                     return(pname);
       } while (*searchpath);
       /* not found */
       return(NULL);
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

const char RCSid[] = "$Id: getpath.c,v 2.17 2004/02/12 18:55:50 greg Exp $" [static]

Definition at line 2 of file getpath.c.