Annotation of quake2/irix/sys_irix.c, revision 1.1.1.2

1.1.1.2 ! root        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: */
1.1       root       20: #include <unistd.h>
                     21: #include <signal.h>
                     22: #include <stdlib.h>
                     23: #include <limits.h>
                     24: #include <sys/time.h>
                     25: #include <sys/types.h>
                     26: #include <unistd.h>
                     27: #include <fcntl.h>
                     28: #include <stdarg.h>
                     29: #include <stdio.h>
                     30: #include <sys/ipc.h>
                     31: #include <sys/shm.h>
                     32: #include <sys/stat.h>
                     33: #include <string.h>
                     34: #include <ctype.h>
                     35: #include <sys/wait.h>
                     36: #include <sys/mman.h>
                     37: #include <errno.h>
                     38: #include <mntent.h>
                     39: 
                     40: #include <dlfcn.h>
                     41: 
                     42: #include "../qcommon/qcommon.h"
                     43: 
                     44: #include "../linux/rw_linux.h"
                     45: 
                     46: cvar_t *nostdout;
                     47: 
                     48: unsigned       sys_frame_time;
                     49: 
                     50: uid_t saved_euid;
                     51: qboolean stdin_active = true;
                     52: 
                     53: // =======================================================================
                     54: // General routines
                     55: // =======================================================================
                     56: 
                     57: void Sys_ConsoleOutput (char *string)
                     58: {
                     59:        if (nostdout && nostdout->value)
                     60:                return;
                     61: 
                     62:        fputs(string, stdout);
                     63: }
                     64: 
                     65: void Sys_Printf (char *fmt, ...)
                     66: {
                     67:        va_list         argptr;
                     68:        char            text[1024];
                     69:        unsigned char           *p;
                     70: 
                     71:        va_start (argptr,fmt);
                     72:        vsprintf (text,fmt,argptr);
                     73:        va_end (argptr);
                     74: 
                     75:        if (strlen(text) > sizeof(text))
                     76:                Sys_Error("memory overwrite in Sys_Printf");
                     77: 
                     78:     if (nostdout && nostdout->value)
                     79:         return;
                     80: 
                     81:        for (p = (unsigned char *)text; *p; p++) {
                     82:                *p &= 0x7f;
                     83:                if ((*p > 128 || *p < 32) && *p != 10 && *p != 13 && *p != 9)
                     84:                        printf("[%02x]", *p);
                     85:                else
                     86:                        putc(*p, stdout);
                     87:        }
                     88: }
                     89: 
                     90: void Sys_Quit (void)
                     91: {
                     92:        CL_Shutdown ();
                     93:        Qcommon_Shutdown ();
                     94:     fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
                     95:        _exit(0);
                     96: }
                     97: 
                     98: void Sys_Init(void)
                     99: {
                    100: #if id386
                    101: //     Sys_SetFPCW();
                    102: #endif
                    103: }
                    104: 
                    105: void Sys_Error (char *error, ...)
                    106: { 
                    107:     va_list     argptr;
                    108:     char        string[1024];
                    109: 
                    110: // change stdin to non blocking
                    111:     fcntl (0, F_SETFL, fcntl (0, F_GETFL, 0) & ~FNDELAY);
                    112:     
                    113:     va_start (argptr,error);
                    114:     vsprintf (string,error,argptr);
                    115:     va_end (argptr);
                    116:        fprintf(stderr, "Error: %s\n", string);
                    117: 
                    118:        CL_Shutdown ();
                    119:        Qcommon_Shutdown ();
                    120:        _exit (1);
                    121: 
                    122: } 
                    123: 
                    124: void Sys_Warn (char *warning, ...)
                    125: { 
                    126:     va_list     argptr;
                    127:     char        string[1024];
                    128:     
                    129:     va_start (argptr,warning);
                    130:     vsprintf (string,warning,argptr);
                    131:     va_end (argptr);
                    132:        fprintf(stderr, "Warning: %s", string);
                    133: } 
                    134: 
                    135: /*
                    136: ============
                    137: Sys_FileTime
                    138: 
                    139: returns -1 if not present
                    140: ============
                    141: */
                    142: int    Sys_FileTime (char *path)
                    143: {
                    144:        struct  stat    buf;
                    145:        
                    146:        if (stat (path,&buf) == -1)
                    147:                return -1;
                    148:        
                    149:        return buf.st_mtime;
                    150: }
                    151: 
                    152: void floating_point_exception_handler(int whatever)
                    153: {
                    154: //     Sys_Warn("floating point exception\n");
                    155:        signal(SIGFPE, floating_point_exception_handler);
                    156: }
                    157: 
                    158: char *Sys_ConsoleInput(void)
                    159: {
                    160:     static char text[256];
                    161:     int     len;
                    162:        fd_set  fdset;
                    163:     struct timeval timeout;
                    164: 
                    165:        if (!dedicated || !dedicated->value)
                    166:                return NULL;
                    167: 
                    168:        if (!stdin_active)
                    169:                return NULL;
                    170: 
                    171:        FD_ZERO(&fdset);
                    172:        FD_SET(0, &fdset); // stdin
                    173:        timeout.tv_sec = 0;
                    174:        timeout.tv_usec = 0;
                    175:        if (select (1, &fdset, NULL, NULL, &timeout) == -1 || !FD_ISSET(0, &fdset))
                    176:                return NULL;
                    177: 
                    178:        len = read (0, text, sizeof(text));
                    179:        if (len == 0) { // eof!
                    180:                stdin_active = false;
                    181:                return NULL;
                    182:        }
                    183: 
                    184:        if (len < 1)
                    185:                return NULL;
                    186:        text[len-1] = 0;    // rip off the /n and terminate
                    187: 
                    188:        return text;
                    189: }
                    190: 
                    191: /*****************************************************************************/
                    192: 
                    193: static void *game_library;
                    194: 
                    195: /*
                    196: =================
                    197: Sys_UnloadGame
                    198: =================
                    199: */
                    200: void Sys_UnloadGame (void)
                    201: {
                    202:        if (game_library) 
                    203:                dlclose (game_library);
                    204:        game_library = NULL;
                    205: }
                    206: 
                    207: /*
                    208: =================
                    209: Sys_GetGameAPI
                    210: 
                    211: Loads the game dll
                    212: =================
                    213: */
                    214: void *Sys_GetGameAPI (void *parms)
                    215: {
                    216: #ifndef REF_HARD_LINKED
                    217:        void    *(*GetGameAPI) (void *);
                    218: 
                    219:        char    name[MAX_OSPATH];
                    220:        char    curpath[MAX_OSPATH];
                    221:        char    *path;
                    222: #ifdef __sgi
                    223:        const char *gamename = "gamemips.so";
                    224: #else
                    225: #error Unknown arch
                    226: #endif
                    227: 
                    228:        setreuid(getuid(), getuid());
                    229:        setegid(getgid());
                    230: 
                    231:        if (game_library)
                    232:                Com_Error (ERR_FATAL, "Sys_GetGameAPI without Sys_UnloadingGame");
                    233: 
                    234:        getcwd(curpath, sizeof(curpath));
                    235: 
                    236:        Com_Printf("------- Loading %s -------", gamename);
                    237: 
                    238:        // now run through the search paths
                    239:        path = NULL;
                    240:        while (1)
                    241:        {
                    242:                path = FS_NextPath (path);
                    243:                if (!path)
                    244:                        return NULL;            // couldn't find one anywhere
                    245:                sprintf (name, "%s/%s/%s", curpath, path, gamename);
                    246:                Com_Printf ("Trying to load library (%s)\n",name);
                    247:                game_library = dlopen (name, RTLD_NOW );
                    248:                if (game_library)
                    249:                {
                    250:                        Com_DPrintf ("LoadLibrary (%s)\n",name);
                    251:                        break;
                    252:                }
                    253:        }
                    254: 
                    255:        GetGameAPI = (void *)dlsym (game_library, "GetGameAPI");
                    256:        if (!GetGameAPI)
                    257:        {
                    258:                Sys_UnloadGame ();              
                    259:                return NULL;
                    260:        }
                    261: 
                    262:        return GetGameAPI (parms);
                    263: #else
                    264:        return (void *)GetGameAPI (parms);
                    265: #endif
                    266: }
                    267: 
                    268: /*****************************************************************************/
                    269: 
                    270: void Sys_AppActivate (void)
                    271: {
                    272: }
                    273: 
                    274: void Sys_SendKeyEvents (void)
                    275: {
                    276:        if (KBD_Update_fp)
                    277:                KBD_Update_fp();
                    278: 
                    279:        // grab frame time 
                    280:        sys_frame_time = Sys_Milliseconds();
                    281: }
                    282: 
                    283: /*****************************************************************************/
                    284: 
                    285: char *Sys_GetClipboardData(void)
                    286: {
                    287:        return NULL;
                    288: }
                    289: 
                    290: int main (int argc, char **argv)
                    291: {
                    292:        int     time, oldtime, newtime;
                    293: 
                    294:        // go back to real user for config loads
                    295:        saved_euid = geteuid();
                    296:        seteuid(getuid());
                    297: 
                    298:        Qcommon_Init(argc, argv);
                    299: 
                    300: /*     fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY); */
                    301: 
                    302:        nostdout = Cvar_Get("nostdout", "0", 0);
                    303:        if (!nostdout->value) {
                    304: /*             fcntl(0, F_SETFL, fcntl (0, F_GETFL, 0) | FNDELAY); */
                    305: //             printf ("Linux Quake -- Version %0.3f\n", LINUX_VERSION);
                    306:        }
                    307: 
                    308:     oldtime = Sys_Milliseconds ();
                    309:     while (1)
                    310:     {
                    311: // find time spent rendering last frame
                    312:                do {
                    313:                        newtime = Sys_Milliseconds ();
                    314:                        time = newtime - oldtime;
                    315:                } while (time < 1);
                    316:                Qcommon_Frame (time);
                    317:                oldtime = newtime;
                    318:     }
                    319: 
                    320: }
                    321: 
                    322: void Sys_CopyProtect(void)
                    323: {
                    324:        FILE *mnt;
                    325:        struct mntent *ent;
                    326:        char path[MAX_OSPATH];
                    327:        struct stat st;
                    328:        qboolean found_cd = false;
                    329: 
                    330:        static qboolean checked = false;
                    331: 
                    332:        if (checked)
                    333:                return;
                    334: 
                    335:         Com_Printf("XXX - Sys_CopyProtect disabled\n");
                    336:        checked = true;
                    337:        return;
                    338: 
                    339:        if ((mnt = setmntent("/etc/mtab", "r")) == NULL)
                    340:                Com_Error(ERR_FATAL, "Can't read mount table to determine mounted cd location.");
                    341: 
                    342:        while ((ent = getmntent(mnt)) != NULL) {
                    343:                if (strcmp(ent->mnt_type, "iso9660") == 0) {
                    344:                        // found a cd file system
                    345:                        found_cd = true;
                    346:                        sprintf(path, "%s/%s", ent->mnt_dir, "install/data/quake2.exe");
                    347:                        if (stat(path, &st) == 0) {
                    348:                                // found it
                    349:                                checked = true;
                    350:                                endmntent(mnt);
                    351:                                return;
                    352:                        }
                    353:                        sprintf(path, "%s/%s", ent->mnt_dir, "Install/Data/quake2.exe");
                    354:                        if (stat(path, &st) == 0) {
                    355:                                // found it
                    356:                                checked = true;
                    357:                                endmntent(mnt);
                    358:                                return;
                    359:                        }
                    360:                        sprintf(path, "%s/%s", ent->mnt_dir, "quake2.exe");
                    361:                        if (stat(path, &st) == 0) {
                    362:                                // found it
                    363:                                checked = true;
                    364:                                endmntent(mnt);
                    365:                                return;
                    366:                        }
                    367:                }
                    368:        }
                    369:        endmntent(mnt);
                    370: 
                    371:        if (found_cd)
                    372:                Com_Error (ERR_FATAL, "Could not find a Quake2 CD in your CD drive.");
                    373:        Com_Error (ERR_FATAL, "Unable to find a mounted iso9660 file system.\n"
                    374:                "You must mount the Quake2 CD in a cdrom drive in order to play.");
                    375: }
                    376: 
                    377: #if 0
                    378: /*
                    379: ================
                    380: Sys_MakeCodeWriteable
                    381: ================
                    382: */
                    383: void Sys_MakeCodeWriteable (unsigned long startaddr, unsigned long length)
                    384: {
                    385: 
                    386:        int r;
                    387:        unsigned long addr;
                    388:        int psize = getpagesize();
                    389: 
                    390:        addr = (startaddr & ~(psize-1)) - psize;
                    391: 
                    392: //     fprintf(stderr, "writable code %lx(%lx)-%lx, length=%lx\n", startaddr,
                    393: //                     addr, startaddr+length, length);
                    394: 
                    395:        r = mprotect((char*)addr, length + startaddr - addr + psize, 7);
                    396: 
                    397:        if (r < 0)
                    398:                Sys_Error("Protection change failed\n");
                    399: 
                    400: }
                    401: 
                    402: #endif

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.