📄 i_system.c
字号:
int meminfo_fd = -1; meminfo_fd = open(MEMINFO_FILE, O_RDONLY); n = read(meminfo_fd, buf, 1023); close(meminfo_fd); if(n<0) { // Error *total = 0L; return 0; } buf[n] = '\0'; if(NULL == (memTag = strstr(buf, MEMTOTAL))) { // Error *total = 0L; return 0; } memTag += sizeof(MEMTOTAL); totalKBytes = atoi(memTag); if(NULL == (memTag = strstr(buf, MEMFREE))) { // Error *total = 0L; return 0; } memTag += sizeof(MEMFREE); freeKBytes = atoi(memTag); *total = totalKBytes << 10; return freeKBytes << 10;}static int quiting=0; /* prevent recursive I_Quit() */void I_Tactile( int on, int off, int total ){ // UNUSED. on = off = total = 0;}ticcmd_t emptycmd;ticcmd_t* I_BaseTiccmd(void){ return &emptycmd;}//// I_GetTime// returns time in 1/TICRATE second tics//tic_t I_GetTime (void){ struct timeval tp; struct timezone tzp; int newtics; static int oldtics=0; static int basetime=0; again: gettimeofday(&tp, &tzp); if (!basetime) basetime = tp.tv_sec; // On systems with RTC drift correction or NTP we need to take // care about the system clock running backwards sometimes. Make // sure the new tic is later then the last one. newtics = (tp.tv_sec-basetime)*TICRATE + tp.tv_usec*TICRATE/1000000; if (!oldtics) oldtics = newtics; if (newtics < oldtics) { I_WaitVBL(1); goto again; } oldtics = newtics; return newtics;}//// I_Init//void I_Init (void){ I_StartupSound(); I_InitMusic(); quiting = 0; // I_InitGraphics();}//// I_Quit//void I_Quit (void){ /* prevent recursive I_Quit() */ if(quiting) return; quiting = 1; //added:16-02-98: when recording a demo, should exit using 'q' key, // but sometimes we forget and use 'F10'.. so save here too. if (demorecording) G_CheckDemoStatus(); D_QuitNetGame (); I_ShutdownMusic(); I_ShutdownSound(); I_ShutdownCD(); // use this for 1.28 19990220 by Kin M_SaveConfig (NULL); I_ShutdownGraphics(); I_ShutdownSystem(); printf("\r"); ShowEndTxt(); exit(0);}void I_WaitVBL(int count){#ifdef SGI sginap(1); #else#ifdef SUN sleep(0);#else usleep (count * (1000000/70) ); #endif#endif}void I_BeginRead(void){}void I_EndRead(void){}byte* I_AllocLow(int length){ byte* mem; mem = (byte *)malloc (length); memset (mem,0,length); return mem;}//// I_Error//extern boolean demorecording;void I_Error (char *error, ...){ va_list argptr; // Message first. va_start (argptr,error); fprintf (stderr, "Error: "); vfprintf (stderr,error,argptr); fprintf (stderr, "\n"); va_end (argptr); fflush( stderr ); // Shutdown. Here might be other errors. if (demorecording) G_CheckDemoStatus(); D_QuitNetGame (); I_ShutdownMusic(); I_ShutdownSound(); I_ShutdownGraphics(); // shutdown everything else which was registered I_ShutdownSystem(); exit(-1);}#define MAX_QUIT_FUNCS 16typedef void (*quitfuncptr)();static quitfuncptr quit_funcs[MAX_QUIT_FUNCS] = { NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL };//// Adds a function to the list that need to be called by I_SystemShutdown().//void I_AddExitFunc(void (*func)()){ int c; for (c=0; c<MAX_QUIT_FUNCS; c++) { if (!quit_funcs[c]) { quit_funcs[c] = func; break; } }}//// Removes a function from the list that need to be called by// I_SystemShutdown().//void I_RemoveExitFunc(void (*func)()){ int c; for (c=0; c<MAX_QUIT_FUNCS; c++) { if (quit_funcs[c] == func) { while (c<MAX_QUIT_FUNCS-1) { quit_funcs[c] = quit_funcs[c+1]; c++; } quit_funcs[MAX_QUIT_FUNCS-1] = NULL; break; } }}//// Closes down everything. This includes restoring the initial// pallete and video mode, and removing whatever mouse, keyboard, and// timer routines have been installed.//// NOTE : Shutdown user funcs. are effectively called in reverse order.//void I_ShutdownSystem(){ int c; for (c=MAX_QUIT_FUNCS-1; c>=0; c--) if (quit_funcs[c]) (*quit_funcs[c])();}void I_GetDiskFreeSpace(long long *freespace) { struct statfs stfs; if(statfs(".",&stfs)==-1) { *freespace = MAXINT; return; } *freespace = stfs.f_bavail*stfs.f_bsize;}char *I_GetUserName(void){ static char username[MAXPLAYERNAME]; char *p; if((p=getenv("USER"))==NULL) if((p=getenv("user"))==NULL) if((p=getenv("USERNAME"))==NULL) if((p=getenv("username"))==NULL) return NULL; strncpy(username,p,MAXPLAYERNAME); if( strcmp(username,"")==0 ) return NULL; return username;}int I_mkdir(const char *dirname, int unixright){ return mkdir(dirname, unixright);}// check if doom3.wad exists in the given pathstatic boolean isWadPathOk(char *path) { char wad3path[256]; sprintf(wad3path, "%s/%s", path, WADKEYWORD); if(access(wad3path, R_OK)) { return false; // no access } return true;}// search for doom3.wad in the given pathstatic char *searchWad(char *searchDir) { int pipeDescr[2]; pid_t childPid; boolean finished; printf("Searching directory '%s' for '%s' ... please wait\n", searchDir, WADKEYWORD); if(pipe(pipeDescr) == -1) { fprintf(stderr, "Unable to open pipe\n"); return NULL; } // generate child process childPid = fork(); if(childPid == -1) { fprintf(stderr, "Unable to fork\n"); return NULL; } if(childPid == 0) { // here comes the child close(pipeDescr[0]); // set stdout to pipe dup2(pipeDescr[1], STDOUT_FILENO); // execute the find command execlp("find", "find", searchDir, "-name", WADKEYWORD, NULL); exit(1); // shouldn't be reached } // parent close(pipeDescr[1]); // now we have to wait for the output of 'find' finished = false; while(!finished) { char *namePtr; int pathLen; pathLen = read(pipeDescr[0], returnWadPath, 256); if(pathLen == 0) { // end of "file" reached return NULL; } if(pathLen == -1) { // should not happen fprintf(stderr, "searchWad: reading in non-blocking mode - please fix me\n"); return NULL; } namePtr = strstr(returnWadPath, WADKEYWORD); // check if we read something sensible if(namePtr--) { *namePtr = 0; //terminate string before doom3.wad finished = true; } } // kill child ... oops kill(childPid, SIGKILL); return returnWadPath;}// go through all possible paths and look for doom3.wadstatic char *locateWad(void){ char *WadPath; char *userhome; // does DOOMWADDIR exist? WadPath = getenv("DOOMWADDIR"); if(WadPath) { if(isWadPathOk(WadPath)) { return WadPath; } } // examine current dir strcpy(returnWadPath, "."); if(isWadPathOk(returnWadPath)) { return returnWadPath; } // examine default dirs strcpy(returnWadPath, DEFAULTWADLOCATION1); if(isWadPathOk(returnWadPath)) { return returnWadPath; } strcpy(returnWadPath, DEFAULTWADLOCATION2); if(isWadPathOk(returnWadPath)) { return returnWadPath; } // find in $HOME userhome = getenv("HOME"); if(userhome) { WadPath = searchWad(userhome); if(WadPath) { return WadPath; } } // find in /usr/local WadPath = searchWad(DEFAULTSEARCHPATH1); if(WadPath) { return WadPath; } // find in /usr/games WadPath = searchWad(DEFAULTSEARCHPATH2); if(WadPath) { return WadPath; } // if nothing was found return NULL;}void I_LocateWad(void) { char *waddir; waddir = locateWad(); if(waddir) { chdir(waddir); // change to the directory where we found doom3.wad } return;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -