From 1690132c1c24f1e082388463d54d25789d6d7d48 Mon Sep 17 00:00:00 2001 From: Henrik Nordstrom Date: Wed, 1 Aug 2012 00:29:07 +0200 Subject: pio: Correct parsing of pin values --- pio.c | 38 ++++++++++++++++++++++++++------------ 1 file changed, 26 insertions(+), 12 deletions(-) (limited to 'pio.c') diff --git a/pio.c b/pio.c index 3474544..d2ccbf4 100644 --- a/pio.c +++ b/pio.c @@ -7,6 +7,7 @@ #include #include #include +#include #include @@ -174,6 +175,19 @@ static void cmd_show_pin(char *buf, const char *pin) pio_print(port, port_nr, &pio); } +static int parse_int(int *dst, const char *in) +{ + int value; + char *next; + errno = 0; + value = strtol(in, &next, 0); + if (!errno && next != in) { + *dst = value; + return 0; + } + return -1; +} + static void cmd_set_pin(char *buf, const char *pin) { int port, port_nr; @@ -182,29 +196,29 @@ static void cmd_set_pin(char *buf, const char *pin) parse_pin(&port, &port_nr, pin); if (!pio_get(buf, port, port_nr, &pio)) usage(1); - t = strchr(t, '<'); + if (t) + t = strchr(t, '<'); if (t) { t++; - if (*t && *t != '>') - pio.mul_sel = atoi(t); + parse_int(&pio.mul_sel, t); } - t = strchr(t, '<'); + if (t) + t = strchr(t, '<'); if (t) { t++; - if (*t && *t != '>') - pio.pull = atoi(t); + parse_int(&pio.pull, t); } - t = strchr(t, '<'); + if (t) + t = strchr(t, '<'); if (t) { t++; - if (*t && *t != '>') - pio.drv_level = atoi(t); + parse_int(&pio.drv_level, t); } - t = strchr(t, '<'); + if (t) + t = strchr(t, '<'); if (t) { t++; - if (*t && *t != '>') - pio.data = atoi(t); + parse_int(&pio.data, t); } pio_set(buf, port, port_nr, &pio); } -- cgit v1.2.3