Compare commits

...

5 Commits

6 changed files with 102 additions and 53 deletions

1
.gitignore vendored
View File

@@ -1 +1,2 @@
main main
user_file.txt

51
file_helpers.c Normal file
View File

@@ -0,0 +1,51 @@
#include <stdlib.h>
#include <stdio.h>
#include "file_helpers.h"
char* file_to_string(char* filename) {
FILE* fp = fopen(filename,"r");
fseek(fp,0,SEEK_END);
long file_size = ftell(fp);
rewind(fp);
char* buffer = calloc(sizeof(char),file_size+1);
fread(buffer,1,file_size,fp);
fclose(fp);
return buffer;
}
/* Finds the number of lines in a file (Which MUST end in a new-line, if the last
line is to be counted) */
int num_of_lines(char* filename) {
int num_lines = 0;
FILE* fp = fopen(filename,"r");
if (fp == NULL) {
return -1;
}
for (int c = getc(fp); c != EOF; c = getc(fp)) {
if (c == '\n') {
num_lines++;
}
}
fclose(fp);
return num_lines;
}

11
file_helpers.h Normal file
View File

@@ -0,0 +1,11 @@
/* Takes a string that represents a filename, and converts the contents
of the file into a string */
char* file_to_string(char* filename);
/*Finds the number of new-line characters in the given file */
int num_of_lines(char* filename);

69
main.c
View File

@@ -1,6 +1,3 @@
#include "message.h"
#include "user.h"
#include "easysock.h"
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@@ -8,6 +5,12 @@
#include <assert.h> #include <assert.h>
#include <unistd.h> #include <unistd.h>
#include "message.h"
#include "user.h"
#include "easysock.h"
#include "file_helpers.h"
#include "message_helpers.h"
#define BUFFER_SIZE 10000 #define BUFFER_SIZE 10000
#define MAX_CONNECTIONS 100 #define MAX_CONNECTIONS 100
@@ -21,6 +24,7 @@ int main() {
fd_set read_fd_set; fd_set read_fd_set;
int conn_sockets[MAX_CONNECTIONS] = {-1}; int conn_sockets[MAX_CONNECTIONS] = {-1};
char* dest_address[MAX_CONNECTIONS] = {NULL};
FD_ZERO(&read_fd_set); FD_ZERO(&read_fd_set);
char buffer[BUFFER_SIZE]; char buffer[BUFFER_SIZE];
User* users = create_user_list("user_file.txt"); User* users = create_user_list("user_file.txt");
@@ -58,10 +62,17 @@ int main() {
if (num_bytes_read <= 0) { if (num_bytes_read <= 0) {
close(conn_sockets[i]); close(conn_sockets[i]);
conn_sockets[i] = 0; conn_sockets[i] = 0;
dest_address[i] = NULL;
} else { } else {
for (int i=0;i<num_bytes_read;i++) { if (dest_address[i] == NULL) {
printf("%c",buffer[i]); dest_address[i] = fetch_address(buffer);
} }
// for (int i=0;i<num_bytes_read;i++) {
// printf("%c",buffer[i]);
// }
} }
@@ -106,51 +117,3 @@ User* create_user_list(char* filename) {
return users; return users;
} }
char* file_to_string(char* filename) {
FILE* fp = fopen(filename,"r");
fseek(fp,0,SEEK_END);
long file_size = ftell(fp);
rewind(fp);
char* buffer = calloc(sizeof(char),file_size+1);
fread(buffer,1,file_size,fp);
fclose(fp);
return buffer;
}
/* Finds the number of lines in a file (Which MUST end in a new-line, if the last
line is to be counted) */
int num_of_lines(char* filename) {
int num_lines = 0;
FILE* fp = fopen(filename,"r");
if (fp == NULL) {
return -1;
}
for (int c = getc(fp); c != EOF; c = getc(fp)) {
if (c == '\n') {
num_lines++;
}
}
fclose(fp);
return num_lines;
}

17
message_helpers.c Normal file
View File

@@ -0,0 +1,17 @@
#include <easysock.h>
#include "message_helpers.h"
char* fetch_address(char* message) {
char* token = malloc (sizeof(char) * strlen(message));
token = strtok(message," ");
if (strcmp(token,"TO:") == 0) {
token = strtok(NULL,"");
}
if (check_ip_ver(token) == -1) {
return NULL;
} else {
return token;
}
}

6
message_helpers.h Normal file
View File

@@ -0,0 +1,6 @@
/* If the message contains a string of the form:
TO: <Username>
then return 'Username' */
char* fetch_address(char* message);