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: #undef __FUNCT__  
 37: /*@C
 38:    PetscGetHomeDirectory - Returns home directory name.

 40:    Not Collective

 42:    Input Parameter:
 43: .  maxlen - maximum lengh allowed

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

 48:    Level: developer

 50:    Note:
 51:    On Windows NT machine the enviornmental variable HOME specifies the home directory.

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

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

 78: #undef __FUNCT__  
 80: /*@C
 81:     PetscFixFilename - Fixes a file name so that it is correct for both Unix and 
 82:     Windows by using the correct / or  to seperate directories.

 84:    Not Collective

 86:    Input Parameter:
 87: .  filein - name of file to be fixed

 89:    Output Parameter:
 90: .  fileout - the fixed name. Should long enough to hold the filename.

 92:    Level: advanced

 94:    Notes:
 95:    Call PetscFixFilename() just before calling fopen().
 96: @*/
 97: int PetscFixFilename(const char filein[],char fileout[])
 98: {
 99:   int i,n,ierr;

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

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

115:   return(0);
116: }