cvm

Форк
0
/
os_v1.c 
82 строки · 2.2 Кб
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
/*
11
    Copyright (C) 2022-2047 Artur Mustafin (artur.mustafin@gmail.com)
12

13
    This program is free software: you can redistribute it and/or modify
14
    it under the terms of the GNU General Public License as published by
15
    the Free Software Foundation, either version 3 of the License, or
16
    (at your option) any later version.
17

18
    This program is distributed in the hope that it will be useful,
19
    but WITHOUT ANY WARRANTY; without even the implied warranty of
20
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21
    GNU General Public License for more details.
22

23
    You should have received a copy of the GNU General Public License
24
    along 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 0x100
41

42
/* definition */
43
static u64 os_getenv(u64 name);
44
static u64 os_getcwd(void);
45
static void os_putc(u64 ptr);
46

47
/* implementation */
48
static u64 os_getenv(u64 ptr) {
49
    if (ptr == 0) {
50
        return 0;
51
    }
52
    const struct pointer* data_ptr = virtual_v1->read_type(ptr, TYPE_STRING);
53
    if (data_ptr == 0) {
54
        return 0;
55
    }
56
    const char* name_data = pointer_v1->read(data_ptr);
57
    u64 value = type_string_v1->load(getenv(name_data));
58
    return value;
59
}
60

61
static u64 os_getcwd(void) {
62
    u64 data_ptr = 0;
63
    char* src = generic_memory_v1->alloc(PATH_MAX + 1);
64
    data_ptr = type_string_v1->load(getcwd(src, PATH_MAX));
65
    generic_memory_v1->free(src, PATH_MAX + 1);
66
    return data_ptr;
67
}
68

69
static void os_putc(u64 ptr) {
70
    const char* unsafe_data = type_string_v1->unsafe(ptr);
71
    if (unsafe_data == 0) {
72
        return;
73
    }
74
    puts(unsafe_data);
75
}
76

77
/* public */
78
const struct os_methods_v1 os_methods_definition_v1 = {
79
    .getenv = os_getenv,
80
    .getcwd = os_getcwd,
81
    .putc = os_putc
82
};
83

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.