1 /* -*-comment-start: "//";comment-end:""-*-
2 * Mes --- Maxwell Equations of Software
3 * Copyright © 2016,2017 Jan 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/>.
24 char **g_environment = 0; // FIXME: todo extern
32 asm ("mov____0x8(%ebp),%ebx !8"); // mov 0x8(%ebp),%ebx
34 asm ("mov____$i32,%eax SYS_exit"); // mov $0x1,%eax
35 asm ("int____$0x80"); // int $0x80
41 asm ("mov____0x8(%ebp),%ebx !8"); // mov 0x8(%ebp),%ebx
42 asm ("mov____0x8(%ebp),%ecx !12"); // mov 0x8(%ebp),%ecx
43 asm ("mov____0x8(%ebp),%edx !16"); // mov 0x8(%ebp),%edx
45 asm ("mov____$i32,%eax SYS_read"); // mov $0x3,%eax
46 asm ("int____$0x80"); // int $0x80
52 asm ("mov____0x8(%ebp),%ebx !8"); // mov 0x8(%ebp),%ebx
53 asm ("mov____0x8(%ebp),%ecx !12"); // mov 0x8(%ebp),%ecx
54 asm ("mov____0x8(%ebp),%edx !16"); // mov 0x8(%ebp),%edx
56 asm ("mov____$i32,%eax SYS_write"); // mov $0x4,%eax
57 asm ("int____$0x80"); // int $0x80
63 asm ("mov____0x8(%ebp),%ebx !8"); // mov 0x8(%ebp),%ebx
64 asm ("mov____0x8(%ebp),%ecx !12"); // mov 0x8(%ebp),%ecx
65 asm ("mov____0x8(%ebp),%edx !16"); // mov 0x8(%ebp),%edx
67 asm ("mov____$i32,%eax SYS_open"); // mov $0x5,%eax
68 asm ("int____$0x80"); // int $0x80
74 asm ("mov____0x8(%ebp),%ebx !8"); // mov 0x8(%ebp),%ebx
75 asm ("mov____0x8(%ebp),%ecx !12"); // mov 0x8(%ebp),%ecx
77 asm ("mov____$i32,%eax SYS_chmod"); // mov $0x0f,%eax
78 asm ("int____$0x80"); // int $0x80
84 asm ("mov____0x8(%ebp),%ebx !8"); // mov 0x8(%ebp),%ebx
85 asm ("mov____0x8(%ebp),%ecx !12"); // mov 0x8(%ebp),%ecx
87 asm ("mov____$i32,%eax SYS_access"); // mov $0x21,%eax
88 asm ("int____$0x80"); // int $0x80
94 asm ("mov____0x8(%ebp),%ebx !8"); // mov 0x8(%ebp),%ebx
96 asm ("mov____$i32,%eax SYS_brk"); // mov $0x2d,%eax
97 asm ("int____$0x80"); // int $0x80
103 asm ("mov____0x8(%ebp),%ebx !8"); // mov 0x8(%ebp),%ebx
105 asm ("mov____$i32,%eax SYS_fsync"); // mov $0x7c,%eax
106 asm ("int____$0x80"); // int $0x80
110 strlen (char const* s)
124 eputs (char const* s)
132 fputs (char const* s, int fd)
150 write (1, (char*)&c, 1);
155 fputc (int c, int fd)
157 write (fd, (char*)&c, 1);
162 assert_fail (char* s)
164 eputs ("assert fail: ");
173 int ungetc_char = -1;
181 if (ungetc_char == -1)
183 int r = read (g_stdin, &c, 1);
184 if (r < 1) return -1;
190 //i = ungetc_buf[ungetc_char--];
191 i = ungetc_buf[ungetc_char];
193 ungetc_char = ungetc_char - 1;
205 int r = read (fd, &c, 1);
206 if (r < 1) return -1;
216 //#define assert(x) ((x) ? (void)0 : assert_fail (#x))
218 ungetc (int c, int fd)
221 //assert (ungetc_char < 2);
222 //assert (ungetc_char == -1 || ungetc_char < 2);
224 //ungetc_buf[++ungetc_char] = c;
226 ungetc_buf[ungetc_char] = c;
231 strcmp (char const* a, char const* b)
233 while (*a && *b && *a == *b)
242 strcpy (char *dest, char const *src)
245 while (*src) *p++ = *src++;
255 //static char itoa_buf[10];
261 //int sign = x < 0; // FIXME
269 *p-- = '0' + (x % 10);
273 if (sign && *(p + 1) != '0')
280 itoab (int x, int base)
282 //static char itoa_buf[10];
288 //int sign = x < 0; // FIXME
297 *p-- = i > 9 ? 'a' + i - 10 : '0' + i;
301 if (sign && *(p + 1) != '0')
310 return (c>='0') && (c<='9');
316 return isdigit (c) || (c>='a') && (c<='f');
320 isnumber (int c, int base)
323 return (c>='0') && (c<='1');
325 return (c>='0') && (c<='7');
333 _atoi (char const **p, int base)
338 if (!base) base = 10;
344 while (isnumber (*s, base))
347 int m = *s > '9' ? 'a' - 10 : '0';
359 return _atoi (&p, 0);
369 if (brk (g_brk + size) == -1)
377 memcpy (void *dest, void const *src, size_t n)
381 while (n--) *p++ = *q++;
386 realloc (void *ptr, size_t size)
388 void *new = malloc (size);
391 memcpy (new, ptr, size);
398 strncmp (char const* a, char const* b, int length)
400 while (*a && *b && *a == *b && --length) {a++;b++;}
405 getenv (char const* s)
407 char **p = g_environment;
408 int length = strlen (s);
411 if (!strncmp (s, *p, length) && *(*p + length) == '=') return (*p + length + 1);
418 vprintf (char const* format, va_list ap)
420 char const *p = format;
430 case '%': {putchar (*p); break;}
431 case 'c': {char c; c = va_arg (ap, char); putchar (c); break;}
432 case 'd': {int d; d = va_arg (ap, int); puts (itoa (d)); break;}
433 case 's': {char *s; s = va_arg (ap, char *); puts (s); break;}
434 default: {putchar (*p); break;}
443 printf (char const* format, ...)
446 va_start (ap, format);
447 int r = vprintf (format, ap);
453 vsprintf (char *str, char const* format, va_list ap)
455 char const *p = format;
465 case '%': {*str++ = *p; break;}
466 case 'c': {char c; c = va_arg (ap, char); *str++ = c; break;}
467 case 'd': {int d; d = va_arg (ap, int); char const *s; s = itoa (d); while (*s) *str++ = *s++; break;}
468 case 's': {char *s; s = va_arg (ap, char *); while (*s) *str++ = *s++; break;}
469 default: {*str++ = *p; break;}
479 sprintf (char *str, char const* format, ...)
482 va_start (ap, format);
483 int r = vsprintf (str, format, ap);