通过sysfs_streq 忽略回车符对字符串匹配的影响

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/tiantao2012/article/details/90110438
平时在通过echo向sysfs的接口写字符串时,在kernel中可以通过sysfs_streq 来判断字符串是否等于固定的值
例如下面的例子.
static ssize_t
store_smt_control(struct device *dev, struct device_attribute *attr,
		  const char *buf, size_t count)
{
	int ctrlval, ret;
#判断buf中的字符串是否等于on
	if (sysfs_streq(buf, "on"))
		ctrlval = CPU_SMT_ENABLED;
	else if (sysfs_streq(buf, "off"))
		ctrlval = CPU_SMT_DISABLED;
	else if (sysfs_streq(buf, "forceoff"))
		ctrlval = CPU_SMT_FORCE_DISABLED;
	else
		return -EINVAL;
}
static DEVICE_ATTR(control, 0644, show_smt_control, store_smt_control);
其实现如下:
bool sysfs_streq(const char *s1, const char *s2)
{
	while (*s1 && *s1 == *s2) {
		s1++;
		s2++;
	}

	if (*s1 == *s2)
		return true;
	if (!*s1 && *s2 == '\n' && !s2[1])
		return true;
	if (*s1 == '\n' && !s1[1] && !*s2)
		return true;
	return false;
}
可以到和平常的字符串相比,这里对s1 也就是通过echo写入到sysfs的字符串多了对或者的判断,
即实际s1比s2 多了一个回车,也认为s1和s2相等.

猜你喜欢

转载自blog.csdn.net/tiantao2012/article/details/90110438
今日推荐