Annotation of quake1/sys_irix.c, revision 1.1.1.1

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

unix.superglobalmegacorp.com

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