2018-06-03 16:54:26 +00:00
|
|
|
/* -*-comment-start: "//";comment-end:""-*-
|
2018-07-22 12:24:36 +00:00
|
|
|
* GNU Mes --- Maxwell Equations of Software
|
2018-06-03 16:54:26 +00:00
|
|
|
* Copyright © 2016,2017,2018 Jan (janneke) Nieuwenhuizen <janneke@gnu.org>
|
|
|
|
*
|
2018-07-22 12:24:36 +00:00
|
|
|
* This file is part of GNU Mes.
|
2018-06-03 16:54:26 +00:00
|
|
|
*
|
2018-07-22 12:24:36 +00:00
|
|
|
* GNU Mes is free software; you can redistribute it and/or modify it
|
2018-06-03 16:54:26 +00:00
|
|
|
* under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation; either version 3 of the License, or (at
|
|
|
|
* your option) any later version.
|
|
|
|
*
|
2018-07-22 12:24:36 +00:00
|
|
|
* GNU Mes is distributed in the hope that it will be useful, but
|
2018-06-03 16:54:26 +00:00
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2018-07-22 12:24:36 +00:00
|
|
|
* along with GNU Mes. If not, see <http://www.gnu.org/licenses/>.
|
2018-06-03 16:54:26 +00:00
|
|
|
*/
|
|
|
|
|
2018-08-15 16:26:55 +00:00
|
|
|
#include <libmes.h>
|
|
|
|
|
2018-06-16 18:51:16 +00:00
|
|
|
#include <fcntl.h>
|
2018-12-07 19:18:01 +00:00
|
|
|
#include <limits.h>
|
2018-06-03 16:54:26 +00:00
|
|
|
#include <stdarg.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <unistd.h>
|
|
|
|
#include <sys/stat.h>
|
|
|
|
#include <sys/wait.h>
|
|
|
|
|
2018-08-15 16:26:55 +00:00
|
|
|
#if __MESC__ && __i386__
|
2018-08-11 09:42:30 +00:00
|
|
|
#include <linux/x86-mes/mes.c>
|
2018-08-15 16:26:55 +00:00
|
|
|
#elif __MESC__ && __x86_64__
|
|
|
|
#include <linux/x86_64-mes/mes.c>
|
2018-08-11 09:42:30 +00:00
|
|
|
#elif __i386__
|
|
|
|
#include <linux/x86-mes-gcc/mes.c>
|
|
|
|
#elif __x86_64__
|
|
|
|
#include <linux/x86_64-mes-gcc/mes.c>
|
|
|
|
#else
|
|
|
|
#error arch not supported
|
|
|
|
#endif
|
2018-06-30 11:11:03 +00:00
|
|
|
|
2018-06-03 16:54:26 +00:00
|
|
|
int
|
|
|
|
fork ()
|
|
|
|
{
|
|
|
|
return _sys_call (SYS_fork);
|
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t
|
|
|
|
read (int filedes, void *buffer, size_t size)
|
|
|
|
{
|
2018-08-15 16:26:55 +00:00
|
|
|
ssize_t bytes = _sys_call3 (SYS_read, (int)filedes, (long)buffer, (long)size);
|
2018-08-11 09:42:30 +00:00
|
|
|
if (__mes_debug () > 3)
|
|
|
|
{
|
|
|
|
if (bytes == 1)
|
|
|
|
{
|
|
|
|
eputs ("read fd="); eputs (itoa ((int)filedes)); eputs (" c="); eputc (*(char*)buffer); eputs ("\n");
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
eputs ("read fd="); eputs (itoa ((int)filedes));
|
|
|
|
eputs (" bytes="); eputs (itoa (bytes)); eputs ("\n");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return bytes;
|
2018-06-03 16:54:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2018-08-15 16:26:55 +00:00
|
|
|
_open3 (char const *file_name, int flags, int mask)
|
2018-06-03 16:54:26 +00:00
|
|
|
{
|
2018-08-15 16:26:55 +00:00
|
|
|
int r = _sys_call3 (SYS_open, (long)file_name, (int)flags, (int)mask);
|
2019-03-16 10:55:00 +00:00
|
|
|
__ungetc_init ();
|
|
|
|
if (r > 2)
|
|
|
|
__ungetc_buf[r] = -1;
|
2018-08-15 16:26:55 +00:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
_open2 (char const *file_name, int flags)
|
|
|
|
{
|
|
|
|
int mask = 0777;
|
|
|
|
return _open3 (file_name, flags, mask);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
open (char const *file_name, int flags, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, flags);
|
|
|
|
int mask = va_arg (ap, int);
|
|
|
|
int r = _open3 (file_name, flags, mask);
|
2018-06-03 16:54:26 +00:00
|
|
|
va_end (ap);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
pid_t
|
|
|
|
waitpid (pid_t pid, int *status_ptr, int options)
|
|
|
|
{
|
2018-08-11 09:42:30 +00:00
|
|
|
#if __i386__
|
2018-08-15 16:26:55 +00:00
|
|
|
return _sys_call3 (SYS_waitpid, (long)pid, (long)status_ptr, (int)options);
|
2018-08-11 09:42:30 +00:00
|
|
|
#elif __x86_64__
|
2018-08-15 16:26:55 +00:00
|
|
|
return _sys_call4 (SYS_wait4, (long)pid, (long)status_ptr, (int)options, 0);
|
2018-08-11 09:42:30 +00:00
|
|
|
#else
|
|
|
|
#error arch not supported
|
|
|
|
#endif
|
2018-06-03 16:54:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
execve (char const* file_name, char *const argv[], char *const env[])
|
|
|
|
{
|
2018-08-11 09:42:30 +00:00
|
|
|
return _sys_call3 (SYS_execve, (long)file_name, (long)argv, (long)env);
|
2018-06-03 16:54:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
chmod (char const *file_name, mode_t mask)
|
|
|
|
{
|
2018-08-11 09:42:30 +00:00
|
|
|
return _sys_call2 (SYS_chmod, (long)file_name, (long)mask);
|
2018-06-03 16:54:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
access (char const *file_name, int how)
|
|
|
|
{
|
2018-08-15 16:26:55 +00:00
|
|
|
return _sys_call2 (SYS_access, (long)file_name, (int)how);
|
2018-06-03 16:54:26 +00:00
|
|
|
}
|
|
|
|
|
2018-08-11 09:42:30 +00:00
|
|
|
long
|
2018-06-03 16:54:26 +00:00
|
|
|
brk (void *addr)
|
|
|
|
{
|
2018-08-11 09:42:30 +00:00
|
|
|
return _sys_call1 (SYS_brk, (long)addr);
|
2018-06-03 16:54:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ioctl (int filedes, unsigned long command, ...)
|
|
|
|
{
|
|
|
|
va_list ap;
|
|
|
|
va_start (ap, command);
|
|
|
|
int data = va_arg (ap, int);
|
2018-08-15 16:26:55 +00:00
|
|
|
int r = _sys_call3 (SYS_ioctl, (int)filedes, (long)command, (int)data);
|
2018-06-03 16:54:26 +00:00
|
|
|
va_end (ap);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
fsync (int filedes)
|
|
|
|
{
|
2018-08-15 16:26:55 +00:00
|
|
|
return _sys_call1 (SYS_fsync, (int)filedes);
|
2018-06-03 16:54:26 +00:00
|
|
|
}
|
2018-10-20 12:18:04 +00:00
|
|
|
|
2018-11-11 09:12:19 +00:00
|
|
|
char *
|
2018-12-07 19:18:01 +00:00
|
|
|
_getcwd (char *buffer, size_t size)
|
2018-11-11 09:12:19 +00:00
|
|
|
{
|
|
|
|
int r = _sys_call2 (SYS_getcwd, (long)buffer, (long)size);
|
|
|
|
if (r >= 0)
|
|
|
|
return buffer;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-12-07 19:18:01 +00:00
|
|
|
char *
|
|
|
|
getcwd (char *buffer, size_t size)
|
|
|
|
{
|
|
|
|
static char buf[PATH_MAX];
|
|
|
|
if (buffer)
|
|
|
|
return _getcwd (buffer, size);
|
|
|
|
return _getcwd (buf, PATH_MAX);
|
|
|
|
}
|
|
|
|
|
2018-11-11 09:13:01 +00:00
|
|
|
int
|
|
|
|
dup (int old)
|
|
|
|
{
|
|
|
|
return _sys_call1 (SYS_dup, (int)old);
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
dup2 (int old, int new)
|
|
|
|
{
|
|
|
|
return _sys_call2 (SYS_dup2, (int)old, (int)new);
|
|
|
|
}
|
|
|
|
|
2018-11-11 10:00:42 +00:00
|
|
|
int
|
|
|
|
unlink (char const *file_name)
|
|
|
|
{
|
|
|
|
return _sys_call1 (SYS_unlink, (long)file_name);
|
|
|
|
}
|
|
|
|
|
2018-10-20 12:18:04 +00:00
|
|
|
#include "linux/clock_gettime.c"
|
|
|
|
#include "linux/gettimeofday.c"
|
|
|
|
#include "linux/time.c"
|