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/>.
39 #include <linux+tcc.c>
43 #include <libc+tcc-mes.c>
47 #include <libc+tcc-gcc.c>
52 search_path (char const *file_name)
55 char *path = getenv ("PATH");
58 eputs ("\n search-path: "); eputs (file_name); eputs ("\n");
62 char *end = strchr (path, ':');
64 end = strchr (path, '\0');
65 strncpy (buf, path, end - path);
69 eputs (" dir: "); eputs (buf); eputs ("\n");
71 if (buf[end - path] != '/')
73 strcat (buf, file_name);
74 if (!access (buf, X_OK))
78 eputs (" found: "); eputs (buf); eputs ("\n");
88 execvp (char const *file_name, char *const argv[])
90 if (file_name[0] != '/')
91 file_name = search_path (file_name);
99 eputs (" EXEC: "); eputs (file_name); eputs ("\n");
103 eputs (" arg["); eputs (itoa (i)); eputs ("]: "); eputs (argv[i]); eputs ("\n");
107 return execve (file_name, argv, environ);
111 fclose (FILE *stream)
113 int fd = (int)stream;
118 fdopen (int fd, char const *mode)
124 ferror (FILE *stream)
126 int fd = (int)stream;
127 if (fd == -1) return -1;
132 fflush (FILE *stream)
138 fprintf (FILE *stream, char const *format, ...)
141 va_start (ap, format);
142 int r = vfprintf (stream, format, ap);
148 _fungetc_p (FILE *stream)
150 return _fdungetc_p ((int)stream);
154 fread (void *data, size_t size, size_t count, FILE *stream)
156 if (! size || !count)
159 size_t todo = size * count;
160 char *buf = (char*)data;
163 while (_fungetc_p (stream) && todo-- && ++bytes)
164 *buf++ = fgetc (stream);
167 int r = read ((int)stream, buf, todo);
176 eputs ("fread fd="); eputs (itoa ((int)stream));
177 eputs (" bytes="); eputs (itoa (bytes)); eputs ("\n");
178 static char buf[4096];
179 if (bytes > 0 && bytes < sizeof (buf))
181 strncpy (buf, data, bytes);
183 eputs ("fread buf="); eputs (buf); eputs ("\n");
194 fwrite (void const *data, size_t size, size_t count, FILE *stream)
196 if (__mes_debug () > 1)
198 eputs ("fwrite "); eputs (itoa ((int)stream));
199 eputs (" "); eputs (itoa (size)); eputs ("\n");
202 if (! size || !count)
204 int bytes = write ((int)stream, data, size * count);
206 if (__mes_debug () > 2)
208 eputs (" => "); eputs (itoa (bytes)); eputs ("\n");
219 return lseek ((int)stream, 0, SEEK_CUR);
223 fopen (char const *file_name, char const *opentype)
227 eputs ("fopen "); eputs (file_name);
228 eputs (" "); eputs (opentype); eputs ("\n");
233 if ((opentype[0] == 'a' || !strcmp (opentype, "r+"))
234 && !access (file_name, O_RDONLY))
237 if (opentype[0] == 'a')
239 fd = open (file_name, flags, mode);
241 else if (opentype[0] == 'w' || opentype[0] == 'a' || !strcmp (opentype, "r+"))
243 char *plus_p = strchr (opentype, '+');
244 int flags = plus_p ? O_RDWR | O_CREAT : O_WRONLY | O_CREAT | O_TRUNC;
245 fd = open (file_name, flags, mode);
248 fd = open (file_name, 0, 0);
252 eputs (" => fd="); eputs (itoa (fd)); eputs ("\n");
257 eputs (" ***MES LIB C*** fopen of stdin: signal me in band\n");
266 fseek (FILE *stream, long offset, int whence)
268 int pos = lseek ((int)stream, offset, whence);
271 eputs ("fread fd="); eputs (itoa ((int)stream));
272 eputs (" =>"); eputs (itoa (pos)); eputs ("\n");
280 gettimeofday (struct timeval *tv, struct timezone *tz)
283 if (__mes_debug () && !stub)
284 eputs ("gettimeofday stub\n");
291 ldexp (double x, int exp)
294 if (__mes_debug () && !stub)
295 eputs ("ldexp stub\n");
301 localtime (time_t const *timep)
304 if (__mes_debug () && !stub)
305 eputs ("localtime stub\n");
312 memmove (void *dest, void const *src, size_t n)
315 return memcpy (dest, src, n);
317 char const *q = src +n;
324 memset (void *s, int c, size_t n)
327 while (n--) *p++ = c;
332 memcmp (void const *s1, void const *s2, size_t size)
338 while (*a == *b && --size)
347 mprotect (void *addr, size_t len, int prot)
353 qswap (void *a, void *b, size_t size)
356 memcpy (buf, a, size);
358 memcpy (b, buf, size);
362 qpart (void *base, size_t count, size_t size, int (*compare)(void const *, void const *))
364 void* p = base + count*size;
366 for (size_t j = 0; j < count; j++)
368 if (compare (base+j*size, p) < 0)
370 qswap (base+i*size, base+j*size, size);
374 if (compare (base+count*size, base+i*size) < 0)
375 qswap (base+i*size, base+count*size, size);
380 qsort (void *base, size_t count, size_t size, int (*compare)(void const *, void const *))
384 int p = qpart (base, count-1, size, compare);
385 qsort (base, p, size, compare);
386 qsort (base+p*size, count-p, size, compare);
391 remove (char const *file_name)
394 if (stat (file_name, &buf) < 0)
396 if (S_ISDIR (buf.st_mode))
397 return rmdir (file_name);
398 return unlink (file_name);
402 sigaction (int signum, struct sigaction const *act, struct sigaction *oldact)
408 sigemptyset (sigset_t *set)
414 snprintf(char *str, size_t size, char const *format, ...)
417 va_start (ap, format);
418 int r = vsprintf (str, format, ap);
424 sscanf (char const *str, const char *template, ...)
427 va_start (ap, template);
428 int r = vsscanf (str, template, ap);
434 strcat (char *to, char const *from)
436 char *p = strchr (to, '\0');
444 strchr (char const *s, int c)
457 strncpy (char *to, char const *from, size_t size)
462 while (*from && size--)
472 strrchr (char const *s, int c)
477 char const *p = s + n;
481 while (n-- && (*p || !c))
490 /** locate a substring. #memmem# finds the first occurrence of
491 #needle# in #haystack#. This is not ANSI-C.
493 The prototype is not in accordance with the Linux Programmer's
494 Manual v1.15, but it is with /usr/include/string.h */
497 _memmem (unsigned char const *haystack, int haystack_len,
498 unsigned char const *needle, int needle_len)
500 unsigned char const *end_haystack = haystack + haystack_len - needle_len + 1;
501 unsigned char const *end_needle = needle + needle_len;
503 /* Ahhh ... Some minimal lowlevel stuff. This *is* nice; Varation
504 is the spice of life */
505 while (haystack < end_haystack)
507 unsigned char const *subneedle = needle;
508 unsigned char const *subhaystack = haystack;
509 while (subneedle < end_needle)
510 if (*subneedle++ != *subhaystack++)
513 /* Completed the needle. Gotcha. */
514 return (unsigned char *) haystack;
522 memmem (void const *haystack, int haystack_len,
523 void const *needle, int needle_len)
525 unsigned char const *haystack_byte_c = (unsigned char const *)haystack;
526 unsigned char const *needle_byte_c = (unsigned char const *)needle;
527 return _memmem (haystack_byte_c, haystack_len, needle_byte_c, needle_len);
531 strstr (char const *haystack, char const *needle)
533 return memmem (haystack, strlen (haystack), needle, strlen (needle));
537 strtod (char const *string, char **tailptr)
540 if (__mes_debug () && !stub)
541 eputs ("strtod stub\n");
547 strtof (char const *string, char **tailptr)
549 return strtod (string, tailptr);
553 strtold (char const *string, char **tailptr)
555 return strtod (string, tailptr);
559 strtol (char const *string, char **tailptr, int base)
561 if (!strncmp (string, "0x", 2))
569 return abtoi (tailptr, base);
572 return abtoi (p, base);
576 strtoll (char const *string, char **tailptr, int base)
578 return strtol (string, tailptr, base);
582 strtoul (char const *string, char **tailptr, int base)
584 return strtol (string, tailptr, base);
588 strtoull (char const *string, char **tailptr, int base)
590 return strtol (string, tailptr, base);
597 if (__mes_debug () && !stub)
598 eputs ("time stub\n");
605 vsnprintf (char *str, size_t size, char const *format, va_list ap)
607 return vsprintf (str, format, ap);
611 calloc (size_t nmemb, size_t size)
613 size_t count = nmemb * size;
614 void *p = malloc (count);
615 memset (p, 0, count);
622 return c >= 'a' && c <= 'z';
628 return c >= 'A' && c <= 'Z';
635 return c + ('a' - 'A');
643 return c - ('a' - 'A');
648 strlwr (char *string)
660 strupr (char *string)
672 vfprintf (FILE* f, char const* format, va_list ap)
675 char const *p = format;
701 if (c >= '0' && c <= '9')
703 width = abtoi (&p, 10);
708 width = va_arg (ap, int);
714 if (c >= '0' && c <= '9')
716 precision = abtoi (&p, 10);
721 precision = va_arg (ap, int);
729 eputs ("vfprintf: skipping second: l\n");
734 case '%': {fputc (*p, fd); count++; break;}
735 case 'c': {char c; c = va_arg (ap, int); fputc (c, fd); break;}
743 int d = va_arg (ap, int);
744 int base = c == 'o' ? 8
745 : c == 'x' || c == 'X' ? 16
747 char const *s = number_to_ascii (d, base, c != 'u' && c != 'x' && c != 'X');
750 int length = strlen (s);
755 while (width-- > precision)
760 while (precision > length)
770 if (precision-- <= 0)
786 char *s = va_arg (ap, char *);
787 int length = strlen (s);
792 while (width-- > precision)
797 while (precision > length)
807 if (precision-- <= 0)
823 int *n = va_arg (ap, int *);
829 eputs ("vfprintf: not supported: %:");
842 vprintf (char const* format, va_list ap)
844 return vfprintf (STDOUT, format, ap);
848 vsscanf (char const *s, char const *template, va_list ap)
851 char const *t = template;
867 case '%': {p++; break;}
870 char *c = va_arg (ap, char*);
879 int *d = va_arg (ap, int*);
886 eputs ("vsscanf: not supported: %:");
900 vsprintf (char *str, char const* format, va_list ap)
902 char const *p = format;
928 if (c >= '0' && c <= '9')
930 width = abtoi (&p, 10);
935 width = va_arg (ap, int);
941 if (c >= '0' && c <= '9')
943 precision = abtoi (&p, 10);
948 precision = va_arg (ap, int);
958 eputs ("vfprintf: skipping second: l\n");
963 case '%': {*str++ = *p; count++; break;}
964 case 'c': {c = va_arg (ap, int); *str++ = c; count++; break;}
972 int d = va_arg (ap, int);
973 int base = c == 'o' ? 8
974 : c == 'x' || c == 'X' ? 16
976 char const *s = number_to_ascii (d, base, c != 'u' && c != 'x' && c != 'X');
979 int length = strlen (s);
984 while (width-- > precision)
989 while (precision > length)
999 if (precision-- <= 0)
1015 char *s = va_arg (ap, char *);
1016 int length = strlen (s);
1017 if (precision == -1)
1021 while (width-- > precision)
1026 while (width > length)
1036 if (precision-- <= 0)
1052 int *n = va_arg (ap, int *);
1058 eputs ("vsprintf: not supported: %:");
1068 return strlen (str);
1072 sprintf (char *str, char const* format, ...)
1075 va_start (ap, format);
1076 int r = vsprintf (str, format, ap);
1082 printf (char const* format, ...)
1085 va_start (ap, format);
1086 int r = vprintf (format, ap);