q_shirix.c 3.8 KB

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