lib/vsprintf.c
1. unsigned long long simple_strtoull(const char *cp, char **endp, unsigned int base)
2. unsigned long simple_strtoul(const char *cp, char **endp, unsigned int base)
3. long simple_strtol(const char *cp, char **endp, unsigned int base)
4. long long simple_strtoll(const char *cp, char **endp, unsigned int base)
5. int strict_strtoul(const char *cp, unsigned int base, unsigned long *res)
6. int strict_strtol(const char *cp, unsigned int base, long *res)
7. int strict_strtoull(const char *cp, unsigned int base, unsigned long long *res)
8. int strict_strtoll(const char *cp, unsigned int base, long long *res)
9. int sprintf(char *buf, const char *fmt, ...)
10. int snprintf(char *buf, size_t size, const char *fmt, ...)
11. int sscanf(const char *buf, const char *fmt, ...)
参数:cp指向字符串的开始,endp指向分析的字符串末尾的位置,base为要用的基数(进制数),base为0表示通过cp来自动判断基数,函数自动可识别的基数:‘0x’表示16进制,‘0’表示8进制,其它都认定为10进制。函数可转换成数字的有效字符为:[0,f]。举例:cp = “0x12str”,base = 0,则返回unsigned long long为18,*endp = “str”。 参数下同。
static ssize_t led_brightness_store(struct device *dev,
struct device_attribute *attr, const char *buf, size_t size)
{
struct led_classdev *led_cdev = dev_get_drvdata(dev);
ssize_t ret = -EINVAL;
char *after;
unsigned long state = simple_strtoul(buf, &after, 10);
size_t count = after - buf;
if (isspace(*after))
count++;
if (count == size) {
ret = count;
if (state == LED_OFF)
led_trigger_remove(led_cdev);
led_set_brightness(led_cdev, state);
}
return ret;
}
说明:该函数对cp指向的字符串严格要求,cp指向的字符串必须为真正的unsigned long形式的字符串。字符串必须以“0x”、“0”、[0,f]开始,中间全部为有效的字符[0,f],否则返回为负。它会处理字符串最后的“\n”字符。下同