<commit_before>/* (C) 2015 <NAME> */

#include <assert.h>
#include <fcntl.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "yank.h"

#define N_YANKS 26
#define LAST_YANK (N_YANKS - 1)
#define YANK_FILE "/tmp/lwe_yank_"

static unsigned yank_sizes[N_YANKS];
static char *yank_buffers[N_YANKS];

static void shiftyanks(void);
static int yank_filename(char buf[8192]);

/*
 * Yanked text is stored in malloc'd buffers.  The yanks array is an
 * array of pointers to these buffers.  When a new item is yanked, we
 * want to shift everything down, dropping the last item in order to
 * make room for the new item.
 */
static void shiftyanks()
{
	if (yank_buffers[LAST_YANK] != NULL)
		free(yank_buffers[LAST_YANK]);
	memmove(&yank_buffers[1], &yank_buffers[0],
			sizeof(yank_buffers[0]) * LAST_YANK);
	memmove(&yank_sizes[1], &yank_sizes[0],
			sizeof(yank_sizes[0]) * LAST_YANK);
}

static int yank_filename(char filename[8192])
{
	struct passwd *pwd;
	uid_t u;
	u = getuid();
	if (!(pwd = getpwuid(u)))
		return -1;
	strlcpy(filename, YANK_FILE, 8192);
	strlcat(filename, pwd->pw_name, 8192);
	return 0;
}

int saveyanks()
{
	char filename[8192];
	FILE *f;
	size_t w;
	int i, err;
	if (yank_filename(filename) < 0)
		return -1;
	if (!(f = fopen(filename, "w")))
		return -1;
	err = 0;
	if (flock(fileno(f), LOCK_EX) < 0) {
		err = -1;
		goto close;
	}
	for (i = 0; i < N_YANKS; i++) {
		if (yank_buffers[i] == NULL)
			break;
		if (fprintf(f, "%u\n", yank_sizes[i]) < 0) {
			err = -1;
			goto close;
		}
		w = fwrite(yank_buffers[i], sizeof(char), yank_sizes[i], f);
		if (w < yank_sizes[i]) {
			err = -1;
			goto close;
		}
		if (fputc('\n', f) == EOF) {<commit_msg>Replace non-portable strlcpy and strlcat<commit_after>/* (C) 2015 <NAME> */

#include <assert.h>
#include <fcntl.h>
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "yank.h"

#define N_YANKS 26
#define LAST_YANK (N_YANKS - 1)
#define YANK_FILE "/tmp/lwe_yank_"

static unsigned yank_sizes[N_YANKS];
static char *yank_buffers[N_YANKS];

static void shiftyanks(void);
static int yank_filename(char buf[8192]);

/*
 * Yanked text is stored in malloc'd buffers.  The yanks array is an
 * array of pointers to these buffers.  When a new item is yanked, we
 * want to shift everything down, dropping the last item in order to
 * make room for the new item.
 */
static void shiftyanks()
{
	if (yank_buffers[LAST_YANK] != NULL)
		free(yank_buffers[LAST_YANK]);
	memmove(&yank_buffers[1], &yank_buffers[0],
			sizeof(yank_buffers[0]) * LAST_YANK);
	memmove(&yank_sizes[1], &yank_sizes[0],
			sizeof(yank_sizes[0]) * LAST_YANK);
}

static int yank_filename(char filename[8192])
{
	struct passwd *pwd;
	uid_t u;
	u = getuid();
	if (!(pwd = getpwuid(u)))
		return -1;
	snprintf(filename, 8192, "%s%s", YANK_FILE, pwd->pw_name);
	return 0;
}

int saveyanks()
{
	char filename[8192];
	FILE *f;
	size_t w;
	int i, err;
	if (yank_filename(filename) < 0)
		return -1;
	if (!(f = fopen(filename, "w")))
		return -1;
	err = 0;
	if (flock(fileno(f), LOCK_EX) < 0) {
		err = -1;
		goto close;
	}
	for (i = 0; i < N_YANKS; i++) {
		if (yank_buffers[i] == NULL)
			break;
		if (fprintf(f, "%u\n", yank_sizes[i]) < 0) {
			err = -1;
			goto close;
		}
		w = fwrite(yank_buffers[i], sizeof(char), yank_sizes[i], f);
		if (w < yank_sizes[i]) {
			err = -1;
			goto close;
		}
		if (fputc('\n', f) == EOF) {
{'fraction_non_alphanumeric': 0.16339491916859122, 'fraction_numerical': 0.024826789838337183, 'mean_word_length': 4.065789473684211, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 21, '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': '3640688', 'n_tokens_mistral': 1632, 'n_tokens_neox': 1522, 'n_words': 387}