Actual source code: ffpath.c

      1: #define PETSC_DLL

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

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

     31:    Not Collective

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

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

     46:    Level: developer

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

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

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

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

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

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

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