psplinux.c
来自「db.* (pronounced dee-be star) is an adva」· C语言 代码 · 共 1,098 行 · 第 1/2 页
C
1,098 行
/*************************************************************************** * * * db.linux * * open source platform support package for Linux (tm) * * * * Copyright (c) 2000 Centura Software Corporation. All rights reseved. * * * * Use of this software, whether in source code format, or in executable, * * binary object code form, is governed by the CENTURA OPEN SOURCE LICENSE * * which is fully described in the LICENSE.TXT file, included within this * * distribution of source code files. * * * **************************************************************************//* pspunix.c - Contains UNIX specific functions */#include <signal.h>#include <dirent.h>#include <unistd.h>#include <mntent.h>#include <sys/stat.h>#include <sys/file.h>#include "psp.h"#include "pspint.h"typedef struct DEVICE_LIST_ { struct DEVICE_LIST_ *next; dev_t device; char *name;} DEVICE_LIST;DEVICE_LIST *deviceHead = NULL;PSP_SEM deviceSem = NO_PSP_SEM;/* ========================================================================== OS Specific initialization*/int psp_osInit( void){ deviceHead = NULL; if ((deviceSem = psp_syncCreate(PSP_MUTEX_SEM)) == NO_PSP_SEM) return PSP_FAILED; signal(SIGPIPE, SIG_IGN); return PSP_OKAY;}/* ========================================================================== OS Specific termination*/void psp_osTerm( void){ psp_syncDelete(deviceSem);}/* ========================================================================== Open a file*/short psp_flOpen( const char *name, unsigned int flags, unsigned short xflags, PSP_FDESC *desc){ if ((*desc = open(name, flags, 0666)) == -1) { *desc = 0; return errno; } return 0;}/* ========================================================================== Close a file*/void psp_flClose( PSP_FDESC desc){ close(desc);}/* ========================================================================== Seek to a specified address in a file*/void psp_flSeek( PSP_FDESC desc, size_t addr, int origin){ lseek(desc, addr, origin);}/* ========================================================================== Write to a file*/size_t psp_flWrite( PSP_FDESC desc, const void *buf, size_t size){ size_t bytes; errno = 0; bytes = write(desc, buf, size); return (bytes <= 0 && errno) ? -1 : bytes;}/* ========================================================================== Read from a file*/size_t psp_flRead( PSP_FDESC desc, void *buf, size_t size){ size_t bytes; errno = 0; bytes = read(desc, buf, size); return (bytes <= 0 && errno) ? -1 : bytes;}/* ========================================================================== Write to a specified address in a file*/size_t psp_flSeekWrite( PSP_FDESC desc, size_t addr, const void *buf, size_t size){ size_t bytes; if (addr != (size_t) -1) { if (lseek(desc, addr, SEEK_SET) == -1) return -1; } errno = 0; bytes = write(desc, buf, size); return (bytes <= 0 && errno) ? -1 : bytes;}/* =========================================================================== Read from a specified address in a file*/size_t psp_flSeekRead( PSP_FDESC desc, size_t addr, void *buf, size_t size){ size_t bytes; if (addr != (size_t) -1) { if (lseek(desc, addr, SEEK_SET) == -1) return -1; } errno = 0; bytes = read(desc, buf, size); return (bytes != size && errno) ? -1 : bytes;}/* =========================================================================== Synchronize writes to a file*/void psp_flSync( PSP_FDESC desc){ fsync(desc);}/* =========================================================================== Find the current size of a file*/size_t psp_flSize( PSP_FDESC desc){ size_t cur = lseek(desc, 0, SEEK_CUR); size_t size = lseek(desc, 0, SEEK_END); lseek(desc, cur, SEEK_SET); return size;}/* =========================================================================== Set the current size of a file*/int psp_flSetSize( PSP_FDESC desc, size_t size){ size_t ii; char buf[10240]; lseek(desc, 0, SEEK_END); memset(buf, 0, sizeof(buf)); for (ii = 0; ii < size / sizeof(buf); ii++) { if (write(desc, buf, sizeof(buf)) < (int) sizeof(buf)) return -1; } if (size % sizeof(buf)) { if (write(desc, buf, size % sizeof(buf)) < (int) (size % sizeof(buf))) return -1; } return 0;}/* =========================================================================== Lock a file*/short psp_flLock( PSP_FDESC desc){ if ((flock(desc, LOCK_EX|LOCK_NB) != 0) && (errno == EWOULDBLOCK || errno == EACCES)) return PSP_FAILED; return PSP_OKAY;}/* =========================================================================== Lock a file*/void psp_flUnlock( PSP_FDESC desc){ flock(desc, LOCK_UN|LOCK_NB);}/* =========================================================================== Get last access time for a file*/long psp_flLastAccess( PSP_FDESC desc){ struct stat stbuf; if (fstat(desc, &stbuf) < 0) return -1; return stbuf.st_atime;}/* =========================================================================== Get modification time for a file*/long psp_flModTime( PSP_FDESC desc){ struct stat stbuf; if (fstat(desc, &stbuf) < 0) return -1; return stbuf.st_mtime;}/* =========================================================================== See if the specified string matches the given file name. On case sensitive file systems (UNIX) this should be a case sensitive. Other wise it should be case insensitive*/int psp_fileNameCmp( const char *str, const char *file){ return strcmp(str, file);}/* =========================================================================== See if the specified string matches the given file name. On case sensitive file systems (UNIX) this should be a case sensitive. Other wise it should be case insensitive*/int psp_fileNamenCmp( const char *str, const char *file, size_t len){ return strncmp(str, file, len);}/* =========================================================================== Rename a file*/int psp_fileRename( const char *src, const char *dest){ return rename(src, dest);}/* =========================================================================== Delete a file*/int psp_fileRemove( const char *name){ return remove(name);}#if 0/* ========================================================================== Return the size of a file*/size_t psp_fileSize( const char *name){ struct stat stbuf; if (stat(name, &stbuf) < 0) return 0; return stbuf.st_size;}#endif/* ========================================================================== Validate the existence of a file*/int psp_fileValidate( const char *name){ return !access(name, 0);}#define INI_BUF_SIZE 1024typedef struct { PSP_FH fh; char *buf;} INI_ENTRY;/* convert a string to an integer, handling octal, decimal, and hex */static int strtoi( const char *s){ int d; int v = 0; int neg = 0; int base = 10; while (isspace(*s)) ++s; if (*s == '-') { neg = 1; ++s; } if (*s == '0') { if (neg) return v; /* can't negate octal or hex numbers */ base = 8; if (*++s == 'x' || *s == 'X') { base = 16; ++s; } } for ( ; *s; s++) { if (v > INT_MAX / base) break; if ((d = *s - '0') < 0 || d > 9) { if ((d = toupper(*s) - 'A') < 0 || d > 5) break; if ((d += 10) >= base) break; } v = v * base + d; } if (neg) return -v; return v;}/* ========================================================================== Open an INI file*/PSP_INI psp_iniOpen( const DB_TCHAR *name){ INI_ENTRY *ini; if ((ini = psp_getMemory(sizeof(INI_ENTRY), 0)) == NULL) return NULL; ini->fh = psp_fileOpen(name, O_RDONLY, PSP_FLAG_DENYNO | PSP_FLAG_STREAM); if (ini->fh == NULL) { psp_freeMemory(ini, 0); return NULL; } if ((ini->buf = psp_getMemory(INI_BUF_SIZE, 0)) == NULL) { psp_fileClose(ini->fh); psp_freeMemory(ini, 0); } return (PSP_INI) ini;}/* ========================================================================== Close an INI file*/void psp_iniClose( PSP_INI ini){ if (ini) { psp_fileClose(((INI_ENTRY *) ini)->fh); psp_freeMemory(((INI_ENTRY *) ini)->buf, 0); psp_freeMemory(ini, 0); }}#define INI_BUF_SIZE 1024static char *readINI( INI_ENTRY *ini, const char *section, const char *key){ size_t len; char *cp; char *np; psp_fileSeek(ini->fh, 0); len = strlen(section); while ((cp = psp_fileGets(ini->fh, ini->buf, INI_BUF_SIZE)) != NULL) { if (cp[0] == '[' && strnicmp(cp + 1, section, len) == 0 && cp[len + 1] == ']') break; } if (!cp) return NULL; /* Find the key */ len = strlen(key); while ((cp = psp_fileGets(ini->fh, ini->buf, INI_BUF_SIZE)) != NULL) { if (cp[0] == '[') return NULL; /* start of new section, key not found */ if (strnicmp(cp, key, len) == 0) { if ((cp = strchr(cp + len, '=')) == NULL) return NULL; /* No value specified */ while (isspace(*(++cp))) ; /* trim any trailing newlien */ if ((np = strchr(cp, '\n')) != NULL) *np = '\0'; return cp; } } return NULL;}/* ========================================================================== Read a short value from an INI file*/short psp_iniShort( PSP_INI ini, const DB_TCHAR *section, const DB_TCHAR *key, short def){ char *str; if (ini && (str = readINI((INI_ENTRY *) ini, section, key)) != NULL) return (short) strtoi(str); return def;}/* ========================================================================== Read a short value from an INI file*/size_t psp_iniString( PSP_INI ini, const DB_TCHAR *section, const DB_TCHAR *key, const DB_TCHAR *def, DB_TCHAR *buf, size_t len){ char *str; if (ini && (str = readINI((INI_ENTRY *) ini, section, key)) != NULL) strncpy(buf, str, len); else strncpy(buf, def, len); buf[len - 1] ='\0'; return strlen(buf);}/* ========================================================================== Suspend the current thread for the specified # of milliseconds*/void psp_sleep( unsigned long millisecs){ struct timespec tm1; struct timespec tm2; tm1.tv_sec = millisecs / 1000; tm1.tv_nsec = (millisecs % 1000) * 1000000; while (nanosleep(&tm1, &tm2) != 0) memcpy(&tm1, &tm2, sizeof(struct timespec));}/* =========================================================================== Return the process id*/unsigned int psp_get_pid( void){ return (unsigned int) getpid();}static int pfd[2];/* =========================================================================== Handle a SIGCHLD message*/void sigchld_handler( int sig){ int stat; wait(&stat);
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?