1 /* -*-comment-start: "//";comment-end:""-*-
2 * Mes --- Maxwell Equations of Software
3 * Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
4 * Copyright © 2018 Jeremiah Orians <jeremiah@pdp10.guru>
6 * This file is part of Mes.
8 * Mes is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 3 of the License, or (at
11 * your option) any later version.
13 * Mes is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with Mes. If not, see <http://www.gnu.org/licenses/>.
22 #include <sys/ioctl.h>
33 return fdputc (c, STDERR);
37 fputc (int c, FILE* stream)
39 return fdputc (c, (int)stream);
43 fputs (char const* s, FILE* stream)
45 return fdputs (s, (int)stream);
49 putc (int c, FILE* stream)
51 return fdputc (c, (int)stream);
55 fopen (char const* file_name, char const* mode)
59 /* 577 is O_WRONLY|O_CREAT|O_TRUNC, 384 is 600 in octal */
60 fd = open (file_name, 577 , 384);
62 /* Everything else is a read */
63 fd = open (file_name, 0, 0);
65 /* Negative numbers are error codes */
75 eputs ("assert fail: ");
86 return fdgetc ((int)stream);
92 return fdgetc ((int)stream);
101 ungetc (int c, FILE *stream)
103 return fdungetc (c, (int)stream);
107 strcmp (char const* a, char const* b)
109 while (*a && *b && *a == *b)
118 strcpy (char *dest, char const *src)
121 while (*src) *p++ = *src++;
133 if (brk (g_brk + size) == (void*)-1)
141 memcpy (void *dest, void const *src, size_t n)
145 while (n--) *p++ = *q++;
150 realloc (void *ptr, size_t size)
152 void *new = malloc (size);
155 memcpy (new, ptr, size);
162 strncmp (char const* a, char const* b, size_t length)
164 while (*a && *b && *a == *b && --length) {a++;b++;}
169 fwrite (void const *data, size_t size, size_t count, FILE *stream)
171 if (! size || !count)
173 int bytes = write ((int)stream, data, size * count);
180 getenv (char const* s)
182 char **p = g_environment;
183 int length = strlen (s);
186 if (!strncmp (s, *p, length) && *(*p + length) == '=') return (*p + length + 1);
193 setenv (char const* s, char const* v, int overwrite_p)
195 char **p = g_environment;
196 int length = strlen (s);
199 if (!strncmp (s, *p, length) && *(*p + length) == '=')
203 char *entry = malloc (length + strlen (v) + 2);
207 strcpy (entry + length, "=");
208 strcpy (entry + length + 1, v);
209 *(entry + length + strlen (v) + 2) = 0;
216 vprintf (char const* format, va_list ap)
218 char const *p = format;
228 case '%': {putchar (*p); break;}
229 case 'c': {char c; c = va_arg (ap, char); putchar (c); break;}
230 case 'd': {int d; d = va_arg (ap, int); puts (itoa (d)); break;}
231 case 's': {char *s; s = va_arg (ap, char *); puts (s); break;}
232 default: {putchar (*p); break;}
241 printf (char const* format, ...)
244 va_start (ap, format);
245 int r = vprintf (format, ap);
251 vsprintf (char *str, char const* format, va_list ap)
253 char const *p = format;
263 case '%': {*str++ = *p; break;}
264 case 'c': {char c; c = va_arg (ap, char); *str++ = c; break;}
265 case 'd': {int d; d = va_arg (ap, int); char const *s; s = itoa (d); while (*s) *str++ = *s++; break;}
266 case 's': {char *s; s = va_arg (ap, char *); while (*s) *str++ = *s++; break;}
267 default: {*str++ = *p; break;}
277 sprintf (char *str, char const* format, ...)
280 va_start (ap, format);
281 int r = vsprintf (str, format, ap);
289 return ioctl (fd, TCGETS, 0) & 0xf0;
293 wait (int *status_ptr)
295 return waitpid (-1, status_ptr, 0);