Actual source code: ghome.c

petsc-3.4.2 2013-07-02
  2: /*
  3:       Code for manipulating files.
  4: */
  5: #include <petscsys.h>
  6: #if defined(PETSC_HAVE_PWD_H)
  7: #include <pwd.h>
  8: #endif
  9: #include <ctype.h>
 10: #include <sys/stat.h>
 11: #if defined(PETSC_HAVE_UNISTD_H)
 12: #include <unistd.h>
 13: #endif
 14: #if defined(PETSC_HAVE_SYS_UTSNAME_H)
 15: #include <sys/utsname.h>
 16: #endif
 17: #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
 18: #include <sys/systeminfo.h>
 19: #endif

 23: /*@C
 24:    PetscGetHomeDirectory - Returns home directory name.

 26:    Not Collective

 28:    Input Parameter:
 29: .  maxlen - maximum lengh allowed

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

 34:    Level: developer

 36:    Note:
 37:    If PETSc cannot determine the home directory it makes dir a null string

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

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

 52: #if defined(PETSC_HAVE_GETPWUID)
 53:   pw = getpwuid(getuid());
 54:   if (pw) d1 = pw->pw_dir;
 55: #else
 56:   d1 = getenv("HOME");
 57: #endif
 58:   if (d1) {
 59:     PetscStrncpy(dir,d1,maxlen);
 60:   } else if (maxlen > 0) dir[0] = 0;
 61:   return(0);
 62: }

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

 70:    Not Collective

 72:    Input Parameter:
 73: .  filein - name of file to be fixed

 75:    Output Parameter:
 76: .  fileout - the fixed name. Should long enough to hold the filename.

 78:    Level: advanced

 80:    Notes:
 81:    Call PetscFixFilename() just before calling fopen().
 82: @*/
 83: PetscErrorCode  PetscFixFilename(const char filein[],char fileout[])
 84: {
 86:   size_t         i,n;

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

 91:   PetscStrlen(filein,&n);
 92:   for (i=0; i<n; i++) {
 93:     if (filein[i] == PETSC_REPLACE_DIR_SEPARATOR) fileout[i] = PETSC_DIR_SEPARATOR;
 94:     else fileout[i] = filein[i];
 95:   }
 96:   fileout[n] = 0;
 97:   return(0);
 98: }