tables.c

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

C
708
字号
/*************************************************************************** *                                                                         * * db.*                                                                    * * open source database, lock manager console utility                      * *                                                                         * * Copyright (c) 2000 Centura Software Corporation. All rights reserved.   * *                                                                         * * 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.                                      *  *                                                                         * **************************************************************************/#include "db.star.h"#include "console.h"#define is_bit_set(m, b) (((DB_BYTE *) m)[b / BITS_PER_BYTE] & (1 << ((BITS_PER_BYTE - 1) - (b % BITS_PER_BYTE))) & 0xff)static int count_bits(DB_BYTE *, int);typedef struct USER_INFO_S{    DB_TCHAR       name[16];    unsigned short pending;    unsigned short timeout;    unsigned short timer;    unsigned short status;    int            recover;    DB_TCHAR       taffile[LOGFILELEN];    DB_TCHAR       logfile[LOGFILELEN];} USER_INFO;typedef struct FILE_INFO_S{    DB_TCHAR       name[FILENMLEN];    unsigned short lockstat;    unsigned short numlocks;    int            user_with_lock;    int            qentry;} FILE_INFO;typedef struct{    short q_locktype;    short q_user;    short q_next;} QUEUETAB;static LM_STATUS *ssp = NULL;static long msgsr = 0;static long msgss = 0;static long granted = 0;static long rejected = 0;static long timedout = 0;static long freed = 0;static TABSIZE cur_sizes;static TABSIZE max_sizes;static size_t  max_userbmbytes;static size_t  max_filebmbytes;static USER_INFO *users = NULL;static FILE_INFO *files = NULL;static DB_TCHAR filename[FILENMLEN];         /* for file detail */static QUEUETAB *queue = NULL;static DB_BYTE *user_o = NULL;static DB_BYTE *user_l = NULL;static DB_BYTE *user_r = NULL;static DB_BYTE *file_o = NULL;static DB_BYTE *file_l = NULL;static DB_TCHAR *status[] ={    DB_TEXT("Empty   "),    DB_TEXT("Live    "),    DB_TEXT("Dead    "),    DB_TEXT("RecOther"),    DB_TEXT("Recover "),    DB_TEXT("RecMe   "),    DB_TEXT("ExLock  ")};void free_table_memory(){    if (users)    {        psp_freeMemory(users, 0);        users = NULL;    }    if (files)    {        psp_freeMemory(files, 0);        files = NULL;    }    if (queue)    {        psp_freeMemory(queue, 0);        queue = NULL;    }    if (user_o)    {        psp_freeMemory(user_o, 0);        user_o = NULL;    }    if (user_l)    {        psp_freeMemory(user_l, 0);        user_l = NULL;    }    if (user_r)    {        psp_freeMemory(user_r, 0);        user_r = NULL;    }    if (file_o)    {        psp_freeMemory(file_o, 0);        file_o = NULL;    }    if (file_l)    {        psp_freeMemory(file_l, 0);        file_l = NULL;    }    if (ssp)    {        psp_lmcFree(ssp);        ssp = NULL;    }}static int get_general_status(){    int        stat;    size_t     userbmbytes = 0;    size_t     filebmbytes = 0;    LR_STATUS *rsp = NULL;    if (!ssp)    {        if ((ssp = (LM_STATUS *) psp_lmcAlloc(sizeof(LM_STATUS))) == NULL)        {            error_msg(DB_TEXT("Out of memory"));            return -1;        }    }    ssp->type_of_status = ST_GENSTAT;    stat = psp_lmcTrans(L_STATUS, ssp, sizeof(LM_STATUS), (void **) &rsp,            NULL, NULL, lmc);    if (stat != PSP_OKAY)    {        error_msg(DB_TEXT("Transmission error"));        if (rsp)            psp_lmcFree(rsp);        return -1;    }    memcpy(&cur_sizes, &rsp->s_tabsize, sizeof(TABSIZE));    if (rsp->s_tabsize.lm_maxusers > max_sizes.lm_maxusers)    {        max_sizes.lm_maxusers = 0;        if (users)            psp_freeMemory(users, 0);        if ((users = (USER_INFO *) psp_cGetMemory(                rsp->s_tabsize.lm_maxusers * sizeof(USER_INFO), 0)) == NULL)            goto nomem;                max_sizes.lm_maxusers = rsp->s_tabsize.lm_maxusers;        userbmbytes = (max_sizes.lm_maxusers - 1) / BITS_PER_BYTE + 1;    }    if (rsp->s_tabsize.lm_maxfiles > max_sizes.lm_maxfiles)    {        max_sizes.lm_maxfiles = 0;        if (files)            psp_freeMemory(files, 0);        if ((files = (FILE_INFO *) psp_cGetMemory(                rsp->s_tabsize.lm_maxfiles * sizeof(FILE_INFO), 0)) == NULL)            goto nomem;                max_sizes.lm_maxfiles = rsp->s_tabsize.lm_maxfiles;        filebmbytes = (max_sizes.lm_maxfiles - 1) / BITS_PER_BYTE + 1;    }    if (userbmbytes > max_userbmbytes)    {        max_userbmbytes = 0;        if (file_o)            psp_freeMemory(file_o, 0);        if ((file_o = (DB_BYTE *) psp_cGetMemory(userbmbytes, 0)) == NULL)            goto nomem;        if (file_l)            psp_freeMemory(file_l, 0);        if ((file_l = (DB_BYTE *) psp_cGetMemory(userbmbytes, 0)) == NULL)            goto nomem;        max_userbmbytes = userbmbytes;    }    if (filebmbytes > max_filebmbytes)    {        max_filebmbytes = 0;        if (user_o)            psp_freeMemory(user_o, 0);        if ((user_o = (DB_BYTE *) psp_cGetMemory(filebmbytes, 0)) == NULL)            goto nomem;        if (user_l)            psp_freeMemory(user_l, 0);        if ((user_l = (DB_BYTE *) psp_cGetMemory(filebmbytes, 0)) == NULL)            goto nomem;        if (user_r)            psp_freeMemory(user_r, 0);        if ((user_r = (DB_BYTE *) psp_cGetMemory(filebmbytes, 0)) == NULL)            goto nomem;        max_filebmbytes = filebmbytes;    }    if (rsp->s_tabsize.lm_maxqueue > max_sizes.lm_maxqueue)    {        max_sizes.lm_maxqueue = 0;        if (queue)            psp_freeMemory(queue, 0);        if ((queue = (QUEUETAB *) psp_cGetMemory(                rsp->s_tabsize.lm_maxqueue * sizeof(QUEUETAB), 0)) == NULL)            goto nomem;        max_sizes.lm_maxqueue = rsp->s_tabsize.lm_maxqueue;    }    psp_lmcFree(rsp);    return 0;nomem:    error_msg(DB_TEXT("Out of memory"));    psp_lmcFree(rsp);    return -1;}void get_tables(){    int i, stat;    LR_TABLES *rtp;    LR_USERTAB *utp;    LR_FILETAB *ftp;    if (get_general_status())        return;    ssp->type_of_status = ST_TABLES;    memcpy(&ssp->tabsize, &cur_sizes, sizeof(TABSIZE));        stat = psp_lmcTrans(L_STATUS, ssp, sizeof(LM_STATUS), (void **) &rtp,            NULL, NULL, lmc);    if (stat != PSP_OKAY)        return;    msgsr    = rtp->t_msgs_received;    msgss    = rtp->t_msgs_sent;    granted  = rtp->t_locks_granted;    rejected = rtp->t_locks_rejected;    timedout = rtp->t_locks_timedout;    freed    = rtp->t_locks_freed;    utp = (LR_USERTAB *) (rtp + 1);    for (i = 0; i < cur_sizes.lm_numusers; i++)    {        users[i].timer   = utp->ut_timer;        users[i].pending = utp->ut_pending;        users[i].status  = utp->ut_status;        users[i].recover = utp->ut_recover;        vtstrcpy(users[i].name, utp->ut_name);        vtstrcpy(users[i].logfile, utp->ut_logfile);        utp++;    }    ftp = (LR_FILETAB *) utp;    for (i = 0; i < cur_sizes.lm_numfiles; i++)    {        vtstrcpy(files[i].name, ftp->ft_name);        files[i].lockstat       = ftp->ft_lockstat;        files[i].numlocks       = ftp->ft_numlocks;        files[i].user_with_lock = ftp->ft_user_with_lock;        files[i].qentry         = ftp->ft_queue_entry;        ftp++;    }    if (cur_sizes.lm_maxqueue > 0)        memcpy(queue, ftp, sizeof(QUEUETAB) * cur_sizes.lm_maxqueue);    psp_lmcFree(rtp);}void get_user_info(int user){    int          i, len, stat;    LR_USERINFO *rup;    DB_BYTE     *bm;    DB_TCHAR    *pname;    LR_FILEINFO *ftp;    if (get_general_status())        return;    ssp->type_of_status = ST_USERINFO;    ssp->number = user;    memcpy(&ssp->tabsize, &cur_sizes, sizeof(TABSIZE));    stat = psp_lmcTrans(L_STATUS, ssp, sizeof(LM_STATUS), (void **) &rup,            NULL, NULL, lmc);    if (stat != PSP_OKAY)        return;    vtstrcpy(users[user].name, rup->ui_name);    users[user].pending = rup->ui_pending;    users[user].timer   = rup->ui_timer;    users[user].timeout = rup->ui_timeout;    users[user].status  = rup->ui_status;    users[user].recover = rup->ui_recovering;        /* If TAF and LOG file names are too long, copy without path */    pname = rup->ui_taffile;    len = vtstrlen(pname);    if (len > LOGFILELEN - 1)    {        for (pname += len; *pname != DIRCHAR && len; len--)            pname--;    }    vtstrncpy(users[user].taffile, pname, LOGFILELEN - 1);    users[user].taffile[LOGFILELEN-1] = 0;        pname = rup->ui_logfile;    len = vtstrlen(pname);    if (len > LOGFILELEN - 1)    {        for (pname += len; *pname != DIRCHAR && len; len--)            pname--;    }    vtstrncpy(users[user].logfile, pname, LOGFILELEN - 1);    users[user].logfile[LOGFILELEN-1] = 0;    bm = (DB_BYTE *) (rup + 1);

⌨️ 快捷键说明

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