Quake-2

Quake 2 GPL Source Release
Log | Files | Refs

q_shirix.c (3897B)


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