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