Actual source code: ffpath.c

 2:  #include petsc.h
 3:  #include petscsys.h
  4: #if defined(PETSC_HAVE_PWD_H)
  5: #include <pwd.h>
  6: #endif
  7: #include <ctype.h>
  8: #include <sys/types.h>
  9: #include <sys/stat.h>
 10: #if defined(PETSC_HAVE_UNISTD_H)
 11: #include <unistd.h>
 12: #endif
 13: #if defined(PETSC_HAVE_STDLIB_H)
 14: #include <stdlib.h>
 15: #endif
 16: #if defined(PETSC_HAVE_SYS_UTSNAME_H)
 17: #include <sys/utsname.h>
 18: #endif
 19: #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
 20: #include <sys/systeminfo.h>
 21: #endif
 22: #include "petscfix.h"

 26: /*@C
 27:    PetscGetFileFromPath - Finds a file from a name and a path string.  A 
 28:                           default can be provided.

 30:    Not Collective

 32:    Input Parameters:
 33: +  path - A string containing "directory:directory:..." (without the
 34:           quotes, of course).
 35:           As a special case, if the name is a single FILE, that file is
 36:           used.
 37: .  defname - default name
 38: .  name - file name to use with the directories from env
 39: -  mode - file mode desired (usually r for readable, w for writable, or e for
 40:           executable)

 42:    Output Parameter:
 43: .  fname - qualified file name

 45:    Level: developer

 47:    Concepts: files^finding in path
 48:    Concepts: path^searching for file

 50: @*/
 51: PetscErrorCode PetscGetFileFromPath(char *path,char *defname,char *name,char *fname,char mode)
 52: {
 53:   char       *p,*cdir,trial[PETSC_MAX_PATH_LEN],*senv,*env;
 54:   size_t     ln;
 56:   PetscTruth flg;

 59:   /* Setup default */
 60:   PetscGetFullPath(defname,fname,PETSC_MAX_PATH_LEN);

 62:   if (path) {
 63:     /* Check to see if the path is a valid regular FILE */
 64:     PetscTestFile(path,mode,&flg);
 65:     if (flg) {
 66:       PetscStrcpy(fname,path);
 67:       PetscFunctionReturn(1);
 68:     }
 69: 
 70:     /* Make a local copy of path and mangle it */
 71:     PetscStrallocpy(path,&senv);
 72:     env  = senv;
 73:     while (env) {
 74:       /* Find next directory in env */
 75:       cdir = env;
 76:       PetscStrchr(env,PETSC_PATH_SEPARATOR,&p);
 77:       if (p) {
 78:         *p  = 0;
 79:         env = p + 1;
 80:       } else
 81:         env = 0;

 83:       /* Form trial file name */
 84:       PetscStrcpy(trial,cdir);
 85:       PetscStrlen(trial,&ln);
 86:       if (trial[ln-1] != '/')  trial[ln++] = '/';
 87: 
 88:       PetscStrcpy(trial + ln,name);

 90:       PetscTestFile(path,mode,&flg);
 91:       if (flg) {
 92:         /* need PetscGetFullPath rather then copy in case path has . in it */
 93:         PetscGetFullPath(trial,fname,PETSC_MAX_PATH_LEN);
 94:         PetscFree(senv);
 95:         PetscFunctionReturn(1);
 96:       }
 97:     }
 98:     PetscFree(senv);
 99:   }

101:   PetscTestFile(path,mode,&flg);
102:   if (flg) PetscFunctionReturn(1);
103:   return(0);
104: }