Convert to keyserv, telnetd and telnet to libcrypto's BIGNUM
[dragonfly.git] / contrib / libgmp / mpbsd / min.c
1 /* min(MINT) -- Do decimal input from standard input and store result in
2    MINT.
3
4 Copyright (C) 1991, 1994 Free Software Foundation, Inc.
5
6 This file is part of the GNU MP Library.
7
8 The GNU MP Library is free software; you can redistribute it and/or modify
9 it under the terms of the GNU Library General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or (at your
11 option) any later version.
12
13 The GNU MP Library is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
16 License for more details.
17
18 You should have received a copy of the GNU Library General Public License
19 along with the GNU MP Library; see the file COPYING.LIB.  If not, write to
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
21 MA 02111-1307, USA. */
22
23 #include <stdio.h>
24 #include <ctype.h>
25 #include "mp.h"
26 #include "gmp.h"
27 #include "gmp-impl.h"
28
29 static int
30 digit_value_in_base (c, base)
31      int c;
32      int base;
33 {
34   int digit;
35
36   if (isdigit (c))
37     digit = c - '0';
38   else if (islower (c))
39     digit = c - 'a' + 10;
40   else if (isupper (c))
41     digit = c - 'A' + 10;
42   else
43     return -1;
44
45   if (digit < base)
46     return digit;
47   return -1;
48 }
49
50 void
51 #if __STDC__
52 min (MINT *dest)
53 #else
54 min (dest)
55      MINT *dest;
56 #endif
57 {
58   char *str;
59   size_t alloc_size, str_size;
60   int c;
61   int negative;
62   mp_size_t dest_size;
63
64   alloc_size = 100;
65   str = (char *) (*_mp_allocate_func) (alloc_size);
66   str_size = 0;
67
68   /* Skip whitespace.  */
69   do
70     c = getc (stdin);
71   while (isspace (c));
72
73   negative = 0;
74   if (c == '-')
75     {
76       negative = 1;
77       c = getc (stdin);
78     }
79
80   if (digit_value_in_base (c, 10) < 0)
81     return;                     /* error if no digits */
82
83   for (;;)
84     {
85       int dig;
86       if (str_size >= alloc_size)
87         {
88           size_t old_alloc_size = alloc_size;
89           alloc_size = alloc_size * 3 / 2;
90           str = (char *) (*_mp_reallocate_func) (str, old_alloc_size, alloc_size);
91         }
92       dig = digit_value_in_base (c, 10);
93       if (dig < 0)
94         break;
95       str[str_size++] = dig;
96       c = getc (stdin);
97     }
98
99   ungetc (c, stdin);
100
101   dest_size = str_size / __mp_bases[10].chars_per_limb + 1;
102   if (dest->_mp_alloc < dest_size)
103     _mp_realloc (dest, dest_size);
104
105   dest_size = mpn_set_str (dest->_mp_d, (unsigned char *) str, str_size, 10);
106   dest->_mp_size = negative ? -dest_size : dest_size;
107
108   (*_mp_free_func) (str, alloc_size);
109   return;
110 }