|
|
1.1 root 1: #include <unistd.h>
2: #include <signal.h>
3: #include <stdlib.h>
4: #include <limits.h>
5: #include <sys/time.h>
6: #include <sys/types.h>
7: #include <unistd.h>
8: #include <fcntl.h>
9: #include <stdarg.h>
10: #include <stdio.h>
11: #include <sys/ipc.h>
12: #include <sys/shm.h>
13: #include <sys/stat.h>
14: #include <string.h>
15: #include <ctype.h>
16: #include <sys/wait.h>
17: #include <errno.h>
18:
19: #include "quakedef.h"
20:
21: #define TICRATE (1.0/30)
22:
23: FILE *debugfile=0;
24:
25: // =======================================================================
26: // General routines
27: // =======================================================================
28:
29: void I_DebugNumber(int y, int val)
30: {
31: }
32:
33: /*
34: void I_Printf (char *fmt, ...)
35: {
36: va_list argptr;
37: char text[1024];
38:
39: va_start (argptr,fmt);
40: vsprintf (text,fmt,argptr);
41: va_end (argptr);
42: fprintf(stderr, "%s", text);
43:
44: Con_Print (text);
45: }
46: */
47:
48: void I_Printf (char *fmt, ...)
49: {
50: va_list argptr;
51: char text[1024], *t_p;
52: int l, r;
53:
54: va_start (argptr,fmt);
55: vsprintf (text,fmt,argptr);
56: va_end (argptr);
57:
58: l = strlen(text);
59: t_p = text;
60:
61: // make sure everything goes through, even though we are non-blocking
62: while (l)
63: {
64: r = write (1, text, l);
65: if (r == -1)
66: {
67: usleep (10);
68: continue;
69: }
70: t_p += r;
71: l -= r;
72: }
73: }
74:
75: void I_Quit (void)
76: {
77: fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
78: S_Shutdown();
79: exit(0);
80: }
81:
82: void I_Error (char *error, ...)
83: {
84: va_list argptr;
85: char string[1024];
86:
87: fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
88: va_start (argptr,error);
89: vsprintf (string,error,argptr);
90: va_end (argptr);
91: fprintf(stderr, "Error: %s\n", string);
92: S_Shutdown();
93: exit(-1);
94: }
95:
96: void I_Warn (char *warning, ...)
97: {
98: va_list argptr;
99: char string[1024];
100:
101: va_start (argptr,warning);
102: vsprintf (string,warning,argptr);
103: va_end (argptr);
104: fprintf(stderr, "Warning: %s", string);
105: }
106:
107: int I_FileOpenRead (char *path, int *handle)
108: {
109: int h;
110: struct stat fileinfo;
111:
112:
113: h = open (path, O_RDONLY, 0666);
114: *handle = h;
115: if (h == -1)
116: return -1;
117:
118: if (fstat (h,&fileinfo) == -1)
119: I_Error ("Error fstating %s", path);
120:
121: return fileinfo.st_size;
122: }
123:
124: int I_FileOpenWrite (char *path)
125: {
126: int handle;
127:
128: umask (0);
129:
130: handle = open(path,O_RDWR | O_CREAT | O_TRUNC
131: , 0666);
132:
133: if (handle == -1)
134: I_Error ("Error opening %s: %s", path,strerror(errno));
135:
136: return handle;
137: }
138:
139: int I_FileWrite (int handle, void *src, int count)
140: {
141: return write (handle, src, count);
142: }
143:
144: void I_FileClose (int handle)
145: {
146: close (handle);
147: }
148:
149: void I_FileSeek (int handle, int position)
150: {
151: lseek (handle, position, SEEK_SET);
152: }
153:
154: int I_FileRead (int handle, void *dest, int count)
155: {
156: return read (handle, dest, count);
157: }
158:
159: void I_DebugLog(char *file, char *fmt, ...)
160: {
161: va_list argptr;
162: static char data[1024];
163: int fd;
164:
165: va_start(argptr, fmt);
166: vsprintf(data, fmt, argptr);
167: va_end(argptr);
168: // fd = open(file, O_WRONLY | O_BINARY | O_CREAT | O_APPEND, 0666);
169: fd = open(file, O_WRONLY | O_CREAT | O_APPEND, 0666);
170: write(fd, data, strlen(data));
171: close(fd);
172: }
173:
174: /*=============
175: StartMSRInterval
176: =============
177: */
178: void StartMSRInterval(int msreg)
179: {
180: }
181:
182: /*
183: =============
184: EndMSRInterval
185: =============
186: */
187: unsigned long EndMSRInterval()
188: {
189: return 0;
190: }
191:
192: /*
193: ============
194: FileTime
195:
196: returns -1 if not present
197: ============
198: */
199: int FileTime (char *path)
200: {
201: struct stat buf;
202:
203: if (stat (path,&buf) == -1)
204: return -1;
205:
206: return buf.st_mtime;
207: }
208:
209: cvar_t sys_showfiles = {"showfiles","1"};
210:
211:
212: void Sys_EditFile(char *filename)
213: {
214:
215: char cmd[256];
216: char *term;
217: char *editor;
218:
219: term = getenv("TERM");
220: if (term && !strcmp(term, "xterm"))
221: {
222: editor = getenv("VISUAL");
223: if (!editor)
224: editor = getenv("EDITOR");
225: if (!editor)
226: editor = getenv("EDIT");
227: if (!editor)
228: editor = "vi";
229: sprintf(cmd, "xterm -e %s %s", editor, filename);
230: system(cmd);
231: }
232:
233: }
234:
235: double I_FloatTime (void)
236: {
237: struct timeval tp;
238: struct timezone tzp;
239: static int secbase;
240:
241: gettimeofday(&tp, &tzp);
242:
243: if (!secbase)
244: {
245: secbase = tp.tv_sec;
246: return tp.tv_usec/1000000.0;
247: }
248:
249: return (tp.tv_sec - secbase) + tp.tv_usec/1000000.0;
250: }
251:
252: // =======================================================================
253: // Sleeps for microseconds
254: // =======================================================================
255:
256: static volatile int oktogo;
257:
258: void alarm_handler(int x)
259: {
260: oktogo=1;
261: }
262:
263: void I_Sleep(int usecs)
264: {
265:
266: struct itimerval it;
267:
268: it.it_interval.tv_sec = 0;
269: it.it_interval.tv_usec = 0;
270: it.it_value.tv_sec = usecs / 1000000;
271: it.it_value.tv_usec = usecs % 1000000;
272:
273: signal(SIGALRM, alarm_handler);
274:
275: oktogo=0;
276: setitimer(ITIMER_REAL, &it, 0);
277: while (!oktogo) sleep(0);
278:
279: }
280:
281: byte *I_ZoneBase (int *size)
282: {
283:
284: char *QUAKEOPT = getenv("QUAKEOPT");
285:
286: *size = 0xc00000;
287: if (QUAKEOPT)
288: {
289: while (*QUAKEOPT)
290: if (tolower(*QUAKEOPT++) == 'm')
291: {
292: *size = atof(QUAKEOPT) * 1024*1024;
293: break;
294: }
295: }
296: return malloc (*size);
297:
298: }
299:
300:
301: void Sys_Sleep(void)
302: {
303: sleep(0);
304: }
305:
306: void I_GetMemory(quakeparms_t *parms)
307: {
308: FILE *f;
309: char buffer[256];
310: char *procparse;
311: int freemem, buffermem, totalmem;
312: int rc, suggestion;
313:
314: parms->memsize = 8*1024*1024;
315:
316: f = fopen("/proc/meminfo", "r");
317: if (f)
318: {
319: fgets(buffer, sizeof buffer, f);
320: procparse = "%s %d %d %d %d %d";
321: rc = fscanf(f, procparse, buffer, &totalmem, buffer, &freemem,
322: buffer, &buffermem);
323:
324: suggestion = (9*buffermem)/10 + freemem;
325: if (suggestion > totalmem)
326: I_Printf("[%s] did not properly parse /proc/meminfo\n", procparse);
327: if (suggestion > parms->memsize && suggestion < totalmem)
328: parms->memsize = suggestion;
329:
330: fclose(f);
331: }
332: else
333: I_Printf("Did you know /proc breaks up painful gas bubbles?\n");
334:
335: parms->membase = malloc (parms->memsize);
336:
337: }
338:
339:
340: void floating_point_exception_handler(int whatever)
341: {
342: I_Warn("floating point exception\n");
343: signal(SIGFPE, floating_point_exception_handler);
344: }
345:
346: int main (int c, char **v)
347: {
348:
349: extern qboolean pr_debugerrors;
350: double time, oldtime, newtime;
351: quakeparms_t parms;
352:
353: pr_debugerrors = false;
354:
355: signal(SIGFPE, floating_point_exception_handler);
356:
357: fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
358:
359: parms.memsize = 0x1000000;
360: parms.membase = malloc (parms.memsize);
361: parms.basedir = "/usr/tmp/lquake/";
362:
363: COM_InitArgv (c, v);
364:
365: parms.argc = com_argc;
366: parms.argv = com_argv;
367:
368: Quake_Init(&parms);
369:
370: if (COM_CheckParm ("-debugfile"))
371: {
372: char hostname[80];
373: char filename[80];
374:
375: gethostname(hostname, sizeof(hostname));
376: sprintf (filename, "%s.dbg",hostname);
377: debugfile = fopen (filename,"w");
378:
379: net_debug = true;
380: }
381:
382: oldtime = I_FloatTime ();
383: while (1)
384: {
385: // find time spent rendering last frame
386: newtime = I_FloatTime ();
387: time = newtime - oldtime;
388:
389: // if ( !cl_connected && time < TICRATE)
390: if ( host_dedicated && time < TICRATE)
391: {
392: I_Sleep (1);
393: continue; // not time to run a server only tic yet
394: }
395:
396: Host_Frame(time);
397: // CL_Frame(time);
398:
399: oldtime = newtime;
400: }
401:
402: Quake_Shutdown();
403:
404: }
405:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.