From: Richard Russon Date: Wed, 17 Jan 2018 18:21:22 +0000 (+0000) Subject: move unsigned atoi functions X-Git-Tag: neomutt-20180223~39^2~3 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=85e5208b62185f5afa217e59897870960a00f9c3;p=neomutt move unsigned atoi functions --- diff --git a/imap/command.c b/imap/command.c index ad36a4834..5fd774eb7 100644 --- a/imap/command.c +++ b/imap/command.c @@ -255,7 +255,7 @@ static void cmd_parse_expunge(struct ImapData *idata, const char *s) mutt_debug(2, "Handling EXPUNGE\n"); - if (mutt_atoui(s, &exp_msn) < 0 || exp_msn < 1 || exp_msn > idata->max_msn) + if (mutt_str_atoui(s, &exp_msn) < 0 || exp_msn < 1 || exp_msn > idata->max_msn) return; h = idata->msn_index[exp_msn - 1]; @@ -300,7 +300,7 @@ static void cmd_parse_fetch(struct ImapData *idata, char *s) mutt_debug(3, "Handling FETCH\n"); - if (mutt_atoui(s, &msn) < 0 || msn < 1 || msn > idata->max_msn) + if (mutt_str_atoui(s, &msn) < 0 || msn < 1 || msn > idata->max_msn) { mutt_debug(3, "#1 FETCH response ignored for this message\n"); return; @@ -346,7 +346,7 @@ static void cmd_parse_fetch(struct ImapData *idata, char *s) { s += 3; SKIPWS(s); - if (mutt_atoui(s, &uid) < 0) + if (mutt_str_atoui(s, &uid) < 0) { mutt_debug(2, "Illegal UID. Skipping update.\n"); return; @@ -614,7 +614,7 @@ static void cmd_parse_search(struct ImapData *idata, const char *s) while ((s = imap_next_word((char *) s)) && *s != '\0') { - if (mutt_atoui(s, &uid) < 0) + if (mutt_str_atoui(s, &uid) < 0) continue; h = (struct Header *) mutt_hash_int_find(idata->uid_hash, uid); if (h) @@ -841,7 +841,7 @@ static int cmd_handle_untagged(struct ImapData *idata) mutt_debug(2, "Handling EXISTS\n"); /* new mail arrived */ - if (mutt_atoui(pn, &count) < 0) + if (mutt_str_atoui(pn, &count) < 0) { mutt_debug(1, "Malformed EXISTS: '%s'\n", pn); } diff --git a/imap/imap.c b/imap/imap.c index 85be54dcb..95751760a 100644 --- a/imap/imap.c +++ b/imap/imap.c @@ -2144,7 +2144,7 @@ static int imap_open_mailbox(struct Context *ctx) mutt_debug(3, "Getting mailbox UIDVALIDITY\n"); pc += 3; pc = imap_next_word(pc); - if (mutt_atoui(pc, &idata->uid_validity) < 0) + if (mutt_str_atoui(pc, &idata->uid_validity) < 0) goto fail; status->uidvalidity = idata->uid_validity; } @@ -2153,7 +2153,7 @@ static int imap_open_mailbox(struct Context *ctx) mutt_debug(3, "Getting mailbox UIDNEXT\n"); pc += 3; pc = imap_next_word(pc); - if (mutt_atoui(pc, &idata->uidnext) < 0) + if (mutt_str_atoui(pc, &idata->uidnext) < 0) goto fail; status->uidnext = idata->uidnext; } diff --git a/imap/message.c b/imap/message.c index 5581b54f0..aedb5baa4 100644 --- a/imap/message.c +++ b/imap/message.c @@ -330,7 +330,7 @@ static int msg_parse_fetch(struct ImapHeader *h, char *s) { s += 3; SKIPWS(s); - if (mutt_atoui(s, &h->data->uid) < 0) + if (mutt_str_atoui(s, &h->data->uid) < 0) return -1; s = imap_next_word(s); @@ -410,7 +410,7 @@ static int msg_fetch_header(struct Context *ctx, struct ImapHeader *h, char *buf /* skip to message number */ buf = imap_next_word(buf); - if (mutt_atoui(buf, &h->data->msn) < 0) + if (mutt_str_atoui(buf, &h->data->msn) < 0) return rc; /* find FETCH tag */ @@ -1101,7 +1101,7 @@ int imap_fetch_message(struct Context *ctx, struct Message *msg, int msgno) if (mutt_str_strncasecmp("UID", pc, 3) == 0) { pc = imap_next_word(pc); - if (mutt_atoui(pc, &uid) < 0) + if (mutt_str_atoui(pc, &uid) < 0) goto bail; if (uid != HEADER_DATA(h)->uid) mutt_error(_( diff --git a/imap/util.c b/imap/util.c index 5b00c5aef..e18c16661 100644 --- a/imap/util.c +++ b/imap/util.c @@ -750,7 +750,7 @@ int imap_get_literal_count(const char *buf, unsigned int *bytes) while (isdigit((unsigned char) *pc)) pc++; *pc = '\0'; - if (mutt_atoui(pn, bytes) < 0) + if (mutt_str_atoui(pn, bytes) < 0) return -1; return 0; diff --git a/mutt/string.c b/mutt/string.c index cd40b0289..ce4f90879 100644 --- a/mutt/string.c +++ b/mutt/string.c @@ -32,6 +32,8 @@ * | mutt_str_atoi() | Convert ASCII string to an integer * | mutt_str_atol() | Convert ASCII string to a long * | mutt_str_atos() | Convert ASCII string to a short + * | mutt_str_atoui() | Convert ASCII string to an unsigned integer + * | mutt_str_atoul() | Convert ASCII string to an unsigned long * | mutt_str_dequote_comment() | Un-escape characters in an email address comment * | mutt_str_find_word() | Find the next word (non-space) * | mutt_str_getenv() | Get an environment variable @@ -246,6 +248,71 @@ int mutt_str_atoi(const char *str, int *dst) return 0; } +/** + * mutt_str_atoui - Convert ASCII string to an unsigned integer + * @param[in] str String to read + * @param[out] dst Store the result + * @retval 1 Successful conversion, with trailing characters + * @retval 0 Successful conversion + * @retval -1 Invalid input + * @retval -2 Input out of range + * + * @note + * This function's return value differs from the other functions. + * They return -1 if there is input beyond the number. + */ +int mutt_str_atoui(const char *str, unsigned int *dst) +{ + int rc; + unsigned long res = 0; + unsigned int tmp = 0; + unsigned int *t = dst ? dst : &tmp; + + *t = 0; + + rc = mutt_str_atoul(str, &res); + if (rc < 0) + return rc; + if ((unsigned int) res != res) + return -2; + + *t = (unsigned int) res; + return rc; +} + +/** + * mutt_str_atoul - Convert ASCII string to an unsigned long + * @param[in] str String to read + * @param[out] dst Store the result + * @retval 1 Successful conversion, with trailing characters + * @retval 0 Successful conversion + * @retval -1 Invalid input + * + * @note + * This function's return value differs from the other functions. + * They return -1 if there is input beyond the number. + */ +int mutt_str_atoul(const char *str, unsigned long *dst) +{ + unsigned long r = 0; + unsigned long *res = dst ? dst : &r; + char *e = NULL; + + /* no input: 0 */ + if (!str || !*str) + { + *res = 0; + return 0; + } + + errno = 0; + *res = strtoul(str, &e, 10); + if ((*res == ULONG_MAX) && (errno == ERANGE)) + return -1; + if (e && (*e != '\0')) + return 1; + return 0; +} /** * mutt_str_strdup - Copy a string, safely * @param s String to copy diff --git a/mutt/string2.h b/mutt/string2.h index 9c73d3a1c..e0858837e 100644 --- a/mutt/string2.h +++ b/mutt/string2.h @@ -66,6 +66,8 @@ void mutt_str_append_item(char **p, const char *item, int sep); int mutt_str_atoi(const char *str, int *dst); int mutt_str_atol(const char *str, long *dst); int mutt_str_atos(const char *str, short *dst); +int mutt_str_atoui(const char *str, unsigned int *dst); +int mutt_str_atoul(const char *str, unsigned long *dst); void mutt_str_dequote_comment(char *s); const char *mutt_str_find_word(const char *src); const char *mutt_str_getenv(const char *name); diff --git a/muttlib.c b/muttlib.c index 8207aba76..dba155b99 100644 --- a/muttlib.c +++ b/muttlib.c @@ -1572,58 +1572,3 @@ int mutt_inbox_cmp(const char *a, const char *b) return 0; } -/* NOTE: this function's return value breaks with the above three functions. - * The imap code lexes uint values out of a stream of characters without - * tokenization. The above functions return -1 if there is input beyond - * the number. - * - * returns: 1 - successful conversion, with trailing characters - * 0 - successful conversion - * -1 - invalid input - * -2 - input out of range - */ -int mutt_atoui(const char *str, unsigned int *dst) -{ - int rc; - unsigned long res; - unsigned int tmp; - unsigned int *t = dst ? dst : &tmp; - - *t = 0; - - if ((rc = mutt_atoul(str, &res)) < 0) - return rc; - if ((unsigned int) res != res) - return -2; - - *t = (unsigned int) res; - return rc; -} - -/* NOTE: this function's return value is different from mutt_atol. - * - * returns: 1 - successful conversion, with trailing characters - * 0 - successful conversion - * -1 - invalid input - */ -int mutt_atoul(const char *str, unsigned long *dst) -{ - unsigned long r; - unsigned long *res = dst ? dst : &r; - char *e = NULL; - - /* no input: 0 */ - if (!str || !*str) - { - *res = 0; - return 0; - } - - errno = 0; - *res = strtoul(str, &e, 10); - if (*res == ULONG_MAX && errno == ERANGE) - return -1; - if (e && *e != '\0') - return 1; - return 0; -} diff --git a/protos.h b/protos.h index fe3b9276a..e2595e95a 100644 --- a/protos.h +++ b/protos.h @@ -364,7 +364,4 @@ bool message_is_visible(struct Context *ctx, int index); int mutt_addrlist_to_intl(struct Address *a, char **err); int mutt_addrlist_to_local(struct Address *a); -int mutt_atoui(const char *str, unsigned int *dst); -int mutt_atoul(const char *str, unsigned long *dst); - #endif /* _MUTT_PROTOS_H */