<commit_before>#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <fcntl.h>
#include <time.h>
#include <sys/types.h> 
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/socket.h>
#include <sys/sendfile.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <openssl/md5.h>

#define BUFFERSIZE 1024
#define FTOKPATH "/home/jenkins"

struct shared_data {
	int recording;
	char stage[32];
	time_t update_time;
};

char *str2md5(const char *str, int length) {
	int n;
	MD5_CTX c;
	unsigned char digest[16];
	char *out = (char*)malloc(33);

	MD5_Init(&c);

	while (length > 0) {
		if (length > 512) {
			MD5_Update(&c, str, 512);
		} else {
			MD5_Update(&c, str, length);
		}
		length -= 512;
		str += 512;
	}

	MD5_Final(digest, &c);

	for (n = 0; n < 16; ++n) {
		snprintf(&(out[n*2]), 16*2, "%02x", (unsigned int)digest[n]);
	}

    return out;
}

void error(char *msg) {
	perror(msg);
	exit(1);
}

int ensure_file_path(char *file_path){
	char command[256];
	sprintf(command, "mkdir -p $(dirname %s)", file_path);
	system(command);
}

int is_eom(char *message){
	if (strlen(message) == 4){
		if (strstr(message, "end\n") != NULL){
			return 1;
		}
	}
	if (strstr(message, "\nend\0") != NULL){
		return 1;
	}
	return 0;
}

struct shared_data *get_shmem(){
	key_t key;
	int shmid;
	struct shared_data *data_ptr;

	if ((key = ftok(FTOKPATH, 'R')) == -1) {
		error("ERROR creating shared memmory key");
	}

	if ((shmid = shmget(key, sizeof(struct shared_data), 0644 | IPC_CREAT)) == -1) {
		error("ERROR getting the shared memmory");
	}

	data_ptr = (struct shared_data *)shmat(shmid, (void *)0, 0);
	if (data_ptr == (struct shared_data *)(-1)) {
		error("ERROR attaching to shared memmory segment");
	}

	return data_ptr;
}

void list_config(struct shared_data *data){
	struct tm *timeinfo;
	timeinfo = localtime(&(data->update_time));
	if (data->recording){
		printf("=> mode: record\n");
	} else {
		printf("=> mode: play\n");
	}
	printf("=> stage: %s\n", data->stage);
	printf("=> update time: %s", asctime(timeinfo));
}

void set_default_config (struct shared_data *data) {
	strcpy(data->stage, str2md5("default", 7));
	data->recording = 0;
	data->update_time = time(NULL);
	list_config(data);
}

void set_stage(struct shared_data *data, char value[100]){
	strcpy(data->stage, str2md5(value, strlen(value)));
	data->update_time = time(NULL);
	list_config(data);
} 

void set_mode(struct shared_data *data, char value[20]){
	if (strcmp(value, "play") == 0){
		data->recording = 0;
	} else if (strcmp(value, "record") == 0){
		data->recording = 1;
	} else {
		error("ERROR unknown mode");
	}
	data->update_time = time(NULL);
	list_config(data);
} 

int main(int argc, char **argv) {
	int optval = 1, n, reading = 1, connection_counter;
	int fd_parent, fd_child, fd_trans, fd_dump, clientlen;
	off_t sendfile_offset;
	struct sockaddr_in server_addr, client_addr, trans_addr;
	struct hostent *hostp, *trans;
	struct stat stat_buf;
	char buffer_in[BUFFERSIZE], buffer_out[BUFFERSIZE], file_path[128];
	char *hostaddrp, *trans_host;
	FILE *fd_in, *fd_out;
	int mock_port, trans_port;

	static const char *base_path="/tmp/mock-server/trans";
	struct shared_data *shmem_data = get_shmem();

	// arguments list
	// set stage "stage name"
	// set mode [play|record]
	// get config
	// server "port" "trans host" "trans port"
	
	if (argc > 1){
		if (strcmp(argv[1], "server") == 0){
			if (argc == 5){
				mock_port = atoi(argv[2]);
				trans_host = argv[3];
				trans_port = atoi(argv[4]);
				shmem_data->recording = 1;
				printf("0> tunnel: %i:%s:%i\n", mock_port, trans_host, trans_port);
			}
			else if (argc == 3){
				mock_port = atoi(argv[2]);
				shmem_data->recording = 0;
				printf("0> player: %i\n", mock_port);
			}
			else {
				error("ERROR wrong number of parameter for server");
			}
		}
		else if (strcmp(argv[1], "set") == 0){<commit_msg>streamsRewrite - change the FTOK key to $HOME<commit_after>#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <netdb.h>
#include <fcntl.h>
#include <time.h>
#include <sys/types.h> 
#include <sys/ipc.h>
#include <sys/shm.h>
#include <sys/socket.h>
#include <sys/sendfile.h>
#include <sys/stat.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <openssl/md5.h>

#define BUFFERSIZE 1024

struct shared_data {
	int recording;
	char stage[32];
	time_t update_time;
};

char *str2md5(const char *str, int length) {
	int n;
	MD5_CTX c;
	unsigned char digest[16];
	char *out = (char*)malloc(33);

	MD5_Init(&c);

	while (length > 0) {
		if (length > 512) {
			MD5_Update(&c, str, 512);
		} else {
			MD5_Update(&c, str, length);
		}
		length -= 512;
		str += 512;
	}

	MD5_Final(digest, &c);

	for (n = 0; n < 16; ++n) {
		snprintf(&(out[n*2]), 16*2, "%02x", (unsigned int)digest[n]);
	}

    return out;
}

void error(char *msg) {
	perror(msg);
	exit(1);
}

int ensure_file_path(char *file_path){
	char command[256];
	sprintf(command, "mkdir -p $(dirname %s)", file_path);
	system(command);
}

int is_eom(char *message){
	if (strlen(message) == 4){
		if (strstr(message, "end\n") != NULL){
			return 1;
		}
	}
	if (strstr(message, "\nend\0") != NULL){
		return 1;
	}
	return 0;
}

struct shared_data *get_shmem(const char *base_path){
	key_t key;
	int shmid;
	struct shared_data *data_ptr;

	if ((key = ftok(base_path, 'R')) == -1) {
		error("ERROR creating shared memmory key");
	}

	if ((shmid = shmget(key, sizeof(struct shared_data), 0644 | IPC_CREAT)) == -1) {
		error("ERROR getting the shared memmory");
	}

	data_ptr = (struct shared_data *)shmat(shmid, (void *)0, 0);
	if (data_ptr == (struct shared_data *)(-1)) {
		error("ERROR attaching to shared memmory segment");
	}

	return data_ptr;
}

void list_config(struct shared_data *data){
	struct tm *timeinfo;
	timeinfo = localtime(&(data->update_time));
	if (data->recording){
		printf("=> mode: record\n");
	} else {
		printf("=> mode: play\n");
	}
	printf("=> stage: %s\n", data->stage);
	printf("=> update time: %s", asctime(timeinfo));
}

void set_default_config (struct shared_data *data) {
	strcpy(data->stage, str2md5("default", 7));
	data->recording = 0;
	data->update_time = time(NULL);
	list_config(data);
}

void set_stage(struct shared_data *data, char value[100]){
	strcpy(data->stage, str2md5(value, strlen(value)));
	data->update_time = time(NULL);
	list_config(data);
} 

void set_mode(struct shared_data *data, char value[20]){
	if (strcmp(value, "play") == 0){
		data->recording = 0;
	} else if (strcmp(value, "record") == 0){
		data->recording = 1;
	} else {
		error("ERROR unknown mode");
	}
	data->update_time = time(NULL);
	list_config(data);
} 

int main(int argc, char **argv) {
	int optval = 1, n, reading = 1, connection_counter;
	int fd_parent, fd_child, fd_trans, fd_dump, clientlen;
	off_t sendfile_offset;
	struct sockaddr_in server_addr, client_addr, trans_addr;
	struct hostent *hostp, *trans;
	struct stat stat_buf;
	char buffer_in[BUFFERSIZE], buffer_out[BUFFERSIZE], file_path[128];
	char *hostaddrp, *trans_host;
	FILE *fd_in, *fd_out;
	int mock_port, trans_port;

	const char *base_path=getenv("HOME");
	struct shared_data *shmem_data = get_shmem(base_path);

	// arguments list
	// set stage "stage name"
	// set mode [play|record]
	// get config
	// server "port" "trans host" "trans port"
	
	if (argc > 1){
		if (strcmp(argv[1], "server") == 0){
			if (argc == 5){
				mock_port = atoi(argv[2]);
				trans_host = argv[3];
				trans_port = atoi(argv[4]);
				shmem_data->recording = 1;
				printf("0> tunnel: %i:%s:%i\n", mock_port, trans_host, trans_port);
			}
			else if (argc == 3){
				mock_port = atoi(argv[2]);
				shmem_data->recording = 0;
				printf("0> player: %i\n", mock_port);
			}
			else {
				error("ERROR wrong number of parameter for server");
			}
		}
		else if (strcmp(argv[1], "set") == 0){
{'fraction_non_alphanumeric': 0.17653772986683577, 'fraction_numerical': 0.023589093214965124, 'mean_word_length': 4.2961719274681, 'pattern_counts': {'":': 0, '<': 37, '<?xml version=': 0, '>': 79, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1408057', 'n_tokens_mistral': 3396, 'n_tokens_neox': 3124, 'n_words': 767}