-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathproject.c
165 lines (134 loc) · 3.81 KB
/
project.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
#include <tchar.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h>
#include <sys/stat.h>
// define the size of arguments, command
#define MAX_INPUT_SIZE 1024
#define MAX_ARGS 32
#define MAX_ARG_SIZE 64
char *sanitize_string(const char *str) {
if (str == NULL) {
return NULL;
}
const char* whitelist = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_-.";
char* output = malloc(strlen(str) + 1);
char* pointer = output;
for (const char *c = str; *c != '\0'; c++) {
if (strchr(whitelist, *c) != NULL) {
*pointer = *c;
pointer++;
}
}
*pointer = '\0';
return output;
}
// command functions
void _touch(char* args);
// command function pointer
typedef void (*command_func_t)(char* args);
// file structure
typedef struct {
wchar_t *name;
wchar_t *content;
float size;
} File;
// command structure
typedef struct {
char* name;
command_func_t fn;
} Command;
// command functions implementation
void _touch(char* args) {
HANDLE file;
SYSTEMTIME sysTime;
FILETIME fileTime;
char* filename = strtok(args, " ");
char* option = strtok(NULL, " ");
char* value = strtok(NULL, " ");
// sanitize the filename, option and value
char* sanitized_filename = sanitize_string(filename);
char* sanitized_option = sanitize_string(option);
char* sanitized_value = sanitize_string(value);
// create the file
struct _stat fileinfo;
DWORD disposition = CREATE_NEW;
if (_stat(sanitized_filename, &fileinfo) == 0) {
disposition = OPEN_EXISTING;
}
file = CreateFile(sanitized_filename, GENERIC_WRITE, 0, NULL, disposition, FILE_ATTRIBUTE_NORMAL, NULL);
if (file == INVALID_HANDLE_VALUE) {
fprintf(stderr, "Error: Unable to create file %s\n", sanitized_filename);
free(sanitized_filename);
free(sanitized_option);
free(sanitized_value);
return;
}
if (sanitized_option != NULL && strcmp(sanitized_option, "--content") == 0 && sanitized_value != NULL) {
DWORD bytesWritten;
if (disposition == OPEN_EXISTING) {
SetFilePointer(file, 0, NULL, FILE_END);
}
if (!WriteFile(file, sanitized_value, strlen(sanitized_value), &bytesWritten, NULL)) {
fprintf(stderr, "Error: Unable to write to file %s\n", sanitized_filename);
free(sanitized_filename);
free(sanitized_option);
free(sanitized_value);
CloseHandle(file);
return;
}
WriteFile(file, "\n", 1, &bytesWritten, NULL);
}
GetSystemTime(&sysTime);
SystemTimeToFileTime(&sysTime, &fileTime);
SetFileTime(file, &fileTime, &fileTime, &fileTime);
CloseHandle(file);
free(sanitized_filename);
free(sanitized_option);
free(sanitized_value);
}
// list of commands
Command commands[] = {
{"touch", _touch},
{NULL, NULL}
};
// command finder function
command_func_t find_command_func(const char* cmd) {
for(int i = 0; commands[i].name != NULL; i++) {
if (strcmp(commands[i].name, cmd) == 0) {
return commands[i].fn;
}
}
return NULL;
}
int main() {
char input[MAX_INPUT_SIZE];
char arguments[MAX_INPUT_SIZE];
int num_args = 0;
while (1)
{
printf("> ");
fflush(stdout);
// read the user input into the command buffer
if (fgets(input, MAX_INPUT_SIZE, stdin) == NULL) {
exit(EXIT_FAILURE);
}
// tokenize the input using the space
char* token = strtok(input, " \n");
// if the exit command is entered, break the loop
if (strcmp(token, "exit") == 0) {
break;
}
// get the user command
command_func_t function = find_command_func(token);
if (function == NULL) {
fprintf(stderr, "The command you entered is unkown to me!!", NULL, 30);
} else {
strncpy(arguments, token + strlen(token) + 1, MAX_INPUT_SIZE - 1);
arguments[MAX_INPUT_SIZE - 1] = '\0';
function(arguments);
}
}
return 0;
};