ks_sys.c 用來建立設定檔,主要配置:[HOST] [ IP] [ DHCP] [HCLONE] [PPPOE] [SWITCH]......
把這個檔案反覆看了幾遍,因為要用到裡面的幾個檔案操作函數,試著分析了一下,能力有限,錯誤難免.
static char *fconfig = "/tmp/sysconfig"; /* file name */
char glvalue[256]; /* globe var */
/*
* 計算行的字元數
*/
static int fgetline(FILE * fwd, char * linebuf)
{
int data;
int i;
i = 0;
while (((data = fgetc(fwd)) != EOF) && (data != '/n')) { // 讀取檔案資料判斷有效並不是斷行符號
*(linebuf+i) = data; /* 給指標賦值.並指向下一個地址. */
i++;
}
*(linebuf+i) = '/0'; /* 最後補上/0,字串. */
if (data == EOF) /* 無值. */
return EOF;
return i; /* 得到的行字元數返回. */
}
/* this is the entry point for shell program sysconfig */
int sysconfig(int argc, char argv[3][200], char *supBuf)
/* index the number of item to be skipped, starting from 1 */
static int print_config(FILE *handle, char *category, char* item, int index )
{
int i, result;
if ( 0 == (result= find_category(handle, category))) // 找尋目錄[ ]
{
/*skip first index items*/
for ( i=1; i<index; i++)
{
result = find_item(handle, item); // 找尋[ ] 中的一行值.
if ( 0 != result )
return (result);
}
/*get to the target item */
if ( 0 == (result = find_item(handle, item)))
print_item_value(handle); // 輸出到設定檔
}
return (result);
}
/* 檔案位置 */
static long find_postion(FILE *handle, char *item, int flag)
{
int index;
long position;
char tmp[3], temp[128], fg;
if ( flag == 0 )
fg = '/n'; /*by whole item string match*/
else
fg = '='; /*by item number in the list first one is 1 */
do
{
index = 0;
position = ftell(handle); // 得到檔案位置值.
do
{
if ( 0 != fread(tmp, sizeof(char), 1, handle) ) // 讀檔案最開始的一個值.
{
temp[index++] = tmp[0]; // 讀到的第一個值存入temp[]數組中.
if ( tmp[0] == '[') // 最開始值為'['
return -1L;
}
else
return (-1L);
}while ( tmp[0] != fg ); // fg 為上面得到的值,由flag參數習決定.
if ( fg == '=')
temp[index] = '/0';
else
temp[index-1] = '/0';
#ifndef WINDOWS
if (!strcasecmp (item, temp))
#else
if (!stricmp(item, temp))
#endif
{
if ( 0 == fseek(handle, position, SEEK_SET))
return (position);
else
return (-1L);
}
else
{
if ( fg == '=')
{
do
{
if ( 0 == fread(tmp, sizeof(char), 1, handle))
return (-1L);
}while (tmp[0] != '/n');
}
position = ftell(handle);
}
} while (tmp[0] != '[');
return (-1L);
}