Commit e13b1a337a608c3301ccb9ea344b6f97279a818f

Guillem Jover 2018-05-21T00:20:49

Import strtoi() and strtou() functions from NetBSD

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
diff --git a/COPYING b/COPYING
index 4377959..10fb229 100644
--- a/COPYING
+++ b/COPYING
@@ -3,7 +3,7 @@ Format: https://www.debian.org/doc/packaging-manuals/copyright-format/1.0/
 Files:
  *
 Copyright:
- Copyright © 2004-2006, 2008-2017 Guillem Jover <guillem@hadrons.org>
+ Copyright © 2004-2006, 2008-2018 Guillem Jover <guillem@hadrons.org>
 License: BSD-3-clause
 
 Files:
@@ -110,6 +110,8 @@ Files:
  man/setmode.3bsd
  man/strmode.3bsd
  man/strnstr.3bsd
+ man/strtoi.3bsd
+ man/strtou.3bsd
  man/unvis.3bsd
  man/vis.3bsd
  man/wcslcpy.3bsd
@@ -121,6 +123,8 @@ Files:
  src/setmode.c
  src/strmode.c
  src/strnstr.c
+ src/strtoi.c
+ src/strtou.c
  src/unvis.c
 Copyright:
  Copyright © 1980, 1982, 1986, 1989-1994
diff --git a/include/Makefile.am b/include/Makefile.am
index 6c74b44..ce3f058 100644
--- a/include/Makefile.am
+++ b/include/Makefile.am
@@ -13,6 +13,7 @@ nobase_include_HEADERS = \
 	bsd/bsd.h \
 	bsd/err.h \
 	bsd/getopt.h \
+	bsd/inttypes.h \
 	bsd/libutil.h \
 	bsd/md5.h \
 	bsd/nlist.h \
diff --git a/include/bsd/inttypes.h b/include/bsd/inttypes.h
new file mode 100644
index 0000000..a2b89f7
--- /dev/null
+++ b/include/bsd/inttypes.h
@@ -0,0 +1,49 @@
+/*
+ * Copyright © 2018 Guillem Jover <guillem@hadrons.org>
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. The name of the author may not be used to endorse or promote products
+ *    derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES,
+ * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
+ * AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
+ * THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
+ * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+#ifdef LIBBSD_OVERLAY
+#include_next <inttypes.h>
+#else
+#include <inttypes.h>
+#endif
+
+#ifndef LIBBSD_INTTYPES_H
+#define LIBBSD_INTTYPES_H
+
+#ifdef LIBBSD_OVERLAY
+#include <sys/cdefs.h>
+#else
+#include <bsd/sys/cdefs.h>
+#endif
+
+__BEGIN_DECLS
+intmax_t strtoi(const char *__restrict nptr, char **__restrict endptr,
+                int base, intmax_t lo, intmax_t hi, int *rstatus);
+uintmax_t strtou(const char *__restrict nptr, char **__restrict endptr,
+                 int base, uintmax_t lo, uintmax_t hi, int *rstatus);
+__END_DECLS
+
+#endif
diff --git a/man/Makefile.am b/man/Makefile.am
index 28192c0..26e893a 100644
--- a/man/Makefile.am
+++ b/man/Makefile.am
@@ -212,7 +212,9 @@ dist_man_MANS = \
 	strnstr.3bsd \
 	strnunvis.3bsd \
 	strnvis.3bsd \
+	strtoi.3bsd \
 	strtonum.3bsd \
+	strtou.3bsd \
 	strunvis.3bsd \
 	strvis.3bsd \
 	strvisx.3bsd \
diff --git a/man/libbsd.7 b/man/libbsd.7
index ea2d281..0521f93 100644
--- a/man/libbsd.7
+++ b/man/libbsd.7
@@ -94,6 +94,7 @@ be prefixed with
 .It In bitstring.h
 .It In err.h
 .It In getopt.h
+.It In inttypes.h
 .It In libutil.h
 .It In md5.h
 .It In netinet/ip_icmp.h
@@ -214,7 +215,9 @@ This function is provided by
 .Xr strlcpy 3bsd ,
 .Xr strmode 3bsd ,
 .Xr strnstr 3bsd ,
+.Xr strtoi 3bsd ,
 .Xr strtonum 3bsd ,
+.Xr strtou 3bsd ,
 .Xr timeradd 3bsd ,
 .Xr timeval 3bsd ,
 .Xr tree 3bsd ,
diff --git a/man/strtoi.3bsd b/man/strtoi.3bsd
new file mode 100644
index 0000000..273d336
--- /dev/null
+++ b/man/strtoi.3bsd
@@ -0,0 +1,238 @@
+.\"	$NetBSD: strtoi.3,v 1.7 2017/07/03 21:32:50 wiz Exp $
+.\"
+.\" Copyright (c) 1990, 1991, 1993
+.\"	The Regents of the University of California.  All rights reserved.
+.\"
+.\" This code is derived from software contributed to Berkeley by
+.\" Chris Torek and the American National Standards Committee X3,
+.\" on Information Processing Systems.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\" 3. Neither the name of the University nor the names of its contributors
+.\"    may be used to endorse or promote products derived from this software
+.\"    without specific prior written permission.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\"     from: @(#)strtol.3	8.1 (Berkeley) 6/4/93
+.\"
+.\" Created by Kamil Rytarowski, based on ID:
+.\" NetBSD: strtol.3,v 1.31 2015/03/11 09:57:35 wiz Exp
+.\"
+.Dd November 13, 2015
+.Dt STRTOI 3bsd
+.Os
+.Sh NAME
+.Nm strtoi
+.Nd convert string value to an intmax_t integer
+.Sh LIBRARY
+.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
+.Lb libbsd
+.Sh SYNOPSIS
+.In inttypes.h
+(See
+.Xr libbsd 7
+for include usage.)
+.Ft intmax_t
+.Fo strtoi
+.Fa "const char * restrict nptr"
+.Fa "char ** restrict endptr"
+.Fa "int base"
+.Fa "intmax_t lo"
+.Fa "intmax_t hi"
+.Fa "int *rstatus"
+.Fc
+.Sh DESCRIPTION
+The
+.Fn strtoi
+function
+converts the string in
+.Fa nptr
+to an
+.Ft intmax_t
+value.
+The
+.Fn strtoi
+function uses internally
+.Xr strtoimax 3
+and ensures that the result is always in the range [
+.Fa lo ..
+.Fa hi
+].
+In adddition it always places
+.Dv 0
+on success or a conversion status in the
+.Fa rstatus
+argument, avoiding the
+.Dv errno
+gymnastics the other functions require.
+The
+.Fa rstatus
+argument can be
+.Dv NULL
+if conversion status is to be ignored.
+.Pp
+The string may begin with an arbitrary amount of white space
+(as determined by
+.Xr isspace 3 )
+followed by a single optional
+.Ql +
+or
+.Ql -
+sign.
+If
+.Fa base
+is zero or 16,
+the string may then include a
+.Ql 0x
+or
+.Ql 0X
+prefix,
+and the number will be read in base 16; otherwise,
+.\" if the
+.\" .Fa base
+.\" is zero or 2,
+.\" the string may then include a
+.\" .Ql 0b
+.\" or
+.\" .Ql 0B
+.\" prefix,
+.\" and the number will be read in base 2; otherwise,
+a zero
+.Fa base
+is taken as 10 (decimal) unless the next character is
+.Ql 0 ,
+in which case it is taken as 8 (octal).
+.Pp
+The remainder of the string is converted to a
+.Em intmax_t
+value in the obvious manner,
+stopping at the first character which is not a valid digit
+in the given base.
+(In bases above 10, the letter
+.Ql A
+in either upper or lower case
+represents 10,
+.Ql B
+represents 11, and so forth, with
+.Ql Z
+representing 35.)
+.Pp
+If
+.Fa endptr
+is non-nil,
+.Fn strtoi
+stores the address of the first invalid character in
+.Fa *endptr .
+If there were no digits at all, however,
+.Fn strtoi
+stores the original value of
+.Fa nptr
+in
+.Fa *endptr .
+(Thus, if
+.Fa *nptr
+is not
+.Ql \e0
+but
+.Fa **endptr
+is
+.Ql \e0
+on return, the entire string was valid.)
+.Sh RETURN VALUES
+The
+.Fn strtoi
+function
+always returns the closest value in the range specified by
+the
+.Fa lo
+and
+.Fa hi
+arguments.
+.Pp
+The
+.Va errno
+value is guaranteed to be left unchanged.
+.Pp
+Errors are stored as the conversion status in the
+.Fa rstatus
+argument.
+.Sh EXAMPLES
+The following example will always return a number in
+.Dv [1..99]
+range no matter what the input is, and warn if the conversion failed.
+.Bd -literal -offset indent
+int e;
+intmax_t lval = strtoi(buf, NULL, 0, 1, 99, &e);
+if (e)
+	warnc(e, "conversion of `%s' to a number failed, using %jd",
+	    buf, lval);
+.Ed
+.Sh ERRORS
+.Bl -tag -width Er
+.It Bq Er ECANCELED
+The string did not contain any characters that were converted.
+.It Bq Er EINVAL
+The
+.Ar base
+is not between 2 and 36 and does not contain the special value 0.
+.It Bq Er ENOTSUP
+The string contained non-numeric characters that did not get converted.
+In this case,
+.Fa endptr
+points to the first unconverted character.
+.It Bq Er ERANGE
+The given string was out of range; the value converted has been clamped;
+or the range given was invalid, i.e.
+.Fa lo
+>
+.Fa hi .
+.El
+.Sh SEE ALSO
+.Xr atof 3 ,
+.Xr atoi 3 ,
+.Xr atol 3 ,
+.Xr atoll 3 ,
+.Xr strtod 3 ,
+.Xr strtoimax 3 ,
+.Xr strtol 3 ,
+.Xr strtoll 3 ,
+.Xr strtou 3bsd ,
+.Xr strtoul 3 ,
+.Xr strtoull 3 ,
+.Xr strtoumax 3
+.Sh STANDARDS
+The
+.Fn strtoi
+function is a
+.Nx
+extension.
+.Sh HISTORY
+The
+.Fn strtoi
+function first appeared in
+.Nx 7 .
+.Ox
+introduced the
+.Fn strtonum 3bsd
+function for the same purpose, but the interface makes it impossible to
+properly differentiate illegal returns.
+.Sh BUGS
+Ignores the current locale.
diff --git a/man/strtou.3bsd b/man/strtou.3bsd
new file mode 100644
index 0000000..fc0f901
--- /dev/null
+++ b/man/strtou.3bsd
@@ -0,0 +1,238 @@
+.\"	$NetBSD: strtou.3,v 1.7 2017/07/03 21:32:50 wiz Exp $
+.\"
+.\" Copyright (c) 1990, 1991, 1993
+.\"	The Regents of the University of California.  All rights reserved.
+.\"
+.\" This code is derived from software contributed to Berkeley by
+.\" Chris Torek and the American National Standards Committee X3,
+.\" on Information Processing Systems.
+.\"
+.\" Redistribution and use in source and binary forms, with or without
+.\" modification, are permitted provided that the following conditions
+.\" are met:
+.\" 1. Redistributions of source code must retain the above copyright
+.\"    notice, this list of conditions and the following disclaimer.
+.\" 2. Redistributions in binary form must reproduce the above copyright
+.\"    notice, this list of conditions and the following disclaimer in the
+.\"    documentation and/or other materials provided with the distribution.
+.\" 3. Neither the name of the University nor the names of its contributors
+.\"    may be used to endorse or promote products derived from this software
+.\"    without specific prior written permission.
+.\"
+.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+.\" ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+.\" SUCH DAMAGE.
+.\"
+.\"     from: @(#)strtoul.3	8.1 (Berkeley) 6/4/93
+.\"
+.\" Created by Kamil Rytarowski, based on ID:
+.\" NetBSD: strtoul.3,v 1.29 2015/03/10 13:00:58 christos Exp
+.\"
+.Dd November 13, 2015
+.Dt STRTOU 3bsd
+.Os
+.Sh NAME
+.Nm strtou
+.Nd convert a string to an uintmax_t integer
+.Sh LIBRARY
+.ds str-Lb-libbsd Utility functions from BSD systems (libbsd, \-lbsd)
+.Lb libbsd
+.Sh SYNOPSIS
+.In inttypes.h
+(See
+.Xr libbsd 7
+for include usage.)
+.Ft uintmax_t
+.Fo strtou
+.Fa "const char * restrict nptr"
+.Fa "char ** restrict endptr"
+.Fa "int base"
+.Fa "uintmax_t lo"
+.Fa "uintmax_t hi"
+.Fa "int *rstatus"
+.Fc
+.Sh DESCRIPTION
+The
+.Fn strtou
+function converts the string in
+.Fa nptr
+to an
+.Ft uintmax_t
+value.
+The
+.Fn strtou
+function uses internally
+.Xr strtoumax 3
+and ensures that the result is always in the range [
+.Fa lo ..
+.Fa hi
+].
+In adddition it always places
+.Dv 0
+on success or a conversion status in the
+.Fa rstatus
+argument, avoiding the
+.Dv errno
+gymnastics the other functions require.
+The
+.Fa rstatus
+argument can be
+.Dv NULL
+if conversion status is to be ignored.
+.Pp
+The string may begin with an arbitrary amount of white space
+(as determined by
+.Xr isspace 3 )
+followed by a single optional
+.Ql +
+or
+.Ql -
+sign.
+If
+.Fa base
+is zero or 16,
+the string may then include a
+.Ql 0x
+or
+.Ql 0X
+prefix,
+and the number will be read in base 16; otherwise,
+.\" if the
+.\" .Fa base
+.\" is zero or 2,
+.\" the string may then include a
+.\" .Ql 0b
+.\" or
+.\" .Ql 0B
+.\" prefix,
+.\" and the number will be read in base 2; otherwise,
+a zero
+.Fa base
+is taken as 10 (decimal) unless the next character is
+.Ql 0 ,
+in which case it is taken as 8 (octal).
+.Pp
+The remainder of the string is converted to an
+.Em uintmax_t
+value in the obvious manner,
+stopping at the end of the string
+or at the first character that does not produce a valid digit
+in the given base.
+(In bases above 10, the letter
+.Ql A
+in either upper or lower case
+represents 10,
+.Ql B
+represents 11, and so forth, with
+.Ql Z
+representing 35.)
+.Pp
+If
+.Fa endptr
+is non-nil,
+.Fn strtou
+stores the address of the first invalid character in
+.Fa *endptr .
+If there were no digits at all, however,
+.Fn strtou
+stores the original value of
+.Fa nptr
+in
+.Fa *endptr .
+(Thus, if
+.Fa *nptr
+is not
+.Ql \e0
+but
+.Fa **endptr
+is
+.Ql \e0
+on return, the entire string was valid.)
+.Sh RETURN VALUES
+The
+.Fn strtou
+function
+always returns the closest value in the range specified by
+the
+.Fa lo
+and
+.Fa hi
+arguments.
+.Pp
+The
+.Va errno
+value is guaranteed to be left unchanged.
+.Pp
+Errors are stored as the conversion status in the
+.Fa rstatus
+argument.
+.Sh EXAMPLES
+The following example will always return a number in
+.Dv [1..99]
+range no matter what the input is, and warn if the conversion failed.
+.Bd -literal -offset indent
+int e;
+uintmax_t lval = strtou(buf, NULL, 0, 1, 99, &e);
+if (e)
+	warnc(e, "conversion of `%s' to a number failed, using %ju",
+	    buf, lval);
+.Ed
+.Sh ERRORS
+.Bl -tag -width Er
+.It Bq Er ECANCELED
+The string did not contain any characters that were converted.
+.It Bq Er EINVAL
+The
+.Ar base
+is not between 2 and 36 and does not contain the special value 0.
+.It Bq Er ENOTSUP
+The string contained non-numeric characters that did not get converted.
+In this case,
+.Fa endptr
+points to the first unconverted character.
+.It Bq Er ERANGE
+The given string was out of range; the value converted has been clamped; or
+the range given was invalid, i.e.
+.Fa lo
+>
+.Fa hi .
+.El
+.Sh SEE ALSO
+.Xr atof 3 ,
+.Xr atoi 3 ,
+.Xr atol 3 ,
+.Xr atoll 3 ,
+.Xr strtod 3 ,
+.Xr strtoi 3bsd ,
+.Xr strtoimax 3 ,
+.Xr strtol 3 ,
+.Xr strtoll 3 ,
+.Xr strtoul 3 ,
+.Xr strtoull 3 ,
+.Xr strtoumax 3
+.Sh STANDARDS
+The
+.Fn strtou
+function is a
+.Nx
+extension.
+.Sh HISTORY
+The
+.Fn strtou
+function first appeared in
+.Nx 7 .
+.Ox
+introduced the
+.Fn strtonum 3bsd
+function for the same purpose, but the interface makes it impossible to
+properly differentiate illegal returns.
+.Sh BUGS
+Ignores the current locale.
diff --git a/src/Makefile.am b/src/Makefile.am
index d72802e..d85dc69 100644
--- a/src/Makefile.am
+++ b/src/Makefile.am
@@ -104,7 +104,9 @@ libbsd_la_SOURCES = \
 	stringlist.c \
 	strmode.c \
 	strnstr.c \
+	strtoi.c \
 	strtonum.c \
+	strtou.c \
 	timeconv.c \
 	unvis.c \
 	vis.c \
diff --git a/src/libbsd.map b/src/libbsd.map
index a91d522..98b95ce 100644
--- a/src/libbsd.map
+++ b/src/libbsd.map
@@ -145,6 +145,9 @@ LIBBSD_0.9 {
 
     pidfile_fileno;
 
+    strtoi;
+    strtou;
+
     nvis;
     snvis;
     stravis;
diff --git a/src/strtoi.c b/src/strtoi.c
new file mode 100644
index 0000000..9e3771d
--- /dev/null
+++ b/src/strtoi.c
@@ -0,0 +1,97 @@
+/*	$NetBSD: _strtoi.h,v 1.2 2015/01/18 17:55:22 christos Exp $	*/
+
+/*-
+ * Copyright (c) 1990, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * Original version ID:
+ * NetBSD: src/lib/libc/locale/_wcstoul.h,v 1.2 2003/08/07 16:43:03 agc Exp
+ *
+ * Created by Kamil Rytarowski, based on ID:
+ * NetBSD: src/common/lib/libc/stdlib/_strtoul.h,v 1.7 2013/05/17 12:55:56 joe…
+ */
+
+#include <sys/cdefs.h>
+
+#include <stddef.h>
+#include <assert.h>
+#include <errno.h>
+#include <inttypes.h>
+
+#define _DIAGASSERT(t)
+
+intmax_t
+strtoi(const char *__restrict nptr,
+       char **__restrict endptr, int base,
+       intmax_t lo, intmax_t hi, int *rstatus)
+{
+	int serrno;
+	intmax_t im;
+	char *ep;
+	int rep;
+
+	_DIAGASSERT(hi >= lo);
+
+	_DIAGASSERT(nptr != NULL);
+	/* endptr may be NULL */
+
+	if (endptr == NULL)
+		endptr = &ep;
+
+	if (rstatus == NULL)
+		rstatus = &rep;
+
+	serrno = errno;
+	errno = 0;
+
+	im = strtoimax(nptr, endptr, base);
+
+	*rstatus = errno;
+	errno = serrno;
+
+	if (*rstatus == 0) {
+		/* No digits were found */
+		if (nptr == *endptr)
+			*rstatus = ECANCELED;
+		/* There are further characters after number */
+		else if (**endptr != '\0')
+			*rstatus = ENOTSUP;
+	}
+
+	if (im < lo) {
+		if (*rstatus == 0)
+			*rstatus = ERANGE;
+		return lo;
+	}
+	if (im > hi) {
+		if (*rstatus == 0)
+			*rstatus = ERANGE;
+		return hi;
+	}
+
+	return im;
+}
diff --git a/src/strtou.c b/src/strtou.c
new file mode 100644
index 0000000..0e22a88
--- /dev/null
+++ b/src/strtou.c
@@ -0,0 +1,97 @@
+/*	$NetBSD: _strtoi.h,v 1.2 2015/01/18 17:55:22 christos Exp $	*/
+
+/*-
+ * Copyright (c) 1990, 1993
+ *	The Regents of the University of California.  All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions
+ * are met:
+ * 1. Redistributions of source code must retain the above copyright
+ *    notice, this list of conditions and the following disclaimer.
+ * 2. Redistributions in binary form must reproduce the above copyright
+ *    notice, this list of conditions and the following disclaimer in the
+ *    documentation and/or other materials provided with the distribution.
+ * 3. Neither the name of the University nor the names of its contributors
+ *    may be used to endorse or promote products derived from this software
+ *    without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
+ * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+ * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
+ * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
+ * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+ * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
+ * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
+ * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
+ * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
+ * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
+ * SUCH DAMAGE.
+ *
+ * Original version ID:
+ * NetBSD: src/lib/libc/locale/_wcstoul.h,v 1.2 2003/08/07 16:43:03 agc Exp
+ *
+ * Created by Kamil Rytarowski, based on ID:
+ * NetBSD: src/common/lib/libc/stdlib/_strtoul.h,v 1.7 2013/05/17 12:55:56 joe…
+ */
+
+#include <sys/cdefs.h>
+
+#include <stddef.h>
+#include <assert.h>
+#include <errno.h>
+#include <inttypes.h>
+
+#define _DIAGASSERT(t)
+
+uintmax_t
+strtou(const char *__restrict nptr,
+       char **__restrict endptr, int base,
+       uintmax_t lo, uintmax_t hi, int *rstatus)
+{
+	int serrno;
+	uintmax_t im;
+	char *ep;
+	int rep;
+
+	_DIAGASSERT(hi >= lo);
+
+	_DIAGASSERT(nptr != NULL);
+	/* endptr may be NULL */
+
+	if (endptr == NULL)
+		endptr = &ep;
+
+	if (rstatus == NULL)
+		rstatus = &rep;
+
+	serrno = errno;
+	errno = 0;
+
+	im = strtoumax(nptr, endptr, base);
+
+	*rstatus = errno;
+	errno = serrno;
+
+	if (*rstatus == 0) {
+		/* No digits were found */
+		if (nptr == *endptr)
+			*rstatus = ECANCELED;
+		/* There are further characters after number */
+		else if (**endptr != '\0')
+			*rstatus = ENOTSUP;
+	}
+
+	if (im < lo) {
+		if (*rstatus == 0)
+			*rstatus = ERANGE;
+		return lo;
+	}
+	if (im > hi) {
+		if (*rstatus == 0)
+			*rstatus = ERANGE;
+		return hi;
+	}
+
+	return im;
+}