-
Notifications
You must be signed in to change notification settings - Fork 0
/
btrm.c
280 lines (235 loc) · 8.11 KB
/
btrm.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <windows.h>
#include <direct.h>
#include <errno.h>
#include <ctype.h>
#include <math.h>
#include <time.h>
#include <Psapi.h>
#include "colors.h"
#include "errhand.h"
#include "resource.h"
#ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
#endif // If your PC supports 'ENABLE_VIRTUAL_TERMINAL_PROCESSING', delete it.
// I recommend not deleting anything of this mess above because you can even screw the ANSI function.
#define EXIT_SUCCESS 0
#define MAX_COMMAND_LENGTH 65536
#define PROMPT ">> "
// --Terminal Settings--
void enableANSI() { // Enables ANSI if running on unsupported ANSI program
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
DWORD dwMode = 0;
GetConsoleMode(hOut, &dwMode);
dwMode |= ENABLE_VIRTUAL_TERMINAL_PROCESSING;
SetConsoleMode(hOut, dwMode);
}
// --Commands--
void shutdownSystem() {
char input[10];
// Step 1: Warn the user
printf("WARNING: This action will shut down the system!\n");
printf("Do you want to proceed? (Y/N): ");
fgets(input, 10, stdin);
// Remove newline character from input
input[strcspn(input, "\n")] = 0;
// Step 2: Check for confirmation
if (strcmp(input, "Y") == 0) {
changeColor("red", NULL);
printf("Shutting down the system...\n");
printf("15-second timeout (if you need to save things)\n");
system("shutdown /s /f /t 15");
} else {
printf("Shutdown canceled.\n");
}
}
void showMEMUsage() {
PROCESS_MEMORY_COUNTERS memInfo;
// Get memory usage information
if (GetProcessMemoryInfo(GetCurrentProcess(), &memInfo, sizeof(memInfo))) {
printf("Current memory used: %lu bytes\n", (unsigned long)memInfo.WorkingSetSize);
} else {
printf("Unable to retrieve memory usage information.\n");
eexit();
errmsg("Unable to get memInfo.WorkingSetSize data.\nError code: -1.");
}
}
void changeDirectory(const char *path) {
if (_chdir(path) != 0) {
perror("Error changing directory");
} else {
printf("Changed directory to: %s\n", path);
}
}
void currentDate() {
time_t rawtime;
struct tm *timeinfo;
char buffer[30]; // Buffer to hold the formatted date string
// Get the current date and time
time(&rawtime);
timeinfo = localtime(&rawtime);
// Format the date as "DD-MM-YYYY, Weekday DD"
strftime(buffer, sizeof(buffer), "%d-%m-%Y, %A %d", timeinfo);
// Print the formatted date
printf("Current date: %s\n", buffer);
}
void currentTime() {
time_t rawtime;
struct tm *timeinfo;
char buffer[10];
char meridiem[3]; // To hold "AM" or "PM"
// Get the current time
time(&rawtime);
timeinfo = localtime(&rawtime);
// Check if it's AM or PM
if (timeinfo->tm_hour >= 12) {
strcpy(meridiem, "PM");
if (timeinfo->tm_hour > 12) {
timeinfo->tm_hour -= 12; // Convert to 12-hour format
}
} else {
strcpy(meridiem, "AM");
if (timeinfo->tm_hour == 0) {
timeinfo->tm_hour = 12; // Midnight edge case for 12-hour format
}
}
// Format the time as HH:MM:SS
strftime(buffer, sizeof(buffer), "%I:%M:%S", timeinfo);
// Print the formatted time with AM/PM
printf("Current time: %s %s\n", buffer, meridiem);
}
void clearScreen() {
printf("\033[2J\033[H");
}
void readFile(const char *filename) {
FILE *file = fopen(filename, "rb"); // Open in binary mode to support any file type
if (file == NULL) {
perror("Error opening file");
return;
}
printf("Reading file: %s\n", filename);
char buffer[1024];
size_t bytesRead;
while (fgets(buffer, sizeof(buffer), file) != NULL) {
printf("%s", buffer);
}
printf("\n");
fclose(file);
}
void writeFile(const char *filename, const char *content) {
FILE *pF = fopen(filename, "a"); // Open in append mode
if (pF == NULL) {
perror("Error opening file");
return;
}
fprintf(pF, "%s\n", content); // Append the content with a new line
fflush(pF); // Ensure all data is written to the file
fclose(pF);
printf("Content written to %s\n", filename);
}
void flushinb() {
int c;
while ((c = getchar()) != '\n' && c != EOF) {
}
}
// --Handlers--
void handleCommand(char *command) {
char *arg1 = strtok(command, " "); // Get the command
if (arg1 == NULL) {
printf("Error: No command entered.\n");
return;
}
if (strcmp(arg1, "help") == 0) {
printf("Available commands: help, read, write, gotodir, col, print, error, warning, info, memuse, exit.\n");
} else if (strcmp(arg1, "read") == 0) {
char *filename = strtok(NULL, ""); // Capture the entire remaining string as the filename
if (filename == NULL) {
printf("Error: Filename is missing.\n");
} else {
readFile(filename); // Pass the filename to the function
}
} else if (strcmp(arg1, "write") == 0) {
char *filename = strtok(NULL, " ");
char *content = strtok(NULL, "");
if (filename == NULL || content == NULL) {
printf("Error: Missing filename or content for write command.\n");
} else {
writeFile(filename, content);
}
} else if (strcmp(arg1, "gotodir") == 0) {
char *path = strtok(NULL, "");
if (path == NULL) {
printf("Error: Directory path is missing.\n");
} else {
changeDirectory(path);
}
} else if (strcmp(arg1, "col") == 0) {
char *color = strtok(NULL, " ");
char *bgColor = strtok(NULL, " "); // Background color is optional
if (color) {
if (bgColor) {
changeColor(color, bgColor); // If both color and background are provided
} else {
changeColor(color, NULL); // Only change the text color if background is not provided
}
} else {
printf("Error: Text color is missing.\n");
}
} else if (strcmp(arg1, "exit") == 0) {
exit(0); // Exit the program
} else if (strcmp(command, "clear") == 0){
clearScreen();
} if (strncmp(command, "print ", 6) == 0) {
char *text = strtok(NULL, "");
if (text != NULL) {
printf("%s\n", text); // Output the text after "print"
}
} else if (strcmp(command, "time") == 0){
currentTime();
} else if (strcmp(command, "date") == 0){
currentDate();
} else if (strcmp(command, "memuse") == 0){
showMEMUsage();
} else if (strcmp(command, "intoverflow") == 0){
signed int overflowmax = pow(2, 16);
printf("integer overflow = %d\n", overflowmax);
} else if (strcmp(command, "shutdown") == 0){
shutdownSystem();
} else if (strcmp(command, "error") == 0){
errmsg("Error triggered by user\nError code: 0");
} else if (strcmp(command, "warning") == 0){
warnmsg("Warning triggered by user");
} else if (strcmp(command, "info") == 0){
infomsg("Information triggered by user\ninfo ._.");
} else {
printf(NULL);
}
}
// --Terminal--
void handleOverflow(char *command) {
if (strlen(command) > MAX_COMMAND_LENGTH) {
// If command length exceeds the limit, clear the input
printf("Error: Command too long. (%d)\n", sizeof(command));
strcpy(command, ""); // Reset the command buffer
}
}
void prompt(){
printf(PROMPT);
}
int main() {
char command[16384];
enableANSI();
SetConsoleTitleA("FishOS BTerminal");
printf("BTerminal 1.6.8b\n");
printf("Type \"help\" for all commands.\n");
while (1) {
prompt();
fgets(command, sizeof(command), stdin);
command[strcspn(command, "\n")] = 0;
handleOverflow(command);
handleCommand(command);
}
return 0;
}