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 */
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 "../solaris/glob.h"
30 
31 #include "../qcommon/qcommon.h"
32 
33 //===============================================================================
34 
35 byte *membase;
36 int maxhunksize;
37 int curhunksize;
38 
Hunk_Begin(int maxsize)39 void *Hunk_Begin (int maxsize)
40 {
41 	// reserve a huge chunk of memory, but don't commit any yet
42 	maxhunksize = maxsize;
43 	curhunksize = 0;
44 	membase = malloc(maxhunksize);
45 	/* DEBUG: eliasm */
46 	memset( membase, 0, maxhunksize );
47 	/* DEBUG: eliasm */
48 	if (membase == NULL)
49 		Sys_Error(ERR_FATAL, "unable to allocate %d bytes", maxsize);
50 
51 	return membase;
52 }
53 
Hunk_Alloc(int size)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(ERR_FATAL, "Hunk_Alloc overflow");
62 	buf = membase + curhunksize;
63 	curhunksize += size;
64 	return buf;
65 }
66 
Hunk_End(void)67 int Hunk_End (void)
68 {
69 	byte *n;
70 
71 	n = realloc(membase, curhunksize);
72 	if (n != membase)
73 		Sys_Error(ERR_FATAL, "Hunk_End:  Could not remap virtual block (%d)", errno);
74 
75 	return curhunksize;
76 }
77 
Hunk_Free(void * base)78 void Hunk_Free (void *base)
79 {
80 	if (base)
81 		free(base);
82 }
83 
84 //===============================================================================
85 
86 
87 /*
88 ================
89 Sys_Milliseconds
90 ================
91 */
92 int curtime;
xSys_Milliseconds(void)93 int xSys_Milliseconds (void)
94 {
95 	struct timeval tp;
96 	struct timezone tzp;
97 	static int		secbase;
98 
99 	gettimeofday(&tp, &tzp);
100 
101 	if (!secbase)
102 	{
103 		secbase = tp.tv_sec;
104 		return tp.tv_usec/1000;
105 	}
106 
107 	curtime = (tp.tv_sec - secbase)*1000 + tp.tv_usec/1000;
108 
109 	return curtime;
110 }
111 
112 extern hrtime_t base_hrtime;
113 
Sys_Milliseconds(void)114 int Sys_Milliseconds( void )
115 {
116   hrtime_t curr_hrtime;
117 
118   curr_hrtime = gethrtime();
119 
120   curtime = (curr_hrtime - base_hrtime) / 1000000LL;
121 
122   return curtime;
123 }
124 
125 
Sys_Mkdir(char * path)126 void Sys_Mkdir (char *path)
127 {
128     mkdir (path, 0777);
129 }
130 
strlwr(char * s)131 char *strlwr (char *s)
132 {
133 	while (*s) {
134 		*s = tolower(*s);
135 		s++;
136 	}
137 }
138 
139 //============================================
140 
141 static	char	findbase[MAX_OSPATH];
142 static	char	findpath[MAX_OSPATH];
143 static	char	findpattern[MAX_OSPATH];
144 static	DIR		*fdir;
145 
CompareAttributes(char * path,char * name,unsigned musthave,unsigned canthave)146 static qboolean CompareAttributes(char *path, char *name,
147 	unsigned musthave, unsigned canthave )
148 {
149 	struct stat st;
150 	char fn[MAX_OSPATH];
151 
152 // . and .. never match
153 	if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
154 		return false;
155 
156 	return true;
157 	//sprintf(fn, "%s/%s", path, name);
158 	if (stat(fn, &st) == -1)
159 		return false; // shouldn't happen
160 
161 	if ( ( st.st_mode & S_IFDIR ) && ( canthave & SFF_SUBDIR ) )
162 		return false;
163 
164 	if ( ( musthave & SFF_SUBDIR ) && !( st.st_mode & S_IFDIR ) )
165 		return false;
166 
167 	return true;
168 }
169 
Sys_FindFirst(char * path,unsigned musthave,unsigned canhave)170 char *Sys_FindFirst (char *path, unsigned musthave, unsigned canhave)
171 {
172 	struct dirent *d;
173 	char *p;
174 
175 	if (fdir)
176 		Sys_Error ("Sys_BeginFind without close");
177 
178 //	COM_FilePath (path, findbase);
179 	strcpy(findbase, path);
180 
181 	if ((p = strrchr(findbase, '/')) != NULL) {
182 		*p = 0;
183 		strcpy(findpattern, p + 1);
184 	} else
185 		strcpy(findpattern, "*");
186 
187 	if (strcmp(findpattern, "*.*") == 0)
188 		strcpy(findpattern, "*");
189 
190 	/*	if ((fdir = opendir(path)) == NULL)*/
191 	if ((fdir = opendir(findbase)) == NULL)
192 		return NULL;
193 	while ((d = readdir(fdir)) != NULL) {
194 		if (!*findpattern || glob_match(findpattern, d->d_name)) {
195 //			if (*findpattern)
196 //				printf("%s matched %s\n", findpattern, d->d_name);
197 			if (CompareAttributes(findbase, d->d_name, musthave, canhave)) {
198 				sprintf (findpath, "%s/%s", findbase, d->d_name);
199 				return findpath;
200 			}
201 		}
202 	}
203 	return NULL;
204 }
205 
Sys_FindNext(unsigned musthave,unsigned canhave)206 char *Sys_FindNext (unsigned musthave, unsigned canhave)
207 {
208 	struct dirent *d;
209 
210 	if (fdir == NULL)
211 		return NULL;
212 	while ((d = readdir(fdir)) != NULL) {
213 		if (!*findpattern || glob_match(findpattern, d->d_name)) {
214 //			if (*findpattern)
215 //				printf("%s matched %s\n", findpattern, d->d_name);
216 			if (CompareAttributes(findbase, d->d_name, musthave, canhave)) {
217 				sprintf (findpath, "%s/%s", findbase, d->d_name);
218 				return findpath;
219 			}
220 		}
221 	}
222 	return NULL;
223 }
224 
Sys_FindClose(void)225 void Sys_FindClose (void)
226 {
227 	if (fdir != NULL)
228 		closedir(fdir);
229 	fdir = NULL;
230 }
231 
232 
233 //============================================
234 
235