Actual source code: ghome.c

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

 29: /*@C
 30:    PetscGetHomeDirectory - Returns home directory name.

 32:    Not Collective

 34:    Input Parameter:
 35: .  maxlen - maximum lengh allowed

 37:    Output Parameter:
 38: .  dir - contains the home directory. Must be long enough to hold the name.

 40:    Level: developer

 42:    Note:
 43:    If PETSc cannot determine the home directory it makes dir a null string

 45:    On Windows machines the enviornmental variable HOME specifies the home directory.

 47:    Concepts: home directory
 48: @*/
 49: PetscErrorCode  PetscGetHomeDirectory(char dir[],size_t maxlen)
 50: {
 52:   char           *d1 = 0;
 53: #if defined(PETSC_HAVE_GETPWUID)
 54:   struct passwd *pw = 0;
 55: #endif

 58: #if defined(PETSC_HAVE_GETPWUID)
 59:   pw = getpwuid(getuid());
 60:   if (pw)  {
 61:     d1 = pw->pw_dir;
 62:   }
 63: #else
 64:   d1 = getenv("HOME");
 65: #endif
 66:   if (d1) {
 67:     PetscStrncpy(dir,d1,maxlen);
 68:   } else if (maxlen > 0) {
 69:     dir[0] = 0;
 70:   }
 71:   return(0);
 72: }

 76: /*@C
 77:     PetscFixFilename - Fixes a file name so that it is correct for both Unix and 
 78:     Windows by using the correct / or \ to separate directories.

 80:    Not Collective

 82:    Input Parameter:
 83: .  filein - name of file to be fixed

 85:    Output Parameter:
 86: .  fileout - the fixed name. Should long enough to hold the filename.

 88:    Level: advanced

 90:    Notes:
 91:    Call PetscFixFilename() just before calling fopen().
 92: @*/
 93: PetscErrorCode  PetscFixFilename(const char filein[],char fileout[])
 94: {
 96:   size_t         i,n;

 99:   if (!filein || !fileout) return(0);

101:   PetscStrlen(filein,&n);
102:   for (i=0; i<n; i++) {
103:     if (filein[i] == PETSC_REPLACE_DIR_SEPARATOR) fileout[i] = PETSC_DIR_SEPARATOR;
104:     else fileout[i] = filein[i];
105:   }
106:   fileout[n] = 0;

108:   return(0);
109: }