免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
楼主: duanjigang
打印 上一主题 下一主题

源码阅读第一期:axel和wget [复制链接]

论坛徽章:
0
61 [报告]
发表于 2011-09-29 17:24 |只看该作者
楼上分析的不错,看完axel回头仔细拜读下,呵呵。:wink:

论坛徽章:
0
62 [报告]
发表于 2011-09-29 21:01 |只看该作者
_______________------------->
subdir_p()
{
......
  if(!op.ignore_case)
   for(; *d1 && *d2 && (!d1 == *d2); ++d1, d2) /*如果不忽略对目录和文件名的检查,那么就检查*/
            ;
   else(; *d1 && *d2 &&(c_tolower( !d1) == c_tolower(*d2)); ++d1; ++d2)
     ;
   return *d1 == '\0' &&( d2  ==  '\0' || *d2 == '/'); /*在此的意思却自己设置了几个文件名,然后用来检查,只有最后那一个不符合返回值为 false*/

}
<----------------____________________
test_subdir_p()
{
....
mu_assert();
return NULL;
}

/*这个到此也分析殆尽*/

论坛徽章:
0
63 [报告]
发表于 2011-09-29 21:59 |只看该作者
本帖最后由 wangzhen11aaa 于 2011-09-29 22:50 编辑

__________________------------------------->
第三个测试,跟过关一样:),不过离主体还有段不小距离。至少得检查完吧。
mu_run_test(test_dir_matches_p);_________------------>
/*src/utils.c*/
test_dir_matches_p()
{
  ......
/*这个和上面一样,都是定义一些名字然后让你去测试,这次好像是从一些目录名称中找到对应的*/
例如  struct {
            char *dirlist[3];
            char *dir;
            bool result;
}test_array[] = {
{ { "/somedir", "someotherdir",NULL}, "somedir", ture}, /*这个显然是有的,所以返回必须是ture,才是正确的,*/
{ { */*dl", "someotherdir", NULL}, "somedir/d1", ture}, /*我认为这里面的*是通配符*/
.... ... ...
};
  . . .
for ( i = 0; i < count(test_array); i++)
{
   bool res = dir_matches_p(test_array.dirlist, test_array.dir);/*_______-------->/src/utils.c*/
   mu_assert ("test ..... , res == test_array.result);
}
return NULL;
}
__________------------>
dir_matches_p()
{
char  **x;
int (*mather) (const char *, const char *, int) = opt.ignore_case? fnmatch_nocase: fnmatch;
   for(x = dirlist; *x; x++)
{
     . . .

. . .  

————————————————————————————----
fnmatch_nocase( )/*___________---------->utils.c*/
#ifdef FNM_CASEDFOLD 进行匹配时,不区分大小写字母
return fnmatch( pattern, string, flags | FNM_CASEFOLD);
#else  /*就是区分大小写先看不区分大小写的匹配*/
char *patcopy = (char *)allca(strlen (pattern) + 1); /*________--------->/lib/alloca.c*/
char *strcopy = (char *)alloca(strlen (string) + 1);
. . .
__________-------------->
/*lib/alloca.c*/
/* If your stack is a linked list of frames, you have to
   provide an "address metric" ADDRESS_FUNCTION macro.  如果栈是页表的链表,那么就提供ADDRESS_FUNCTION 的宏*/

alloca()
{
  ADDRESS_FUNCTION(); /*检查栈__________---------------->同一个文件*/
___________---------->
#if defined (CRAY) && defined(CRAY_STACKES_END)
#define ADDRESS_FUNCTION(arg) (char *) i00afunc(&(arg)) /*_____--------->alloca.c*/
#else
#define ADDRESS_FUNCTION(arg) &(arg)
#endif
___________---------->
static long i100afunc(long *address)
{
     struct stk_stat status; __________-------------->/*lib/alloca.c*/
     struct sk_trailer *trailer; ___________---------->/*lib/alloca.c*/
. . .
}
_____-------------->
struct skt_stat
{
   275   {
276     long now;                   /* Current total stack size. 当前堆栈总大小 */
277     long maxc;                  /* Amount of contiguous space which would
278                                    be required to satisfy the maximum
279                                    stack demand to date.  */
280     long high_water;            /* Stack high-water mark. 栈高水位标记 */
281     long overflows;             /* Number of stack overflow ($STKOFEN) calls    .  */
282     long hits;                  /* Number of internal buffer hits.  内部命中的次数*/
283     long extends;               /* Number of block extensions.  块的扩展数目*/
284     long stko_mallocs;          /* Block allocations by $STKOFEN.  申请块*/
285     long underflows;            /* Number of stack underflow calls ($STKRETN    ).  */
286     long stko_free;             /* Number of deallocations by $STKRETN.  */
287     long stkm_free;             /* Number of deallocations by $STKMRET.  */
288     long segments;              /* Current number of stack segments.  当前栈的段数目*/
289     long maxs;                  /* Maximum number of stack segments so far.  目前为止最大的段数目 */
290     long pad_size;              /* Stack pad size.  栈块大小*/
291     long current_address;       /* Current stack segment address.当前栈段地址  */
292     long current_size;          /* Current stack segment size.  This
293                                    number is actually corrupted by STKSTAT to
294                                    include the fifteen word trailer area.  *    /
295     long initial_address;       /* Address of initial segment.  */
296     long initial_size;          /* Size of initial segment.  */
297   };
_______---------------->
303 struct stk_trailer
304   {
305     long this_address;          /* Address of this block.  */
306     long this_size;             /* Size of this block (does not include
307                                    this trailer).  */
308     long unknown2;
309     long unknown3;
310     long link;                  /* Address of trailer block of previous
311                                    segment.  */
312     long unknown5;
313     long unknown6;
314     long unknown7;
315     long unknown8;
316     long unknown9;
317     long unknown10;
318     long unknown11;
319     long unknown12;
320     long unknown13;
321     long unknown14;
322   };
<-----------------------_______________
返回到
i00afunc()
{
..
trailer  = (struct stk_trailer *) (status.current_address + status.current_size -15) /*见上访的current_size.栈上有15个字被STKSTAT贪污,此处地址就拿回来*/
if ( trailer  == 0)
  abort();
while( trailer ! = 0)
{
  block = (long* ) trailer->this_address;
  size = trailer->this_size;
  if (block == 0 | |size == 0)
   abort();
  trailer = (struct sk_trailer *) trailer->link; /*上一个此结构的地址,早已经有联系*/
  if (( block <= address) &&(address < (block + size))) /*发现这个地址正好在此块中*/
    break;
}
/*此处,栈上本来在初始化时就有这些变量,现在只是取出来使用*/
   result = address - block;

   if (trailer == 0)
     {
      return result;
     }

   do
     {
      if (trailer->this_size <= 0)
        abort ();
      result += trailer->this_size;
       trailer = (struct stk_trailer *) trailer->link;
     }
   while (trailer != 0);
  return (result); /*返回总的可以分配的大小。往前分配*/
}

论坛徽章:
0
64 [报告]
发表于 2011-09-30 09:26 |只看该作者
回复 61# duanjigang
呵呵,您真谦虚。
这个论坛是刚刚开始的吧。要不怎么这么少帖子?

论坛徽章:
0
65 [报告]
发表于 2011-09-30 09:50 |只看该作者
回复  duanjigang
呵呵,您真谦虚。
这个论坛是刚刚开始的吧。要不怎么这么少帖子?
wangzhen11aaa 发表于 2011-09-30 09:26



    嗯,是的,刚开始不到一年吧,呵呵,一期建设!

论坛徽章:
49
15-16赛季CBA联赛之福建
日期:2016-06-22 16:22:002015年亚洲杯之中国
日期:2015-01-23 16:25:12丑牛
日期:2015-01-20 09:39:23未羊
日期:2015-01-14 23:55:57巳蛇
日期:2015-01-06 18:21:36双鱼座
日期:2015-01-02 22:04:33午马
日期:2014-11-25 09:58:35辰龙
日期:2014-11-18 10:40:07寅虎
日期:2014-11-13 22:47:15申猴
日期:2014-10-22 15:29:50摩羯座
日期:2014-08-27 10:49:43辰龙
日期:2014-08-21 10:47:58
66 [报告]
发表于 2011-09-30 09:56 |只看该作者
回复  duanjigang
呵呵,您真谦虚。
这个论坛是刚刚开始的吧。要不怎么这么少帖子?
wangzhen11aaa 发表于 2011-09-30 09:26



    论坛今年已经是第十个年头了,但是架构设计版块是第一个年头,如何建设好,还要靠版主和各位活跃的用户啊,呵呵

论坛徽章:
3
CU大牛徽章
日期:2013-05-20 10:43:41CU大牛徽章
日期:2013-05-20 10:44:06CU大牛徽章
日期:2013-05-20 10:44:16
67 [报告]
发表于 2011-09-30 10:02 |只看该作者
刚刚稍微看了一下axel的配置文件解析。
推荐一篇好文:

PS:楼上的弟兄们也不要光灌水啊。
c04n05 发表于 2011-09-26 17:46



    我帮你转过去了
conf.h
  1. typedef struct
  2. {//关于配置信息的struct
  3.         char default_filename[MAX_STRING];
  4.         char http_proxy[MAX_STRING];
  5.         char no_proxy[MAX_STRING];
  6.         int strip_cgi_parameters;
  7.         int save_state_interval;
  8.         int connection_timeout;
  9.         int reconnect_delay;
  10.         int num_connections;
  11.         int buffer_size;
  12.         int max_speed;
  13.         int verbose;
  14.         int alternate_output;

  15.         if_t *interfaces;

  16.         int search_timeout;
  17.         int search_threads;
  18.         int search_amount;
  19.         int search_top;

  20.         int add_header_count;
  21.         char add_header[MAX_ADD_HEADERS][MAX_STRING];

  22.         char user_agent[MAX_STRING];
  23. } conf_t;

  24. int conf_loadfile( conf_t *conf, char *file );
  25. int conf_init( conf_t *conf );
复制代码
conf.c
  1. #include "axel.h"

  2. //两个比较key和name然后设置相应key的值的宏
  3. /* Some nifty macro's..                                                        */
  4. #define get_config_string( name )                                \
  5.         if( strcmp( key, #name ) == 0 )                                \
  6.         {                                                        \
  7.                 st = 1;                                                \
  8.                 strcpy( conf->name, value );                        \
  9.         }
  10. #define get_config_number( name )                                \
  11.         if( strcmp( key, #name ) == 0 )                                \
  12.         {                                                        \
  13.                 st = 1;                                                \
  14.                 sscanf( value, "%i", &conf->name );                \
  15.         }

  16. int parse_interfaces( conf_t *conf, char *s );

  17. int conf_loadfile( conf_t *conf, char *file )
  18. {
  19.         int i, line = 0;
  20.         FILE *fp;
  21.         char s[MAX_STRING], key[MAX_STRING], value[MAX_STRING];

  22.         fp = fopen( file, "r" );
  23.         if( fp == NULL )
  24.                 return( 1 );                        /* Not a real failure        */

  25.         while( !feof( fp ) )
  26.         {
  27.                 int st;

  28.                 line ++;

  29.                 *s = 0;
  30.                 //读入100个字符直到遇见换行或者#号
  31.                 fscanf( fp, "%100[^\n#]s", s );
  32.                 //丢弃剩下的字符直到遇见换行
  33.                 fscanf( fp, "%*[^\n]s" );
  34.                 fgetc( fp );                        /* Skip newline                */
  35.                 //没有等号, 重试
  36.                 if( strchr( s, '=' ) == NULL )
  37.                         continue;                /* Probably empty?        */
  38.                 sscanf( s, "%[^= \t]s", key );
  39.                 for( i = 0; s[i]; i ++ )
  40.                         if( s[i] == '=' )
  41.                         {
  42.                                 for( i ++; isspace( (int) s[i] ) && s[i]; i ++ );
  43.                                 break;
  44.                         }
  45.                 strcpy( value, &s[i] );
  46.                 //从value的末尾往前, 丢掉空白字符
  47.                 for( i = strlen( value ) - 1; isspace( (int) value[i] ); i -- )
  48.                         value[i] = 0;

  49.                 st = 0;

  50.                 /* Long live macros!!                                        */
  51.                 get_config_string( default_filename );
  52.                 get_config_string( http_proxy );
  53.                 get_config_string( no_proxy );
  54.                 get_config_number( strip_cgi_parameters );
  55.                 get_config_number( save_state_interval );
  56.                 get_config_number( connection_timeout );
  57.                 get_config_number( reconnect_delay );
  58.                 get_config_number( num_connections );
  59.                 get_config_number( buffer_size );
  60.                 get_config_number( max_speed );
  61.                 get_config_number( verbose );
  62.                 get_config_number( alternate_output );

  63.                 get_config_number( search_timeout );
  64.                 get_config_number( search_threads );
  65.                 get_config_number( search_amount );
  66.                 get_config_number( search_top );

  67.                 /* Option defunct but shouldn't be an error                */
  68.                 if( strcmp( key, "speed_type" ) == 0 )
  69.                         st = 1;

  70.                 if( strcmp( key, "interfaces" ) == 0 )
  71.                         st = parse_interfaces( conf, value );

  72.                 if( !st )
  73.                 {
  74.                         fprintf( stderr, _("Error in %s line %i.\n"), file, line );
  75.                         return( 0 );
  76.                 }
  77.                 get_config_number( add_header_count );
  78.                 for(i=0;i<conf->add_header_count;i++)
  79.                         get_config_string( add_header[i] );
  80.                 get_config_string( user_agent );
  81.         }

  82.         fclose( fp );
  83.         return( 1 );
  84. }

  85. int conf_init( conf_t *conf )
  86. {//给conf_t变量开辟空间, 初始化一些默认值
  87.         char s[MAX_STRING], *s2;
  88.         int i;

  89.         /* Set defaults                                                        */
  90.         memset( conf, 0, sizeof( conf_t ) );
  91.         strcpy( conf->default_filename, "default" );
  92.         *conf->http_proxy                = 0;
  93.         *conf->no_proxy                        = 0;
  94.         conf->strip_cgi_parameters        = 1;
  95.         conf->save_state_interval        = 10;
  96.         conf->connection_timeout        = 45;
  97.         conf->reconnect_delay                = 20;
  98.         conf->num_connections                = 4;
  99.         conf->buffer_size                = 5120;
  100.         conf->max_speed                        = 0;
  101.         conf->verbose                        = 1;
  102.         conf->alternate_output                = 0;

  103.         conf->search_timeout                = 10;
  104.         conf->search_threads                = 3;
  105.         conf->search_amount                = 15;
  106.         conf->search_top                = 3;
  107.         conf->add_header_count                = 0;
  108.         strncpy( conf->user_agent, DEFAULT_USER_AGENT, MAX_STRING );

  109.         conf->interfaces = malloc( sizeof( if_t ) );
  110.         memset( conf->interfaces, 0, sizeof( if_t ) );
  111.         conf->interfaces->next = conf->interfaces;

  112.         if( ( s2 = getenv( "http_proxy" ) ) != NULL )
  113.                 strncpy( conf->http_proxy, s2, MAX_STRING );
  114.         else if( ( s2 = getenv( "HTTP_PROXY" ) ) != NULL )
  115.                 strncpy( conf->http_proxy, s2, MAX_STRING );

  116.         if( !conf_loadfile( conf, ETCDIR "/axelrc" ) )
  117.                 return( 0 );

  118.         if( ( s2 = getenv( "HOME" ) ) != NULL )
  119.         {
  120.                 sprintf( s, "%s/%s", s2, ".axelrc" );
  121.                 if( !conf_loadfile( conf, s ) )
  122.                         return( 0 );
  123.         }

  124.         /* Convert no_proxy to a 0-separated-and-00-terminated list..        */
  125.         for( i = 0; conf->no_proxy[i]; i ++ )
  126.                 if( conf->no_proxy[i] == ',' )
  127.                         conf->no_proxy[i] = 0;
  128.         conf->no_proxy[i+1] = 0;

  129.         return( 1 );
  130. }

  131. int parse_interfaces( conf_t *conf, char *s )
  132. {
  133.         char *s2;
  134.         if_t *iface;

  135.         //typedef struct
  136.         //{
  137.         //    void *next;
  138.         //    char text[MAX_STRING];
  139.         //} message_t;
  140.         //
  141.         //typedef message_t url_t;
  142.         //typedef message_t if_t;

  143.         //清空conf->interfaces指向的链表
  144.         iface = conf->interfaces->next;
  145.         while( iface != conf->interfaces )
  146.         {
  147.                 if_t *i;

  148.                 i = iface->next;
  149.                 free( iface );
  150.                 iface = i;
  151.         }
  152.         free( conf->interfaces );

  153.         if( !*s )
  154.         {
  155.                 conf->interfaces = malloc( sizeof( if_t ) );
  156.                 memset( conf->interfaces, 0, sizeof( if_t ) );
  157.                 conf->interfaces->next = conf->interfaces;
  158.                 return( 1 );
  159.         }

  160.         s[strlen(s)+1] = 0;
  161.         conf->interfaces = iface = malloc( sizeof( if_t ) );
  162.         while( 1 )
  163.         {
  164.                 while( ( *s == ' ' || *s == '\t' ) && *s ) s ++; //跳过开始的空格和制表符
  165.                 for( s2 = s; *s2 != ' ' && *s2 != '\t' && *s2; s2 ++ );
  166.                 *s2 = 0;//s到s2为一个iterface
  167.                 if( *s < '0' || *s > '9' )
  168.                         get_if_ip( s, iface->text );//ip
  169.                 else
  170.                         strcpy( iface->text, s );//字符串如eth0
  171.                 s = s2 + 1;
  172.                 if( *s )//s是用00终止的
  173.                 {
  174.                         iface->next = malloc( sizeof( if_t ) );
  175.                         iface = iface->next;
  176.                 }
  177.                 else
  178.                 {
  179.                         iface->next = conf->interfaces;
  180.                         break;
  181.                 }
  182.         }

  183.         return( 1 );
  184. }
复制代码

论坛徽章:
2
巳蛇
日期:2014-10-26 22:38:12天蝎座
日期:2016-01-08 09:25:17
68 [报告]
发表于 2011-09-30 12:24 |只看该作者
这个不错,支持。

论坛徽章:
3
金牛座
日期:2014-06-14 22:04:062015年辞旧岁徽章
日期:2015-03-03 16:54:152015年迎新春徽章
日期:2015-03-04 09:49:45
69 [报告]
发表于 2011-09-30 12:44 |只看该作者
抓紧合贴,汇总一下!

论坛徽章:
0
70 [报告]
发表于 2011-09-30 16:59 |只看该作者
还不知道axel
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP