#include <petscsys.h>
#if defined(PETSC_HAVE_PWD_H)
  #include <pwd.h>
#endif
#include <ctype.h>
#include <sys/stat.h>
#if defined(PETSC_HAVE_UNISTD_H)
  #include <unistd.h>
#endif
#if defined(PETSC_HAVE_SYS_UTSNAME_H)
  #include <sys/utsname.h>
#endif
#if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
  #include <sys/systeminfo.h>
#endif

/*@C
  PetscGetRelativePath - Given a filename, returns the relative path (removes
  all directory specifiers).

  Not Collective; No Fortran Support

  Input Parameters:
+ fullpath - full pathname
- flen     - size of `path`

  Output Parameter:
. path - buffer that holds relative pathname

  Level: developer

.seealso: `PetscGetFullPath()`
@*/
PetscErrorCode PetscGetRelativePath(const char fullpath[], char path[], size_t flen)
{
  char *p = NULL;

  PetscFunctionBegin;
  /* Find string after last / or entire string if no / */
  PetscCall(PetscStrrchr(fullpath, '/', &p));
  PetscCall(PetscStrncpy(path, p, flen));
  PetscFunctionReturn(PETSC_SUCCESS);
}
