|
| 1 | +/* |
| 2 | + * Copyright (c) 2023, Real-Thread Technology Co., Ltd |
| 3 | + * |
| 4 | + * SPDX-License-Identifier: GPL-2.0 |
| 5 | + * |
| 6 | + * Change Logs: |
| 7 | + * Date Author Notes |
| 8 | + * 2023-06-06 Mr'Zhou the first version |
| 9 | + */ |
| 10 | + |
| 11 | +#include <stdlib.h> |
| 12 | +#include <stdio.h> |
| 13 | +#include <time.h> |
| 14 | +#include <unistd.h> |
| 15 | +#include <getopt.h> |
| 16 | +#include <sys/utsname.h> |
| 17 | +#include <sys/statfs.h> |
| 18 | +#include <sys/sysinfo.h> |
| 19 | +#include "smart_fetch.h" |
| 20 | +#include <string.h> |
| 21 | + |
| 22 | +static void smart_fetch_print_info() |
| 23 | +{ |
| 24 | + printf("\033[20A \n"); |
| 25 | + printf(RED " %s time: " NONE BLUE "%d-%d-%d " NONE GREEN "%d:%d:%d\n" NONE, RIGHT_MOVE, (1900 + smart_p->tm_year), ( 1 + smart_p->tm_mon), smart_p->tm_mday, (smart_p->tm_hour + 12), smart_p->tm_min, smart_p->tm_sec); |
| 26 | + printf("%s %s sysname: " NONE "%s %s" NONE "\n",RIGHT_MOVE, smart_title_color, smart_info_color, smart_sys_info.sysname); |
| 27 | + printf("%s %s nodename: " NONE "%s %s" NONE "\n",RIGHT_MOVE, smart_title_color, smart_info_color, smart_sys_info.nodename); |
| 28 | + printf("%s %s release: " NONE "%s %s" NONE "\n",RIGHT_MOVE, smart_title_color, smart_info_color, smart_sys_info.release); |
| 29 | + printf("%s %s version: " NONE "%s %s" NONE "\n",RIGHT_MOVE, smart_title_color, smart_info_color, smart_sys_info.version); |
| 30 | + printf("%s %s machine: " NONE "%s %s" NONE "\n",RIGHT_MOVE, smart_title_color, smart_info_color, smart_sys_info.machine); |
| 31 | + printf("%s %s domainname: " NONE "%s %s" NONE "\n",RIGHT_MOVE, smart_title_color, smart_info_color, smart_sys_info.__domainname); |
| 32 | + printf("%s %s f_bsize: " NONE "%s %u" NONE "\n",RIGHT_MOVE, smart_title_color, smart_info_color, smart_sys_block.f_bsize); |
| 33 | + printf("%s %s f_blocks: " NONE "%s %u" NONE "\n",RIGHT_MOVE, smart_title_color, smart_info_color, smart_sys_block.f_blocks); |
| 34 | + printf("%s %s f_bfree: " NONE "%s %u" NONE "\n",RIGHT_MOVE, smart_title_color, smart_info_color, smart_sys_block.f_bfree); |
| 35 | + printf("%s %s f_bavail: " NONE "%s %u" NONE "\n",RIGHT_MOVE, smart_title_color, smart_info_color, smart_sys_block.f_bavail); |
| 36 | + printf("%s \n", RIGHT_MOVE); |
| 37 | + printf("%s \n", RIGHT_MOVE); |
| 38 | + printf("%s \n", RIGHT_MOVE); |
| 39 | + printf("%s \n", RIGHT_MOVE); |
| 40 | + printf("%s " RED "github id:" NONE FONT_FLICKER GREEN GITHUB_ID NONE "\n", RIGHT_MOVE); |
| 41 | + printf("%s \n", RIGHT_MOVE); |
| 42 | + printf(" %s" FONT_FLICKER BG_BLACK " " BG_RED " " BG_GREEN " " BG_YELLOW " " BG_BLUE " " BG_PURPLE " " BG_CYAN " " BG_WHITE " " NONE "\n", RIGHT_MOVE ); |
| 43 | + printf(" %s" BG_BLACK " " BG_RED " " BG_GREEN " " BG_YELLOW " " BG_BLUE " " BG_PURPLE " " BG_CYAN " " BG_WHITE " " NONE "\n", RIGHT_MOVE); |
| 44 | + |
| 45 | +} |
| 46 | + |
| 47 | +static void smart_fetch_print_logo() |
| 48 | +{ |
| 49 | + printf("\n \ |
| 50 | + \e[0;31m.oyssssssssssssssssssssssssssy+.\e[0m \n \ |
| 51 | + \e[0;31m/yossssssss\e[0mrrrr\e[0;31msssss\e[0mrrrr\e[0;31msssssssoy+\e[0m \n \ |
| 52 | + \e[0;31m:h/sssssssss\e[0mrrrr\e[0;31msss\e[0mrrrr\e[0;31msssssssssh+-\e[0m \n \ |
| 53 | + \e[0;31mys.ssssssssss\e[0mrrrrrrr\e[0;31msssssssssssssss+-p\e[0m \n \ |
| 54 | + \e[0;31mh+-sssssssssss\e[0mrrrr\e[0;31msssssssssssssssssss:h/\e[0m \n \ |
| 55 | + \e[0;31mh+-ssssssssssss\e[0mrrrr\e[0;31mssssssssssssssssssss+a'\e[0m \n \ |
| 56 | + \e[0;31m/dmsssssssssssss\e[0mrrrr\e[0;31msssssssssssssssssssssz8`\e[0m \n \ |
| 57 | + \e[0;31mc/ysssssssssssssss\e[0m\e[5m\e[3;32mrt-thread\e[0m\e[0;31mssssssssssssssss-f'\e[0m \n \ |
| 58 | + \e[0;31mp/osssss\e[0mttttttttttttt\e[0;31mssssss\e[0mtttttttttttt\e[0;31msssss+j;\e[0m \n \ |
| 59 | + \e[0;31mp/osssss\e[0mttttttttttttt\e[0;31mssssss\e[0mttttttttttttt\e[0;31msssss+j;\e[0m \n \ |
| 60 | + \e[0;31mc/yssssssss\e[0mttttt\e[0;31mssssssssssssss\e[0mttttt\e[0;31mssssssss-k/\e[0m \n \ |
| 61 | + \e[0;31m/dmssssssss\e[0mttttt\e[0;31mssssssssssssss\e[0mttttt\e[0;31msssssss*c'\e[0m \n \ |
| 62 | + \e[0;31mh+-sssssss\e[0mttttt\e[0;31mssssssssssssss\e[0mttttt\e[0;31mssssss+a'\e[0m \n \ |
| 63 | + \e[0;31mh+-ssssss\e[0mttttt\e[0;31mssssssssssssss\e[0mttttt\e[0;31msssss:h/\e[0m \n \ |
| 64 | + \e[0;31mys.sssss\e[0mttttt\e[0;31mssssssssssssss\e[0mttttt\e[0;31mssss+-p\e[0m \n \ |
| 65 | + \e[0;31m:h/ssss\e[0mttttt\e[0;31mssssssssssssss\e[0mttttt\e[0;31msssh+-\e[0m \n \ |
| 66 | + \e[0;31m/yosssssssssssssssssssssssssssssoy+\e[0m \n \ |
| 67 | + \e[0;31m.oysssssssssssssssssssssssssssy+.\e[0m \n \ |
| 68 | + "); |
| 69 | +} |
| 70 | + |
| 71 | +void smart_fetch_get_time() |
| 72 | +{ |
| 73 | + time_t timep; |
| 74 | + time(&timep); |
| 75 | + smart_p = localtime(&timep); |
| 76 | +} |
| 77 | + |
| 78 | +int smart_fetch_get_utsname() |
| 79 | +{ |
| 80 | + memset(&smart_sys_info, '\0', sizeof(smart_sys_info)); |
| 81 | + if(uname(&smart_sys_info) != -1) |
| 82 | + { |
| 83 | + return SUCCESS_SYS_INFO; |
| 84 | + } |
| 85 | + return ERROR_SYS_INFO; |
| 86 | +} |
| 87 | + |
| 88 | +int smart_fetch_get_statfs() |
| 89 | +{ |
| 90 | + if(statfs(smart_sys_path,&smart_sys_block) != -1) |
| 91 | + { |
| 92 | + return SUCCESS_SYS_INFO; |
| 93 | + } |
| 94 | + return ERROR_SYS_INFO; |
| 95 | +} |
| 96 | + |
| 97 | +int smart_fetch_get_memory() |
| 98 | +{ |
| 99 | + char buffer[1024]; |
| 100 | + FILE *fp; |
| 101 | + fp = popen("free", "r"); |
| 102 | + if(fp < 0) |
| 103 | + { |
| 104 | + return ; |
| 105 | + } |
| 106 | + while(fgets(buffer , sizeof(buffer), fp) != NULL) |
| 107 | + { |
| 108 | + printf("%s", buffer); |
| 109 | + } |
| 110 | + pclose(fp); |
| 111 | + return SUCCESS_SYS_INFO; |
| 112 | +} |
| 113 | + |
| 114 | +static void smart_fetch_color_parser(int b_font,char *t_color) |
| 115 | +{ |
| 116 | + char color[16]; |
| 117 | + if(t_color == NULL) |
| 118 | + { |
| 119 | + return ; |
| 120 | + } |
| 121 | + memset(color, '\0', sizeof(color)); |
| 122 | + if(smart_bold_font != 0) |
| 123 | + { |
| 124 | + if((strcmp(t_color,"BLACK")) == 0) |
| 125 | + { |
| 126 | + strcpy(color,L_BLACK); |
| 127 | + } |
| 128 | + else if((strcmp(t_color,"RED")) == 0) |
| 129 | + { |
| 130 | + strcpy(color,L_RED); |
| 131 | + } |
| 132 | + else if((strcmp(t_color,"GREEN")) == 0) |
| 133 | + { |
| 134 | + strcpy(color,L_GREEN); |
| 135 | + } |
| 136 | + else if((strcmp(t_color,"YELLOW")) == 0) |
| 137 | + { |
| 138 | + strcpy(color,YELLOW); |
| 139 | + } |
| 140 | + else if((strcmp(t_color,"BLUE")) == 0) |
| 141 | + { |
| 142 | + strcpy(color,L_BLUE); |
| 143 | + } |
| 144 | + else if((strcmp(t_color,"PURPLE")) == 0) |
| 145 | + { |
| 146 | + strcpy(color,L_PURPLE); |
| 147 | + } |
| 148 | + else if((strcmp(t_color,"CYAN")) == 0) |
| 149 | + { |
| 150 | + strcpy(color,L_CYAN); |
| 151 | + } |
| 152 | + else if((strcmp(t_color,"WHITE")) == 0) |
| 153 | + { |
| 154 | + strcpy(color,WHITE); |
| 155 | + } |
| 156 | + else |
| 157 | + { |
| 158 | + strcpy(color,L_RED); |
| 159 | + } |
| 160 | + |
| 161 | + } |
| 162 | + else |
| 163 | + { |
| 164 | + if((strcmp(t_color,"BLACK")) == 0) |
| 165 | + { |
| 166 | + strcpy(color,BLACK); |
| 167 | + } |
| 168 | + else if((strcmp(t_color,"RED")) == 0) |
| 169 | + { |
| 170 | + strcpy(color,RED); |
| 171 | + } |
| 172 | + else if((strcmp(t_color,"GREEN")) == 0) |
| 173 | + { |
| 174 | + strcpy(color,GREEN); |
| 175 | + } |
| 176 | + else if((strcmp(t_color,"YELLOW")) == 0) |
| 177 | + { |
| 178 | + strcpy(color,BROWN); |
| 179 | + } |
| 180 | + else if((strcmp(t_color,"BLUE")) == 0) |
| 181 | + { |
| 182 | + strcpy(color,BLUE); |
| 183 | + } |
| 184 | + else if((strcmp(t_color,"PURPLE")) == 0) |
| 185 | + { |
| 186 | + strcpy(color,PURPLE); |
| 187 | + } |
| 188 | + else if((strcmp(t_color,"CYAN")) == 0) |
| 189 | + { |
| 190 | + strcpy(color,CYAN); |
| 191 | + } |
| 192 | + else if((strcmp(t_color,"WHITE")) == 0) |
| 193 | + { |
| 194 | + strcpy(color,GRAY); |
| 195 | + } |
| 196 | + else |
| 197 | + { |
| 198 | + strcpy(color,GREEN); |
| 199 | + } |
| 200 | + } |
| 201 | + memset(t_color, '\0', sizeof(t_color)); |
| 202 | + strcpy(t_color, color); |
| 203 | +} |
| 204 | + |
| 205 | +static void smart_fetch_color_deal() |
| 206 | +{ |
| 207 | + smart_fetch_color_parser(smart_bold_font, smart_title_color); |
| 208 | + smart_fetch_color_parser(smart_bold_font, smart_info_color); |
| 209 | +} |
| 210 | + |
| 211 | +static void smart_fetch_help() |
| 212 | +{ |
| 213 | + printf("\t\tsmart_fetch help\n"); |
| 214 | + printf("\t\t cmd\texplain\n"); |
| 215 | + printf("\t\t -t \t:update titile color\n"); |
| 216 | + printf("\t\t -c \t:update information color\n"); |
| 217 | + printf("\t\t -d \t:disable info\n"); |
| 218 | + printf("\t\t -b \t:blod font\n"); |
| 219 | + printf("\t\t example : smart-fetch -b -c YELLOW -t GREEN \n"); |
| 220 | +} |
| 221 | + |
| 222 | +int smart_fetch_param_parser(int argc, char **argv) |
| 223 | +{ |
| 224 | + char *opt; |
| 225 | + int idx; |
| 226 | + if (argc < 1) |
| 227 | + { |
| 228 | + return SUCCESS_SYS_INFO; |
| 229 | + } |
| 230 | + memset(smart_title_color, '\0', sizeof(smart_title_color)); |
| 231 | + memset(smart_info_color, '\0', sizeof(smart_info_color)); |
| 232 | + while((idx = getopt_long(argc, argv, smart_short_option, smart_long_option, NULL)) != EOF) |
| 233 | + { |
| 234 | + switch(idx) |
| 235 | + { |
| 236 | + case 't' : |
| 237 | + opt = optarg; |
| 238 | + strcpy(smart_title_color, opt); |
| 239 | + break; |
| 240 | + case 'c' : |
| 241 | + opt = optarg; |
| 242 | + strcpy(smart_info_color, opt); |
| 243 | + break; |
| 244 | + case 'd' : |
| 245 | + opt = optarg; |
| 246 | + break; |
| 247 | + case 'b' : |
| 248 | + smart_bold_font = 1; |
| 249 | + break; |
| 250 | + case 'h' : |
| 251 | + smart_fetch_help(); |
| 252 | + return ; |
| 253 | + default : |
| 254 | + break; |
| 255 | + } |
| 256 | + } |
| 257 | + smart_fetch_color_deal(); |
| 258 | + smart_fetch_get_time(); |
| 259 | + smart_fetch_get_utsname(); |
| 260 | + smart_fetch_get_statfs(); |
| 261 | + smart_fetch_print_logo(); |
| 262 | + smart_fetch_print_info(); |
| 263 | + return SUCCESS_SYS_INFO; |
| 264 | +} |
| 265 | + |
| 266 | +int main(int argc, char **argv) |
| 267 | +{ |
| 268 | + smart_fetch_param_parser(argc, argv); |
| 269 | + return 0; |
| 270 | +} |
0 commit comments