LCOV - code coverage report
Current view: top level - ports/unix - modos.c (source / functions) Hit Total Coverage
Test: unix_coverage_v1.22.0-344-ge60e8079a.info Lines: 37 39 94.9 %
Date: 2024-04-26 14:58:11 Functions: 6 6 100.0 %
Branches: 9 12 75.0 %

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            :  * This file is part of the MicroPython project, http://micropython.org/
       3                 :            :  *
       4                 :            :  * The MIT License (MIT)
       5                 :            :  *
       6                 :            :  * Copyright (c) 2014-2018 Paul Sokolovsky
       7                 :            :  * Copyright (c) 2017-2022 Damien P. George
       8                 :            :  *
       9                 :            :  * Permission is hereby granted, free of charge, to any person obtaining a copy
      10                 :            :  * of this software and associated documentation files (the "Software"), to deal
      11                 :            :  * in the Software without restriction, including without limitation the rights
      12                 :            :  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
      13                 :            :  * copies of the Software, and to permit persons to whom the Software is
      14                 :            :  * furnished to do so, subject to the following conditions:
      15                 :            :  *
      16                 :            :  * The above copyright notice and this permission notice shall be included in
      17                 :            :  * all copies or substantial portions of the Software.
      18                 :            :  *
      19                 :            :  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
      20                 :            :  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
      21                 :            :  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
      22                 :            :  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
      23                 :            :  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
      24                 :            :  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
      25                 :            :  * THE SOFTWARE.
      26                 :            :  */
      27                 :            : 
      28                 :            : #include <errno.h>
      29                 :            : #include <stdlib.h>
      30                 :            : #include <string.h>
      31                 :            : 
      32                 :            : #include "py/runtime.h"
      33                 :            : #include "py/mphal.h"
      34                 :            : 
      35                 :          8 : static mp_obj_t mp_os_getenv(size_t n_args, const mp_obj_t *args) {
      36                 :          8 :     const char *s = getenv(mp_obj_str_get_str(args[0]));
      37         [ +  + ]:          8 :     if (s == NULL) {
      38         [ +  + ]:          4 :         if (n_args == 2) {
      39                 :          2 :             return args[1];
      40                 :            :         }
      41                 :            :         return mp_const_none;
      42                 :            :     }
      43                 :          4 :     return mp_obj_new_str(s, strlen(s));
      44                 :            : }
      45                 :            : static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_os_getenv_obj, 1, 2, mp_os_getenv);
      46                 :            : 
      47                 :          4 : static mp_obj_t mp_os_putenv(mp_obj_t key_in, mp_obj_t value_in) {
      48                 :          4 :     const char *key = mp_obj_str_get_str(key_in);
      49                 :          4 :     const char *value = mp_obj_str_get_str(value_in);
      50                 :          4 :     int ret;
      51                 :            : 
      52                 :            :     #if _WIN32
      53                 :            :     ret = _putenv_s(key, value);
      54                 :            :     #else
      55                 :          4 :     ret = setenv(key, value, 1);
      56                 :            :     #endif
      57                 :            : 
      58         [ -  + ]:          4 :     if (ret == -1) {
      59                 :          0 :         mp_raise_OSError(errno);
      60                 :            :     }
      61                 :          4 :     return mp_const_none;
      62                 :            : }
      63                 :            : static MP_DEFINE_CONST_FUN_OBJ_2(mp_os_putenv_obj, mp_os_putenv);
      64                 :            : 
      65                 :          2 : static mp_obj_t mp_os_unsetenv(mp_obj_t key_in) {
      66                 :          2 :     const char *key = mp_obj_str_get_str(key_in);
      67                 :          2 :     int ret;
      68                 :            : 
      69                 :            :     #if _WIN32
      70                 :            :     ret = _putenv_s(key, "");
      71                 :            :     #else
      72                 :          2 :     ret = unsetenv(key);
      73                 :            :     #endif
      74                 :            : 
      75         [ -  + ]:          2 :     if (ret == -1) {
      76                 :          0 :         mp_raise_OSError(errno);
      77                 :            :     }
      78                 :          2 :     return mp_const_none;
      79                 :            : }
      80                 :            : static MP_DEFINE_CONST_FUN_OBJ_1(mp_os_unsetenv_obj, mp_os_unsetenv);
      81                 :            : 
      82                 :          2 : static mp_obj_t mp_os_system(mp_obj_t cmd_in) {
      83                 :          2 :     const char *cmd = mp_obj_str_get_str(cmd_in);
      84                 :            : 
      85                 :          2 :     MP_THREAD_GIL_EXIT();
      86                 :          2 :     int r = system(cmd);
      87                 :          2 :     MP_THREAD_GIL_ENTER();
      88                 :            : 
      89         [ -  + ]:          2 :     RAISE_ERRNO(r, errno);
      90                 :            : 
      91                 :          2 :     return MP_OBJ_NEW_SMALL_INT(r);
      92                 :            : }
      93                 :            : static MP_DEFINE_CONST_FUN_OBJ_1(mp_os_system_obj, mp_os_system);
      94                 :            : 
      95                 :          2 : static mp_obj_t mp_os_urandom(mp_obj_t num) {
      96                 :          2 :     mp_int_t n = mp_obj_get_int(num);
      97                 :          2 :     vstr_t vstr;
      98                 :          2 :     vstr_init_len(&vstr, n);
      99                 :          2 :     mp_hal_get_random(n, vstr.buf);
     100                 :          2 :     return mp_obj_new_bytes_from_vstr(&vstr);
     101                 :            : }
     102                 :            : static MP_DEFINE_CONST_FUN_OBJ_1(mp_os_urandom_obj, mp_os_urandom);
     103                 :            : 
     104                 :          4 : static mp_obj_t mp_os_errno(size_t n_args, const mp_obj_t *args) {
     105         [ +  + ]:          4 :     if (n_args == 0) {
     106                 :          2 :         return MP_OBJ_NEW_SMALL_INT(errno);
     107                 :            :     }
     108                 :            : 
     109                 :          2 :     errno = mp_obj_get_int(args[0]);
     110                 :          2 :     return mp_const_none;
     111                 :            : }
     112                 :            : static MP_DEFINE_CONST_FUN_OBJ_VAR_BETWEEN(mp_os_errno_obj, 0, 1, mp_os_errno);

Generated by: LCOV version 1.15-5-g462f71d