1*f1987b28Schristos /* $NetBSD: strtonum.c,v 1.7 2024/01/20 16:13:39 christos Exp $ */
240195068Schristos /*-
340195068Schristos * Copyright (c) 2014 The NetBSD Foundation, Inc.
440195068Schristos * All rights reserved.
540195068Schristos *
640195068Schristos * This code is derived from software contributed to The NetBSD Foundation
740195068Schristos * by Christos Zoulas.
840195068Schristos *
940195068Schristos * Redistribution and use in source and binary forms, with or without
1040195068Schristos * modification, are permitted provided that the following conditions
1140195068Schristos * are met:
1240195068Schristos * 1. Redistributions of source code must retain the above copyright
1340195068Schristos * notice, this list of conditions and the following disclaimer.
1440195068Schristos * 2. Redistributions in binary form must reproduce the above copyright
1540195068Schristos * notice, this list of conditions and the following disclaimer in the
1640195068Schristos * documentation and/or other materials provided with the distribution.
1740195068Schristos *
1840195068Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
1940195068Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
2040195068Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
2140195068Schristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
2240195068Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
2340195068Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
2440195068Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
2540195068Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
2640195068Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
2740195068Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
2840195068Schristos * POSSIBILITY OF SUCH DAMAGE.
2940195068Schristos */
3040195068Schristos
3140195068Schristos #include <sys/cdefs.h>
32*f1987b28Schristos __RCSID("$NetBSD: strtonum.c,v 1.7 2024/01/20 16:13:39 christos Exp $");
33c5b83981Skamil
34c5b83981Skamil #include "namespace.h"
3540195068Schristos
3640195068Schristos #define _OPENBSD_SOURCE
3740195068Schristos #include <stdio.h>
3840195068Schristos #include <stdlib.h>
3940195068Schristos #include <errno.h>
4040195068Schristos #include <inttypes.h>
4140195068Schristos
4240195068Schristos long long
strtonum(const char * nptr,long long minval,long long maxval,const char ** errstr)433d8157c1Schristos strtonum(const char *nptr, long long minval, long long maxval,
443d8157c1Schristos const char **errstr)
4540195068Schristos {
4640195068Schristos int e;
473d8157c1Schristos long long rv;
4840195068Schristos const char *resp;
49*f1987b28Schristos char *eptr;
5040195068Schristos
513d8157c1Schristos if (errstr == NULL)
523d8157c1Schristos errstr = &resp;
5340195068Schristos
54*f1987b28Schristos if (minval > maxval)
55*f1987b28Schristos goto out;
56cae5584fSkamil
57*f1987b28Schristos rv = (long long)strtoi(nptr, &eptr, 10, minval, maxval, &e);
583d8157c1Schristos
59*f1987b28Schristos switch (e) {
60*f1987b28Schristos case 0:
613d8157c1Schristos *errstr = NULL;
6240195068Schristos return rv;
63*f1987b28Schristos case ECANCELED:
64*f1987b28Schristos case ENOTSUP:
65*f1987b28Schristos goto out;
66*f1987b28Schristos case ERANGE:
67*f1987b28Schristos if (*eptr)
68*f1987b28Schristos goto out;
69*f1987b28Schristos *errstr = rv == maxval ? "too large" : "too small";
70*f1987b28Schristos return 0;
71*f1987b28Schristos default:
72*f1987b28Schristos abort();
7340195068Schristos }
743d8157c1Schristos
75*f1987b28Schristos out:
763d8157c1Schristos *errstr = "invalid";
7740195068Schristos return 0;
7840195068Schristos }
79