1 /* -*-comment-start: "//";comment-end:""-*-
2 * Mes --- Maxwell Equations of Software
3 * Copyright © 2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
4 * Copyright © 2018 Jeremiah Orians <jeremiah@pdp10.guru>
5 * Copyright (C) 2018 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 * This file is part of Mes.
9 * Mes is free software; you can redistribute it and/or modify it
10 * under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 3 of the License, or (at
12 * your option) any later version.
14 * Mes is distributed in the hope that it will be useful, but
15 * WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with Mes. If not, see <http://www.gnu.org/licenses/>.
38 #include <linux+tcc.c>
42 #include <libc+tcc-mes.c>
46 #include <libc+tcc-gcc.c>
51 dlclose (void *handle)
57 dlopen (char const *filename, int flags)
63 search_path (char const *file_name)
66 char *path = getenv ("PATH");
67 if (getenv ("MESC_DEBUG"))
69 eputs ("\n search-path: "); eputs (file_name); eputs ("\n");
73 char *end = strchr (path, ':');
75 end = strchr (path, '\0');
76 strncpy (buf, path, end - path);
78 if (getenv ("MESC_DEBUG"))
80 eputs (" dir: "); eputs (buf); eputs ("\n");
82 if (buf[end - path] != '/')
84 strcat (buf, file_name);
85 if (!access (buf, X_OK))
87 if (getenv ("MESC_DEBUG"))
89 eputs (" found: "); eputs (buf); eputs ("\n");
99 execvp (char const *file_name, char *const argv[])
101 if (file_name[0] != '/')
102 file_name = search_path (file_name);
108 if (getenv ("MESC_DEBUG"))
110 eputs (" EXEC: "); eputs (file_name); eputs ("\n");
114 eputs (" arg["); eputs (itoa (i)); eputs ("]: "); eputs (argv[i]); eputs ("\n");
118 return execve (file_name, argv, environ);
122 fclose (FILE *stream)
124 int fd = (int)stream;
129 fdopen (int fd, char const *mode)
135 ferror (FILE *stream)
137 int fd = (int)stream;
138 if (fd == -1) return -1;
143 fflush (FILE *stream)
149 fprintf (FILE *stream, char const *format, ...)
152 va_start (ap, format);
153 int r = vfprintf (stream, format, ap);
159 fread (void *data, size_t size, size_t count, FILE *stream)
161 if (! size || !count)
163 int bytes = read ((int)stream, data, size * count);
171 fwrite (void const *data, size_t size, size_t count, FILE *stream)
173 if (! size || !count)
175 int bytes = write ((int)stream, data, size * count);
184 return lseek ((int)stream, 0, SEEK_CUR);
188 fopen (char const *file_name, char const *opentype)
192 if (opentype[0] == 'a' && !access (file_name, O_RDONLY))
194 fd = open (file_name, O_RDWR, mode);
195 lseek (fd, 0, SEEK_END);
197 else if (opentype[0] == 'w' || opentype[0] == 'a')
199 char *plus_p = strchr (opentype, '+');
200 int flags = plus_p ? O_RDWR | O_CREAT : O_WRONLY | O_CREAT | O_TRUNC;
201 fd = open (file_name, flags, mode);
204 fd = open (file_name, 0, 0);
210 fseek (FILE *stream, long offset, int whence)
212 int pos = lseek ((int)stream, offset, whence);
219 gettimeofday (struct timeval *tv, struct timezone *tz)
221 eputs ("gettimeofday stub\n");
226 ldexp (double x, int exp)
228 eputs ("ldexp stub\n");
233 localtime (time_t const *timep)
235 eputs ("localtime stub\n");
240 memmove (void *dest, void const *src, size_t n)
243 return memcpy (dest, src, n);
245 char const *q = src +n;
252 memset (void *s, int c, size_t n)
255 while (n--) *p++ = c;
260 memcmp (void const *s1, void const *s2, size_t n)
264 while (*a == *b && --n) {a++;b++;}
269 mprotect (void *addr, size_t len, int prot)
275 qswap (void *a, void *b, size_t size)
278 memcpy (buf, a, size);
280 memcpy (b, buf, size);
284 qpart (void *base, size_t count, size_t size, int (*compare)(void const *, void const *))
286 void* p = base + count*size;
288 for (size_t j = 0; j < count; j++)
290 if (compare (base+j*size, p) < 0)
292 qswap (base+i*size, base+j*size, size);
296 if (compare (base+count*size, base+i*size) < 0)
297 qswap (base+i*size, base+count*size, size);
302 qsort (void *base, size_t count, size_t size, int (*compare)(void const *, void const *))
306 int p = qpart (base, count-1, size, compare);
307 qsort (base, p, size, compare);
308 qsort (base+p*size, count-p, size, compare);
313 remove (char const *file_name)
316 if (stat (file_name, &buf) < 0)
318 if (S_ISDIR (buf.st_mode))
319 return rmdir (file_name);
320 return unlink (file_name);
324 sigaction (int signum, struct sigaction const *act, struct sigaction *oldact)
330 sigemptyset (sigset_t *set)
336 snprintf(char *str, size_t size, char const *format, ...)
339 va_start (ap, format);
340 int r = vsprintf (str, format, ap);
346 sscanf (char const *str, const char *template, ...)
349 va_start (ap, template);
350 int r = vsscanf (str, template, ap);
356 strcat (char *to, char const *from)
358 char *p = strchr (to, '\0');
366 strchr (char const *s, int c)
379 strncpy (char *to, char const *from, size_t size)
384 while (*from && size--)
394 strrchr (char const *s, int c)
398 char const *p = s + n - 1;
401 if (c == *p) return p;
407 /** locate a substring. #memmem# finds the first occurrence of
408 #needle# in #haystack#. This is not ANSI-C.
410 The prototype is not in accordance with the Linux Programmer's
411 Manual v1.15, but it is with /usr/include/string.h */
414 _memmem (unsigned char const *haystack, int haystack_len,
415 unsigned char const *needle, int needle_len)
417 unsigned char const *end_haystack = haystack + haystack_len - needle_len + 1;
418 unsigned char const *end_needle = needle + needle_len;
420 /* Ahhh ... Some minimal lowlevel stuff. This *is* nice; Varation
421 is the spice of life */
422 while (haystack < end_haystack)
424 unsigned char const *subneedle = needle;
425 unsigned char const *subhaystack = haystack;
426 while (subneedle < end_needle)
427 if (*subneedle++ != *subhaystack++)
430 /* Completed the needle. Gotcha. */
431 return (unsigned char *) haystack;
439 memmem (void const *haystack, int haystack_len,
440 void const *needle, int needle_len)
442 unsigned char const *haystack_byte_c = (unsigned char const *)haystack;
443 unsigned char const *needle_byte_c = (unsigned char const *)needle;
444 return _memmem (haystack_byte_c, haystack_len, needle_byte_c, needle_len);
448 strstr (char const *haystack, char const *needle)
450 return memmem (haystack, strlen (haystack), needle, strlen (needle));
454 strtod (char const *string, char **tailptr)
456 eputs ("strtod stub\n");
460 strtof (char const *string, char **tailptr)
462 return strtod (string, tailptr);
466 strtold (char const *string, char **tailptr)
468 return strtod (string, tailptr);
472 strtol (char const *string, char **tailptr, int base)
474 if (!strncmp (string, "0x", 2))
482 return abtoi (tailptr, base);
485 return abtoi (p, base);
491 strtoll (char const *string, char **tailptr, int base)
493 return strtol (string, tailptr, base);
497 strtoul (char const *string, char **tailptr, int base)
499 return strtol (string, tailptr, base);
503 strtoull (char const *string, char **tailptr, int base)
505 return strtol (string, tailptr, base);
511 strtoll (char const *string, char **tailptr, int base)
513 eputs ("strtoll stub\n");
518 strtoul (char const *string, char **tailptr, int base)
520 eputs ("strtoul stub\n");
525 strtoull (char const *string, char **tailptr, int base)
528 // return abtoi (endptr, base);
529 eputs ("strtoull stub\n");
538 eputs ("time stub\n");
543 vsnprintf (char *str, size_t size, char const *format, va_list ap)
545 return vsprintf (str, format, ap);
549 calloc (size_t nmemb, size_t size)
551 size_t count = nmemb * size;
552 void *p = malloc (count);
553 memset (p, 0, count);
560 return c >= 'a' && c <= 'z';
566 return c >= 'A' && c <= 'Z';
573 return c + ('a' - 'A');
581 return c - ('a' - 'A');
586 strlwr (char *string)
598 strupr (char *string)
610 vfprintf (FILE* f, char const* format, va_list ap)
613 char const *p = format;
646 if (c >= '0' && c <= '9')
648 width = abtoi (&p, 10);
653 width = va_arg (ap, int);
660 case '%': {fputc (*p, fd); count++; break;}
661 case 'c': {char c; c = va_arg (ap, int); fputc (c, fd); break;}
669 int d = va_arg (ap, int);
670 int base = c == 'o' ? 8
671 : c == 'x' || c == 'X' ? 16
673 char const *s = number_to_ascii (d, base, c != 'u' && c != 'x' && c != 'X');
676 if (!precision_p && width >= 0)
677 width = width - strlen (s);
678 if (!left_p && !precision_p)
679 while (!precision_p && width-- > 0)
686 if (precision_p && width-- == 0)
691 while (!precision_p && width-- > 0)
700 char *s = va_arg (ap, char *);
701 if (!precision_p && width >= 0)
702 width = width - strlen (s);
703 if (!left_p && !precision_p)
704 while (!precision_p && width-- > 0)
711 if (precision_p && width-- == 0)
716 while (!precision_p && width-- > 0)
725 int *n = va_arg (ap, int *);
731 eputs ("vfprintf: not supported: %");
744 vprintf (char const* format, va_list ap)
746 return vfprintf (STDOUT, format, ap);
750 vsscanf (char const *s, char const *template, va_list ap)
753 char const *t = template;
767 case '%': {p++; break;}
770 char *c = va_arg (ap, char*);
779 int *d = va_arg (ap, int*);
786 eputs ("vsscanf: not supported: %");
800 vsprintf (char *str, char const* format, va_list ap)
802 char const *p = format;
835 if (c >= '0' && c <= '9')
837 width = abtoi (&p, 10);
842 width = va_arg (ap, int);
849 case '%': {*str++ = *p; count++; break;}
850 case 'c': {c = va_arg (ap, int); *str++ = c; count++; break;}
858 int d = va_arg (ap, int);
859 int base = c == 'o' ? 8
860 : c == 'x' || c == 'X' ? 16
862 char const *s = number_to_ascii (d, base, c != 'u' && c != 'x' && c != 'X');
865 if (!precision_p && width >= 0)
866 width = width - strlen (s);
867 if (!left_p && !precision_p)
868 while (!precision_p && width-- > 0)
875 if (precision_p && width-- == 0)
880 while (!precision_p && width-- > 0)
889 char *s = va_arg (ap, char *);
890 if (!precision_p && width >= 0)
891 width = width - strlen (s);
892 if (!left_p && !precision_p)
893 while (!precision_p && width-- > 0)
900 if (precision_p && width-- == 0)
905 while (!precision_p && width-- > 0)
914 int *n = va_arg (ap, int *);
920 eputs ("vsprintf: not supported: %");
934 sprintf (char *str, char const* format, ...)
937 va_start (ap, format);
938 int r = vsprintf (str, format, ap);
944 printf (char const* format, ...)
947 va_start (ap, format);
948 int r = vprintf (format, ap);