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");
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);
80 eputs (" dir: "); eputs (buf); eputs ("\n");
82 if (buf[end - path] != '/')
84 strcat (buf, file_name);
85 if (!access (buf, X_OK))
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);
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)
175 eputs ("fwrite "); eputs (itoa ((int)stream));
176 eputs (" "); eputs (itoa (size)); eputs ("\n");
179 if (! size || !count)
181 int bytes = write ((int)stream, data, size * count);
185 eputs (" => "); eputs (itoa (bytes)); eputs ("\n");
196 return lseek ((int)stream, 0, SEEK_CUR);
200 fopen (char const *file_name, char const *opentype)
204 eputs ("fopen "); eputs (file_name);
205 eputs (" "); eputs (opentype); eputs ("\n");
210 if ((opentype[0] == 'a' || !strcmp (opentype, "r+"))
211 && !access (file_name, O_RDONLY))
214 if (opentype[0] == 'a')
216 fd = open (file_name, flags, mode);
218 else if (opentype[0] == 'w' || opentype[0] == 'a' || !strcmp (opentype, "r+"))
220 char *plus_p = strchr (opentype, '+');
221 int flags = plus_p ? O_RDWR | O_CREAT : O_WRONLY | O_CREAT | O_TRUNC;
222 fd = open (file_name, flags, mode);
225 fd = open (file_name, 0, 0);
229 eputs (" => fd="); eputs (itoa (fd)); eputs ("\n");
234 eputs (" ***MES LIB C*** fopen of stdin: signal me in band\n");
243 fseek (FILE *stream, long offset, int whence)
245 int pos = lseek ((int)stream, offset, whence);
252 gettimeofday (struct timeval *tv, struct timezone *tz)
255 if (__mes_debug () && !stub)
256 eputs ("gettimeofday stub\n");
263 ldexp (double x, int exp)
266 if (__mes_debug () && !stub)
267 eputs ("ldexp stub\n");
273 localtime (time_t const *timep)
276 if (__mes_debug () && !stub)
277 eputs ("localtime stub\n");
284 memmove (void *dest, void const *src, size_t n)
287 return memcpy (dest, src, n);
289 char const *q = src +n;
296 memset (void *s, int c, size_t n)
299 while (n--) *p++ = c;
304 memcmp (void const *s1, void const *s2, size_t n)
308 while (*a == *b && --n) {a++;b++;}
313 mprotect (void *addr, size_t len, int prot)
319 qswap (void *a, void *b, size_t size)
322 memcpy (buf, a, size);
324 memcpy (b, buf, size);
328 qpart (void *base, size_t count, size_t size, int (*compare)(void const *, void const *))
330 void* p = base + count*size;
332 for (size_t j = 0; j < count; j++)
334 if (compare (base+j*size, p) < 0)
336 qswap (base+i*size, base+j*size, size);
340 if (compare (base+count*size, base+i*size) < 0)
341 qswap (base+i*size, base+count*size, size);
346 qsort (void *base, size_t count, size_t size, int (*compare)(void const *, void const *))
350 int p = qpart (base, count-1, size, compare);
351 qsort (base, p, size, compare);
352 qsort (base+p*size, count-p, size, compare);
357 remove (char const *file_name)
360 if (stat (file_name, &buf) < 0)
362 if (S_ISDIR (buf.st_mode))
363 return rmdir (file_name);
364 return unlink (file_name);
368 sigaction (int signum, struct sigaction const *act, struct sigaction *oldact)
374 sigemptyset (sigset_t *set)
380 snprintf(char *str, size_t size, char const *format, ...)
383 va_start (ap, format);
384 int r = vsprintf (str, format, ap);
390 sscanf (char const *str, const char *template, ...)
393 va_start (ap, template);
394 int r = vsscanf (str, template, ap);
400 strcat (char *to, char const *from)
402 char *p = strchr (to, '\0');
410 strchr (char const *s, int c)
423 strncpy (char *to, char const *from, size_t size)
428 while (*from && size--)
438 strrchr (char const *s, int c)
442 char const *p = s + n - 1;
445 if (c == *p) return p;
451 /** locate a substring. #memmem# finds the first occurrence of
452 #needle# in #haystack#. This is not ANSI-C.
454 The prototype is not in accordance with the Linux Programmer's
455 Manual v1.15, but it is with /usr/include/string.h */
458 _memmem (unsigned char const *haystack, int haystack_len,
459 unsigned char const *needle, int needle_len)
461 unsigned char const *end_haystack = haystack + haystack_len - needle_len + 1;
462 unsigned char const *end_needle = needle + needle_len;
464 /* Ahhh ... Some minimal lowlevel stuff. This *is* nice; Varation
465 is the spice of life */
466 while (haystack < end_haystack)
468 unsigned char const *subneedle = needle;
469 unsigned char const *subhaystack = haystack;
470 while (subneedle < end_needle)
471 if (*subneedle++ != *subhaystack++)
474 /* Completed the needle. Gotcha. */
475 return (unsigned char *) haystack;
483 memmem (void const *haystack, int haystack_len,
484 void const *needle, int needle_len)
486 unsigned char const *haystack_byte_c = (unsigned char const *)haystack;
487 unsigned char const *needle_byte_c = (unsigned char const *)needle;
488 return _memmem (haystack_byte_c, haystack_len, needle_byte_c, needle_len);
492 strstr (char const *haystack, char const *needle)
494 return memmem (haystack, strlen (haystack), needle, strlen (needle));
498 strtod (char const *string, char **tailptr)
501 if (__mes_debug () && !stub)
502 eputs ("strtod stub\n");
508 strtof (char const *string, char **tailptr)
510 return strtod (string, tailptr);
514 strtold (char const *string, char **tailptr)
516 return strtod (string, tailptr);
520 strtol (char const *string, char **tailptr, int base)
522 if (!strncmp (string, "0x", 2))
530 return abtoi (tailptr, base);
533 return abtoi (p, base);
537 strtoll (char const *string, char **tailptr, int base)
539 return strtol (string, tailptr, base);
543 strtoul (char const *string, char **tailptr, int base)
545 return strtol (string, tailptr, base);
549 strtoull (char const *string, char **tailptr, int base)
551 return strtol (string, tailptr, base);
558 if (__mes_debug () && !stub)
559 eputs ("time stub\n");
566 vsnprintf (char *str, size_t size, char const *format, va_list ap)
568 return vsprintf (str, format, ap);
572 calloc (size_t nmemb, size_t size)
574 size_t count = nmemb * size;
575 void *p = malloc (count);
576 memset (p, 0, count);
583 return c >= 'a' && c <= 'z';
589 return c >= 'A' && c <= 'Z';
596 return c + ('a' - 'A');
604 return c - ('a' - 'A');
609 strlwr (char *string)
621 strupr (char *string)
633 vfprintf (FILE* f, char const* format, va_list ap)
636 char const *p = format;
669 if (c >= '0' && c <= '9')
671 width = abtoi (&p, 10);
676 width = va_arg (ap, int);
683 case '%': {fputc (*p, fd); count++; break;}
684 case 'c': {char c; c = va_arg (ap, int); fputc (c, fd); break;}
692 int d = va_arg (ap, int);
693 int base = c == 'o' ? 8
694 : c == 'x' || c == 'X' ? 16
696 char const *s = number_to_ascii (d, base, c != 'u' && c != 'x' && c != 'X');
699 if (!precision_p && width >= 0)
700 width = width - strlen (s);
701 if (!left_p && !precision_p)
702 while (!precision_p && width-- > 0)
709 if (precision_p && width-- == 0)
714 while (!precision_p && width-- > 0)
723 char *s = va_arg (ap, char *);
724 if (!precision_p && width >= 0)
725 width = width - strlen (s);
726 if (!left_p && !precision_p)
727 while (!precision_p && width-- > 0)
734 if (precision_p && width-- == 0)
739 while (!precision_p && width-- > 0)
748 int *n = va_arg (ap, int *);
754 eputs ("vfprintf: not supported: %");
767 vprintf (char const* format, va_list ap)
769 return vfprintf (STDOUT, format, ap);
773 vsscanf (char const *s, char const *template, va_list ap)
776 char const *t = template;
790 case '%': {p++; break;}
793 char *c = va_arg (ap, char*);
802 int *d = va_arg (ap, int*);
809 eputs ("vsscanf: not supported: %");
823 vsprintf (char *str, char const* format, va_list ap)
825 char const *p = format;
858 if (c >= '0' && c <= '9')
860 width = abtoi (&p, 10);
865 width = va_arg (ap, int);
872 case '%': {*str++ = *p; count++; break;}
873 case 'c': {c = va_arg (ap, int); *str++ = c; count++; break;}
881 int d = va_arg (ap, int);
882 int base = c == 'o' ? 8
883 : c == 'x' || c == 'X' ? 16
885 char const *s = number_to_ascii (d, base, c != 'u' && c != 'x' && c != 'X');
888 if (!precision_p && width >= 0)
889 width = width - strlen (s);
890 if (!left_p && !precision_p)
891 while (!precision_p && width-- > 0)
898 if (precision_p && width-- == 0)
903 while (!precision_p && width-- > 0)
912 char *s = va_arg (ap, char *);
913 if (!precision_p && width >= 0)
914 width = width - strlen (s);
915 if (!left_p && !precision_p)
916 while (!precision_p && width-- > 0)
923 if (precision_p && width-- == 0)
928 while (!precision_p && width-- > 0)
937 int *n = va_arg (ap, int *);
943 eputs ("vsprintf: not supported: %");
957 sprintf (char *str, char const* format, ...)
960 va_start (ap, format);
961 int r = vsprintf (str, format, ap);
967 printf (char const* format, ...)
970 va_start (ap, format);
971 int r = vprintf (format, ap);