cvm
1/*-*-coding:utf-8 -*-
2* Auto updated?
3* Yes
4* Created:
5* 11 December 2023 at 9:06:14 GMT+3
6* Modified:
7* January 30, 2024 at 4:58:14 PM GMT+3
8*
9*/
10/*
11Copyright (C) 2022-2047 Artur Mustafin (artur.mustafin@gmail.com)
12
13This program is free software: you can redistribute it and/or modify
14it under the terms of the GNU General Public License as published by
15the Free Software Foundation, either version 3 of the License, or
16(at your option) any later version.
17
18This program is distributed in the hope that it will be useful,
19but WITHOUT ANY WARRANTY; without even the implied warranty of
20MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21GNU General Public License for more details.
22
23You should have received a copy of the GNU General Public License
24along with this program. If not, see <https://www.gnu.org/licenses/>.
25*/
26
27#include "generic/memory_v1.h"28
29#include "std/data.h"30#include "std/headers.h"31
32#include "vm/v1/os/os_v1.h"33#include "vm/v1/pointer/pointer_v1.h"34#include "vm/v1/types/string/string_v1.h"35#include "vm/v1/types/list/list_v1.h"36#include "vm/v1/virtual/virtual_v1.h"37#include "vm/v1/vm_type.h"38#include "vm/v1/vm_v1.h"39
40#define DEFAULT_SIZE 0x10041
42/* definition */
43static u64 os_getenv(u64 name);44static u64 os_getcwd(void);45static void os_putc(u64 ptr);46
47/* implementation */
48static u64 os_getenv(u64 ptr) {49if (ptr == 0) {50return 0;51}52const struct pointer* data_ptr = virtual_v1->read_type(ptr, TYPE_STRING);53if (data_ptr == 0) {54return 0;55}56const char* name_data = pointer_v1->read(data_ptr);57u64 value = type_string_v1->load(getenv(name_data));58return value;59}
60
61static u64 os_getcwd(void) {62u64 data_ptr = 0;63char* src = generic_memory_v1->alloc(PATH_MAX + 1);64data_ptr = type_string_v1->load(getcwd(src, PATH_MAX));65generic_memory_v1->free(src, PATH_MAX + 1);66return data_ptr;67}
68
69static void os_putc(u64 ptr) {70const char* unsafe_data = type_string_v1->unsafe(ptr);71if (unsafe_data == 0) {72return;73}74puts(unsafe_data);75}
76
77/* public */
78const struct os_methods_v1 os_methods_definition_v1 = {79.getenv = os_getenv,80.getcwd = os_getcwd,81.putc = os_putc82};83