11 int client(
const char* address) {
13 char buffer[command_buffer_lenght];
14 printf(
"Connection made. socket: %d\n", sockd);
17 char command[command_buffer_lenght];
20 if (strcmp(HELP, command) == 0) {
22 fprintf(stderr,
"Error while sending help command. Abort!\n");
26 }
else if (strcmp(QUIT, command) == 0) {
29 "Error while sending quit command. Restart server! Abort!\n");
33 }
else if (strcmp(INIT, command) == 0) {
35 fprintf(stderr,
"Error while sending init command. Abort!\n");
39 }
else if (strcmp(READ_FS, command) == 0) {
41 fprintf(stderr,
"Error while sending read_fs command. Abort!\n");
45 }
else if (strcmp(LS, command) == 0) {
46 if (first_arg_pos == NULL || strlen(first_arg_pos) == 0) {
47 printf(
"Ls requires path\n");
50 char path[command_buffer_lenght];
53 fprintf(stderr,
"Error while sending ls command. Abort!\n");
57 }
else if (strcmp(MKDIR, command) == 0) {
58 if (first_arg_pos == NULL || strlen(first_arg_pos) == 0) {
59 printf(
"Mkdir requires path\n");
62 char path[command_buffer_lenght];
65 fprintf(stderr,
"Error while sending mkdir command. Abort!\n");
69 }
else if (strcmp(TOUCH, command) == 0) {
70 if (first_arg_pos == NULL || strlen(first_arg_pos) == 0) {
71 printf(
"Touch requires path\n");
74 char path[command_buffer_lenght];
77 fprintf(stderr,
"Error while sending touch command. Abort!\n");
81 }
else if (strcmp(OPEN, command) == 0) {
82 if (first_arg_pos == NULL || strlen(first_arg_pos) == 0) {
83 printf(
"Open requires path\n");
86 char path[command_buffer_lenght];
89 fprintf(stderr,
"Error while sending open command. Abort!\n");
93 }
else if (strcmp(CLOSE, command) == 0) {
94 if (first_arg_pos == NULL || strlen(first_arg_pos) == 0) {
95 printf(
"Close requires fd\n");
98 char fd_to_close_text[command_buffer_lenght];
100 uint16_t fd_to_close = strtol(fd_to_close_text, NULL, 10);
103 fprintf(stderr,
"Error while sending close command. Abort!\n");
107 }
else if (strcmp(LSEEK, command) == 0) {
108 if (first_arg_pos == NULL || strlen(first_arg_pos) == 0) {
109 printf(
"Lseek requires fd\n");
112 char fd_to_seek_text[command_buffer_lenght];
113 char* second_arg_position =
parse_command(first_arg_pos, fd_to_seek_text);
114 uint16_t fd_to_seek = strtol(fd_to_seek_text, NULL, 10);
116 if (second_arg_position == NULL || strlen(second_arg_position) == 0) {
117 printf(
"Lseek requires position\n");
121 char pos_text[command_buffer_lenght];
123 uint32_t pos = strtol(pos_text, NULL, 10);
126 fprintf(stderr,
"Error while sending lseek command. Abort!\n");
130 }
else if (strcmp(WRITE, command) == 0) {
131 if (first_arg_pos == NULL || strlen(first_arg_pos) == 0) {
132 printf(
"Write requires fd\n");
136 char fd_to_write_text[command_buffer_lenght];
137 char* second_arg_position =
139 uint16_t fd_to_write = strtol(fd_to_write_text, NULL, 10);
141 if (second_arg_position == NULL || strlen(second_arg_position) == 0) {
142 printf(
"Write requires data\n");
145 char data[command_buffer_lenght];
148 fprintf(stderr,
"Error while sending write command. Abort!\n");
152 }
else if (strcmp(READ, command) == 0) {
153 char fd_to_read_text[command_buffer_lenght];
154 char* second_arg_position =
parse_command(first_arg_pos, fd_to_read_text);
155 uint16_t fd_to_read = strtol(fd_to_read_text, NULL, 10);
157 if (second_arg_position == NULL || strlen(second_arg_position) == 0) {
158 printf(
"Read requires size\n");
162 char size_to_read_text[command_buffer_lenght];
164 uint32_t size = strtol(size_to_read_text, NULL, 10);
167 fprintf(stderr,
"Error while sending read command. Abort!\n");
171 }
else if (strcmp(WRITE_FROM, command) == 0) {
172 if (first_arg_pos == NULL || strlen(first_arg_pos) == 0) {
173 printf(
"Write from requires fd\n");
177 char fd_to_write_text[command_buffer_lenght];
178 char* second_arg_position =
180 uint16_t fd_to_write = strtol(fd_to_write_text, NULL, 10);
182 if (second_arg_position == NULL || strlen(second_arg_position) == 0) {
183 printf(
"Write from requires path\n");
186 char path[command_buffer_lenght];
190 fprintf(stderr,
"Error while sending write from command. Abort!\n");
194 }
else if (strcmp(READ_TO, command) == 0) {
195 if (first_arg_pos == NULL || strlen(first_arg_pos) == 0) {
196 printf(
"Read to requires fd\n");
200 char fd_to_write_text[command_buffer_lenght];
201 char* second_arg_position =
203 uint16_t fd_to_write = strtol(fd_to_write_text, NULL, 10);
205 if (second_arg_position == NULL || strlen(second_arg_position) == 0) {
206 printf(
"Read to requires path\n");
209 char path[command_buffer_lenght];
210 char* third_argument_pos =
parse_command(second_arg_position, path);
212 if (third_argument_pos == NULL || strlen(third_argument_pos) == 0) {
213 printf(
"Read to requires size\n");
217 char size_text[command_buffer_lenght];
219 uint32_t size = strtol(size_text, NULL, 10);
221 fprintf(stderr,
"Error while sending read to command. Abort!\n");
226 printf(
"Unknown command\n");
Contains handlers for commands from server.
bool send_close_command(int sockd, int fd)
bool send_mkdir_command(int sockd, const char *path)
bool send_read_command(int sockd, int fd, size_t size)
bool send_lseek_command(int sockd, int fd, int size)
Contains some useful methods.
bool send_quit_command(int sockd)
bool send_read_fs_command(int sockd)
bool send_open_command(int sockd, const char *path)
bool send_write_command(int sockd, int fd, char *data, size_t size)
bool send_write_from_command(int sockd, int fd, const char *path)
char * parse_command(char *command_buffer, char *command)
Parse command Parse command to command and args. Put command to arg command and return position of fi...
void read_command_from_stdin(char *command_buffer, size_t buffer_size)
Read line from stdin.
bool send_init_command(int sockd)
void close_connection(int sockd)
bool send_ls_command(int sockd, const char *path)
bool send_help_command(int sockd)
bool send_read_to_command(int sockd, int fd, const char *path, size_t size)
bool send_touch_command(int sockd, const char *path)
int make_connection(const char *address)