q_shsolaris.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. #include <sys/types.h>
  2. #include <errno.h>
  3. #include <stdio.h>
  4. #include <dirent.h>
  5. #include <sys/stat.h>
  6. #include <unistd.h>
  7. #include <sys/mman.h>
  8. #include <sys/time.h>
  9. #include "../linux/glob.h"
  10. #include "../qcommon/qcommon.h"
  11. //===============================================================================
  12. byte *membase;
  13. int maxhunksize;
  14. int curhunksize;
  15. void *Hunk_Begin (int maxsize)
  16. {
  17. // reserve a huge chunk of memory, but don't commit any yet
  18. maxhunksize = maxsize;
  19. curhunksize = 0;
  20. membase = malloc(maxhunksize);
  21. if (membase == NULL)
  22. Sys_Error(ERR_FATAL, "unable to allocate %d bytes", maxsize);
  23. return membase;
  24. }
  25. void *Hunk_Alloc (int size)
  26. {
  27. byte *buf;
  28. // round to cacheline
  29. size = (size+31)&~31;
  30. if (curhunksize + size > maxhunksize)
  31. Sys_Error(ERR_FATAL, "Hunk_Alloc overflow");
  32. buf = membase + curhunksize;
  33. curhunksize += size;
  34. return buf;
  35. }
  36. int Hunk_End (void)
  37. {
  38. byte *n;
  39. n = realloc(membase, curhunksize);
  40. if (n != membase)
  41. Sys_Error(ERR_FATAL, "Hunk_End: Could not remap virtual block (%d)", errno);
  42. return curhunksize;
  43. }
  44. void Hunk_Free (void *base)
  45. {
  46. if (base)
  47. free(base);
  48. }
  49. //===============================================================================
  50. /*
  51. ================
  52. Sys_Milliseconds
  53. ================
  54. */
  55. int curtime;
  56. int Sys_Milliseconds (void)
  57. {
  58. struct timeval tp;
  59. struct timezone tzp;
  60. static int secbase;
  61. gettimeofday(&tp, &tzp);
  62. if (!secbase)
  63. {
  64. secbase = tp.tv_sec;
  65. return tp.tv_usec/1000;
  66. }
  67. curtime = (tp.tv_sec - secbase)*1000 + tp.tv_usec/1000;
  68. return curtime;
  69. }
  70. void Sys_Mkdir (char *path)
  71. {
  72. mkdir (path, 0777);
  73. }
  74. char *strlwr (char *s)
  75. {
  76. while (*s) {
  77. *s = tolower(*s);
  78. s++;
  79. }
  80. }
  81. //============================================
  82. static char findbase[MAX_OSPATH];
  83. static char findpath[MAX_OSPATH];
  84. static char findpattern[MAX_OSPATH];
  85. static DIR *fdir;
  86. static qboolean CompareAttributes(char *path, char *name,
  87. unsigned musthave, unsigned canthave )
  88. {
  89. struct stat st;
  90. char fn[MAX_OSPATH];
  91. // . and .. never match
  92. if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
  93. return false;
  94. sprintf(fn, "%s/%s", path, name);
  95. if (stat(fn, &st) == -1)
  96. return false; // shouldn't happen
  97. if ( ( st.st_mode & S_IFDIR ) && ( canthave & SFF_SUBDIR ) )
  98. return false;
  99. if ( ( musthave & SFF_SUBDIR ) && !( st.st_mode & S_IFDIR ) )
  100. return false;
  101. return true;
  102. }
  103. char *Sys_FindFirst (char *path, unsigned musthave, unsigned canhave)
  104. {
  105. struct dirent *d;
  106. char *p;
  107. if (fdir)
  108. Sys_Error ("Sys_BeginFind without close");
  109. // COM_FilePath (path, findbase);
  110. strcpy(findbase, path);
  111. if ((p = strrchr(findbase, '/')) != NULL) {
  112. *p = 0;
  113. strcpy(findpattern, p + 1);
  114. } else
  115. strcpy(findpattern, "*");
  116. if (strcmp(findpattern, "*.*") == 0)
  117. strcpy(findpattern, "*");
  118. if ((fdir = opendir(path)) == NULL)
  119. return NULL;
  120. while ((d = readdir(fdir)) != NULL) {
  121. if (!*findpattern || glob_match(findpattern, d->d_name)) {
  122. // if (*findpattern)
  123. // printf("%s matched %s\n", findpattern, d->d_name);
  124. if (CompareAttributes(findbase, d->d_name, musthave, canhave)) {
  125. sprintf (findpath, "%s/%s", findbase, d->d_name);
  126. return findpath;
  127. }
  128. }
  129. }
  130. return NULL;
  131. }
  132. char *Sys_FindNext (unsigned musthave, unsigned canhave)
  133. {
  134. struct dirent *d;
  135. if (fdir == NULL)
  136. return NULL;
  137. while ((d = readdir(fdir)) != NULL) {
  138. if (!*findpattern || glob_match(findpattern, d->d_name)) {
  139. // if (*findpattern)
  140. // printf("%s matched %s\n", findpattern, d->d_name);
  141. if (CompareAttributes(findbase, d->d_name, musthave, canhave)) {
  142. sprintf (findpath, "%s/%s", findbase, d->d_name);
  143. return findpath;
  144. }
  145. }
  146. }
  147. return NULL;
  148. }
  149. void Sys_FindClose (void)
  150. {
  151. if (fdir != NULL)
  152. closedir(fdir);
  153. fdir = NULL;
  154. }
  155. //============================================