xref: /petsc/src/sys/fileio/fwd.c (revision ab4ee011827fbbb77f789779c813f6db6bb0cbfa)
1 
2 /*
3       Code for manipulating 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 #if defined(PETSC_HAVE_DIRECT_H)
18   #include <direct.h>
19 #endif
20 #if defined(PETSC_HAVE_SYS_SYSTEMINFO_H)
21   #include <sys/systeminfo.h>
22 #endif
23 #include <errno.h>
24 
25 /*@C
26    PetscGetWorkingDirectory - Gets the current working directory.
27 
28    Not Collective
29 
30    Input Parameter:
31 .  len  - maximum length of path
32 
33    Output Parameter:
34 .  path - use to hold the result value. The string should be long enough
35           to hold the path.
36 
37    Level: developer
38 
39 .seealso: `PetscGetTmp()`, `PetscSharedTmp()`, `PetscSharedWorkingDirectory()`, `PetscGetHomeDirectory()`
40 @*/
41 PetscErrorCode PetscGetWorkingDirectory(char path[], size_t len)
42 {
43   PetscFunctionBegin;
44 #if defined(PETSC_HAVE_GETCWD)
45   PetscCheck(getcwd(path, len), PETSC_COMM_SELF, PETSC_ERR_LIB, "Error in getcwd() due to \"%s\"", strerror(errno));
46 #elif defined(PETSC_HAVE__GETCWD)
47   _getcwd(path, len);
48 #elif defined(PETSC_HAVE_GETWD)
49   getwd(path);
50 #else
51   SETERRQ(PETSC_COMM_SELF, PETSC_ERR_SUP_SYS, "Could not find getcwd() or getwd()");
52 #endif
53   PetscFunctionReturn(PETSC_SUCCESS);
54 }
55