Line data Source code
1 1 : /** 2 : * @file arch/io.c 3 : * 4 : * @brief Generic input-output facilities 5 : * 6 : * This module defines architecture independent input-oputput facilities for the 7 : * use in the simulator 8 : * 9 : * SPDX-FileCopyrightText: 2008-2021 HPDCS Group <rootsim@googlegroups.com> 10 : * SPDX-License-Identifier: GPL-3.0-only 11 : */ 12 : #include <arch/io.h> 13 : 14 : /** 15 : * @fn io_terminal_can_colorize(void) 16 : * @brief Determines if stdout supports colored text 17 : * @return true if colors escape sequences can be used, false otherwise 18 : */ 19 : 20 : /** 21 : * @fn io_local_time_get(char res[IO_TIME_BUFFER_LEN]) 22 : * @brief Fills in a formatted string of the current time 23 : * @param res a pointer to a memory area with at least IO_TIME_BUFFER_LEN chars 24 : * 25 : * The resulting string is written in @a res. 26 : */ 27 : 28 : /** 29 : * @fn io_file_tmp_get(void) 30 : * @brief Creates a temporary file 31 : * @return a temporary file, an opaque object to be used in this module 32 : */ 33 : 34 : #include <core/core.h> 35 : 36 : #include <stdlib.h> 37 : #include <string.h> 38 : #include <time.h> 39 : #include <unistd.h> 40 : 41 : #ifdef __POSIX 42 : 43 : bool io_terminal_can_colorize(void) 44 : { 45 : return isatty(STDERR_FILENO); 46 : } 47 : 48 : void io_local_time_get(char res[IO_TIME_BUFFER_LEN]) 49 : { 50 : time_t t = time(NULL); 51 : struct tm *loc_t = localtime(&t); 52 : strftime(res, IO_TIME_BUFFER_LEN, "%H:%M:%S", loc_t); 53 : } 54 : 55 : FILE *io_file_tmp_get(void) 56 : { 57 : return tmpfile(); 58 : } 59 : 60 : #endif 61 : 62 : #ifdef __WINDOWS 63 : 64 : #include <io.h> 65 : 66 : bool io_terminal_can_colorize(void) 67 : { 68 : HANDLE term = GetStdHandle(STD_ERROR_HANDLE); 69 : if (term == NULL || term == INVALID_HANDLE_VALUE) 70 : return false; 71 : 72 : DWORD cmode; 73 : if (!GetConsoleMode(term, &cmode)) 74 : return false; 75 : 76 : return SetConsoleMode(term, cmode | ENABLE_VIRTUAL_TERMINAL_PROCESSING); 77 : } 78 : 79 : void io_local_time_get(char res[IO_TIME_BUFFER_LEN]) 80 : { 81 : struct tm loc_t; 82 : __time64_t long_time; 83 : _time64(&long_time); 84 : 85 : _localtime64_s(&loc_t, &long_time); 86 : strftime(res, IO_TIME_BUFFER_LEN, "%H:%M:%S", &loc_t); 87 : } 88 : 89 : FILE *io_file_tmp_get(void) 90 : { 91 : TCHAR tmp_folder_path[MAX_PATH + 1]; 92 : if (!GetTempPath(MAX_PATH + 1, tmp_folder_path)) 93 : return NULL; 94 : 95 : TCHAR tmp_path[MAX_PATH]; 96 : if (!GetTempFileName(tmp_folder_path, TEXT("ROOTSIM"), 0, tmp_path)) 97 : return NULL; 98 : 99 : SECURITY_ATTRIBUTES tmp_sa = {sizeof(SECURITY_ATTRIBUTES), NULL, TRUE}; 100 : HANDLE h = CreateFile( 101 : tmp_path, GENERIC_READ | GENERIC_WRITE, 102 : FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, 103 : &tmp_sa, CREATE_ALWAYS, 104 : FILE_ATTRIBUTE_TEMPORARY | FILE_FLAG_DELETE_ON_CLOSE, NULL); 105 : if (h == INVALID_HANDLE) 106 : return NULL; 107 : 108 : int fd = _open_osfhandle((intptr_t)h, _O_RDWR); 109 : if (fd == -1) 110 : return NULL; 111 : 112 : return _fdopen(fd, "rb+"); 113 : } 114 : 115 : #endif