|
|
1.1.1.2 ! root 1: /* ! 2: Copyright (C) 1997-2001 Id Software, Inc. ! 3: ! 4: This program is free software; you can redistribute it and/or ! 5: modify it under the terms of the GNU General Public License ! 6: as published by the Free Software Foundation; either version 2 ! 7: of the License, or (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. ! 12: ! 13: See the GNU General Public License for more details. ! 14: ! 15: You should have received a copy of the GNU General Public License ! 16: along with this program; if not, write to the Free Software ! 17: Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. ! 18: ! 19: */ 1.1 root 20: #include <sys/types.h> 1.1.1.2 ! root 21: #include <sys/stat.h> 1.1 root 22: #include <errno.h> 23: #include <stdio.h> 24: #include <dirent.h> 25: #include <unistd.h> 26: #include <sys/mman.h> 27: #include <sys/time.h> 28: 29: #include "../linux/glob.h" 30: 31: #include "../qcommon/qcommon.h" 32: 33: //=============================================================================== 34: 35: byte *membase; 36: int maxhunksize; 37: int curhunksize; 38: 39: void *Hunk_Begin (int maxsize) 40: { 41: // reserve a huge chunk of memory, but don't commit any yet 42: maxhunksize = maxsize + sizeof(int); 43: curhunksize = 0; 44: membase = mmap(0, maxhunksize, PROT_READ|PROT_WRITE, 45: MAP_PRIVATE|MAP_ANONYMOUS, -1, 0); 46: if (membase == NULL || membase == (byte *)-1) 47: Sys_Error("unable to virtual allocate %d bytes", maxsize); 48: 49: *((int *)membase) = curhunksize; 50: 51: return membase + sizeof(int); 52: } 53: 54: void *Hunk_Alloc (int size) 55: { 56: byte *buf; 57: 58: // round to cacheline 59: size = (size+31)&~31; 60: if (curhunksize + size > maxhunksize) 61: Sys_Error("Hunk_Alloc overflow"); 62: buf = membase + sizeof(int) + curhunksize; 63: curhunksize += size; 64: return buf; 65: } 66: 67: int Hunk_End (void) 68: { 69: byte *n; 70: 71: n = mremap(membase, maxhunksize, curhunksize + sizeof(int), 0); 72: if (n != membase) 73: Sys_Error("Hunk_End: Could not remap virtual block (%d)", errno); 74: *((int *)membase) = curhunksize + sizeof(int); 75: 76: return curhunksize; 77: } 78: 79: void Hunk_Free (void *base) 80: { 81: byte *m; 82: 83: if (base) { 84: m = ((byte *)base) - sizeof(int); 85: if (munmap(m, *((int *)m))) 86: Sys_Error("Hunk_Free: munmap failed (%d)", errno); 87: } 88: } 89: 90: //=============================================================================== 91: 92: 93: /* 94: ================ 95: Sys_Milliseconds 96: ================ 97: */ 98: int curtime; 99: int Sys_Milliseconds (void) 100: { 101: struct timeval tp; 102: struct timezone tzp; 103: static int secbase; 104: 105: gettimeofday(&tp, &tzp); 106: 107: if (!secbase) 108: { 109: secbase = tp.tv_sec; 110: return tp.tv_usec/1000; 111: } 112: 113: curtime = (tp.tv_sec - secbase)*1000 + tp.tv_usec/1000; 114: 115: return curtime; 116: } 117: 118: void Sys_Mkdir (char *path) 119: { 120: mkdir (path, 0777); 121: } 122: 123: char *strlwr (char *s) 124: { 125: while (*s) { 126: *s = tolower(*s); 127: s++; 128: } 129: } 130: 131: //============================================ 132: 133: static char findbase[MAX_OSPATH]; 134: static char findpath[MAX_OSPATH]; 135: static char findpattern[MAX_OSPATH]; 136: static DIR *fdir; 137: 138: static qboolean CompareAttributes(char *path, char *name, 139: unsigned musthave, unsigned canthave ) 140: { 141: struct stat st; 142: char fn[MAX_OSPATH]; 143: 144: // . and .. never match 145: if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) 146: return false; 147: 1.1.1.2 ! root 148: return true; ! 149: 1.1 root 150: if (stat(fn, &st) == -1) 151: return false; // shouldn't happen 152: 153: if ( ( st.st_mode & S_IFDIR ) && ( canthave & SFF_SUBDIR ) ) 154: return false; 155: 156: if ( ( musthave & SFF_SUBDIR ) && !( st.st_mode & S_IFDIR ) ) 157: return false; 158: 159: return true; 160: } 161: 162: char *Sys_FindFirst (char *path, unsigned musthave, unsigned canhave) 163: { 164: struct dirent *d; 165: char *p; 166: 167: if (fdir) 168: Sys_Error ("Sys_BeginFind without close"); 169: 170: // COM_FilePath (path, findbase); 171: strcpy(findbase, path); 172: 173: if ((p = strrchr(findbase, '/')) != NULL) { 174: *p = 0; 175: strcpy(findpattern, p + 1); 176: } else 177: strcpy(findpattern, "*"); 178: 179: if (strcmp(findpattern, "*.*") == 0) 180: strcpy(findpattern, "*"); 181: 182: if ((fdir = opendir(findbase)) == NULL) 183: return NULL; 184: while ((d = readdir(fdir)) != NULL) { 185: if (!*findpattern || glob_match(findpattern, d->d_name)) { 186: // if (*findpattern) 187: // printf("%s matched %s\n", findpattern, d->d_name); 188: if (CompareAttributes(findbase, d->d_name, musthave, canhave)) { 189: sprintf (findpath, "%s/%s", findbase, d->d_name); 190: return findpath; 191: } 192: } 193: } 194: return NULL; 195: } 196: 197: char *Sys_FindNext (unsigned musthave, unsigned canhave) 198: { 199: struct dirent *d; 200: 201: if (fdir == NULL) 202: return NULL; 203: while ((d = readdir(fdir)) != NULL) { 204: if (!*findpattern || glob_match(findpattern, d->d_name)) { 205: // if (*findpattern) 206: // printf("%s matched %s\n", findpattern, d->d_name); 207: if (CompareAttributes(findbase, d->d_name, musthave, canhave)) { 208: sprintf (findpath, "%s/%s", findbase, d->d_name); 209: return findpath; 210: } 211: } 212: } 213: return NULL; 214: } 215: 216: void Sys_FindClose (void) 217: { 218: if (fdir != NULL) 219: closedir(fdir); 220: fdir = NULL; 221: } 222: 223: 224: //============================================ 225:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.