📄 os2.c
字号:
{ int len = ((prepend) ? strlen(prepend) : 0) + ((append) ? strlen(append) : 0) + strlen(dirName) + 1; char *retval = malloc(len); char *p; BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL); if (prepend) strcpy(retval, prepend); else retval[0] = '\0'; strcat(retval, dirName); if (append) strcat(retval, append); for (p = strchr(retval, '/'); p != NULL; p = strchr(p + 1, '/')) *p = '\\'; return(retval);} /* __PHYSFS_platformCvtToDependent */LinkedStringList *__PHYSFS_platformEnumerateFiles(const char *dirname, int omitSymLinks){ char spec[CCHMAXPATH]; LinkedStringList *ret = NULL, *p = NULL; FILEFINDBUF3 fb; HDIR hdir = HDIR_CREATE; ULONG count = 1; APIRET rc; BAIL_IF_MACRO(strlen(dirname) > sizeof (spec) - 5, ERR_BAD_FILENAME, NULL); strcpy(spec, dirname); strcat(spec, (spec[strlen(spec) - 1] != '\\') ? "\\*.*" : "*.*"); rc = DosFindFirst(spec, &hdir, FILE_DIRECTORY | FILE_ARCHIVED | FILE_READONLY | FILE_HIDDEN | FILE_SYSTEM, &fb, sizeof (fb), &count, FIL_STANDARD); BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, 0); while (count == 1) { if ((strcmp(fb.achName, ".") != 0) && (strcmp(fb.achName, "..") != 0)) ret = __PHYSFS_addToLinkedStringList(ret, &p, fb.achName, -1); DosFindNext(hdir, &fb, sizeof (fb), &count); } /* while */ DosFindClose(hdir); return(ret);} /* __PHYSFS_platformEnumerateFiles */char *__PHYSFS_platformCurrentDir(void){ char *retval; ULONG currentDisk; ULONG dummy; ULONG pathSize = 0; APIRET rc; BYTE byte; rc = DosQueryCurrentDisk(¤tDisk, &dummy); BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, NULL); /* The first call just tells us how much space we need for the string. */ rc = DosQueryCurrentDir(currentDisk, &byte, &pathSize); pathSize++; /* Add space for null terminator. */ retval = (char *) malloc(pathSize + 3); /* plus "x:\\" */ BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL); /* Actually get the string this time. */ rc = DosQueryCurrentDir(currentDisk, (PBYTE) (retval + 3), &pathSize); if (os2err(rc) != NO_ERROR) { free(retval); return(NULL); } /* if */ retval[0] = ('A' + (currentDisk - 1)); retval[1] = ':'; retval[2] = '\\'; return(retval);} /* __PHYSFS_platformCurrentDir */char *__PHYSFS_platformRealPath(const char *path){ char buf[CCHMAXPATH]; char *retval; APIRET rc = DosQueryPathInfo(path, FIL_QUERYFULLNAME, buf, sizeof (buf)); BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, NULL); retval = (char *) malloc(strlen(buf) + 1); BAIL_IF_MACRO(retval == NULL, ERR_OUT_OF_MEMORY, NULL); strcpy(retval, buf); return(retval);} /* __PHYSFS_platformRealPath */int __PHYSFS_platformMkDir(const char *path){ return(os2err(DosCreateDir(path, NULL)) == NO_ERROR);} /* __PHYSFS_platformMkDir */void *__PHYSFS_platformOpenRead(const char *filename){ ULONG actionTaken = 0; HFILE hfile = NULLHANDLE; /* * File must be opened SHARE_DENYWRITE and ACCESS_READONLY, otherwise * DosQueryFileInfo() will fail if we try to get a file length, etc. */ os2err(DosOpen(filename, &hfile, &actionTaken, 0, FILE_NORMAL, OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_FAIL_IF_NEW, OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_LOCALITY | OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYWRITE | OPEN_ACCESS_READONLY, NULL)); return((void *) hfile);} /* __PHYSFS_platformOpenRead */void *__PHYSFS_platformOpenWrite(const char *filename){ ULONG actionTaken = 0; HFILE hfile = NULLHANDLE; /* * File must be opened SHARE_DENYWRITE and ACCESS_READWRITE, otherwise * DosQueryFileInfo() will fail if we try to get a file length, etc. */ os2err(DosOpen(filename, &hfile, &actionTaken, 0, FILE_NORMAL, OPEN_ACTION_REPLACE_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW, OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_LOCALITY | OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYWRITE | OPEN_ACCESS_READWRITE, NULL)); return((void *) hfile);} /* __PHYSFS_platformOpenWrite */void *__PHYSFS_platformOpenAppend(const char *filename){ ULONG dummy = 0; HFILE hfile = NULLHANDLE; APIRET rc; /* * File must be opened SHARE_DENYWRITE and ACCESS_READWRITE, otherwise * DosQueryFileInfo() will fail if we try to get a file length, etc. */ rc = os2err(DosOpen(filename, &hfile, &dummy, 0, FILE_NORMAL, OPEN_ACTION_OPEN_IF_EXISTS | OPEN_ACTION_CREATE_IF_NEW, OPEN_FLAGS_FAIL_ON_ERROR | OPEN_FLAGS_NO_LOCALITY | OPEN_FLAGS_NOINHERIT | OPEN_SHARE_DENYWRITE | OPEN_ACCESS_READWRITE, NULL)); if (rc == NO_ERROR) { if (os2err(DosSetFilePtr(hfile, 0, FILE_END, &dummy)) != NO_ERROR) { DosClose(hfile); hfile = NULLHANDLE; } /* if */ } /* if */ return((void *) hfile);} /* __PHYSFS_platformOpenAppend */PHYSFS_sint64 __PHYSFS_platformRead(void *opaque, void *buffer, PHYSFS_uint32 size, PHYSFS_uint32 count){ HFILE hfile = (HFILE) opaque; PHYSFS_sint64 retval; ULONG br; for (retval = 0; retval < count; retval++) { os2err(DosRead(hfile, buffer, size, &br)); if (br < size) { DosSetFilePtr(hfile, -br, FILE_CURRENT, &br); /* try to cleanup. */ return(retval); } /* if */ buffer = (void *) ( ((char *) buffer) + size ); } /* for */ return(retval);} /* __PHYSFS_platformRead */PHYSFS_sint64 __PHYSFS_platformWrite(void *opaque, const void *buffer, PHYSFS_uint32 size, PHYSFS_uint32 count){ HFILE hfile = (HFILE) opaque; PHYSFS_sint64 retval; ULONG bw; for (retval = 0; retval < count; retval++) { os2err(DosWrite(hfile, buffer, size, &bw)); if (bw < size) { DosSetFilePtr(hfile, -bw, FILE_CURRENT, &bw); /* try to cleanup. */ return(retval); } /* if */ buffer = (void *) ( ((char *) buffer) + size ); } /* for */ return(retval);} /* __PHYSFS_platformWrite */int __PHYSFS_platformSeek(void *opaque, PHYSFS_uint64 pos){ ULONG dummy; HFILE hfile = (HFILE) opaque; LONG dist = (LONG) pos; /* hooray for 32-bit filesystem limits! :) */ BAIL_IF_MACRO((PHYSFS_uint64) dist != pos, ERR_SEEK_OUT_OF_RANGE, 0); return(os2err(DosSetFilePtr(hfile, dist, FILE_BEGIN, &dummy)) == NO_ERROR);} /* __PHYSFS_platformSeek */PHYSFS_sint64 __PHYSFS_platformTell(void *opaque){ ULONG pos; HFILE hfile = (HFILE) opaque; APIRET rc = os2err(DosSetFilePtr(hfile, 0, FILE_CURRENT, &pos)); BAIL_IF_MACRO(rc != NO_ERROR, NULL, -1); return((PHYSFS_sint64) pos);} /* __PHYSFS_platformTell */PHYSFS_sint64 __PHYSFS_platformFileLength(void *opaque){ FILESTATUS3 fs; HFILE hfile = (HFILE) opaque; APIRET rc = DosQueryFileInfo(hfile, FIL_STANDARD, &fs, sizeof (fs)); BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, -1); return((PHYSFS_sint64) fs.cbFile);} /* __PHYSFS_platformFileLength */int __PHYSFS_platformEOF(void *opaque){ PHYSFS_sint64 len, pos; len = __PHYSFS_platformFileLength(opaque); BAIL_IF_MACRO(len == -1, NULL, 1); /* (*shrug*) */ pos = __PHYSFS_platformTell(opaque); BAIL_IF_MACRO(pos == -1, NULL, 1); /* (*shrug*) */ return(pos >= len);} /* __PHYSFS_platformEOF */int __PHYSFS_platformFlush(void *opaque){ return(os2err(DosResetBuffer((HFILE) opaque) == NO_ERROR));} /* __PHYSFS_platformFlush */int __PHYSFS_platformClose(void *opaque){ return(os2err(DosClose((HFILE) opaque) == NO_ERROR));} /* __PHYSFS_platformClose */int __PHYSFS_platformDelete(const char *path){ if (__PHYSFS_platformIsDirectory(path)) return(os2err(DosDeleteDir(path)) == NO_ERROR); return(os2err(DosDelete(path) == NO_ERROR));} /* __PHYSFS_platformDelete */PHYSFS_sint64 __PHYSFS_platformGetLastModTime(const char *fname){ PHYSFS_sint64 retval; struct tm tm; FILESTATUS3 fs; APIRET rc = DosQueryPathInfo(fname, FIL_STANDARD, &fs, sizeof (fs)); BAIL_IF_MACRO(os2err(rc) != NO_ERROR, NULL, -1); /* Convert to a format that mktime() can grok... */ tm.tm_sec = ((PHYSFS_uint32) fs.ftimeLastWrite.twosecs) * 2; tm.tm_min = fs.ftimeLastWrite.minutes; tm.tm_hour = fs.ftimeLastWrite.hours; tm.tm_mday = fs.fdateLastWrite.day; tm.tm_mon = fs.fdateLastWrite.month; tm.tm_year = ((PHYSFS_uint32) fs.fdateLastWrite.year) + 80; tm.tm_wday = -1 /*st_localtz.wDayOfWeek*/; tm.tm_yday = -1; tm.tm_isdst = -1; /* Convert to a format PhysicsFS can grok... */ retval = (PHYSFS_sint64) mktime(&tm); BAIL_IF_MACRO(retval == -1, strerror(errno), -1); return(retval);} /* __PHYSFS_platformGetLastModTime *//* Much like my college days, try to sleep for 10 milliseconds at a time... */void __PHYSFS_platformTimeslice(void){ DosSleep(10);} /* __PHYSFS_platformTimeslice(void) */PHYSFS_uint64 __PHYSFS_platformGetThreadID(void){ PTIB ptib; PPIB ppib; /* * Allegedly, this API never fails, but we'll punt and return a * default value (zero might as well do) if it does. */ BAIL_IF_MACRO(os2err(DosGetInfoBlocks(&ptib, &ppib)) != NO_ERROR, 0, 0); return((PHYSFS_uint64) ptib->tib_ordinal);} /* __PHYSFS_platformGetThreadID */void *__PHYSFS_platformCreateMutex(void){ HMTX hmtx = NULLHANDLE; os2err(DosCreateMutexSem(NULL, &hmtx, 0, 0)); return((void *) hmtx);} /* __PHYSFS_platformCreateMutex */void __PHYSFS_platformDestroyMutex(void *mutex){ DosCloseMutexSem((HMTX) mutex);} /* __PHYSFS_platformDestroyMutex */int __PHYSFS_platformGrabMutex(void *mutex){ /* Do _NOT_ call os2err() (which sets the physfs error msg) in here! */ return(DosRequestMutexSem((HMTX) mutex, SEM_INDEFINITE_WAIT) == NO_ERROR);} /* __PHYSFS_platformGrabMutex */void __PHYSFS_platformReleaseMutex(void *mutex){ DosReleaseMutexSem((HMTX) mutex);} /* __PHYSFS_platformReleaseMutex */#endif /* defined OS2 *//* end of os2.c ... */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -