ardix/kernel/fs/write.c

51 lines
1.4 KiB
C
Raw Normal View History

/* SPDX-License-Identifier: GPL-3.0-or-later */
/* See the end of this file for copyright, license, and warranty information. */
2021-01-05 14:38:06 +01:00
#include <ardix/malloc.h>
#include <ardix/serial.h>
#include <ardix/syscall.h>
#include <ardix/userspace.h>
#include <errno.h>
#include <stddef.h>
#include <toolchain.h>
2021-01-07 12:29:45 +01:00
ssize_t sys_write(int fd, __user const void *buf, size_t len)
2021-01-05 14:38:06 +01:00
{
2021-01-05 17:50:46 +01:00
ssize_t ret;
2021-01-05 14:38:06 +01:00
void *copy;
2021-01-07 12:29:45 +01:00
if (fd != 1) /* we only support stdout (serial console) right now */
return -EBADF;
2021-01-05 14:38:06 +01:00
copy = malloc(len);
if (copy == NULL)
return -ENOMEM;
2021-01-07 12:29:45 +01:00
ret = (ssize_t)copy_from_user(copy, buf, len);
2021-01-05 14:38:06 +01:00
/* TODO: reschedule if blocking */
ret = serial_write(serial_default_device, copy, (size_t)ret);
2021-01-05 14:38:06 +01:00
2021-01-07 12:29:45 +01:00
free(copy);
2021-01-07 12:30:32 +01:00
return ret;
2021-01-05 14:38:06 +01:00
}
/*
* This file is part of Ardix.
* Copyright (c) 2020, 2021 Felix Kopp <owo@fef.moe>.
2021-01-05 14:38:06 +01:00
*
* Ardix is free software: you can redistribute it and/or modify it
* 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.
2021-01-05 14:38:06 +01:00
*
* Ardix is distributed in the hope that it will be useful, but
* 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.
2021-01-05 14:38:06 +01:00
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
2021-01-05 14:38:06 +01:00
*/