<commit_before>/**
 * @file ptspair.c
 * @brief 
 *
 * @date 5 mai 2015
 * @author <EMAIL>
 */
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif /* _GNU_SOURCE */
#include <sys/epoll.h>

#include <fcntl.h>
#include <unistd.h>

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include <errno.h>

#include "../include/ptspair.h"

static void clean_pts(struct pts *pts)
{
	if (pts == NULL)
		return;

	if (pts->writer > 0) {
		close(pts->writer);
		pts->writer = -1;
	}
	if (pts->master > 0) {
		close(pts->master);
		pts->master = -1;
	}
	memset(pts, 0, sizeof(*pts));
}

static int init_pts(struct pts *pts)
{
	int ret;

	memset(pts, 0, sizeof(*pts));
	pts->writer = -1;
	pts->master = posix_openpt(O_RDWR | O_NOCTTY);
	if (pts->master < 0) {
		ret = -errno;
		goto err;
	}
	ret = grantpt(pts->master);
	if (ret < 0) {
		ret = -errno;
		goto err;
	}
	ret = unlockpt(pts->master);
	if (ret < 0) {
		ret = -errno;
		goto err;
	}
	ret = ptsname_r(pts->master, pts->slave_path, PTSPAIR_PATH_MAX);
	/* a buffer which is too short sets errno to ERANGE */
	if (ret < 0) {
		ret = -errno;
		goto err;
	}
	pts->writer = open(pts->slave_path, O_WRONLY | O_CLOEXEC);
	if (pts->writer == -1) {
		ret = -errno;
		goto err;
	}

	return 0;
err:
	clean_pts(pts);

	return ret;
}

static int pts_epoll_ctl(struct ptspair *ptspair, struct pts *pts, int op,
		int evts)
{
	struct epoll_event event = {
			.events = evts,
			.data = {
					.ptr = pts,
			},
	};

	return epoll_ctl(ptspair->epollfd, op, pts->master, &event);
}

static int register_pts_read(struct ptspair *ptspair, struct pts *pts)
{
	return pts_epoll_ctl(ptspair, pts, EPOLL_CTL_ADD, EPOLLIN);
}

static int register_pts_write(struct ptspair *ptspair, struct pts *pts)
{<commit_msg>[DEV] initialize slave's configuration<commit_after>/**
 * @file ptspair.c
 * @brief 
 *
 * @date 5 mai 2015
 * @author <EMAIL>
 */
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif /* _GNU_SOURCE */
#include <sys/epoll.h>

#include <fcntl.h>
#include <termios.h>
#include <unistd.h>

#include <stdlib.h>
#include <string.h>
#include <stdio.h>

#include <errno.h>

#include "../include/ptspair.h"

static void clean_pts(struct pts *pts)
{
	if (pts == NULL)
		return;

	if (pts->writer > 0) {
		close(pts->writer);
		pts->writer = -1;
	}
	if (pts->master > 0) {
		close(pts->master);
		pts->master = -1;
	}
	memset(pts, 0, sizeof(*pts));
}

static int init_pts(struct pts *pts)
{
	int ret;
	struct termios tios;

	memset(pts, 0, sizeof(*pts));
	pts->writer = -1;
	pts->master = posix_openpt(O_RDWR | O_NOCTTY);
	if (pts->master < 0) {
		ret = -errno;
		goto err;
	}
	ret = grantpt(pts->master);
	if (ret < 0) {
		ret = -errno;
		goto err;
	}
	ret = unlockpt(pts->master);
	if (ret < 0) {
		ret = -errno;
		goto err;
	}
	ret = ptsname_r(pts->master, pts->slave_path, PTSPAIR_PATH_MAX);
	/* a buffer which is too short sets errno to ERANGE */
	if (ret < 0) {
		ret = -errno;
		goto err;
	}
	pts->writer = open(pts->slave_path, O_WRONLY | O_CLOEXEC);
	if (pts->writer == -1) {
		ret = -errno;
		goto err;
	}
	memset(&tios, 0, sizeof(tios));
	cfmakeraw(&tios);
	ret = tcsetattr(pts->writer, TCSANOW, &tios);
	if (ret < 0) {
		ret = -errno;
		goto err;
	}

	return 0;
err:
	clean_pts(pts);

	return ret;
}

static int pts_epoll_ctl(struct ptspair *ptspair, struct pts *pts, int op,
		int evts)
{
	struct epoll_event event = {
			.events = evts,
			.data = {
					.ptr = pts,
			},
	};

	return epoll_ctl(ptspair->epollfd, op, pts->master, &event);
}

static int register_pts_read(struct ptspair *ptspair, struct pts *pts)
{
	return pts_epoll_ctl(ptspair, pts, EPOLL_CTL_ADD, EPOLLIN);
}

static int register_pts_write(struct ptspair *ptspair, struct pts *pts)
{
{'fraction_non_alphanumeric': 0.16693766937669377, 'fraction_numerical': 0.010298102981029811, 'mean_word_length': 3.4685230024213074, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 61, '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': '292928', 'n_tokens_mistral': 1684, 'n_tokens_neox': 1582, 'n_words': 383}