environment.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  1. /* libanode: the Anode C reference implementation
  2. * Copyright (C) 2009-2010 Adam Ierymenko <[email protected]>
  3. *
  4. * This program is free software: you can redistribute it and/or modify
  5. * it under the terms of the GNU General Public License as published by
  6. * the Free Software Foundation, either version 3 of the License, or
  7. * (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. * GNU General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program. If not, see <http://www.gnu.org/licenses/>. */
  16. #include <stdio.h>
  17. #include <stdlib.h>
  18. #include "environment.h"
  19. #ifdef WINDOWS
  20. #include <windows.h>
  21. #else
  22. #include <sys/stat.h>
  23. #include <string.h>
  24. #endif
  25. static char Anode_cache_base[1024] = { 0 };
  26. const char *Anode_get_cache()
  27. {
  28. if (Anode_cache_base[0])
  29. return Anode_cache_base;
  30. #ifdef WINDOWS
  31. #else
  32. char tmp[1024];
  33. char home[1024];
  34. unsigned int i;
  35. struct stat st;
  36. const char *_home = getenv("HOME");
  37. if (!_home)
  38. return (const char *)0;
  39. for(i=0;i<sizeof(home);++i) {
  40. home[i] = _home[i];
  41. if (!home[i]) {
  42. if (i == 0)
  43. return (const char *)0;
  44. else if (home[i-1] == ANODE_PATH_SEPARATOR)
  45. home[i-1] = (char)0;
  46. break;
  47. }
  48. }
  49. if (i == sizeof(home))
  50. return (const char *)0;
  51. #ifdef __APPLE__
  52. snprintf(tmp,sizeof(tmp),"%s%cLibrary",home,ANODE_PATH_SEPARATOR);
  53. tmp[sizeof(tmp)-1] = (char)0;
  54. if (!stat(tmp,&st)) {
  55. sprintf(tmp,"%s%cLibrary%cCaches",home,ANODE_PATH_SEPARATOR,ANODE_PATH_SEPARATOR);
  56. if (stat(tmp,&st)) {
  57. if (mkdir(tmp,0700))
  58. return (const char *)0;
  59. }
  60. snprintf(Anode_cache_base,sizeof(Anode_cache_base),"%s%ccom.zerotier.anode",tmp,ANODE_PATH_SEPARATOR);
  61. Anode_cache_base[sizeof(Anode_cache_base)-1] = (char)0;
  62. if (stat(Anode_cache_base,&st)) {
  63. if (mkdir(Anode_cache_base,0700)) {
  64. Anode_cache_base[0] = (char)0;
  65. return (const char *)0;
  66. }
  67. }
  68. return Anode_cache_base;
  69. }
  70. #endif
  71. snprintf(tmp,sizeof(tmp),"%s%c.anode",home,ANODE_PATH_SEPARATOR);
  72. tmp[sizeof(tmp)-1] = (char)0;
  73. if (stat(tmp,&st)) {
  74. if (mkdir(tmp,0700)) {
  75. Anode_cache_base[0] = (char)0;
  76. return (const char *)0;
  77. }
  78. }
  79. snprintf(Anode_cache_base,sizeof(Anode_cache_base),"%s%ccaches",tmp,ANODE_PATH_SEPARATOR);
  80. Anode_cache_base[sizeof(Anode_cache_base)-1] = (char)0;
  81. if (stat(Anode_cache_base,&st)) {
  82. if (mkdir(Anode_cache_base,0700)) {
  83. Anode_cache_base[0] = (char)0;
  84. return (const char *)0;
  85. }
  86. }
  87. return Anode_cache_base;
  88. #endif
  89. }
  90. char *Anode_get_cache_sub(const char *cache_subdir,char *buf,unsigned int len)
  91. {
  92. struct stat st;
  93. const char *cache_base = Anode_get_cache();
  94. if (!len)
  95. return (char *)0;
  96. if (!cache_base)
  97. return (char *)0;
  98. snprintf(buf,len,"%s%c%s",cache_base,ANODE_PATH_SEPARATOR,cache_subdir);
  99. buf[len-1] = (char)0;
  100. if (stat(buf,&st)) {
  101. if (mkdir(buf,0700))
  102. return (char *)0;
  103. }
  104. return buf;
  105. }