Skip to content

Commit

Permalink
Merge branch 'Master' into CheckSum
Browse files Browse the repository at this point in the history
  • Loading branch information
MikailuReeves authored Dec 21, 2023
2 parents 6efc35e + 4840abc commit cf81712
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 5 deletions.
2 changes: 2 additions & 0 deletions WebShare-Connect/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ add_executable (WebShare-Connect "WebShare-Connect.c" "WebShare-Connect.h") # Ad
add_library (sha512 "sha512.c" "sha512.h")
add_library (server "server.c" "server.h")
add_library (client "client.c" "client.h")
add_library (removeQuotes "removeQuotes.c" "removeQuotes.h")

find_package(czmq CONFIG REQUIRED)
find_package(ZeroMQ CONFIG REQUIRED)
Expand All @@ -19,6 +20,7 @@ target_link_libraries(WebShare-Connect PRIVATE sha512)
target_link_libraries(WebShare-Connect PRIVATE server)
target_link_libraries(WebShare-Connect PRIVATE client)
target_link_libraries(WebShare-Connect PRIVATE OpenSSL::SSL OpenSSL::Crypto)
target_link_libraries(WebShare-Connect PRIVATE removeQuotes)
target_link_libraries(server PRIVATE czmq)
target_link_libraries(server PRIVATE libzmq)
target_link_libraries(sha512 PRIVATE OpenSSL::SSL OpenSSL::Crypto)
Expand Down
52 changes: 48 additions & 4 deletions WebShare-Connect/WebShare-Connect.c
Original file line number Diff line number Diff line change
@@ -1,22 +1,66 @@
#include "WebShare-Connect.h"
#include "server.h"
#include "client.h"
#include "removeQuotes.h"

int main(int argc, char const* argv[])
{
//if there are no arguments then ask for them
if (argc < 2) {
printf("Usage: %s [server|client]\n", argv[0]);
return 1;
char* user_argv[5];
printf("Which one? server or client?\n");
char mode[10]; // mode[10] is an array of 10 characters
scanf("%9s", mode); // scanf("%9s", mode) reads a string of up to 9 characters from stdin and stores it in mode

if (strcmp(mode, "server") == 0 || strcmp(mode, "client") == 0) {
printf("Which port?\n");
char port[6];
scanf("%5s", port);

printf("How many threads?\n");
char threads[4];
scanf("%3s", threads);

printf("What is the file path?\n");
char filePath[256];
scanf("%255s", filePath);

user_argv[0] = strdup(argv[0]); // Program name
user_argv[1] = strdup(mode);
user_argv[2] = strdup(port);
user_argv[3] = strdup(threads);

if (containsQuotes(filePath)) {
printf("contains quotes and removing them\n");
char* newFilePath = removeQuotes(filePath);
user_argv[4] = strdup(newFilePath);
}
else user_argv[4] = strdup(filePath);

if (strcmp(mode, "server") == 0) {

return server_main(4, user_argv + 1); // 4 is the number of arguments in user_argv
}
else {
return client_main(argc, port, threads, filePath);
}
}
else {
printf("Invalid mode entered.\n");
return 1;
}

}

if (strcmp(argv[1], "server") == 0) {
return server_main(argc - 1, argv + 1);
return server_main(argc - 1, argv + 1); //what does argv + 1 do? a: it is a pointer to the next element in the array of arguments
}
else if (strcmp(argv[1], "client") == 0) {
return client_main(argc - 1, argv + 1);
}
else {
printf("---------------------\n");
printf("Usage: %s [server|client]\n", argv[0]);
return 1;
return 1;
}
}
37 changes: 37 additions & 0 deletions WebShare-Connect/removeQuotes.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include <stdlib.h>
#include <stdio.h>
#include "removeQuotes.h"


int containsQuotes(const char* str) {
while (*str != '\0') {
if (*str == '"' || *str == '\'') {
return 1;
}
str++;
}
return 0;
}

char* removeQuotes(const char* str) {
size_t len = strlen(str);
char* newStr = (char*)malloc(len + 1); // Allocate memory for the modified string
if (newStr == NULL) {
fprintf(stderr, "Memory allocation failed\n");
exit(EXIT_FAILURE);
}

const char* current = str; // Pointer for iteration
char* newStrPtr = newStr; // Pointer to build the modified string

while (*current != '\0') {
if (*current != '"' && *current != '\'') {
*newStrPtr = *current;
newStrPtr++;
}
current++;
}
*newStrPtr = '\0'; // Null-terminate the modified string

return newStr;
}
2 changes: 2 additions & 0 deletions WebShare-Connect/removeQuotes.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
int containsQuotes(const char* str);
char *removeQuotes(char* str);
4 changes: 3 additions & 1 deletion WebShare-Connect/server.c
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,8 @@ void server_send(zsock_t* serv_sock, const char* file_path)
chunk_size = CHUNK_SIZE_50GB;
}
else {
chunk_size = pow(2, 24);
// 2^24 = 16 MiB
chunk_size = 1 << 24;
}

printf(" Chunk size: %d bytes\n", chunk_size);
Expand Down Expand Up @@ -162,6 +163,7 @@ int server_main(int argc, char const* argv[]) {
const char* port = argv[1];
int threads = atoi(argv[2]);
const char* file_path = argv[3];
printf("Port: %s\nThreads: %d\nFile path: %s\n", port, threads, file_path);

void* context = zmq_ctx_new();
assert(context);
Expand Down

0 comments on commit cf81712

Please sign in to comment.