|
|
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> 21: #include <errno.h> 22: #include <stdio.h> 23: #include <dirent.h> 24: #include <sys/stat.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: maxhunksize = maxsize + sizeof(int); 42: curhunksize = 0; 43: /* membase = mmap(0, maxhunksize, PROT_READ|PROT_WRITE, */ 44: /* MAP_PRIVATE, -1, 0); */ 45: /* if ((membase == NULL) || (membase == MAP_FAILED)) */ 46: membase = malloc(maxhunksize); 47: if (membase == NULL) 48: Com_Error(ERR_FATAL, "unable to virtual allocate %d bytes", maxsize); 49: 50: *((int *)membase) = curhunksize; 51: 52: return membase + sizeof(int); 53: } 54: 55: void *Hunk_Alloc (int size) 56: { 57: byte *buf; 58: 59: // round to cacheline 60: size = (size+31)&~31; 61: if (curhunksize + size > maxhunksize) 62: Com_Error(ERR_FATAL, "Hunk_Alloc overflow"); 63: buf = membase + sizeof(int) + curhunksize; 64: curhunksize += size; 65: return buf; 66: } 67: 68: int Hunk_End (void) 69: { 70: return curhunksize; 71: } 72: 73: void Hunk_Free (void *base) 74: { 75: byte *m; 76: 77: if (base) { 78: m = ((byte *)base) - sizeof(int); 79: free(m); 80: } 81: } 82: 83: //=============================================================================== 84: 85: 86: /* 87: ================ 88: Sys_Milliseconds 89: ================ 90: */ 91: int curtime; 92: int Sys_Milliseconds (void) 93: { 94: struct timeval tp; 95: struct timezone tzp; 96: static int secbase; 97: 98: gettimeofday(&tp, &tzp); 99: 100: if (!secbase) 101: { 102: secbase = tp.tv_sec; 103: return tp.tv_usec/1000; 104: } 105: 106: curtime = (tp.tv_sec - secbase)*1000 + tp.tv_usec/1000; 107: 108: return curtime; 109: } 110: 111: void Sys_Mkdir (char *path) 112: { 113: mkdir (path, 0777); 114: } 115: 116: char *strlwr (char *s) 117: { 118: char *origs = s; 119: while (*s) { 120: *s = tolower(*s); 121: s++; 122: } 123: return origs; 124: } 125: 126: //============================================ 127: 128: static char findbase[MAX_OSPATH]; 129: static char findpath[MAX_OSPATH]; 130: static char findpattern[MAX_OSPATH]; 131: static DIR *fdir; 132: 133: static qboolean CompareAttributes(char *path, char *name, 134: unsigned musthave, unsigned canthave ) 135: { 136: struct stat st; 137: char fn[MAX_OSPATH]; 138: 139: // . and .. never match 140: if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0) 141: return false; 142: 143: sprintf(fn, "%s/%s", path, name); 144: if (stat(fn, &st) == -1) 145: return false; // shouldn't happen 146: 147: if ( ( st.st_mode & S_IFDIR ) && ( canthave & SFF_SUBDIR ) ) 148: return false; 149: 150: if ( ( musthave & SFF_SUBDIR ) && !( st.st_mode & S_IFDIR ) ) 151: return false; 152: 153: return true; 154: } 155: 156: char *Sys_FindFirst (char *path, unsigned musthave, unsigned canhave) 157: { 158: struct dirent *d; 159: char *p; 160: 161: if (fdir) 162: Sys_Error ("Sys_BeginFind without close"); 163: 164: // COM_FilePath (path, findbase); 165: strcpy(findbase, path); 166: 167: if ((p = strrchr(findbase, '/')) != NULL) { 168: *p = 0; 169: strcpy(findpattern, p + 1); 170: } else 171: strcpy(findpattern, "*"); 172: 173: if (strcmp(findpattern, "*.*") == 0) 174: strcpy(findpattern, "*"); 175: 176: if ((fdir = opendir(findbase)) == NULL) 177: return NULL; 178: while ((d = readdir(fdir)) != NULL) { 179: if (!*findpattern || glob_match(findpattern, d->d_name)) { 180: // if (*findpattern) 181: // printf("%s matched %s\n", findpattern, d->d_name); 182: if (CompareAttributes(findbase, d->d_name, musthave, canhave)) { 183: sprintf (findpath, "%s/%s", findbase, d->d_name); 184: return findpath; 185: } 186: } 187: } 188: return NULL; 189: } 190: 191: char *Sys_FindNext (unsigned musthave, unsigned canhave) 192: { 193: struct dirent *d; 194: 195: if (fdir == NULL) 196: return NULL; 197: while ((d = readdir(fdir)) != NULL) { 198: if (!*findpattern || glob_match(findpattern, d->d_name)) { 199: // if (*findpattern) 200: // printf("%s matched %s\n", findpattern, d->d_name); 201: if (CompareAttributes(findbase, d->d_name, musthave, canhave)) { 202: sprintf (findpath, "%s/%s", findbase, d->d_name); 203: return findpath; 204: } 205: } 206: } 207: return NULL; 208: } 209: 210: void Sys_FindClose (void) 211: { 212: if (fdir != NULL) 213: closedir(fdir); 214: fdir = NULL; 215: } 216: 217: 218: //============================================ 219:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.