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: #ifndef MAXPATHLEN
 34: #define MAXPATHLEN 1024
 35: #endif

 37: /*@C
 38:    PetscGetFileFromPath - Finds a file from a name and a path string.  A 
 39:                           default can be provided.

 41:    Not Collective

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

 53:    Output Parameter:
 54: .  fname - qualified file name

 56:    Level: developer

 58:    Concepts: files^finding in path
 59:    Concepts: path^searching for file

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

 70:   /* Setup default */
 71:   PetscGetFullPath(defname,fname,MAXPATHLEN);

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

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

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

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