strtonum.c revision 1.1
11.1Schristos/* $NetBSD: strtonum.c,v 1.1 2015/01/16 18:41:33 christos Exp $ */ 21.1Schristos/*- 31.1Schristos * Copyright (c) 2014 The NetBSD Foundation, Inc. 41.1Schristos * All rights reserved. 51.1Schristos * 61.1Schristos * This code is derived from software contributed to The NetBSD Foundation 71.1Schristos * by Christos Zoulas. 81.1Schristos * 91.1Schristos * Redistribution and use in source and binary forms, with or without 101.1Schristos * modification, are permitted provided that the following conditions 111.1Schristos * are met: 121.1Schristos * 1. Redistributions of source code must retain the above copyright 131.1Schristos * notice, this list of conditions and the following disclaimer. 141.1Schristos * 2. Redistributions in binary form must reproduce the above copyright 151.1Schristos * notice, this list of conditions and the following disclaimer in the 161.1Schristos * documentation and/or other materials provided with the distribution. 171.1Schristos * 181.1Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 191.1Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 201.1Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 211.1Schristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 221.1Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 231.1Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 241.1Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 251.1Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 261.1Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 271.1Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 281.1Schristos * POSSIBILITY OF SUCH DAMAGE. 291.1Schristos */ 301.1Schristos 311.1Schristos#include <sys/cdefs.h> 321.1Schristos__RCSID("$NetBSD: strtonum.c,v 1.1 2015/01/16 18:41:33 christos Exp $"); 331.1Schristos 341.1Schristos#define _OPENBSD_SOURCE 351.1Schristos#include <stdio.h> 361.1Schristos#include <stdlib.h> 371.1Schristos#include <errno.h> 381.1Schristos#include <inttypes.h> 391.1Schristos 401.1Schristos/* 411.1Schristos * Problems with the strtonum(3) API: 421.1Schristos * - will return 0 on failure; 0 might not be in range, so 431.1Schristos * that necessitates an error check even if you want to avoid it. 441.1Schristos * - does not differentiate 'illegal' returns, so we can't tell 451.1Schristos * the difference between partial and no conversions. 461.1Schristos * - returns english strings 471.1Schristos * - can't set the base, or find where the conversion ended 481.1Schristos */ 491.1Schristoslong long 501.1Schristosstrtonum(const char * __restrict ptr, long long lo, long long hi, 511.1Schristos const char ** __restrict res) 521.1Schristos{ 531.1Schristos int e; 541.1Schristos intmax_t rv; 551.1Schristos const char *resp; 561.1Schristos 571.1Schristos if (res == NULL) 581.1Schristos res = &resp; 591.1Schristos 601.1Schristos rv = strtoi(ptr, NULL, 0, lo, hi, &e); 611.1Schristos if (e == 0) { 621.1Schristos *res = NULL; 631.1Schristos return rv; 641.1Schristos } 651.1Schristos *res = e != ERANGE ? "invalid" : (rv == hi ? "too large" : "too small"); 661.1Schristos return 0; 671.1Schristos} 68