xref: /petsc/src/sys/fileio/fpath.c (revision db4deed73f06ae556a0f2e02a0fd6e016e156849)
1 
2 /*
3       Code for opening and closing 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/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 #include <fcntl.h>
22 #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
23 #include <sys/systeminfo.h>
24 #endif
25 
26 #if defined(PETSC_HAVE_PWD_H)
27 
28 #undef __FUNCT__
29 #define __FUNCT__ "PetscGetFullPath"
30 /*@C
31    PetscGetFullPath - Given a filename, returns the fully qualified file name.
32 
33    Not Collective
34 
35    Input Parameters:
36 +  path     - pathname to qualify
37 .  fullpath - pointer to buffer to hold full pathname
38 -  flen     - size of fullpath
39 
40    Level: developer
41 
42    Concepts: full path
43    Concepts: path^full
44 
45 .seealso: PetscGetRelativePath()
46 @*/
47 PetscErrorCode  PetscGetFullPath(const char path[],char fullpath[],size_t flen)
48 {
49   struct passwd *pwde;
50   PetscErrorCode ierr;
51   size_t        ln;
52   PetscBool     flg;
53 
54   PetscFunctionBegin;
55   if (path[0] == '/') {
56     ierr = PetscStrncmp("/tmp_mnt/",path,9,&flg);CHKERRQ(ierr);
57     if (flg) {ierr = PetscStrncpy(fullpath,path + 8,flen);CHKERRQ(ierr);}
58     else      {ierr = PetscStrncpy(fullpath,path,flen);CHKERRQ(ierr);}
59     PetscFunctionReturn(0);
60   }
61   ierr = PetscGetWorkingDirectory(fullpath,flen);CHKERRQ(ierr);
62   ierr = PetscStrlen(fullpath,&ln);CHKERRQ(ierr);
63   ierr = PetscStrncat(fullpath,"/",flen - ln);CHKERRQ(ierr);
64   if (path[0] == '.' && path[1] == '/') {
65     ierr = PetscStrlen(fullpath,&ln);CHKERRQ(ierr);
66     ierr = PetscStrncat(fullpath,path+2,flen - ln - 1);CHKERRQ(ierr);
67   } else {
68     ierr = PetscStrlen(fullpath,&ln);CHKERRQ(ierr);
69     ierr = PetscStrncat(fullpath,path,flen - ln - 1);CHKERRQ(ierr);
70   }
71 
72   /* Remove the various "special" forms (~username/ and ~/) */
73   if (fullpath[0] == '~') {
74     char tmppath[PETSC_MAX_PATH_LEN];
75     if (fullpath[1] == '/') {
76 #if defined(PETSC_HAVE_GETPWUID)
77         pwde = getpwuid(geteuid());
78         if (!pwde) PetscFunctionReturn(0);
79         ierr = PetscStrcpy(tmppath,pwde->pw_dir);CHKERRQ(ierr);
80         ierr = PetscStrlen(tmppath,&ln);CHKERRQ(ierr);
81         if (tmppath[ln-1] != '/') {ierr = PetscStrcat(tmppath+ln-1,"/");CHKERRQ(ierr);}
82         ierr = PetscStrcat(tmppath,fullpath + 2);CHKERRQ(ierr);
83         ierr = PetscStrncpy(fullpath,tmppath,flen);CHKERRQ(ierr);
84 #else
85         PetscFunctionReturn(0);
86 #endif
87     } else {
88         char *p,*name;
89 
90         /* Find username */
91         name = fullpath + 1;
92         p    = name;
93         while (*p && *p != '/') p++;
94         *p = 0; p++;
95         pwde = getpwnam(name);
96         if (!pwde) PetscFunctionReturn(0);
97 
98         ierr = PetscStrcpy(tmppath,pwde->pw_dir);CHKERRQ(ierr);
99         ierr = PetscStrlen(tmppath,&ln);CHKERRQ(ierr);
100         if (tmppath[ln-1] != '/') {ierr = PetscStrcat(tmppath+ln-1,"/");CHKERRQ(ierr);}
101         ierr = PetscStrcat(tmppath,p);CHKERRQ(ierr);
102         ierr = PetscStrncpy(fullpath,tmppath,flen);CHKERRQ(ierr);
103     }
104   }
105   /* Remove the automounter part of the path */
106   ierr = PetscStrncmp(fullpath,"/tmp_mnt/",9,&flg);CHKERRQ(ierr);
107   if (flg) {
108     char tmppath[PETSC_MAX_PATH_LEN];
109     ierr = PetscStrcpy(tmppath,fullpath + 8);CHKERRQ(ierr);
110     ierr = PetscStrcpy(fullpath,tmppath);CHKERRQ(ierr);
111   }
112   /* We could try to handle things like the removal of .. etc */
113   PetscFunctionReturn(0);
114 }
115 #elif defined(PETSC_HAVE__FULLPATH)
116 #undef __FUNCT__
117 #define __FUNCT__ "PetscGetFullPath"
118 PetscErrorCode  PetscGetFullPath(const char path[],char fullpath[],size_t flen)
119 {
120   PetscFunctionBegin;
121   _fullpath(fullpath,path,flen);
122   PetscFunctionReturn(0);
123 }
124 #else
125 #undef __FUNCT__
126 #define __FUNCT__ "PetscGetFullPath"
127 PetscErrorCode  PetscGetFullPath(const char path[],char fullpath[],size_t flen)
128 {
129   PetscErrorCode ierr;
130 
131   PetscFunctionBegin;
132   ierr = PetscStrcpy(fullpath,path);CHKERRQ(ierr);
133   PetscFunctionReturn(0);
134 }
135 #endif
136