1 /* -*-comment-start: "//";comment-end:""-*-
2 * Mes --- Maxwell Equations of Software
3 * Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
4 * Copyright © 2018 Jeremiah Orians <jeremiah@pdp10.guru>
6 * This file is part of Mes.
8 * Mes is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or (at
11 * your option) any later version.
13 * Mes is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Mes. If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/ioctl.h>
37 return (c>='0') && (c<='9');
43 return isdigit (c) || (c>='a') && (c<='f');
49 return (c == '\t' || c == '\n' || c == '\v' || c == '\f' || c == '\r' || c == ' ');
53 isnumber (int c, int base)
56 return (c>='0') && (c<='1');
58 return (c>='0') && (c<='7');
66 abtoi (char const **p, int base)
77 while (isnumber (*s, base))
80 int m = *s > '9' ? 'a' - 10 : '0';
98 static char itoa_buf[12];
99 char *p = itoa_buf + 11;
112 *p-- = '0' + (u % 10);
116 if (sign && *(p + 1) != '0')
123 itoab (int x, int base)
125 static char itoa_buf[12];
126 char *p = itoa_buf + 11;
140 *p-- = i > 9 ? 'a' + i - 10 : '0' + i;
144 if (sign && *(p + 1) != '0')
151 fdputc (int c, int fd)
153 write (fd, (char*)&c, 1);
158 fdputs (char const* s, int fd)
167 ///char **g_environment = 0; // FIXME: todo extern
175 return fdputc (c, STDERR);
179 fputc (int c, FILE* stream)
181 return fdputc (c, (int)stream);
185 fputs (char const* s, FILE* stream)
187 return fdputs (s, (int)stream);
191 putc (int c, FILE* stream)
193 return fdputc (c, (int)stream);
197 fopen (char const* file_name, char const* mode)
201 /* 577 is O_WRONLY|O_CREAT|O_TRUNC, 384 is 600 in octal */
202 f = open (file_name, 577 , 384);
204 /* Everything else is a read */
205 f = open (file_name, 0, 0);
207 /* Negative numbers are error codes */
217 write (STDOUT, (char*)&c, 1);
222 assert_fail (char* s)
224 eputs ("assert fail: ");
233 int ungetc_char = -1;
241 if (ungetc_char == -1)
243 int r = read (g_stdin, &c, 1);
244 if (r < 1) return -1;
250 //i = ungetc_buf[ungetc_char--];
251 i = ungetc_buf[ungetc_char];
253 ungetc_char = ungetc_char - 1;
265 int r = read (fd, &c, 1);
266 if (r < 1) return -1;
276 //#define assert(x) ((x) ? (void)0 : assert_fail (#x))
278 ungetc (int c, int fd)
281 //assert (ungetc_char < 2);
282 //assert (ungetc_char == -1 || ungetc_char < 2);
284 //ungetc_buf[++ungetc_char] = c;
286 ungetc_buf[ungetc_char] = c;
291 strcmp (char const* a, char const* b)
293 while (*a && *b && *a == *b)
302 strcpy (char *dest, char const *src)
305 while (*src) *p++ = *src++;
317 if (brk (g_brk + size) == (void*)-1)
325 memcpy (void *dest, void const *src, size_t n)
329 while (n--) *p++ = *q++;
334 realloc (void *ptr, size_t size)
336 void *new = malloc (size);
339 memcpy (new, ptr, size);
346 strncmp (char const* a, char const* b, size_t length)
348 while (*a && *b && *a == *b && --length) {a++;b++;}
353 fwrite (void const *data, size_t size, size_t count, FILE *stream)
355 if (! size || !count)
357 int bytes = write ((int)stream, data, size * count);
365 getenv (char const* s)
367 char **p = g_environment;
368 int length = strlen (s);
371 if (!strncmp (s, *p, length) && *(*p + length) == '=') return (*p + length + 1);
378 setenv (char const* s, char const* v, int overwrite_p)
380 char **p = g_environment;
381 int length = strlen (s);
384 if (!strncmp (s, *p, length) && *(*p + length) == '=')
388 char *entry = malloc (length + strlen (v) + 2);
392 strcpy (entry + length, "=");
393 strcpy (entry + length + 1, v);
394 *(entry + length + strlen (v) + 2) = 0;
401 vprintf (char const* format, va_list ap)
403 char const *p = format;
413 case '%': {putchar (*p); break;}
414 case 'c': {char c; c = va_arg (ap, char); putchar (c); break;}
415 case 'd': {int d; d = va_arg (ap, int); puts (itoa (d)); break;}
416 case 's': {char *s; s = va_arg (ap, char *); puts (s); break;}
417 default: {putchar (*p); break;}
426 printf (char const* format, ...)
429 va_start (ap, format);
430 int r = vprintf (format, ap);
436 vsprintf (char *str, char const* format, va_list ap)
438 char const *p = format;
448 case '%': {*str++ = *p; break;}
449 case 'c': {char c; c = va_arg (ap, char); *str++ = c; break;}
450 case 'd': {int d; d = va_arg (ap, int); char const *s; s = itoa (d); while (*s) *str++ = *s++; break;}
451 case 's': {char *s; s = va_arg (ap, char *); while (*s) *str++ = *s++; break;}
452 default: {*str++ = *p; break;}
462 sprintf (char *str, char const* format, ...)
465 va_start (ap, format);
466 int r = vsprintf (str, format, ap);
474 return ioctl (fd, TCGETS, 0) & 0xf0;
478 wait (int *status_ptr)
480 return waitpid (-1, status_ptr, 0);