tiny-shell 0.2
A mini shell project aiming to gain knowledge about Win32 and Linux API
Loading...
Searching...
No Matches
utils.c
Go to the documentation of this file.
1#include "utils.h"
2#include "../os/operations.h"
3
4#include <stddef.h>
5#include <stdio.h>
6#include <string.h>
7
8bool is_number(const char *c) {
9 if(c == NULL || c[0] == '\0')
10 return false;
11 const unsigned int length = strlen(c);
12 static const unsigned int MAX_INT_DIGITS = 10;
13 if(length > MAX_INT_DIGITS)
14 return false;
15 for(int i = 0; i < length; ++i) {
16 if(c[i] < '0' || c[i] > '9')
17 return false;
18 }
19 return true;
20}
21
22#ifdef _WIN32
23# include <VersionHelpers.h>
24bool priv_support_color() {
25 HANDLE handle = GetStdHandle(STD_OUTPUT_HANDLE);
26 if(handle == INVALID_HANDLE_VALUE) {
27 return false;
28 }
29 DWORD mode = 0;
30 if(!GetConsoleMode(handle, &mode)) {
31 return false;
32 }
33 mode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
34 const bool succeed = SetConsoleMode(handle, mode);
35 return succeed;
36}
37#elif defined(__linux__)
38# include <stdlib.h>
39bool priv_support_color() {
40 const char *term = getenv("TERM"); // NOLINT
41 if(term == NULL) {
42 return false;
43 }
44 const char *color_terms[] = {
45 "xterm", "xterm-color", "xterm-256color", "screen", "screen-256color",
46 "tmux", "tmux-256color", "linux", "cygwin", "rxvt-unicode-256color",
47 "xterm-kitty"};
48 const unsigned int len = sizeof(color_terms) / sizeof(color_terms[0]);
49 for(size_t i = 0; i < len; i++) {
50 if(strcmp(term, color_terms[i]) == 0) {
51 return true;
52 }
53 }
54 return false;
55}
56#endif
57
59 static bool res = false;
60 static bool checked = false;
61
62 if(!checked) {
63 res = priv_support_color();
64 checked = true;
65 }
66
67 return res;
68}
bool support_color()
Definition utils.c:58
bool is_number(const char *c)
Definition utils.c:8