pspecos.c

来自「db.* (pronounced dee-be star) is an adva」· C语言 代码 · 共 903 行 · 第 1/2 页

C
903
字号
/*************************************************************************** *                                                                         * * 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.                                      * *                                                                         * **************************************************************************//* pspecos.c - Contains ECOS specific functions */// signal handling turned off//#include <signal.h>#include <dirent.h>#include <unistd.h>#include <sys/stat.h>#include <time.h>#include "psp.h"#include "pspint.h"/* ==========================================================================    OS Specific initialization*/int psp_osInit(    void){//    signal(SIGPIPE, SIG_IGN);    return PSP_OKAY;}/* ==========================================================================   OS Specific termination*/void psp_osTerm(    void){}/* ==========================================================================    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 && errno == EINVAL) {	    // nither ramfs nor jffs2 don't provide file extending	    // using seek, so we need to write zeros	    	    off_t size = lseek(desc, 0, SEEK_END);	    if (addr <= size ||		psp_flSetSize(desc, addr) < 0)		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){    // Original Linux implementation seems to append size bytes rather than    // to set file size        size_t ii;    char   buf[10240];    size -= 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 unlink(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 */

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?