From d5c812c549b81bec3650c72f16d56b8d2302c1bb Mon Sep 17 00:00:00 2001 From: Thadeu Lima de Souza Cascardo Date: Sat, 27 Jun 2020 22:02:57 -0300 Subject: [PATCH] =?utf8?q?Suporta=20imprimir=20atributo=20como=20n=C3=BAme?= =?utf8?q?ro.?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- lib/attr.c | 26 ++++++++++++++++++++++++++ lib/attr.h | 1 + 2 files changed, 27 insertions(+) diff --git a/lib/attr.c b/lib/attr.c index 3f98dbc..e74ab0a 100644 --- a/lib/attr.c +++ b/lib/attr.c @@ -62,3 +62,29 @@ int attr_out(FILE *f, struct pmhash *attr, char *key, int size) { return fprintf(f, "%-*.*s", size, size, attr_get(attr, key) ?: ""); } + +int attr_out_int(FILE *f, struct pmhash *attr, char *key, int size) +{ + + /* + * As there is no validation of the string when parsed, there is no + * point returning any error when there is a parsing failure here. We + * emit only a warning, then. Unfortunately, there is no line + * information here. + */ + + char *val; + unsigned long long num = 0; + val = attr_get(attr, key); + if (val) { + char *end; + errno = 0; + num = strtoull(val, &end, 0); + if ((end && *end != '\0') || errno) { + fprintf(stderr, "Found invalid value in attribute %s: %s: not a number\n", key, val); + num = 0; + } + } + + return fprintf(f, "%0*llu", size, num); +} diff --git a/lib/attr.h b/lib/attr.h index c0e3bd1..8515f86 100644 --- a/lib/attr.h +++ b/lib/attr.h @@ -26,5 +26,6 @@ int attr_set(struct pmhash **pmhash, char *key, char *val); int attr_parse(struct pmhash **pmhash, char *arg); char * attr_get(struct pmhash *pmhash, char *key); int attr_out(FILE *f, struct pmhash *attr, char *key, int size); +int attr_out_int(FILE *f, struct pmhash *attr, char *key, int size); #endif -- 2.20.1