-
Notifications
You must be signed in to change notification settings - Fork 124
/
Copy pathsys_posix.c
736 lines (604 loc) · 14 KB
/
sys_posix.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
/*
Copyright (C) 1996-1997 Id Software, Inc.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
#include <unistd.h>
#include <signal.h>
#include <stdlib.h>
#include <limits.h>
#include <sys/time.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdarg.h>
#include <stdio.h>
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/stat.h>
#include <string.h>
#include <ctype.h>
#include <sys/wait.h>
#include <sys/mman.h>
#include <semaphore.h>
#include <sys/ioctl.h>
#include <sys/poll.h>
#include <sched.h>
#include <errno.h>
#include <dirent.h>
#include <SDL.h>
#include <dlfcn.h>
#include "quakedef.h"
#include "server.h"
#include "pcre.h"
// BSD only defines FNDELAY:
#ifndef O_NDELAY
# define O_NDELAY FNDELAY
#endif
int noconinput = 0;
qbool stdin_ready;
int do_stdin = 1;
cvar_t sys_nostdout = {"sys_nostdout", "0"};
void Sys_Printf (char *fmt, ...)
{
#ifdef DEBUG
va_list argptr;
char text[2048];
unsigned char *p;
return;
va_start (argptr,fmt);
vsnprintf (text, sizeof(text), fmt, argptr);
va_end (argptr);
if (sys_nostdout.value)
return;
for (p = (unsigned char *) text; *p; p++)
if ((*p > 128 || *p < 32) && *p != 10 && *p != 13 && *p != 9)
printf("[%02x]", *p);
else
putc(*p, stdout);
#else
return;
#endif
}
void Sys_Quit(void)
{
fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~O_NDELAY);
exit(0);
}
void Sys_Init(void)
{
#ifdef __APPLE__
extern void init_url_handler();
init_url_handler();
#endif
}
void Sys_Error(char *error, ...)
{
extern FILE *qconsole_log;
va_list argptr;
char string[1024];
fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~O_NDELAY); //change stdin to non blocking
va_start (argptr, error);
vsnprintf (string, sizeof(string), error, argptr);
va_end (argptr);
fprintf(stderr, "Error: %s\n", string);
if (qconsole_log)
fprintf(qconsole_log, "Error: %s\n", string);
Host_Shutdown ();
exit(1);
}
void Sys_mkdir (const char *path)
{
mkdir (path, 0777);
}
/*
================
Sys_remove
================
*/
int Sys_remove(char *path)
{
return unlink(path);
}
int Sys_rmdir(const char *path)
{
return rmdir(path);
}
int Sys_FileSizeTime(char *path, int *time1)
{
struct stat buf;
if (stat(path, &buf) == -1)
{
*time1 = -1;
return 0;
}
else
{
*time1 = buf.st_mtime;
return buf.st_size;
}
}
/*
================
Sys_listdir
================
*/
dir_t Sys_listdir (const char *path, const char *ext, int sort_type)
{
static file_t list[MAX_DIRFILES];
dir_t dir;
char pathname[MAX_OSPATH];
DIR *d;
DIR *testdir; //bliP: list dir
struct dirent *oneentry;
qbool all;
int r;
pcre *preg = NULL;
const char *errbuf;
memset(list, 0, sizeof(list));
memset(&dir, 0, sizeof(dir));
dir.files = list;
all = !strncmp(ext, ".*", 3);
if (!all)
if (!(preg = pcre_compile(ext, PCRE_CASELESS, &errbuf, &r, NULL)))
{
Con_Printf("Sys_listdir: pcre_compile(%s) error: %s at offset %d\n",
ext, errbuf, r);
Q_free(preg);
return dir;
}
if (!(d = opendir(path)))
{
if (!all)
Q_free(preg);
return dir;
}
while ((oneentry = readdir(d)))
{
if (!strncmp(oneentry->d_name, ".", 2) || !strncmp(oneentry->d_name, "..", 3))
continue;
if (!all)
{
switch (r = pcre_exec(preg, NULL, oneentry->d_name,
strlen(oneentry->d_name), 0, 0, NULL, 0))
{
case 0: break;
case PCRE_ERROR_NOMATCH: continue;
default:
Con_Printf("Sys_listdir: pcre_exec(%s, %s) error code: %d\n",
ext, oneentry->d_name, r);
Q_free(preg);
return dir;
}
}
snprintf(pathname, sizeof(pathname), "%s/%s", path, oneentry->d_name);
if ((testdir = opendir(pathname)))
{
dir.numdirs++;
list[dir.numfiles].isdir = true;
list[dir.numfiles].size = list[dir.numfiles].time = 0;
closedir(testdir);
}
else
{
list[dir.numfiles].isdir = false;
//list[dir.numfiles].time = Sys_FileTime(pathname);
dir.size +=
(list[dir.numfiles].size = Sys_FileSizeTime(pathname, &list[dir.numfiles].time));
}
strlcpy (list[dir.numfiles].name, oneentry->d_name, MAX_DEMO_NAME);
if (++dir.numfiles == MAX_DIRFILES - 1)
break;
}
closedir(d);
if (!all)
Q_free(preg);
switch (sort_type)
{
case SORT_NO: break;
case SORT_BY_DATE:
qsort((void *)list, dir.numfiles, sizeof(file_t), Sys_compare_by_date);
break;
case SORT_BY_NAME:
qsort((void *)list, dir.numfiles, sizeof(file_t), Sys_compare_by_name);
break;
}
return dir;
}
int Sys_chdir(const char *path)
{
return (chdir(path) == 0);
}
char *Sys_getcwd(char *buf, int bufsize)
{
return getcwd(buf, bufsize);
}
#if (_POSIX_TIMERS > 0) && defined(_POSIX_MONOTONIC_CLOCK)
#include <time.h>
double Sys_DoubleTime(void)
{
static unsigned int secbase;
struct timespec ts;
#ifdef __linux__
clock_gettime(CLOCK_MONOTONIC_RAW, &ts);
#else
clock_gettime(CLOCK_MONOTONIC, &ts);
#endif
if (!secbase) {
secbase = ts.tv_sec;
return ts.tv_nsec / 1000000000.0;
}
return (ts.tv_sec - secbase) + ts.tv_nsec / 1000000000.0;
}
#else
double Sys_DoubleTime(void)
{
struct timeval tp;
struct timezone tzp;
static int secbase;
gettimeofday(&tp, &tzp);
if (!secbase) {
secbase = tp.tv_sec;
return tp.tv_usec/1000000.0;
}
return (tp.tv_sec - secbase) + tp.tv_usec / 1000000.0;
}
#endif
int main(int argc, char **argv)
{
double time, oldtime, newtime;
int i;
#ifdef __linux__
extern void InitSig(void);
InitSig();
#endif
COM_InitArgv (argc, argv);
// let me use -condebug C:\condebug.log before Quake FS init, so I get ALL messages before quake fully init
if ((i = COM_CheckParm("-condebug")) && i < COM_Argc() - 1) {
extern FILE *qconsole_log;
char *s = COM_Argv(i + 1);
if (*s != '-' && *s != '+')
qconsole_log = fopen(s, "a");
}
signal(SIGFPE, SIG_IGN);
// we need to check for -noconinput and -nostdout before Host_Init is called
if (!(noconinput = COM_CheckParm("-noconinput")))
fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY);
if (COM_CheckParm("-nostdout"))
sys_nostdout.value = 1;
Host_Init (argc, argv, 32 * 1024 * 1024);
oldtime = Sys_DoubleTime ();
while (1) {
// find time spent rendering last frame
newtime = Sys_DoubleTime();
time = newtime - oldtime;
oldtime = newtime;
Host_Frame(time);
}
}
void Sys_MakeCodeWriteable (unsigned long startaddr, unsigned long length) {
int r;
unsigned long addr;
int psize = getpagesize();
addr = (startaddr & ~(psize - 1)) - psize;
r = mprotect((char*) addr, length + startaddr - addr + psize, 7);
if (r < 0)
Sys_Error("Protection change failed");
}
int Sys_CreateThread(DWORD WINAPI (*func)(void *), void *param)
{
SDL_Thread *thread = SDL_CreateThread((SDL_ThreadFunction)func, NULL, param);
SDL_DetachThread(thread);
return 1;
}
// kazik -->
// directory listing functions
int CopyDirent(sys_dirent *ent, struct dirent *tmpent)
{
struct stat statbuf;
struct tm * lintime;
if (stat(tmpent->d_name, &statbuf) != 0)
return 0;
strncpy(ent->fname, tmpent->d_name, MAX_PATH_LENGTH);
ent->fname[MAX_PATH_LENGTH-1] = 0;
lintime = localtime(&statbuf.st_mtime);
UnixtimeToWintime(&ent->time, lintime);
ent->size = statbuf.st_size;
ent->directory = (statbuf.st_mode & S_IFDIR);
return 1;
}
unsigned long Sys_ReadDirFirst(sys_dirent *ent)
{
struct dirent *tmpent;
DIR *dir = opendir(".");
if (dir == NULL)
return 0;
tmpent = readdir(dir);
if (tmpent == NULL)
{
closedir(dir);
return 0;
}
if (!CopyDirent(ent, tmpent))
{
closedir(dir);
return 0;
}
return (unsigned long)dir;
}
int Sys_ReadDirNext(unsigned long search, sys_dirent *ent)
{
struct dirent *tmpent;
tmpent = readdir((DIR*)search);
if (tmpent == NULL)
return 0;
if (!CopyDirent(ent, tmpent))
return 0;
return 1;
}
void Sys_ReadDirClose(unsigned long search)
{
closedir((DIR *)search);
}
void _splitpath(const char *path, char *drive, char *dir, char *file, char *ext)
{
const char *f, *e;
if (drive)
drive[0] = 0;
f = path;
while (strchr(f, '/'))
f = strchr(f, '/') + 1;
if (dir)
{
strncpy(dir, path, min(f-path, _MAX_DIR));
dir[_MAX_DIR-1] = 0;
}
e = f;
while (*e == '.') // skip dots at beginning
e++;
if (strchr(e, '.'))
{
while (strchr(e, '.'))
e = strchr(e, '.')+1;
e--;
}
else
e += strlen(e);
if (file)
{
strncpy(file, f, min(e-f, _MAX_FNAME));
file[min(e-f, _MAX_FNAME-1)] = 0;
}
if (ext)
{
strncpy(ext, e, _MAX_EXT);
ext[_MAX_EXT-1] = 0;
}
}
// full path
char *Sys_fullpath(char *absPath, const char *relPath, int maxLength)
{
// too small buffer, copy in tmp[] and then look is enough space in output buffer aka absPath
if (maxLength-1 < PATH_MAX) {
char tmp[PATH_MAX+1];
if (realpath(relPath, tmp) && absPath && strlen(tmp) < maxLength+1) {
strlcpy(absPath, tmp, maxLength+1);
return absPath;
}
return NULL;
}
return realpath(relPath, absPath);
}
// kazik <--
int Sys_EnumerateFiles (char *gpath, char *match, int (*func)(char *, int, void *), void *parm)
{
DIR *dir, *dir2;
char apath[MAX_OSPATH];
char file[MAX_OSPATH];
char truepath[MAX_OSPATH];
char *s;
struct dirent *ent;
//printf("path = %s\n", gpath);
//printf("match = %s\n", match);
if (!gpath)
gpath = "";
*apath = '\0';
strncpy(apath, match, sizeof(apath));
for (s = apath+strlen(apath)-1; s >= apath; s--)
{
if (*s == '/')
{
s[1] = '\0';
match += s - apath+1;
break;
}
}
if (s < apath) //didn't find a '/'
*apath = '\0';
snprintf(truepath, sizeof(truepath), "%s/%s", gpath, apath);
//printf("truepath = %s\n", truepath);
//printf("gamepath = %s\n", gpath);
//printf("apppath = %s\n", apath);
//printf("match = %s\n", match);
dir = opendir(truepath);
if (!dir)
{
Com_DPrintf("Failed to open dir %s\n", truepath);
return true;
}
do
{
ent = readdir(dir);
if (!ent)
break;
if (*ent->d_name != '.')
if (wildcmp(match, ent->d_name))
{
snprintf(file, sizeof(file), "%s/%s", truepath, ent->d_name);
//would use stat, but it breaks on fat32.
if ((dir2 = opendir(file)))
{
closedir(dir2);
snprintf(file, sizeof(file), "%s%s/", apath, ent->d_name);
//printf("is directory = %s\n", file);
}
else
{
snprintf(file, sizeof(file), "%s%s", apath, ent->d_name);
//printf("file = %s\n", file);
}
if (!func(file, -2, parm))
{
closedir(dir);
return false;
}
}
} while(1);
closedir(dir);
return true;
}
/*************************** INTER PROCESS CALLS *****************************/
#define PIPE_BUFFERSIZE 1024
FILE *fifo_pipe;
static char *Sys_PipeFile(void) {
static char pipe[MAX_PATH] = {0};
if (*pipe)
return pipe;
snprintf(pipe, sizeof(pipe), "/tmp/ezquake_fifo_%s", getlogin());
return pipe;
}
void Sys_InitIPC(void)
{
int fd;
mode_t old;
/* Don't use the user's umask, make sure we set the proper access */
old = umask(0);
if (mkfifo(Sys_PipeFile(), 0600)) {
umask(old);
// We failed ...
return;
}
umask(old); // Reset old mask
/* Open in non blocking mode */
if ((fd = open(Sys_PipeFile(), O_RDONLY | O_NONBLOCK)) == -1) {
// We failed ...
return;
}
if (!(fifo_pipe = fdopen(fd, "r"))) {
// We failed ...
return;
}
}
void Sys_CloseIPC(void)
{
if (fifo_pipe) {
fclose(fifo_pipe);
unlink(Sys_PipeFile());
}
}
void Sys_ReadIPC(void)
{
char buf[PIPE_BUFFERSIZE] = {0};
int num_bytes_read = 0;
if (!fifo_pipe)
return;
num_bytes_read = fread(buf, sizeof(buf), 1, fifo_pipe);
COM_ParseIPCData(buf, num_bytes_read);
}
unsigned int Sys_SendIPC(const char *buf)
{
FILE *fifo_out;
int nelms_written;
if (!(fifo_out = fopen(Sys_PipeFile(), "w")))
return true;
nelms_written = fwrite(buf, strlen(buf) + 1, 1, fifo_out);
fclose(fifo_out);
if (nelms_written != 1)
return false;
return true;
}
/********************************** SEMAPHORES *******************************/
/* Sys_Sem*() returns 0 on success; on error, -1 is returned */
int Sys_SemInit(sem_t *sem, int value, int max_value)
{
return sem_init(sem, 0, value); // Don't share between processes
}
int Sys_SemWait(sem_t *sem)
{
return sem_wait(sem);
}
int Sys_SemPost(sem_t *sem)
{
return sem_post(sem);
}
int Sys_SemDestroy(sem_t *sem)
{
return sem_destroy(sem);
}
/*********************************************************************************/
int Sys_Script (const char *path, const char *args)
{
char str[1024];
snprintf(str, sizeof(str), "cd %s\n./%s.qws %s &\ncd ..", fs_gamedir, path, args);
if (system(str) == -1)
return 0;
return 1;
}
DL_t Sys_DLOpen(const char *path)
{
return dlopen(path,
#ifdef __OpenBSD__
DL_LAZY
#else
RTLD_NOW
#endif
);
}
qbool Sys_DLClose(DL_t dl)
{
return !dlclose(dl);
}
void *Sys_DLProc(DL_t dl, const char *name)
{
return dlsym(dl, name);
}
/*********************************************************************************/
void Sys_CloseLibrary(dllhandle_t *lib)
{
dlclose((void*)lib);
}
dllhandle_t *Sys_LoadLibrary(const char *name, dllfunction_t *funcs)
{
int i;
dllhandle_t lib;
lib = dlopen (name, RTLD_LAZY);
if (!lib)
return NULL;
for (i = 0; funcs[i].name; i++)
{
*funcs[i].funcptr = dlsym(lib, funcs[i].name);
if (!*funcs[i].funcptr)
break;
}
if (funcs[i].name)
{
Sys_CloseLibrary((dllhandle_t*)lib);
lib = NULL;
}
return (dllhandle_t*)lib;
}
void *Sys_GetAddressForName(dllhandle_t *module, const char *exportname)
{
if (!module)
return NULL;
return dlsym(module, exportname);
}