executionEnvironment.cxx 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Filename: executionEnvironment.cxx
  2. // Created by: drose (15May00)
  3. //
  4. ////////////////////////////////////////////////////////////////////
  5. //
  6. // PANDA 3D SOFTWARE
  7. // Copyright (c) 2001, Disney Enterprises, Inc. All rights reserved
  8. //
  9. // All use of this software is subject to the terms of the Panda 3d
  10. // Software license. You should have received a copy of this license
  11. // along with this source code; you will also find a current copy of
  12. // the license at http://www.panda3d.org/license.txt .
  13. //
  14. // To contact the maintainers of this program write to
  15. // [email protected] .
  16. //
  17. ////////////////////////////////////////////////////////////////////
  18. #include "executionEnvironment.h"
  19. #ifdef HAVE_UNISTD_H
  20. #include <unistd.h>
  21. #endif
  22. #include <errno.h>
  23. #include <stdio.h> // for perror
  24. #ifdef WIN32_VC
  25. // Windows requires this for getcwd().
  26. #include <direct.h>
  27. #define getcwd _getcwd
  28. #endif
  29. ////////////////////////////////////////////////////////////////////
  30. // Function: ExecutionEnviroment::get_cwd
  31. // Access: Public, Static
  32. // Description: Returns the name of the current working directory.
  33. ////////////////////////////////////////////////////////////////////
  34. Filename ExecutionEnvironment::
  35. get_cwd() {
  36. // getcwd() requires us to allocate a dynamic buffer and grow it on
  37. // demand.
  38. static size_t bufsize = 1024;
  39. static char *buffer = NULL;
  40. if (buffer == (char *)NULL) {
  41. buffer = new char[bufsize];
  42. }
  43. while (getcwd(buffer, bufsize) == (char *)NULL) {
  44. if (errno != ERANGE) {
  45. perror("getcwd");
  46. return string();
  47. }
  48. delete[] buffer;
  49. bufsize = bufsize * 2;
  50. buffer = new char[bufsize];
  51. assert(buffer != (char *)NULL);
  52. }
  53. return Filename::from_os_specific(buffer);
  54. }