Annotation of quake2/solaris/q_shsolaris.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 <sys/types.h>
                     21: #include <errno.h>
                     22: #include <stdio.h>
                     23: #include <dirent.h>
                     24: #include <sys/stat.h>
                     25: #include <unistd.h>
                     26: #include <sys/mman.h>
                     27: #include <sys/time.h>
                     28: 
                     29: #include "../linux/glob.h"
                     30: 
                     31: #include "../qcommon/qcommon.h"
                     32: 
                     33: //===============================================================================
                     34: 
                     35: byte *membase;
                     36: int maxhunksize;
                     37: int curhunksize;
                     38: 
                     39: void *Hunk_Begin (int maxsize)
                     40: {
                     41:        // reserve a huge chunk of memory, but don't commit any yet
                     42:        maxhunksize = maxsize;
                     43:        curhunksize = 0;
                     44:        membase = malloc(maxhunksize);
                     45:        if (membase == NULL)
                     46:                Sys_Error(ERR_FATAL, "unable to allocate %d bytes", maxsize);
                     47: 
                     48:        return membase;
                     49: }
                     50: 
                     51: void *Hunk_Alloc (int size)
                     52: {
                     53:        byte *buf;
                     54: 
                     55:        // round to cacheline
                     56:        size = (size+31)&~31;
                     57:        if (curhunksize + size > maxhunksize)
                     58:                Sys_Error(ERR_FATAL, "Hunk_Alloc overflow");
                     59:        buf = membase + curhunksize;
                     60:        curhunksize += size;
                     61:        return buf;
                     62: }
                     63: 
                     64: int Hunk_End (void)
                     65: {
                     66:        byte *n;
                     67: 
                     68:        n = realloc(membase, curhunksize);
                     69:        if (n != membase)
                     70:                Sys_Error(ERR_FATAL, "Hunk_End:  Could not remap virtual block (%d)", errno);
                     71:        
                     72:        return curhunksize;
                     73: }
                     74: 
                     75: void Hunk_Free (void *base)
                     76: {
                     77:        if (base) 
                     78:                free(base);
                     79: }
                     80: 
                     81: //===============================================================================
                     82: 
                     83: 
                     84: /*
                     85: ================
                     86: Sys_Milliseconds
                     87: ================
                     88: */
                     89: int curtime;
                     90: int Sys_Milliseconds (void)
                     91: {
                     92:        struct timeval tp;
                     93:        struct timezone tzp;
                     94:        static int              secbase;
                     95: 
                     96:        gettimeofday(&tp, &tzp);
                     97:        
                     98:        if (!secbase)
                     99:        {
                    100:                secbase = tp.tv_sec;
                    101:                return tp.tv_usec/1000;
                    102:        }
                    103: 
                    104:        curtime = (tp.tv_sec - secbase)*1000 + tp.tv_usec/1000;
                    105:        
                    106:        return curtime;
                    107: }
                    108: 
                    109: void Sys_Mkdir (char *path)
                    110: {
                    111:     mkdir (path, 0777);
                    112: }
                    113: 
                    114: char *strlwr (char *s)
                    115: {
                    116:        while (*s) {
                    117:                *s = tolower(*s);
                    118:                s++;
                    119:        }
                    120: }
                    121: 
                    122: //============================================
                    123: 
                    124: static char    findbase[MAX_OSPATH];
                    125: static char    findpath[MAX_OSPATH];
                    126: static char    findpattern[MAX_OSPATH];
                    127: static DIR             *fdir;
                    128: 
                    129: static qboolean CompareAttributes(char *path, char *name,
                    130:        unsigned musthave, unsigned canthave )
                    131: {
                    132:        struct stat st;
                    133:        char fn[MAX_OSPATH];
                    134: 
                    135: // . and .. never match
                    136:        if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
                    137:                return false;
                    138: 
                    139:        sprintf(fn, "%s/%s", path, name);
                    140:        if (stat(fn, &st) == -1)
                    141:                return false; // shouldn't happen
                    142: 
                    143:        if ( ( st.st_mode & S_IFDIR ) && ( canthave & SFF_SUBDIR ) )
                    144:                return false;
                    145: 
                    146:        if ( ( musthave & SFF_SUBDIR ) && !( st.st_mode & S_IFDIR ) )
                    147:                return false;
                    148: 
                    149:        return true;
                    150: }
                    151: 
                    152: char *Sys_FindFirst (char *path, unsigned musthave, unsigned canhave)
                    153: {
                    154:        struct dirent *d;
                    155:        char *p;
                    156: 
                    157:        if (fdir)
                    158:                Sys_Error ("Sys_BeginFind without close");
                    159: 
                    160: //     COM_FilePath (path, findbase);
                    161:        strcpy(findbase, path);
                    162:        
                    163:        if ((p = strrchr(findbase, '/')) != NULL) {
                    164:                *p = 0;
                    165:                strcpy(findpattern, p + 1);
                    166:        } else
                    167:                strcpy(findpattern, "*");
                    168: 
                    169:        if (strcmp(findpattern, "*.*") == 0)
                    170:                strcpy(findpattern, "*");
                    171:        
                    172:        if ((fdir = opendir(path)) == NULL)
                    173:                return NULL;
                    174:        while ((d = readdir(fdir)) != NULL) {
                    175:                if (!*findpattern || glob_match(findpattern, d->d_name)) {
                    176: //                     if (*findpattern)
                    177: //                             printf("%s matched %s\n", findpattern, d->d_name);
                    178:                        if (CompareAttributes(findbase, d->d_name, musthave, canhave)) {
                    179:                                sprintf (findpath, "%s/%s", findbase, d->d_name);
                    180:                                return findpath;
                    181:                        }
                    182:                }
                    183:        }
                    184:        return NULL;
                    185: }
                    186: 
                    187: char *Sys_FindNext (unsigned musthave, unsigned canhave)
                    188: {
                    189:        struct dirent *d;
                    190: 
                    191:        if (fdir == NULL)
                    192:                return NULL;
                    193:        while ((d = readdir(fdir)) != NULL) {
                    194:                if (!*findpattern || glob_match(findpattern, d->d_name)) {
                    195: //                     if (*findpattern)
                    196: //                             printf("%s matched %s\n", findpattern, d->d_name);
                    197:                        if (CompareAttributes(findbase, d->d_name, musthave, canhave)) {
                    198:                                sprintf (findpath, "%s/%s", findbase, d->d_name);
                    199:                                return findpath;
                    200:                        }
                    201:                }
                    202:        }
                    203:        return NULL;
                    204: }
                    205: 
                    206: void Sys_FindClose (void)
                    207: {
                    208:        if (fdir != NULL)
                    209:                closedir(fdir);
                    210:        fdir = NULL;
                    211: }
                    212: 
                    213: 
                    214: //============================================
                    215: 

unix.superglobalmegacorp.com

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