ks_sys.c 檔案分析

來源:互聯網
上載者:User

        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);
}

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.