Actual source code: ghome.c

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

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

 31:    Not Collective

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

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

 39:    Level: developer

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

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

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

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

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

 79:    Not Collective

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

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

 87:    Level: advanced

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

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

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

107:   return(0);
108: }