1

I'm using the Cygwin gcc compiler to compile and test a C program using Code::Blocks. I have a problem with the output of the "getcwd" function (unistd.h):

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

int main(int argc, char *argv[]) {
    char workingDir[PATH_MAX];

    if (getcwd(workingDir, sizeof(workingDir)) != NULL) {
        fprintf(stdout, "Working directory: %s\n", workingDir);
    }
}

When running in Code::Blocks, the getcwd function returns a full Cygwin path:

C:\Users\mrosh\CodeBlocks\GetcwdTest\bin\Debug\GetcwdTest.exe
Working directory: /cygdrive/c/Users/mrosh/GetcwdTest/GetcwdTest/bin/Debug

When running from the Windows command line, I only get the current working folder:

C:\Users\mrosh\CodeBlocks\GetcwdTest\bin\Debug>GetcwdTest.exe
Working directory: /Debug

I really need access to the full path from within my program, while running from the Windows command line. Any idea how I achieve that?

mroshaw
  • 337
  • 1
  • 4
  • 12
  • Possible duplicate of [How to get Current Directory?](https://stackoverflow.com/questions/875249/how-to-get-current-directory) – Swordfish Nov 16 '18 at 13:59
  • @Swordfish - that article is C++ related, unfortunately, and refers to a "windows.h" header that's not available to me in my Cygwin C environment. – mroshaw Nov 16 '18 at 14:05
  • When you don't have `` you will have to live with it that `getcwd()` doesn't work outside the cygwin-garden. – Swordfish Nov 16 '18 at 14:06
  • https://stackoverflow.com/a/9981933/3975177 – Swordfish Nov 16 '18 at 14:14
  • Thank you for this! I'm happy with the output I'm getting when I run within Code:Blocks (/cygdrive/c/.../debug) as I can work with that. The problem is when I'm running outside of Code:Blocks, in the command line, where I'm only getting the current folder (/debug). So, it's not a Windows path I'm after, it's just the complete path that I see when running within my IDE. – mroshaw Nov 16 '18 at 14:37
  • 2
    if you don't run your program in a cygwin shell there simply IS NO "cygwin"-path – Swordfish Nov 16 '18 at 14:43
  • Ha! Yes, gotcha! Running from $bash obviously solves my problem. What a dope! Hahaha! – mroshaw Nov 16 '18 at 14:51
  • 1
    Does Cygwin still have a run.exe (c:\cygwin\bin\run.exe or c:\cygwin64\bin\run.exe) for running Cygwin executables from a "normal" windows environment? – Ian Abbott Nov 16 '18 at 16:59

0 Answers0