summaryrefslogtreecommitdiff
path: root/source/sys_unix.c
blob: 0368a776e5be76236be2a49c159528cacb5ec924 (plain)
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
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
/*
Copyright (C) 1997-2001 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 <errno.h>
#include <dirent.h>
#include <dlfcn.h>
#include <termios.h>
#include <sys/ioctl.h>
#ifndef __linux__
#include <machine/param.h>
#endif

#include "com_local.h"
#include "q_list.h"
#include "q_field.h"
#include "prompt.h"
#include "files.h"
#if USE_REF
#include "vid_public.h"
#endif
#include "sys_public.h"
#include "io_sleep.h"

cvar_t  *sys_basedir;
cvar_t  *sys_libdir;
cvar_t  *sys_homedir;
cvar_t  *sys_parachute;


/*
===============================================================================

TERMINAL SUPPORT

===============================================================================
*/

#if USE_SYSCON

cvar_t  *sys_console;

static qboolean         tty_enabled;
static struct termios   tty_orig;
static commandPrompt_t  tty_prompt;
static int              tty_hidden;
static ioentry_t        *tty_io;


static void tty_hide_input( void ) {
    int i;

    if( !tty_hidden ) {
        for( i = 0; i <= tty_prompt.inputLine.cursorPos; i++ ) {
            write( 1, "\b \b", 3 );
        }
    }
    tty_hidden++;
}

static void tty_show_input( void ) {
    if( !tty_hidden ) {
        return;
    }

    tty_hidden--;
    if( !tty_hidden ) {
        write( 1, "]", 1 );    
        write( 1, tty_prompt.inputLine.text,
            tty_prompt.inputLine.cursorPos );
    }
}

static void tty_init_input( void ) {
    struct termios tty;
#ifdef TIOCGWINSZ
    struct winsize ws;
#endif
    int width;

    if( !isatty( 0 ) || !isatty( 1 ) ) {
        Com_Printf( "stdin/stdout don't both refer to a TTY\n" );
        Cvar_Set( "sys_console", "1" );
        return;
    }
    
    tcgetattr( 0, &tty_orig );
    tty = tty_orig;
    tty.c_lflag &= ~( ECHO | ICANON | INPCK | ISTRIP );
    tty.c_cc[VMIN] = 1;
    tty.c_cc[VTIME] = 0;
    tcsetattr( 0, TCSADRAIN, &tty );

    // determine terminal width
    width = 80;
#ifdef TIOCGWINSZ
    if( ioctl( 0, TIOCGWINSZ, &ws ) == 0 ) {
        if( ws.ws_col ) {
            width = ws.ws_col;
        }
    }
#endif
    tty_prompt.widthInChars = width;
    tty_prompt.printf = Sys_Printf;
    tty_enabled = qtrue;

    // figure out input line width
    width--;
    if( width > MAX_FIELD_TEXT - 1 ) {
        width = MAX_FIELD_TEXT - 1;
    }
    IF_Init( &tty_prompt.inputLine, width, width );

    // display command prompt
    write( 1, "]", 1 );    
}

static void tty_shutdown_input( void ) {
    if( tty_io ) {
        IO_Remove( 0 );
        tty_io = NULL;
    }
    if( tty_enabled ) {
        tty_hide_input();
        tcsetattr( 0, TCSADRAIN, &tty_orig );
        tty_enabled = qfalse;
    }
}

void Sys_SetConsoleColor( color_index_t color ) {
    static const char color_to_ansi[8] =
        { '0', '1', '2', '3', '4', '6', '5', '7' };
    char buf[5];
    size_t len;

    if( !sys_console || !sys_console->integer ) {
        return;
    }

    if( !tty_enabled ) {
        return;
    }

    buf[0] = '\033';
    buf[1] = '[';
    switch( color ) {
    case COLOR_NONE:
        buf[2] = '0';
        buf[3] = 'm';
        len = 4;
        break;
    case COLOR_ALT:
        buf[2] = '3';
        buf[3] = '2';
        buf[4] = 'm';
        len = 5;
        break;
    default:
        buf[2] = '3';
        buf[3] = color_to_ansi[color];
        buf[4] = 'm';
        len = 5;
        break;
    }

    write( 1, buf, len );
}

static void tty_write_output( const char *text ) {
    char    buf[MAXPRINTMSG];
    size_t  len;

    for( len = 0; len < MAXPRINTMSG; len++ ) {
        int c = *text++;
        if( !c ) {
            break;
        }
        buf[len] = Q_charascii( c );
    }

    write( 1, buf, len );
}

/*
=================
Sys_ConsoleOutput
=================
*/
void Sys_ConsoleOutput( const char *text ) {
    if( !sys_console || !sys_console->integer ) {
        return;
    }

    if( !tty_enabled ) {
        tty_write_output( text );
    } else {
        tty_hide_input();
        tty_write_output( text );
        tty_show_input();
    }
}

void Sys_SetConsoleTitle( const char *title ) {
    char buffer[MAX_STRING_CHARS];
    size_t len;

    if( !sys_console || !sys_console->integer ) {
        return;
    }

    if( !tty_enabled ) {
        return;
    }

    len = Q_snprintf( buffer, sizeof( buffer ), "\033]0;%s\007", title );
    if( len < sizeof( buffer ) ) {
        write( 1, buffer, len );    
    }
}

static void tty_parse_input( const char *text ) {
    inputField_t *f;
    char *s;
    int i, key;

    f = &tty_prompt.inputLine;
    while( *text ) {
        key = *text++;

        if( key == tty_orig.c_cc[VERASE] || key == 127 || key == 8 ) {
            if( f->cursorPos ) {
                f->text[--f->cursorPos] = 0;
                write( 1, "\b \b", 3 );    
            }
            continue;
        }

        if( key == tty_orig.c_cc[VKILL] ) {
            for( i = 0; i < f->cursorPos; i++ ) {
                write( 1, "\b \b", 3 );    
            }
            f->cursorPos = 0;
            continue;
        }

        if( key >= 32 ) {
            if( f->cursorPos == f->maxChars - 1 ) {
                write( 1, va( "\b \b%c", key ), 4 );    
                f->text[f->cursorPos+0] = key;
                f->text[f->cursorPos+1] = 0;
            } else {
                write( 1, va( "%c", key ), 1 );    
                f->text[f->cursorPos+0] = key;
                f->text[f->cursorPos+1] = 0;
                f->cursorPos++;
            }
            continue;
        }
    
        if( key == '\n' ) {
            tty_hide_input();
            s = Prompt_Action( &tty_prompt );
            if( s ) {
                if( *s == '\\' || *s == '/' ) {
                    s++;
                }
                Sys_Printf( "]%s\n", s );
                Cbuf_AddText( &cmd_buffer, s );
            } else {
                write( 1, "]\n", 2 );    
            }
            tty_show_input();
            continue;
        }

        if( key == '\t' ) {
            tty_hide_input();
            Prompt_CompleteCommand( &tty_prompt, qfalse );
            f->cursorPos = strlen( f->text ); // FIXME
            tty_show_input();
            continue;
        }

        if( *text ) {
            key = *text++;
            if( key == '[' || key == 'O' ) {
                if( *text ) {
                    key = *text++;
                    switch( key ) {
                    case 'A':
                        tty_hide_input();
                        Prompt_HistoryUp( &tty_prompt );
                        tty_show_input();
                        break;
                    case 'B':
                        tty_hide_input();
                        Prompt_HistoryDown( &tty_prompt );
                        tty_show_input();
                        break;
#if 0
                    case 'C':
                        if( f->text[f->cursorPos] ) {
                            Sys_ConsoleWrite( "\033[C", 3 );
                            f->cursorPos++;
                        }
                        break;
                    case 'D':
                        if( f->cursorPos ) {
                            Sys_ConsoleWrite( "\033[D", 3 );
                            f->cursorPos--;
                        }
                        break;
#endif
                    }
                }
            }
        }
    }
}

void Sys_RunConsole( void ) {
    char text[MAX_STRING_CHARS];
    int ret;

    if( !sys_console || !sys_console->integer ) {
        return;
    }

    if( !tty_io || !tty_io->canread ) {
        return;
    }

    ret = read( 0, text, sizeof( text ) - 1 );
    if( !ret ) {
        Com_DPrintf( "Read EOF from stdin.\n" );
        tty_shutdown_input();
        Cvar_Set( "sys_console", "0" );
        return;
    }
    if( ret < 0 ) {
        if( errno == EINTR ) {
            return;
        }
        Com_Error( ERR_FATAL, "%s: read() failed: %s",
            __func__, strerror( errno ) );
    }
    text[ret] = 0;

    if( !tty_enabled ) {    
        Cbuf_AddText( &cmd_buffer, text );
        return;
    }

    tty_parse_input( text );
}

#endif // USE_SYSCON

/*
===============================================================================

HUNK

===============================================================================
*/

void Hunk_Begin( mempool_t *pool, size_t maxsize ) {
    void *buf;

    // reserve a huge chunk of memory, but don't commit any yet
    pool->maxsize = ( maxsize + 4095 ) & ~4095;
    pool->cursize = 0;
    buf = mmap( NULL, pool->maxsize, PROT_READ|PROT_WRITE,
        MAP_PRIVATE|MAP_ANON, -1, 0 );
    if( buf == NULL || buf == ( void * )-1 ) {
        Com_Error( ERR_FATAL, "%s: unable to reserve %"PRIz" bytes: %s",
            __func__, pool->maxsize, strerror( errno ) );
    }
    pool->base = buf;
    pool->mapped = pool->maxsize;
}

void *Hunk_Alloc( mempool_t *pool, size_t size ) {
    void *buf;

    // round to cacheline
    size = ( size + 63 ) & ~63;
    if( pool->cursize + size > pool->maxsize ) {
        Com_Error( ERR_FATAL, "%s: unable to allocate %"PRIz" bytes out of %"PRIz,
            __func__, size, pool->maxsize );
    }
    buf = ( byte * )pool->base + pool->cursize;
    pool->cursize += size;
    return buf;
}

void Hunk_End( mempool_t *pool ) {
    size_t newsize = ( pool->cursize + 4095 ) & ~4095;

    if( newsize > pool->maxsize ) {
        Com_Error( ERR_FATAL, "%s: newsize > maxsize", __func__ );
    }

    if( newsize < pool->maxsize ) {
#ifdef _GNU_SOURCE
        void *buf = mremap( pool->base, pool->maxsize, newsize, 0 );
#else
        void *unmap_base = ( byte * )pool->base + newsize;
        size_t unmap_len = pool->maxsize - newsize;
        void *buf = munmap( unmap_base, unmap_len ) + pool->base;
#endif
        if( buf != pool->base ) {
            Com_Error( ERR_FATAL, "%s: could not remap virtual block: %s",
                __func__, strerror( errno ) );
        }
    }
    pool->mapped = newsize;
}

void Hunk_Free( mempool_t *pool ) {
    if( pool->base ) {
        if( munmap( pool->base, pool->mapped ) ) {
            Com_Error( ERR_FATAL, "%s: munmap failed: %s",
                __func__, strerror( errno ) );
        }
    }
    memset( pool, 0, sizeof( *pool ) );
}

/*
===============================================================================

GENERAL ROUTINES

===============================================================================
*/

void Sys_DebugBreak( void ) {
    raise( SIGTRAP );
}

unsigned Sys_Milliseconds( void ) {
    struct timeval tp;
    unsigned time;
    
    gettimeofday( &tp, NULL );
    time = tp.tv_sec * 1000 + tp.tv_usec / 1000;
    return time;
}

/*
================
Sys_GetPathInfo
================
*/
qboolean Sys_GetPathInfo( const char *path, file_info_t *info ) {
    struct stat st;

    if( stat( path, &st ) == -1 ) {
        return qfalse;
    }

    if( !S_ISREG( st.st_mode ) ) {
        return qfalse;
    }

    if( info ) {
        info->size = st.st_size;
        info->ctime = st.st_ctime;
        info->mtime = st.st_mtime;
    }

    return qtrue;
}

qboolean Sys_GetFileInfo( FILE *fp, file_info_t *info ) {
    struct stat st;

    if( fstat( fileno( fp ), &st ) == -1 ) {
        return qfalse;
    }

    if( !S_ISREG( st.st_mode ) ) {
        return qfalse;
    }

    if( info ) {
        info->size = st.st_size;
        info->ctime = st.st_ctime;
        info->mtime = st.st_mtime;
    }

    return qtrue;
}

/*
=================
Sys_Quit

This function never returns.
=================
*/
void Sys_Quit( void ) {
#if USE_SYSCON
    tty_shutdown_input();
#endif
    exit( 0 );
}

/*
=================
Sys_GetCurrentDirectory
=================
*/
char *Sys_GetCurrentDirectory( void ) {
    static char curpath[MAX_OSPATH];

    getcwd( curpath, sizeof( curpath ) );

    return curpath;
}

void Sys_AddDefaultConfig( void ) {
    FILE *fp;
    struct stat st;
    size_t len, r;

    fp = fopen( SYS_SITECFG_NAME, "r" );
    if( !fp ) {
        return;
    }

    if( fstat( fileno( fp ), &st ) == 0 ) {
        Com_Printf( "Execing " SYS_SITECFG_NAME "\n" );

        len = st.st_size;
        if( len >= cmd_buffer.maxsize ) {
            len = cmd_buffer.maxsize - 1;
        }

        r = fread( cmd_buffer.text, 1, len, fp );
        cmd_buffer.text[r] = 0;

        cmd_buffer.cursize = COM_Compress( cmd_buffer.text );
    }

    fclose( fp );
}

void Sys_Sleep( int msec ) {
    struct timespec req;

    req.tv_sec = msec / 1000;
    req.tv_nsec = ( msec % 1000 ) * 1000000;
    nanosleep( &req, NULL );
}

#if USE_AC_CLIENT
qboolean Sys_GetAntiCheatAPI( void ) {
    Sys_Sleep( 1500 );
    return qfalse;
}
#endif

void Sys_FixFPCW( void ) {
#ifdef __i386__
    uint16_t cw;

    __asm__ __volatile__( "fnstcw %0" : "=m" (cw) );

    Com_DPrintf( "FPU control word: %x\n", cw );

    if( cw & 0x300 ) {
        Com_DPrintf( "Setting FPU to single precision mode\n" );
        cw &= ~0x300;
    }
    if( cw & 0xC00 ) {
        Com_DPrintf( "Setting FPU to round to nearest mode\n" );
        cw &= ~0xC00;
    }

    __asm__ __volatile__( "fldcw %0" : : "m" (cw) );
#endif
}

static void hup_handler( int signum ) {
    Com_FlushLogs();
}

static void term_handler( int signum ) {
#ifdef _GNU_SOURCE
    Com_Printf( "%s\n", strsignal( signum ) );
#else
    Com_Printf( "Received signal %d, exiting\n", signum );
#endif
    Com_Quit( NULL, KILL_DROP );
}

static void kill_handler( int signum ) {
#if USE_SYSCON
    tty_shutdown_input();
#endif

#if USE_CLIENT && USE_REF
    VID_FatalShutdown();
#endif

#ifdef _GNU_SOURCE
    fprintf( stderr, "%s\n", strsignal( signum ) );
#else
    fprintf( stderr, "Received signal %d, aborting\n", signum );
#endif

    exit( 1 );
}

/*
=================
Sys_Init
=================
*/
void Sys_Init( void ) {
    char    *homedir;

    signal( SIGTERM, term_handler );
    signal( SIGINT, term_handler );
    signal( SIGTTIN, SIG_IGN );
    signal( SIGTTOU, SIG_IGN );
    signal( SIGUSR1, hup_handler );

    // basedir <path>
    // allows the game to run from outside the data tree
    sys_basedir = Cvar_Get( "basedir", DATADIR, CVAR_NOSET );
    
    // homedir <path>
    // specifies per-user writable directory for demos, screenshots, etc
    if( HOMEDIR[0] == '~' ) {
        char *s = getenv( "HOME" );
        if( s && *s ) {
            homedir = va( "%s%s", s, HOMEDIR + 1 );
        } else {
            homedir = "";
        }
    } else {
        homedir = HOMEDIR;
    }
    sys_homedir = Cvar_Get( "homedir", homedir, CVAR_NOSET );
    sys_libdir = Cvar_Get( "libdir", LIBDIR, CVAR_NOSET );

#if USE_SYSCON
    // we want TTY support enabled if started from terminal,
    // but don't want any output by default if launched without one
    // (from X session for example)
    sys_console = Cvar_Get( "sys_console", isatty( 0 ) &&
        isatty( 1 ) ? "2" : "0", CVAR_NOSET );

    if( sys_console->integer > 0 ) {
        // change stdin to non-blocking and stdout to blocking
        fcntl( 0, F_SETFL, fcntl( 0, F_GETFL, 0 ) | FNDELAY );
        fcntl( 1, F_SETFL, fcntl( 1, F_GETFL, 0 ) & ~FNDELAY );

        // add stdin to the list of descriptors to wait on
        tty_io = IO_Add( 0 );
        tty_io->wantread = qtrue;

        // init optional TTY support
        if( sys_console->integer > 1 ) {
            tty_init_input(); 
        }
        signal( SIGHUP, term_handler );
    } else
#endif
    if( Com_IsDedicated() ) {
        signal( SIGHUP, hup_handler );
    }

    sys_parachute = Cvar_Get( "sys_parachute", "1", CVAR_NOSET );

    if( sys_parachute->integer ) {
        // perform some cleanup when crashing
        signal( SIGSEGV, kill_handler );
        signal( SIGILL, kill_handler );
        signal( SIGFPE, kill_handler );
        signal( SIGTRAP, kill_handler );
    }

    Sys_FixFPCW();
}

#if USE_SYSCON
/*
================
Sys_Printf
================
*/
void Sys_Printf( const char *fmt, ... ) {
    va_list     argptr;
    char        msg[MAXPRINTMSG];

    va_start( argptr, fmt );
    Q_vsnprintf( msg, sizeof( msg ), fmt, argptr );
    va_end( argptr );

    Sys_ConsoleOutput( msg );
}
#endif

/*
=================
Sys_Error
=================
*/
void Sys_Error( const char *error, ... ) {
    va_list     argptr;
    char        text[MAXPRINTMSG];

#if USE_SYSCON
    tty_shutdown_input();
#endif

#if USE_CLIENT && USE_REF
    VID_FatalShutdown();
#endif

    va_start( argptr, error );
    Q_vsnprintf( text, sizeof( text ), error, argptr );
    va_end( argptr );
    
    fprintf( stderr, "********************\n"
                     "FATAL: %s\n"
                     "********************\n", text );
    exit( 1 );
}

/*
========================================================================

DLL LOADING

========================================================================
*/

/*
=================
Sys_FreeLibrary
=================
*/
void Sys_FreeLibrary( void *handle ) {
    if( handle && dlclose( handle ) ) {
        Com_Error( ERR_FATAL, "dlclose failed on %p: %s", handle, dlerror() );
    }
}

/*
=================
Sys_LoadLibrary
=================
*/
void *Sys_LoadLibrary( const char *path, const char *sym, void **handle ) {
    void    *module, *entry;

    *handle = NULL;

    module = dlopen( path, RTLD_LAZY );
    if( !module ) {
        Com_DPrintf( "%s failed: %s\n", __func__, dlerror() );
        return NULL;
    }

    if( sym ) {
        entry = dlsym( module, sym );
        if( !entry ) {
            Com_DPrintf( "%s failed: %s\n", __func__, dlerror() );
            dlclose( module );
            return NULL;
        }
    } else {
        entry = NULL;
    }

    Com_DPrintf( "%s succeeded: %s\n", __func__, path );

    *handle = module;

    return entry;
}

void *Sys_GetProcAddress( void *handle, const char *sym ) {
    return dlsym( handle, sym );
}

/*
===============================================================================

MISC

===============================================================================
*/

/*
=================
Sys_ListFilteredFiles
=================
*/
static void Sys_ListFilteredFiles(  void        **listedFiles,
                                    int         *count,
                                    const char  *path,
                                    const char  *filter,
                                    int         flags,
                                    size_t      baselen,
                                    int         depth )
{
    struct dirent *findInfo;
    DIR        *findHandle;
    struct  stat st;
    char    findPath[MAX_OSPATH];
    char    *name;
    size_t  len;

    if( depth >= 32 ) {
        return;
    }

    if( *count >= MAX_LISTED_FILES ) {
        return;
    }

    if( ( findHandle = opendir( path ) ) == NULL ) {
        return;
    }

    while( ( findInfo = readdir( findHandle ) ) != NULL ) {
        if( !strcmp( findInfo->d_name, "." ) ) {
            continue;
        }
        if( !strcmp( findInfo->d_name, ".." ) ) {
            continue;
        }
        len = Q_concat( findPath, sizeof( findPath ),
            path, "/", findInfo->d_name, NULL );
        if( len >= sizeof( findPath ) ) {
            continue;
        }

        if( stat( findPath, &st ) == -1 ) {
            continue;
        }

        if( st.st_mode & S_IFDIR ) {
            Sys_ListFilteredFiles( listedFiles, count, findPath,
                filter, flags, baselen, depth + 1 );
        }

        if( ( flags & FS_SEARCHDIRS_MASK ) == FS_SEARCHDIRS_ONLY ) {
            if( !( st.st_mode & S_IFDIR ) ) {
                continue;
            }
        } else if( ( flags & FS_SEARCHDIRS_MASK ) == FS_SEARCHDIRS_NO ) {
            if( st.st_mode & S_IFDIR ) {
                continue;
            }
        }

        if( !FS_WildCmp( filter, findPath + baselen ) ) {
            continue;
        }
        if( flags & FS_SEARCH_SAVEPATH ) {
            name = findPath + baselen;
        } else {
            name = findInfo->d_name;
        }

        if( flags & FS_SEARCH_EXTRAINFO ) {
            listedFiles[( *count )++] = FS_CopyInfo( name,
                st.st_size, st.st_ctime, st.st_mtime );
        } else {
            listedFiles[( *count )++] = FS_CopyString( name );
        }
        if( *count >= MAX_LISTED_FILES ) {
            break;
        }

    }

    closedir( findHandle );
}


/*
=================
Sys_ListFiles
=================
*/
void **Sys_ListFiles(   const char  *path,
                        const char  *extension,
                        int         flags,
                        size_t      baselen,
                        int         *numFiles )
{
    struct dirent *findInfo;
    DIR     *findHandle;
    struct stat st;
    char    findPath[MAX_OSPATH];
    void    *listedFiles[MAX_LISTED_FILES];
    int     count = 0;
    char    *s;
    size_t  len;

    if( numFiles ) {
        *numFiles = 0;
    }

    if( flags & FS_SEARCH_BYFILTER ) {
        Sys_ListFilteredFiles( listedFiles, &count, path,
            extension, flags, baselen, 0 );
    } else {
        if( ( findHandle = opendir( path ) ) == NULL ) {
            return NULL;
        }

        while( ( findInfo = readdir( findHandle ) ) != NULL ) {
            if( !strcmp( findInfo->d_name, "." ) ) {
                continue;
            }
            if( !strcmp( findInfo->d_name, ".." ) ) {
                continue;
            }

            len = Q_concat( findPath, sizeof( findPath ),
                path, "/", findInfo->d_name, NULL );
            if( len >= sizeof( findPath ) ) {
                continue;
            }

            if( stat( findPath, &st ) == -1 ) {
                continue;
            }
            if( ( flags & FS_SEARCHDIRS_MASK ) == FS_SEARCHDIRS_ONLY ) {
                if( !( st.st_mode & S_IFDIR ) ) {
                    continue;
                }
            } else if( ( flags & FS_SEARCHDIRS_MASK ) == FS_SEARCHDIRS_NO ) {
                if( st.st_mode & S_IFDIR ) {
                    continue;
                }
            }

            if( extension && !FS_ExtCmp( extension, findInfo->d_name ) ) {
                continue;
            }
            
            if( flags & FS_SEARCH_SAVEPATH ) {
                s = findPath + baselen;
            } else {
                s = findInfo->d_name;
            }
            if( flags & FS_SEARCH_EXTRAINFO ) {
                listedFiles[count++] = FS_CopyInfo( s, st.st_size,
                    st.st_ctime, st.st_mtime );
            } else {
                listedFiles[count++] = FS_CopyString( s );
            }

            if( count >= MAX_LISTED_FILES ) {
                break;
            }
        }

        closedir( findHandle );
    }

    if( !count ) {
        return NULL;
    }

    if( numFiles ) {
        *numFiles = count;
    }

    return FS_CopyList( listedFiles, count );
}

/*
=================
main
=================
*/
int main( int argc, char **argv ) {
    if( argc > 1 ) {
        if( !strcmp( argv[1], "-v" ) || !strcmp( argv[1], "--version" ) ) {
            printf( APPLICATION " " VERSION " " __DATE__ " " BUILDSTRING " "
                    CPUSTRING "\n" );
            return 0;
        }
        if( !strcmp( argv[1], "-h" ) || !strcmp( argv[1], "--help" ) ) {
            printf( "Usage: %s [+command arguments] [...]\n", argv[0] );
            return 0;
        }
    }

    if( !getuid() || !geteuid() ) {
        printf(  "You can not run " APPLICATION " as superuser "
                 "for security reasons!\n" );
        return 1;
    }

    Qcommon_Init( argc, argv );
    while( 1 ) {
        Qcommon_Frame();
    }

    return 1; // never gets here
}