xref: /petsc/src/sys/fileio/rpath.c (revision 7a2f837dfb5b1b0a1e6bb2716a3d28bcaab1482d)
1 
2 #include <petscsys.h>
3 #if defined(PETSC_HAVE_PWD_H)
4   #include <pwd.h>
5 #endif
6 #include <ctype.h>
7 #include <sys/stat.h>
8 #if defined(PETSC_HAVE_UNISTD_H)
9   #include <unistd.h>
10 #endif
11 #if defined(PETSC_HAVE_SYS_UTSNAME_H)
12   #include <sys/utsname.h>
13 #endif
14 #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
15   #include <sys/systeminfo.h>
16 #endif
17 
18 /*@C
19   PetscGetRelativePath - Given a filename, returns the relative path (removes
20   all directory specifiers).
21 
22   Not Collective; No Fortran Support
23 
24   Input Parameters:
25 + fullpath - full pathname
26 - flen     - size of `path`
27 
28   Output Parameter:
29 . path - buffer that holds relative pathname
30 
31   Level: developer
32 
33 .seealso: `PetscGetFullPath()`
34 @*/
35 PetscErrorCode PetscGetRelativePath(const char fullpath[], char path[], size_t flen)
36 {
37   char *p = NULL;
38 
39   PetscFunctionBegin;
40   /* Find string after last / or entire string if no / */
41   PetscCall(PetscStrrchr(fullpath, '/', &p));
42   PetscCall(PetscStrncpy(path, p, flen));
43   PetscFunctionReturn(PETSC_SUCCESS);
44 }
45