ks_sys.c 文件分析

来源:互联网 发布:mysql not like 编辑:程序博客网 时间:2024/06/10 00:33

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

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 


 

原创粉丝点击