Is the code below actually the best way to get the present working directory? The code must be portable to many flavors of Unix and Windows. The code works, but is very long for a simple thing.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define WIN
#ifdef UNIX
#include <strings.h>
#endif
main()
{
FILE *fp;
char str[100], *cmd;
#ifdef UNIX
cmd = "pwd >pwd.out";
#else
cmd = "cd >pwd.out";
#endif
system(cmd);
fp = fopen("pwd.out", "r");
fgets(str, 100, fp);
fclose(fp);
str[strlen(str)-1] = '\0'; // get rid of newline.
printf("%s\n", str);
}