platform_linux.c 1.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. /* This file is part of asmc, a bootstrapping OS with minimal seed
  2. Copyright (C) 2018 Giovanni Mascellani <gio@debian.org>
  3. https://gitlab.com/giomasce/asmc
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation, either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <https://www.gnu.org/licenses/>. */
  14. #include <stdlib.h>
  15. #include <fcntl.h>
  16. #include <unistd.h>
  17. #include "platform.h"
  18. __attribute__((noreturn)) void platform_panic() {
  19. abort();
  20. }
  21. void platform_exit() {
  22. exit(0);
  23. }
  24. int platform_open_file(char *fname) {
  25. int ret = open(fname, O_RDONLY);
  26. if (ret < 0) {
  27. platform_panic();
  28. }
  29. return ret;
  30. }
  31. int platform_reset_file(int fd) {
  32. int res = lseek(fd, 0, SEEK_SET);
  33. if (res < 0) {
  34. platform_panic();
  35. }
  36. }
  37. int platform_read_char(int fd) {
  38. int buf = 0;
  39. int ret = read(fd, &buf, 1);
  40. if (ret == 0) {
  41. return -1;
  42. }
  43. if (ret != 1) {
  44. platform_panic();
  45. }
  46. return buf;
  47. }
  48. void platform_write_char(int fd, int c) {
  49. int buf = c;
  50. int ret = write(fd, &buf, 1);
  51. if (ret != 1) {
  52. platform_panic();
  53. }
  54. }
  55. void platform_log(int fd, char *s) {
  56. while (*s != '\0') {
  57. platform_write_char(fd, *s);
  58. s++;
  59. }
  60. }
  61. void *platform_allocate(int size) {
  62. return malloc(size);
  63. }