?? misc.c
字號:
/*** $Id: misc.c,v 1.32 2003/09/04 03:46:47 weiym Exp $**** misc.c: This file include some miscelleous functions. **** Copyright (C) 2003 Feynman Software.** Copyright (C) 1999 ~ 2002 Wei Yongming.**** Create date: 1998/12/31**** Current maintainer: Wei Yongming.*//*** This program is free software; you can redistribute it and/or modify** it under the terms of the GNU General Public License as published by** the Free Software Foundation; either version 2 of the License, or** (at your option) any later version.**** This program is distributed in the hope that it will be useful,** but WITHOUT ANY WARRANTY; without even the implied warranty of** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the** GNU General Public License for more details.**** You should have received a copy of the GNU General Public License** along with this program; if not, write to the Free Software** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA*//*** TODO:*/ #include <stdio.h>#include <stdlib.h>#include <string.h>#include <unistd.h>#include <errno.h>#include <limits.h>#include <pwd.h>#include <sys/ioctl.h>#include <sys/types.h>#include <linux/kd.h>#include <asm/param.h>#include "common.h"#include "minigui.h"#include "gdi.h"#include "window.h"#include "misc.h"#ifndef _INCORE_RESchar ETCFILEPATH [MAX_PATH + 1];#define ETCFILENAME "MiniGUI.cfg"static BOOL LookForEtcFile (void){ char etcfile [MAX_PATH + 1]; char buff [10]; struct passwd *pwd; if ((pwd = getpwuid (geteuid ())) != NULL) { strcpy (etcfile, pwd->pw_dir); if (etcfile [strlen (etcfile) - 1] != '/') strcat (etcfile, "/"); strcat (etcfile, "."); strcat (etcfile, ETCFILENAME); if (GetValueFromEtcFile (etcfile, "system", "gal_engine", buff, 8) == ETC_OK) { strcpy (ETCFILEPATH, etcfile); return TRUE; } } strcpy (etcfile, "/usr/local/etc/" ETCFILENAME); if (GetValueFromEtcFile (etcfile, "system", "gal_engine", buff, 8) == ETC_OK) { strcpy (ETCFILEPATH, etcfile); return TRUE; } strcpy (etcfile, "/etc/" ETCFILENAME); if (GetValueFromEtcFile (etcfile, "system", "gal_engine", buff, 8) == ETC_OK) { strcpy (ETCFILEPATH, etcfile); return TRUE; } return FALSE;}BOOL InitMisc (void){ if (!LookForEtcFile ()) { fprintf (stderr, "MISC: Can not locate your MiniGUI.cfg file or bad files!\n"); return FALSE; } return TRUE;}void TerminateMisc (void){}#endif /* _INCORE_RES *//****************************** ETC file support ******************************/// This function locate the specified section in the etc file.static int etc_LocateSection(FILE* fp, const char* pSection, FILE* bak_fp){ char szBuff[ETC_MAXLINE + 1]; char* current; char* tail; while (TRUE) { if (!fgets(szBuff, ETC_MAXLINE, fp)) { if (feof (fp)) return ETC_SECTIONNOTFOUND; else return ETC_FILEIOFAILED; } else if (bak_fp && fputs (szBuff, bak_fp) == EOF) return ETC_FILEIOFAILED; current = szBuff; while (*current == ' ' || *current == '\t') current++; if (*current == ';' || *current == '#') continue; if (*current++ == '[') while (*current == ' ' || *current == '\t') current ++; else continue; // Locate the tail. tail = current; while (*tail != ']' && *tail != '\n' && *tail != ';' && *tail != '#' && *tail != '\0') tail++; *tail = '\0'; while (*tail == ' ' || *tail == '\t') { *tail = '\0'; tail--; } if (strcmp (current, pSection) == 0) return ETC_OK; } return ETC_SECTIONNOTFOUND;}// This function locate the specified key in the etc file.static int etc_LocateKeyValue(FILE* fp, const char* pKey, BOOL bCurSection, char* pValue, int iLen, FILE* bak_fp, char* nextSection){ char szBuff[ETC_MAXLINE + 1 + 1]; char* current; char* tail; char* value; while (TRUE) { if (!fgets(szBuff, ETC_MAXLINE, fp)) return ETC_FILEIOFAILED; if (szBuff [strlen (szBuff) - 1] == '\n') szBuff [strlen (szBuff) - 1] = '\0'; current = szBuff; while (*current == ' ' || *current == '\t') current++; if (*current == ';' || *current == '#') continue; if (*current == '[') { if (bCurSection) { strcpy (nextSection, current); return ETC_KEYNOTFOUND; } else continue; } tail = current; while (*tail != '=' && *tail != '\n' && *tail != ';' && *tail != '#' && *tail != '\0') tail++; value = tail + 1; if (*tail != '=') *value = '\0'; *tail-- = '\0'; while (*tail == ' ' || *tail == '\t') { *tail = '\0'; tail--; } if (strcmp (current, pKey) == 0) { tail = value; while (*tail != '\n' && *tail != '\0') tail++; *tail = '\0'; if (pValue) strncpy (pValue, value, iLen); return ETC_OK; } else if (bak_fp && *current != '\0') fprintf (bak_fp, "%s=%s\n", current, value); } return ETC_KEYNOTFOUND;}/* 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)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -