Uboot 中*getenv 、getenv_r 和env_get_char函数

      Uboot 中有getenv 、getenv_r 和env_get_char函数,有时候区分不开,容易搞混。现在把它们分别列出来加以区别,方便以后查看。

1 首先看env_get_char(int)

static uchar env_get_char_init (int index);
uchar (*env_get_char)(int) = env_get_char_init;

env_get_char(int)是个普通的函数,参数是int,返回值是字符,没有指针,比较简单。

env_get_char(int)等同于env_get_char_init(int)

static uchar env_get_char_init (int index)
{
	uchar c;

	/* if crc was bad, use the default environment */
	if (gd->env_valid)
	{
		c = env_get_char_spec(index);
	} else {
		c = default_environment[index];   
	}

	return (c);
}
env_get_char_init(int)实际上就是按指定位置取字符。

环境变量表中都是字符串组成的,这个函数是其他函数的基础,用来查找环境变量表。


2 其次看*env_get_char(int)

uchar *env_get_addr (int index)
{
	if (gd->env_valid) {
		return ( ((uchar *)(gd->env_addr + index)) );
	} else {
		return (&default_environment[index]);
	}
}

与前一个不同的是这个函数是指针函数,返回值不是字符,而是所找到的字符的位置,即指针。


3 再看 *getenv (char *name)

char *getenv (char *name)
{
	int i, nxt;

	WATCHDOG_RESET();

	for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
		int val;

		for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) {
			if (nxt >= CFG_ENV_SIZE) {
				return (NULL);
			}
		}
		if ((val=envmatch((uchar *)name, i)) < 0)
			continue;
		return ((char *)env_get_addr(val));
	}

	return (NULL);
}

一旦查找到匹配的字符串*name,则返回 *name的地址。


4  最后看 getenv (char *name,char *buf, unsigned len)

int getenv_r (char *name, char *buf, unsigned len)
{
	int i, nxt;

	for (i=0; env_get_char(i) != '\0'; i=nxt+1) {
		int val, n;

		for (nxt=i; env_get_char(nxt) != '\0'; ++nxt) {
			if (nxt >= CFG_ENV_SIZE) {
				return (-1);
			}
		}
		if ((val=envmatch((uchar *)name, i)) < 0)
			continue;
		/* found; copy out */
		n = 0;
		while ((len > n++) && (*buf++ = env_get_char(val++)) != '\0')
			;
		if (len == n)
			*buf = '\0';
		return (n);
	}
	return (-1);
}

可以看到这个函数与上一个很类似,区别是找到匹配的字符串后即copy到buf中,长度不大于len,最后在buf末尾加上.

 

                
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值