Actual source code: ffpath.c

  1: /*$Id: ffpath.c,v 1.36 2001/03/23 23:20:30 balay Exp $*/

 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(PARCH_win32)
 18: #include <sys/utsname.h>
 19: #endif
 20: #if defined(PARCH_win32)
 21: #include <windows.h>
 22: #include <io.h>
 23: #include <direct.h>
 24: #endif
 25: #if defined (PARCH_win32_gnu)
 26: #include <windows.h>
 27: #endif
 28: #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
 29: #include <sys/systeminfo.h>
 30: #endif
 31: #include "petscfix.h"

 33: #undef __FUNCT__  
 35: /*@C
 36:    PetscGetFileFromPath - Finds a file from a name and a path string.  A 
 37:                           default can be provided.

 39:    Not Collective

 41:    Input Parameters:
 42: +  path - A string containing "directory:directory:..." (without the
 43:           quotes, of course).
 44:           As a special case, if the name is a single FILE, that file is
 45:           used.
 46: .  defname - default name
 47: .  name - file name to use with the directories from env
 48: -  mode - file mode desired (usually r for readable, w for writable, or e for
 49:           executable)

 51:    Output Parameter:
 52: .  fname - qualified file name

 54:    Level: developer

 56:    Concepts: files^finding in path
 57:    Concepts: path^searching for file

 59: @*/
 60: int PetscGetFileFromPath(char *path,char *defname,char *name,char *fname,char mode)
 61: {
 62: #if !defined(PARCH_win32)
 63:   char       *p,*cdir,trial[PETSC_MAX_PATH_LEN],*senv,*env;
 64:   int        ln,ierr;
 65:   PetscTruth flg;

 68:   /* Setup default */
 69:   PetscGetFullPath(defname,fname,PETSC_MAX_PATH_LEN);

 71:   if (path) {
 72:     /* Check to see if the path is a valid regular FILE */
 73:     PetscTestFile(path,mode,&flg);
 74:     if (flg) {
 75:       PetscStrcpy(fname,path);
 76:       PetscFunctionReturn(1);
 77:     }
 78: 
 79:     /* Make a local copy of path and mangle it */
 80:     PetscStrallocpy(path,&senv);
 81:     env  = senv;
 82:     while (env) {
 83:       /* Find next directory in env */
 84:       cdir = env;
 85:       PetscStrchr(env,':',&p);
 86:       if (p) {
 87:         *p  = 0;
 88:         env = p + 1;
 89:       } else
 90:         env = 0;

 92:       /* Form trial file name */
 93:       PetscStrcpy(trial,cdir);
 94:       PetscStrlen(trial,&ln);
 95:       if (trial[ln-1] != '/')  trial[ln++] = '/';
 96: 
 97:       PetscStrcpy(trial + ln,name);

 99:       PetscTestFile(path,mode,&flg);
100:       if (flg) {
101:         /* need PetscGetFullPath rather then copy in case path has . in it */
102:         PetscGetFullPath(trial,fname,PETSC_MAX_PATH_LEN);
103:         PetscFree(senv);
104:         PetscFunctionReturn(1);
105:       }
106:     }
107:     PetscFree(senv);
108:   }

110:   PetscTestFile(path,mode,&flg);
111:   if (flg) PetscFunctionReturn(1);
112: #endif
113:   return(0);
114: }