Search code examples
cdllfortran

current working directory when executing a dll


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?


Solution

  • 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