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.c79
1 files changed, 71 insertions, 8 deletions
diff --git a/03_exercise/srv/shell.c b/03_exercise/srv/shell.c
index 5d7d33d..7a90945 100644
--- a/03_exercise/srv/shell.c
+++ b/03_exercise/srv/shell.c
@@ -18,13 +18,13 @@ char *get_current_dir_name(void);
process *processes;
-int write_all(int sock, char *buffer, int size) {
+int write_all(int target_sock, char *buffer, size_t size) {
int nwrite, sent = 0;
while (sent < size) {
- if ((nwrite = write(sock, buffer + sent, size - sent)) < 0) {
+ if ((nwrite = write(target_sock, buffer + sent, size - sent)) < 0) {
perror("Write");
- exit(1);
+ return -1;
}
sent += nwrite;
@@ -54,11 +54,60 @@ void receive_file(int sock, char *path, int size) {
}
if (write_all(file_fd, buffer, strlen(buffer) + 1) == strlen(buffer) + 1) {
- printf("successfully wrote to remote file \"%s/%s\"\n", get_current_dir_name(), path);
+ printf("successfully wrote to remote file \"%s/%s\"", get_current_dir_name(), path);
+ printf("\n");
+ } else {
+ printf("error writing remote file \"%s/%s\"", get_current_dir_name(), path);
+ printf("\n");
}
+
close(file_fd);
}
+void send_file(int client_fd, char *path) {
+ printf("Trying to get \"%s\" from remote...\n", path);
+ char buffer[BUF_SIZE];
+ memset(buffer, 0, BUF_SIZE);
+ int tmp_fd, file_fd;
+
+ if ((tmp_fd = open(path, O_RDONLY)) == -1) {
+ perror("Open");
+ return;
+ }
+ FILE *file = fdopen(tmp_fd, "r");
+ fseek(file, 0L, SEEK_END);
+ long int sz = ftell(file);
+ fseek(file, 0L, SEEK_SET);
+ fclose(file);
+ close(tmp_fd);
+ if ((file_fd = open(path, O_RDONLY)) == -1) {
+ perror("Open");
+ return;
+ }
+
+ int length = snprintf(NULL, 0, "%ld", sz);
+ sprintf(buffer, "<<!%ld!", sz);
+ sprintf(&(buffer[3 + length + 1]), "%s\n", path);
+
+ write(client_fd, buffer, strlen(buffer));
+
+ usleep(150);
+
+ memset(buffer, 0, BUF_SIZE);
+
+ errno = 0;
+ while (read(file_fd, buffer, sizeof(buffer)) > 0) {
+ /*printf("Sent %i bytes\n", */write_all(client_fd, buffer, strlen(buffer) + 1)/*)*/;
+ }
+
+ if (errno)
+ perror("wad");
+
+ close(file_fd);
+
+ //printf("done.\n");
+}
+
void signal_handler(int signal) {
if (signal == SIGINT) {
for (size_t i = 0; i < arrayLen(processes); ++i) {
@@ -121,8 +170,9 @@ int shell(int in_fd) {
"fg: `fg pid` - pulls a process from the background back in the foreground\n"
"\n"
"You can put processes in the background using `&`\n"
- "And with `|` you can pipe the output from one process to the input of another\n"
+ "And with `|` you can pipe the output from one process to the input of another"
);
+ printf("\n$> ");
char const *const original_wd = get_current_dir_name();
//char const *prompt = relative_path(original_wd, original_wd);
@@ -158,6 +208,7 @@ int shell(int in_fd) {
if (strcmp(processes[0].argv[0], "cd") == 0) {
if (arrayLen(processes) != 1) {
perror("Can't chain cd with other processes");
+ printf("\n");
}
int ret;
switch (arrayLen(processes[0].argv)) {
@@ -168,18 +219,28 @@ int shell(int in_fd) {
ret = chdir(original_wd);
break;
default:
- fprintf(stderr, "usage: cd <path>\n");
+ fprintf(stderr, "usage: cd <path>");
+ fprintf(stderr, "\n");
fflush(stderr);
ret = -1;
}
if (ret)
printf("[%i] ", ret);
- else
- printf("%s\n", get_current_dir_name());
+ else {
+ printf("%s", get_current_dir_name());
+ printf("\n");
+ }
} else if (strcmp(processes[0].argv[0], "exit") == 0) {
done = true;
+ } else if (strcmp(processes[0].argv[0], "get") == 0) {
+ if (arrayLen(processes[0].argv) != 3) {
+ printf("please provide a file name to get!");
+ printf("\n");
+ } else {
+ send_file(in_fd, processes[0].argv[1]);
+ }
} else if (strcmp(processes[0].argv[0], "wait") == 0) {
builtin_wait(processes[0], false);
} else if (strcmp(processes[0].argv[0], "fg") == 0) {
@@ -194,12 +255,14 @@ int shell(int in_fd) {
/*if (ret)
printf("[%i] ", ret);*/
}
+ printf("\n");
}
free_processes(&processes);
}
printf("Disconnecting...");
+ printf("\n");
free((void *) original_wd);