1 /* -*-comment-start: "//";comment-end:""-*-
2 * Mes --- Maxwell Equations of Software
3 * Copyright © 2016,2017 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/>.
21 #include <sys/ioctl.h>
36 return (c>='0') && (c<='9');
42 return isdigit (c) || (c>='a') && (c<='f');
48 return (c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r' || c == ' ');
52 isnumber (int c, int base)
55 return (c>='0') && (c<='1');
57 return (c>='0') && (c<='7');
65 abtoi (char const **p, int base)
76 while (isnumber (*s, base))
79 int m = *s > '9' ? 'a' - 10 : '0';
99 //static char itoa_buf[10];
105 //int sign = x < 0; // FIXME
113 *p-- = '0' + (x % 10);
117 if (sign && *(p + 1) != '0')
124 itoab (int x, int base)
126 //static char itoa_buf[10];
132 //int sign = x < 0; // FIXME
141 *p-- = i > 9 ? 'a' + i - 10 : '0' + i;
145 if (sign && *(p + 1) != '0')
152 fdputc (int c, int fd)
154 write (fd, (char*)&c, 1);
159 fdputs (char const* s, int fd)
168 ///char **g_environment = 0; // FIXME: todo extern
176 return fdputc (c, STDERR);
182 write (STDOUT, (char*)&c, 1);
187 assert_fail (char* s)
189 eputs ("assert fail: ");
198 int ungetc_char = -1;
206 if (ungetc_char == -1)
208 int r = read (g_stdin, &c, 1);
209 if (r < 1) return -1;
215 //i = ungetc_buf[ungetc_char--];
216 i = ungetc_buf[ungetc_char];
218 ungetc_char = ungetc_char - 1;
230 int r = read (fd, &c, 1);
231 if (r < 1) return -1;
241 //#define assert(x) ((x) ? (void)0 : assert_fail (#x))
243 ungetc (int c, int fd)
246 //assert (ungetc_char < 2);
247 //assert (ungetc_char == -1 || ungetc_char < 2);
249 //ungetc_buf[++ungetc_char] = c;
251 ungetc_buf[ungetc_char] = c;
256 strcmp (char const* a, char const* b)
258 while (*a && *b && *a == *b)
267 strcpy (char *dest, char const *src)
270 while (*src) *p++ = *src++;
282 if (brk (g_brk + size) == -1)
290 memcpy (void *dest, void const *src, size_t n)
294 while (n--) *p++ = *q++;
299 realloc (void *ptr, size_t size)
301 void *new = malloc (size);
304 memcpy (new, ptr, size);
311 strncmp (char const* a, char const* b, size_t length)
313 while (*a && *b && *a == *b && --length) {a++;b++;}
318 getenv (char const* s)
320 char **p = g_environment;
321 int length = strlen (s);
324 if (!strncmp (s, *p, length) && *(*p + length) == '=') return (*p + length + 1);
331 setenv (char const* s, char const* v, int overwrite_p)
333 char **p = g_environment;
334 int length = strlen (s);
337 if (!strncmp (s, *p, length) && *(*p + length) == '=')
341 char *entry = malloc (length + strlen (v) + 2);
345 strcpy (entry + length, "=");
346 strcpy (entry + length + 1, v);
347 *(entry + length + strlen (v) + 2) = 0;
354 vprintf (char const* format, va_list ap)
356 char const *p = format;
366 case '%': {putchar (*p); break;}
367 case 'c': {char c; c = va_arg (ap, char); putchar (c); break;}
368 case 'd': {int d; d = va_arg (ap, int); puts (itoa (d)); break;}
369 case 's': {char *s; s = va_arg (ap, char *); puts (s); break;}
370 default: {putchar (*p); break;}
379 printf (char const* format, ...)
382 va_start (ap, format);
383 int r = vprintf (format, ap);
389 vsprintf (char *str, char const* format, va_list ap)
391 char const *p = format;
401 case '%': {*str++ = *p; break;}
402 case 'c': {char c; c = va_arg (ap, char); *str++ = c; break;}
403 case 'd': {int d; d = va_arg (ap, int); char const *s; s = itoa (d); while (*s) *str++ = *s++; break;}
404 case 's': {char *s; s = va_arg (ap, char *); while (*s) *str++ = *s++; break;}
405 default: {*str++ = *p; break;}
415 sprintf (char *str, char const* format, ...)
418 va_start (ap, format);
419 int r = vsprintf (str, format, ap);
427 return ioctl (fd, TCGETS, 0) & 0xf0;