Actual source code: ghome.c

  1: /*$Id: ghome.c,v 1.39 2001/03/23 23:20:30 balay Exp $*/
  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(PARCH_win32)
 20: #include <sys/utsname.h>
 21: #endif
 22: #if defined(PARCH_win32)
 23: #include <windows.h>
 24: #include <io.h>
 25: #include <direct.h>
 26: #endif
 27: #if defined (PARCH_win32_gnu)
 28: #include <windows.h>
 29: #endif
 30: #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
 31: #include <sys/systeminfo.h>
 32: #endif
 33: #include "petscfix.h"

 35: /*@C
 36:    PetscGetHomeDirectory - Returns home directory name.

 38:    Not Collective

 40:    Input Parameter:
 41: .  maxlen - maximum lengh allowed

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

 46:    Level: developer

 48:    Note:
 49:    On Windows NT machine the enviornmental variable HOME specifies the home directory.

 51:    Concepts: home directory
 52: @*/
 53: int PetscGetHomeDirectory(char dir[],int maxlen)
 54: {
 56: #if defined(PARCH_win32) || defined(PARCH_win32_gnu)
 57:   char *d1 = getenv("HOME");
 58: #else
 59:   struct passwd *pw = 0;
 60: #endif

 63: #if defined(PARCH_win32) || defined(PARCH_win32_gnu)
 64:   if (!d1) d1 ="c:";
 65:   PetscStrncpy(dir,d1,maxlen);
 66: #elif !defined(PETSC_MISSING_GETPWUID)
 67:   pw = getpwuid(getuid());
 68:   if (!pw)  {dir[0] = 0; return(0);}
 69:   PetscStrncpy(dir,pw->pw_dir,maxlen);
 70: #else 
 71:   dir[0] = 0;
 72: #endif
 73:   return(0);
 74: }

 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 seperate 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: int PetscFixFilename(const char filein[],char fileout[])
 94: {
 95:   int i,n,ierr;

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

100:   PetscStrlen(filein,&n);
101:   for (i=0; i<n; i++) {
102: #if defined(PARCH_win32)
103:     if (filein[i] == '/') fileout[i] = '\';
104: #else
105:     if (filein[i] == '\') fileout[i] = '/';
106: #endif
107:     else fileout[i] = filein[i];
108:   }
109:   fileout[n] = 0;

111:   return(0);
112: }