1

I run some fortran source code from a C program using a dll. I want to use CALL GETCWD(DIRNAME) in Fortran to access files. Is the Current Working Directory (CWD) the directory where my fortran dll is located or where my C-code is located?

  • 1
    ***C**urrent **W**orking **D**irectory*. http://man7.org/linux/man-pages/man2/getcwd.2.html. It's the directory where you started the program from. – CristiFati Jan 22 '19 at 11:31
  • Be aware that `getcwd` is not part of the fortran standard but a common extension of various compilers such as gfortran, ifort, etc ... . Related: https://stackoverflow.com/questions/30279228/is-there-an-alternative-to-getcwd-in-fortran-2003-2008 – kvantour Jan 22 '19 at 11:39
  • @kvantour i didn't know that, thanks! i'm using gfortran. – Alexander Vandenberghe Jan 22 '19 at 11:47
  • @CristiFati the source you link says "the current working directory of the calling process". I assume the calling process is then the DLL and not my C-code right? – Alexander Vandenberghe Jan 22 '19 at 11:49
  • No, it's the executable that results from building your *C* code (and probably that loads the *.dll*). – CristiFati Jan 22 '19 at 11:53
  • ok that makes sense! Thanks! Feel free to post your comment as an answer. – Alexander Vandenberghe Jan 22 '19 at 11:53

1 Answers1

1

CWD stands for Current Working Directory, and it's (usually) the directory where the current process was launched from. Check [Man7]: GETCWD(3) for more details. I prepared a small example for better understanding what's going on.

code00.c:

#include <errno.h>
#include <stdio.h>
#include <unistd.h>

#define PATH_SIZE 0x0200


int main()
{
    char buf[PATH_SIZE];
    if (getcwd(buf, PATH_SIZE) == NULL) {
        printf("Error %d getting CWD\n", errno);
        return 1;
    }
    printf("CWD: [%s]\n", buf);
    return 0;
}

Output:

[cfati@cfati-5510-0:/mnt/e/Work/Dev/StackOverflow/q054306561]> ~/sopr.sh
### Set shorter prompt to better fit when pasted in StackOverflow (or other) pages ###

[064bit prompt]> ls
code00.c
[064bit prompt]> gcc -o cwd code00.c
[064bit prompt]>
[064bit prompt]> ls
code00.c  cwd
[064bit prompt]> ./cwd
CWD: [/mnt/e/Work/Dev/StackOverflow/q054306561]
[064bit prompt]>
[064bit prompt]> pushd .. && ./q054306561/cwd && popd
/mnt/e/Work/Dev/StackOverflow /mnt/e/Work/Dev/StackOverflow/q054306561
CWD: [/mnt/e/Work/Dev/StackOverflow]
/mnt/e/Work/Dev/StackOverflow/q054306561
[064bit prompt]>
[064bit prompt]> mkdir test && pushd test && ../cwd && popd
/mnt/e/Work/Dev/StackOverflow/q054306561/test /mnt/e/Work/Dev/StackOverflow/q054306561
CWD: [/mnt/e/Work/Dev/StackOverflow/q054306561/test]
/mnt/e/Work/Dev/StackOverflow/q054306561
CristiFati
  • 38,250
  • 9
  • 50
  • 87