⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 misc.c

📁 有了操作系统、TCP/IP协议栈、文件系统
💻 C
📖 第 1 页 / 共 2 页
字号:
    int max_sect_nr = 15;    ETC_S *petc;    if (!(fp = fopen(pEtcFile, "r")))         return ETC_FILENOTFOUND;    petc = (ETC_S*) malloc (sizeof(ETC_S));    petc->section_nr = 0;    /* we allocate 15 sections first */    petc->sections = (PETCSECTION) malloc (sizeof(ETCSECTION)*max_sect_nr);    while (etc_ReadSection (fp, petc->sections + petc->section_nr) == ETC_OK) {        petc->section_nr ++;        if (petc->section_nr == max_sect_nr) {            /* add 5 sections each time we realloc */            max_sect_nr += 5;            petc->sections = realloc (petc->sections, sizeof(ETCSECTION)*max_sect_nr);        }    }    fclose (fp);    return (GHANDLE)petc;}int GUIAPI UnloadEtcFile (GHANDLE hEtc){    int i;    ETC_S *petc = (ETC_S*) hEtc;    if (!petc)        return -1;    for (i=0; i<petc->section_nr; i++) {        PETCSECTION psect = petc->sections + i;        int j;        if (!psect)           continue;        for (j=0; j<psect->key_nr; j++) {            free (psect->keys[j]);            free (psect->values[j]);        }        free (psect->keys);        free (psect->values);        free (psect->name);    }    free (petc->sections);    free (petc);    return 0;}int GUIAPI GetValueFromEtc (GHANDLE hEtc, const char* pSection,                            const char* pKey, char* pValue, int iLen){    int i;    ETC_S *petc = (ETC_S*) hEtc;    PETCSECTION psect = NULL;    if (!petc || !pValue)        return -1;    for (i=0; i<petc->section_nr; i++) {        psect = petc->sections + i;        if (!psect)           continue;        if (strcmp (psect->name, pSection) == 0) {            break;        }    }    if (i >= petc->section_nr)        return ETC_SECTIONNOTFOUND;    for (i=0; i<psect->key_nr; i++) {        if (strcmp (psect->keys[i], pKey) == 0) {            break;        }    }    if (i >= psect->key_nr)        return ETC_KEYNOTFOUND;    if (iLen > 0) { /* get value */        strncpy (pValue, psect->values[i], iLen);    }    else { /* set value */        free (psect->values[i]);        psect->values[i] = strdup(pValue);    }    return ETC_OK;}int GUIAPI GetIntValueFromEtc (GHANDLE hEtc, const char* pSection,                               const char* pKey, int* value){    int ret;    char szBuff [51];    ret = GetValueFromEtc (hEtc, pSection, pKey, szBuff, 50);    if (ret < 0) {        return ret;    }    *value = strtol (szBuff, NULL, 0);    if ((*value == LONG_MIN || *value == LONG_MAX) && errno == ERANGE)        return ETC_INTCONV;    return ETC_OK;}/* Function: GetValueFromEtcFile(const char* pEtcFile, const char* pSection, *                               const char* pKey, char* pValue, int iLen); * Parameter: *     pEtcFile: etc file path name. *     pSection: Section name. *     pKey:     Key name. *     pValue:   The buffer will store the value of the key. *     iLen:     The max length of value string. * Return: *     int               meaning *     ETC_FILENOTFOUND           The etc file not found.  *     ETC_SECTIONNOTFOUND        The section is not found.  *     ETC_EKYNOTFOUND        The Key is not found. *     ETC_OK            OK. */int GUIAPI GetValueFromEtcFile(const char* pEtcFile, const char* pSection,                               const char* pKey, char* pValue, int iLen){    FILE* fp;    char tempSection [ETC_MAXLINE + 2];    if (!(fp = fopen(pEtcFile, "r")))         return ETC_FILENOTFOUND;    if (pSection)         if (etc_LocateSection (fp, pSection, NULL) != ETC_OK) {             fclose (fp);             return ETC_SECTIONNOTFOUND;         }    if (etc_LocateKeyValue (fp, pKey, pSection != NULL,                 pValue, iLen, NULL, tempSection) != ETC_OK) {         fclose (fp);         return ETC_KEYNOTFOUND;    }    fclose (fp);    return ETC_OK;}/* Function: GetIntValueFromEtcFile(const char* pEtcFile, const char* pSection, *                               const char* pKey); * Parameter: *     pEtcFile: etc file path name. *     pSection: Section name. *     pKey:     Key name. * Return: *     int                      meaning *     ETC_FILENOTFOUND             The etc file not found.  *     ETC_SECTIONNOTFOUND          The section is not found.  *     ETC_EKYNOTFOUND              The Key is not found. *     ETC_OK                       OK. */int GUIAPI GetIntValueFromEtcFile(const char* pEtcFile, const char* pSection,                               const char* pKey, int* value){    int ret;    char szBuff [51];    ret = GetValueFromEtcFile (pEtcFile, pSection, pKey, szBuff, 50);    if (ret < 0)        return ret;    *value = strtol (szBuff, NULL, 0);    if ((*value == LONG_MIN || *value == LONG_MAX) && errno == ERANGE)        return ETC_INTCONV;    return ETC_OK;}static int etc_CopyAndLocate (FILE* etc_fp, FILE* tmp_fp,                 const char* pSection, const char* pKey, char* tempSection){    if (pSection && etc_LocateSection (etc_fp, pSection, tmp_fp) != ETC_OK)        return ETC_SECTIONNOTFOUND;    if (etc_LocateKeyValue (etc_fp, pKey, pSection != NULL,                 NULL, 0, tmp_fp, tempSection) != ETC_OK)        return ETC_KEYNOTFOUND;    return ETC_OK;}static int etc_FileCopy (FILE* sf, FILE* df){    char line [ETC_MAXLINE + 1];        while (fgets (line, ETC_MAXLINE + 1, sf) != NULL)        if (fputs (line, df) == EOF) {            return ETC_FILEIOFAILED;        }    return ETC_OK;}/* Function: SetValueToEtcFile(const char* pEtcFile, const char* pSection, *                               const char* pKey, char* pValue); * Parameter: *     pEtcFile: etc file path name. *     pSection: Section name. *     pKey:     Key name. *     pValue:   Value. * Return: *     int                      meaning *     ETC_FILENOTFOUND         The etc file not found. *     ETC_TMPFILEFAILED        Create tmp file failure. *     ETC_OK                   OK. */int GUIAPI SetValueToEtcFile (const char* pEtcFile, const char* pSection,                               const char* pKey, char* pValue){    FILE* etc_fp;    FILE* tmp_fp;    int rc;    char tempSection [ETC_MAXLINE + 2];#ifndef HAVE_TMPFILE    char tmp_nam [256];    sprintf (tmp_nam, "/tmp/mg-etc-tmp-%lx", time(NULL));    if ((tmp_fp = fopen (tmp_nam, "w+")) == NULL)        return ETC_TMPFILEFAILED;#else    if ((tmp_fp = tmpfile ()) == NULL)        return ETC_TMPFILEFAILED;#endif    if (!(etc_fp = fopen (pEtcFile, "r+"))) {        fclose (tmp_fp);#ifndef HAVE_TMPFILE        unlink (tmp_nam);#endif        if (!(etc_fp = fopen (pEtcFile, "w"))) {            return ETC_FILEIOFAILED;        }        fprintf (etc_fp, "[%s]\n", pSection);        fprintf (etc_fp, "%s=%s\n", pKey, pValue);        fclose (etc_fp);        return ETC_OK;    }    switch (etc_CopyAndLocate (etc_fp, tmp_fp, pSection, pKey, tempSection)) {    case ETC_SECTIONNOTFOUND:        fprintf (tmp_fp, "\n[%s]\n", pSection);        fprintf (tmp_fp, "%s=%s\n", pKey, pValue);        break;        case ETC_KEYNOTFOUND:        fprintf (tmp_fp, "%s=%s\n\n", pKey, pValue);        fprintf (tmp_fp, "%s\n", tempSection);    break;    default:        fprintf (tmp_fp, "%s=%s\n", pKey, pValue);        break;    }    if ((rc = etc_FileCopy (etc_fp, tmp_fp)) != ETC_OK)        goto error;        // replace etc content with tmp file content    // truncate etc content first    fclose (etc_fp);    if (!(etc_fp = fopen (pEtcFile, "w"))) {        fclose (tmp_fp);#ifndef HAVE_TMPFILE        unlink (tmp_nam);#endif        return ETC_FILEIOFAILED;    }        rewind (tmp_fp);    rc = etc_FileCopy (tmp_fp, etc_fp);error:    fclose (etc_fp);    fclose (tmp_fp);#ifndef HAVE_TMPFILE    unlink (tmp_nam);#endif    return rc;}/****************************** Ping and Beep *********************************/void GUIAPI Ping(void){    putchar ('\a');    fflush (stdout);}#if !defined (__NOUNIX__) && !defined(__CYGWIN__) && defined(i386)#include <linux/kd.h>#include <asm/param.h>void GUIAPI Tone (int frequency_hz, int duration_ms){    /* FIXME: Tone will not work in X Window */    long argument = (1190000 / frequency_hz) | ((duration_ms / (1000/HZ)) << 16);    ioctl (0, KDMKTONE, (long) argument);}#endifchar* strnchr (const char* s, size_t n, int c){    size_t i;        for (i=0; i<n; i++) {        if ( *s == c)            return (char *)s;        s ++;    }    return NULL;}int substrlen (const char* text, int len, char delimiter, int* nr_delim){    char* substr;    *nr_delim = 0;    if ( (substr = strnchr (text, len, delimiter)) == NULL)        return len;    len = substr - text;    while (*substr == delimiter) {        (*nr_delim) ++;        substr ++;    }    return len;}char * strtrimall( char *src){    int  nIndex1;    int  nLen;    if (src == NULL)        return NULL;    if (src [0] == '\0')        return src;    nLen = strlen (src);    nIndex1 = 0;    while (isspace (src [nIndex1]))        nIndex1 ++;    if (nIndex1 == nLen) {        *src = '\0';        return src;    }    strcpy (src, src + nIndex1);    nLen = strlen (src);    nIndex1 = nLen - 1;    while (isspace (src [nIndex1]))        nIndex1 --;    src [nIndex1 + 1] = '\0';      return src;}

⌨️ 快捷键说明

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