main.cxx 1.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #if defined(_WIN32) && (defined(_MSC_VER) || defined(__WATCOMC__) || defined(__BORLANDC__) || defined(__MINGW32__))
  4. #include <io.h>
  5. #include <direct.h>
  6. #if defined(__WATCOMC__)
  7. #include <direct.h>
  8. #define _getcwd getcwd
  9. #endif
  10. inline const char* Getcwd(char* buf, unsigned int len)
  11. {
  12. const char* ret = _getcwd(buf, len);
  13. if(!ret)
  14. {
  15. fprintf(stderr, "No current working directory.\n");
  16. abort();
  17. }
  18. // make sure the drive letter is capital
  19. if(strlen(buf) > 1 && buf[1] == ':')
  20. {
  21. buf[0] = toupper(buf[0]);
  22. }
  23. return ret;
  24. }
  25. #else
  26. #include <sys/types.h>
  27. #include <fcntl.h>
  28. #include <unistd.h>
  29. inline const char* Getcwd(char* buf, unsigned int len)
  30. {
  31. const char* ret = getcwd(buf, len);
  32. if(!ret)
  33. {
  34. fprintf(stderr, "No current working directory\n");
  35. abort();
  36. }
  37. return ret;
  38. }
  39. #endif
  40. int main(int argc, char *argv[])
  41. {
  42. char buf[2048];
  43. const char *cwd = Getcwd(buf, sizeof(buf));
  44. fprintf(stdout, "Working directory: %s\n", cwd);
  45. return 0;
  46. }