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)
225 ldexp (double x, int exp)
227 eputs ("ldexp stub\n");
232 localtime (time_t const *timep)
234 eputs ("localtime stub\n");
239 memmove (void *dest, void const *src, size_t n)
242 return memcpy (dest, src, n);
244 char const *q = src +n;
251 memset (void *s, int c, size_t n)
254 while (n--) *p++ = c;
259 memcmp (void const *s1, void const *s2, size_t n)
263 while (*a == *b && --n) {a++;b++;}
268 mprotect (void *addr, size_t len, int prot)
274 qswap (void *a, void *b, size_t size)
277 memcpy (buf, a, size);
279 memcpy (b, buf, size);
283 qpart (void *base, size_t count, size_t size, int (*compare)(void const *, void const *))
285 void* p = base + count*size;
287 for (size_t j = 0; j < count; j++)
289 if (compare (base+j*size, p) < 0)
291 qswap (base+i*size, base+j*size, size);
295 if (compare (base+count*size, base+i*size) < 0)
296 qswap (base+i*size, base+count*size, size);
301 qsort (void *base, size_t count, size_t size, int (*compare)(void const *, void const *))
305 int p = qpart (base, count-1, size, compare);
306 qsort (base, p, size, compare);
307 qsort (base+p*size, count-p, size, compare);
312 remove (char const *file_name)
315 if (stat (file_name, &buf) < 0)
317 if (S_ISDIR (buf.st_mode))
318 return rmdir (file_name);
319 return unlink (file_name);
323 sigaction (int signum, struct sigaction const *act, struct sigaction *oldact)
329 sigemptyset (sigset_t *set)
335 snprintf(char *str, size_t size, char const *format, ...)
338 va_start (ap, format);
339 int r = vsprintf (str, format, ap);
345 sscanf (char const *str, const char *template, ...)
348 va_start (ap, template);
349 int r = vsscanf (str, template, ap);
355 strcat (char *to, char const *from)
357 char *p = strchr (to, '\0');
365 strchr (char const *s, int c)
378 strncpy (char *to, char const *from, size_t size)
383 while (*from && size--)
393 strrchr (char const *s, int c)
397 char const *p = s + n - 1;
400 if (c == *p) return p;
406 /** locate a substring. #memmem# finds the first occurrence of
407 #needle# in #haystack#. This is not ANSI-C.
409 The prototype is not in accordance with the Linux Programmer's
410 Manual v1.15, but it is with /usr/include/string.h */
413 _memmem (unsigned char const *haystack, int haystack_len,
414 unsigned char const *needle, int needle_len)
416 unsigned char const *end_haystack = haystack + haystack_len - needle_len + 1;
417 unsigned char const *end_needle = needle + needle_len;
419 /* Ahhh ... Some minimal lowlevel stuff. This *is* nice; Varation
420 is the spice of life */
421 while (haystack < end_haystack)
423 unsigned char const *subneedle = needle;
424 unsigned char const *subhaystack = haystack;
425 while (subneedle < end_needle)
426 if (*subneedle++ != *subhaystack++)
429 /* Completed the needle. Gotcha. */
430 return (unsigned char *) haystack;
438 memmem (void const *haystack, int haystack_len,
439 void const *needle, int needle_len)
441 unsigned char const *haystack_byte_c = (unsigned char const *)haystack;
442 unsigned char const *needle_byte_c = (unsigned char const *)needle;
443 return _memmem (haystack_byte_c, haystack_len, needle_byte_c, needle_len);
447 strstr (char const *haystack, char const *needle)
449 return memmem (haystack, strlen (haystack), needle, strlen (needle));
453 strtod (char const *nptr, char **endptr)
455 eputs ("strtod stub\n");
459 strtof (char const *nptr, char **endptr)
461 return strtod (nptr, endptr);
465 strtold (char const *nptr, char **endptr)
467 return strtod (nptr, endptr);
471 strtol (char const *nptr, char **endptr, int base)
473 if (!strncmp (nptr, "0x", 2))
475 char const *p = nptr + 2;
476 return abtoi (&p, 16);
478 return abtoi (&nptr, base);
482 strtoll (char const *nptr, char **endptr, int base)
484 eputs ("strtoll stub\n");
489 strtoul (char const *nptr, char **endptr, int base)
491 eputs ("strtoul stub\n");
496 strtoull (char const *p, char **endptr, int base)
499 return abtoi (endptr, base);
502 time_t time (time_t *tloc)
508 vsnprintf (char *str, size_t size, char const *format, va_list ap)
510 return vsprintf (str, format, ap);
514 calloc (size_t nmemb, size_t size)
516 size_t count = nmemb * size;
517 void *p = malloc (count);
518 memset (p, 0, count);
526 return c >= 'a' && c <= 'z';
531 return c >= 'A' && c <= 'Z';
538 return c + ('a' - 'A');
546 return c - ('a' - 'A');
551 strlwr (char *string)
563 strupr (char *string)
575 vfprintf (FILE* f, char const* format, va_list ap)
578 char const *p = format;
611 if (c >= '0' && c <= '9')
613 width = abtoi (&p, 10);
618 width = va_arg (ap, int);
625 case '%': {fputc (*p, fd); count++; break;}
626 case 'c': {char c; c = va_arg (ap, int); fputc (c, fd); break;}
634 int d = va_arg (ap, int);
635 int base = c == 'o' ? 8
636 : c == 'x' || c == 'X' ? 16
638 char const *s = number_to_ascii (d, base, c != 'u' && c != 'x' && c != 'X');
641 if (!precision_p && width >= 0)
642 width = width - strlen (s);
643 if (!left_p && !precision_p)
644 while (!precision_p && width-- > 0)
651 if (precision_p && width-- == 0)
656 while (!precision_p && width-- > 0)
665 char *s = va_arg (ap, char *);
666 if (!precision_p && width >= 0)
667 width = width - strlen (s);
668 if (!left_p && !precision_p)
669 while (!precision_p && width-- > 0)
676 if (precision_p && width-- == 0)
681 while (!precision_p && width-- > 0)
690 int *n = va_arg (ap, int *);
696 eputs ("vfprintf: not supported: %");
709 vprintf (char const* format, va_list ap)
711 return vfprintf (STDOUT, format, ap);
715 vsscanf (char const *s, char const *template, va_list ap)
718 char const *t = template;
732 case '%': {p++; break;}
735 char *c = va_arg (ap, char*);
744 int *d = va_arg (ap, int*);
751 eputs ("vsscanf: not supported: %");
765 vsprintf (char *str, char const* format, va_list ap)
767 char const *p = format;
800 if (c >= '0' && c <= '9')
802 width = abtoi (&p, 10);
807 width = va_arg (ap, int);
814 case '%': {*str++ = *p; count++; break;}
815 case 'c': {c = va_arg (ap, int); *str++ = c; count++; break;}
823 int d = va_arg (ap, int);
824 int base = c == 'o' ? 8
825 : c == 'x' || c == 'X' ? 16
827 char const *s = number_to_ascii (d, base, c != 'u' && c != 'x' && c != 'X');
830 if (!precision_p && width >= 0)
831 width = width - strlen (s);
832 if (!left_p && !precision_p)
833 while (!precision_p && width-- > 0)
840 if (precision_p && width-- == 0)
845 while (!precision_p && width-- > 0)
854 char *s = va_arg (ap, char *);
855 if (!precision_p && width >= 0)
856 width = width - strlen (s);
857 if (!left_p && !precision_p)
858 while (!precision_p && width-- > 0)
865 if (precision_p && width-- == 0)
870 while (!precision_p && width-- > 0)
879 int *n = va_arg (ap, int *);
885 eputs ("vsprintf: not supported: %");
899 sprintf (char *str, char const* format, ...)
902 va_start (ap, format);
903 int r = vsprintf (str, format, ap);
909 printf (char const* format, ...)
912 va_start (ap, format);
913 int r = vprintf (format, ap);