summaryrefslogtreecommitdiffstats
path: root/03_exercise/srv/shell.c
diff options
context:
space:
mode:
Diffstat (limited to '03_exercise/srv/shell.c')
-rw-r--r--03_exercise/srv/shell.c72
1 files changed, 55 insertions, 17 deletions
diff --git a/03_exercise/srv/shell.c b/03_exercise/srv/shell.c
index 449905b..aea1a68 100644
--- a/03_exercise/srv/shell.c
+++ b/03_exercise/srv/shell.c
@@ -38,24 +38,21 @@ void receive_file(int sock, char *path, int size) {
if ((file_fd = open(path, O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1) { perror("Open"); }
- while (true) {
- received = 0;
-
- /* Read all data */
- while (received < size) {
- if ((nread = read(sock, buffer + received, size - received)) < 0) {
- perror("Read");
- //pthread_exit(NULL);
- exit(1);
- }
+ received = 0;
- received += nread;
+ /* Read all data */
+ while (received < size) {
+ if ((nread = read(sock, buffer + received, size - received)) < 0) {
+ perror("Read");
+ //pthread_exit(NULL);
+ exit(1);
}
- if (strncmp(buffer, "end", 4) == 0) { break; }
-
- write_all(file_fd, buffer, strlen(buffer) + 1);
+ received += nread;
}
+
+ write_all(file_fd, buffer, strlen(buffer) + 1);
+ close(file_fd);
}
void signal_handler(int signal) {
@@ -69,6 +66,44 @@ void signal_handler(int signal) {
}
}
+bool check_put(char *buffer, int sock) {
+ // "<<!bytecount!name"
+ char cmd[4];
+ char rest[BUF_SIZE - 3];
+ size_t file_size;
+
+ memcpy(cmd, buffer, 3);
+ cmd[3] = 0;
+
+ if (strcmp(cmd, "<<!") == 0) {
+ int offset = 3;
+ int i = offset;
+ for (; i < BUF_SIZE; ++i) {
+ if (buffer[i] == '!')
+ break;
+ rest[i - offset] = buffer[i];
+ }
+ rest[i - offset] = 0;
+ file_size = atoi(rest);
+
+ offset = ++i;
+ for (; i < BUF_SIZE; ++i) {
+ if (buffer[i] == '!')
+ break;
+ rest[i - offset] = buffer[i];
+ }
+ rest[i - offset] = 0;
+ char path[strlen(rest) + 1];
+ memcpy(path, rest, strlen(rest));
+ path[strlen(rest)] = 0;
+
+ receive_file(sock, path, file_size);
+
+ return true;
+ }
+ return false;
+}
+
int shell(int in_fd) {
setvbuf(stderr, NULL, _IONBF, 0);
setvbuf(stdout, NULL, _IONBF, 0);
@@ -85,7 +120,7 @@ int shell(int in_fd) {
"And with `|` you can pipe the output from one process to the input of another\n"
);
- char const *const original_wd = get_current_dir_name();
+ char const *const original_wd = ".";//get_current_dir_name();
//char const *prompt = relative_path(original_wd, original_wd);
bool done = false;
@@ -110,6 +145,9 @@ int shell(int in_fd) {
continue;
}
+ if (check_put(line, in_fd))
+ continue;
+
processes = NULL;
parse_line(line, &processes);
@@ -134,7 +172,7 @@ int shell(int in_fd) {
if (ret)
printf("[%i] ", ret);
else
- printf("%s\n", get_current_dir_name());
+ printf("%s\n", "."/*get_current_dir_name()*/);
} else if (strcmp(processes[0].argv[0], "exit") == 0) {
done = true;
@@ -159,7 +197,7 @@ int shell(int in_fd) {
printf("Disconnecting...");
- free((void *) original_wd);
+ //free((void *) original_wd);
return 0;
}