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