1 /* -*-comment-start: "//";comment-end:""-*-
2 * Mes --- Maxwell Equations of Software
3 * Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
5 * This file is part of Mes.
7 * Mes is free software; you can redistribute it and/or modify it
8 * under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 3 of the License, or (at
10 * your option) any later version.
12 * Mes is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Mes. If not, see <http://www.gnu.org/licenses/>.
29 return c >= '0' && c <= '9';
35 return isdigit (c) || c >= 'a' && c <= 'f';
41 return (c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r' || c == ' ');
45 isnumber (int c, int base)
48 return (c >= '0') && (c <= '1');
50 return (c >= '0') && (c <= '7');
58 abtoi (char const **p, int base)
69 while (isnumber (*s, base))
72 int m = *s > '9' ? 'a' - 10 : '0';
88 number_to_ascii (int x, int base, int signed_p)
90 static char itoa_buf[12];
91 char *p = itoa_buf + 11;
96 if (signed_p && x < 0)
105 *p-- = i > 9 ? 'a' + i - 10 : '0' + i;
109 if (sign && *(p + 1) != '0')
116 itoab (int x, int base)
118 return number_to_ascii (x, base, 1);
124 return number_to_ascii (x, 10, 1);
130 return number_to_ascii (x, 10, 0);
133 int _ungetc_pos = -1;
135 char _ungetc_buf[10];
142 if (_ungetc_pos == -1)
144 int r = read (fd, &c, 1);
151 if (_ungetc_fd != fd)
153 eputs (" ***MES LIB C*** fdgetc ungetc conflict unget-fd=");
154 eputs (itoa (_ungetc_fd));
155 eputs (", fdgetc-fd=");
160 i = _ungetc_buf[_ungetc_pos];
162 if (_ungetc_pos == -1)
172 fdputc (int c, int fd)
174 write (fd, (char*)&c, 1);
179 fdputs (char const* s, int fd)
187 fdungetc (int c, int fd)
189 if (_ungetc_pos == -1)
191 else if (_ungetc_fd != fd)
193 eputs (" ***MES LIB C*** fdungetc ungetc conflict unget-fd=");
194 eputs (itoa (_ungetc_fd));
195 eputs (", fdungetc-fd=");
201 _ungetc_buf[_ungetc_pos] = c;
208 return _ungetc_pos > -1;
211 #if POSIX || __x86_64__
214 eputs (char const* s)
217 write (STDERR, s, i);
225 return fdputc (c, STDERR);