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