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