/premake/Src/platform_posix.c

https://github.com/nmacherey/rheia · C · 249 lines · 173 code · 53 blank · 23 comment · 38 complexity · 03d8ca6214ca008aad891054d7ed15c4 MD5 · raw file

  1. /**********************************************************************
  2. * Premake - platform_posix.h
  3. * Windows-specific functions.
  4. *
  5. * Copyright (c) 2002-2005 Jason Perkins.
  6. *
  7. * This program is free software; you can redistribute it and/or
  8. * modify it under the terms of the GNU General Public License
  9. * as published by the Free Software Foundation; either version 2
  10. * of the License, or (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License in the file LICENSE.txt for details.
  16. **********************************************************************/
  17. #include "os.h"
  18. #if defined(PLATFORM_POSIX)
  19. #include <stdio.h>
  20. #include <stdlib.h>
  21. #include <dlfcn.h>
  22. #include <dirent.h>
  23. #include <fnmatch.h>
  24. #include <string.h>
  25. #include <unistd.h>
  26. #include <sys/stat.h>
  27. #include <ctype.h>
  28. #include "io.h"
  29. #include "path.h"
  30. #include "util.h"
  31. static char buffer[8192];
  32. struct PlatformMaskData
  33. {
  34. DIR* handle;
  35. struct dirent* entry;
  36. char* mask;
  37. };
  38. int platform_chdir(const char* path)
  39. {
  40. return !chdir(path);
  41. }
  42. int platform_copyfile(const char* src, const char* dest)
  43. {
  44. sprintf(buffer, "cp %s %s", src, dest);
  45. return (system(buffer) == 0);
  46. }
  47. static int findLibHelper(const char* lib, const char* path)
  48. {
  49. struct stat sb;
  50. sprintf(buffer, "%s/lib%s.so", path, lib);
  51. if (stat(buffer, &sb) == 0 && !S_ISDIR(sb.st_mode)) return 1;
  52. sprintf(buffer, "%s/%s.so", path, lib);
  53. if (stat(buffer, &sb) == 0 && !S_ISDIR(sb.st_mode)) return 1;
  54. sprintf(buffer, "%s/%s", path, lib);
  55. if (stat(buffer, &sb) == 0 && !S_ISDIR(sb.st_mode)) return 1;
  56. return 0;
  57. }
  58. int platform_findlib(const char* name, char* buffer, int len)
  59. {
  60. FILE* file;
  61. char* libpaths;
  62. char* token;
  63. libpaths = getenv("LD_LIBRARY_PATH");
  64. token = libpaths != NULL ? strtok(libpaths, ":") : NULL;
  65. while (token != NULL)
  66. {
  67. if (findLibHelper(name, buffer))
  68. {
  69. strcpy(buffer, token);
  70. return 1;
  71. }
  72. token = strtok(NULL, ":");
  73. }
  74. file = fopen("/etc/ld.so.conf", "rt");
  75. if (file == NULL)
  76. return 0;
  77. while (!feof(file))
  78. {
  79. /* Read a line and trim off any trailing whitespace */
  80. char* ptr;
  81. fgets(buffer, 4096, file);
  82. ptr = &buffer[strlen(buffer) - 1];
  83. while (isspace(*ptr))
  84. *(ptr--) = '\0';
  85. if (findLibHelper(name, buffer))
  86. {
  87. fclose(file);
  88. return 1;
  89. }
  90. }
  91. fclose(file);
  92. if (findLibHelper(name, "/lib"))
  93. {
  94. strcpy(buffer, "/lib");
  95. return 1;
  96. }
  97. if (findLibHelper(name, "/usr/lib"))
  98. {
  99. strcpy(buffer, "/usr/lib");
  100. return 1;
  101. }
  102. return 0;
  103. }
  104. int platform_getcwd(char* buffer, int len)
  105. {
  106. return (getcwd(buffer, len) == 0);
  107. }
  108. void platform_getuuid(char* uuid)
  109. {
  110. FILE* rnd = fopen("/dev/random", "rb");
  111. fread(uuid, 16, 1, rnd);
  112. fclose(rnd);
  113. }
  114. char platform_getseparator()
  115. {
  116. return '/';
  117. }
  118. int platform_isAbsolutePath(const char* path)
  119. {
  120. return (path[0] == '/');
  121. }
  122. int platform_mask_close(MaskHandle data)
  123. {
  124. if (data->handle != NULL)
  125. closedir(data->handle);
  126. free(data->mask);
  127. free(data);
  128. return 1;
  129. }
  130. const char* platform_mask_getname(MaskHandle data)
  131. {
  132. strcpy(buffer, path_getdir(data->mask));
  133. if (strlen(buffer) > 0)
  134. strcat(buffer, "/");
  135. strcat(buffer, data->entry->d_name);
  136. return buffer;
  137. }
  138. int platform_mask_getnext(MaskHandle data)
  139. {
  140. const char* mask = path_getname(data->mask);
  141. if (data->handle == NULL)
  142. return 0;
  143. data->entry = readdir(data->handle);
  144. while (data->entry != NULL)
  145. {
  146. if (fnmatch(mask, data->entry->d_name, 0) == 0)
  147. return 1;
  148. data->entry = readdir(data->handle);
  149. }
  150. return 0;
  151. }
  152. int platform_mask_isfile(MaskHandle data)
  153. {
  154. /* Converting to absolute removes any directories from
  155. * the path that haven't been created yet (see below) */
  156. struct stat info;
  157. const char* name = platform_mask_getname(data);
  158. name = path_absolute(name);
  159. if (stat(name, &info) == 0)
  160. {
  161. return S_ISREG(info.st_mode);
  162. }
  163. return 0;
  164. }
  165. MaskHandle platform_mask_open(const char* mask)
  166. {
  167. const char* path = path_getdir(mask);
  168. if (strlen(path) == 0)
  169. path = ".";
  170. MaskHandle data = ALLOCT(struct PlatformMaskData);
  171. data->mask = (char*)malloc(strlen(mask) + 1);
  172. strcpy(data->mask, mask);
  173. /* Converting to absolute path removes any directories
  174. * from the path that haven't been created yet. Can
  175. * happen if package.path = (something) where (something)
  176. * doesn't exist at the time the script is running. */
  177. path = path_absolute(path);
  178. data->handle = opendir(path);
  179. return data;
  180. }
  181. int platform_mkdir(const char* path)
  182. {
  183. return (mkdir(path, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH) == 0);
  184. }
  185. int platform_remove(const char* path)
  186. {
  187. unlink(path);
  188. return 1;
  189. }
  190. int platform_rmdir(const char* path)
  191. {
  192. strcpy(buffer, "rm -rf ");
  193. strcat(buffer, path);
  194. return (system(buffer) == 0);
  195. }
  196. #endif