<commit_before>#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include "battery.h"

int main(int argc, char *argv[])
{
	const int batMax = getValue(BAT_MAX);
	int batState = getValue(AC_CHRG);
	int batCur = 0;
	int batPre = 0;
	int batAvg = 0;
	int countIter = 0;
	FILE *fdout;

	if (argc == 3 && argv[1][1] == 'w') {
		fdout = fopen(argv[2], "w");
		if (fdout == NULL) {
			fprintf(stderr, "Error opening %s: ", argv[2]);
			perror("");
			exit(1);
		}
	}
	else {
		fdout = stdout;
	}

	while (1) {
		batCur = getValue(BAT_CUR);

		// On the first iteration, or after a
		// battery change, set batPre to the
		// current value
		if (countIter == 0) {
			batPre = batCur;
		}
		// Calculate average increase/decrease
		// of battery when defined iterations
		// are done
		else if (countIter % POLL_ITR == 0) {
			batAvg = (batPre - batCur) / POLL_AVG;
			batPre = batCur;
		}
		// If batState changed between discharging and charging
		// reset average counters
		if (batState != getValue(AC_CHRG)) {
			countIter = 0;
			batAvg = 0;
		}

		batState = getValue(AC_CHRG);
		printOutput(fdout, batCur, batMax, batAvg, batState);
		if (fdout != stdout) {
			if (ftruncate(fileno(fdout), 0) == -1) {
				fputs("Error truncating file", stderr);
				fclose(fdout);
				exit(1);
			}
			rewind(fdout);
			if (fflush(fdout) != 0) {
				perror("Couldn't flush buffer to file: ");
				exit(1);
			}
		}

		sleep(POLL_INT);
		countIter++;
	}

	fclose(fdout);
	return 0;
}

int getValue(const char *filename) {
	int fd;
	char line[MAX_LINE];
	ssize_t n;

	fd = open(filename, O_RDONLY);
	if (fd == -1) {
		fputs("Error opening file", stderr);
		exit(1);
	}

	n = read(fd, line, MAX_LINE);
	if (n == -1) {
		fputs("Error reading file", stderr);
		exit(1);
	}

	close(fd);
	return atoi(line);
}

void printOutput(FILE *stream, int batCur, int batMax, int batAvg, int batState)
{
	if (batState) {
		if (batAvg) {
			fprintf(stream, "Charging: ");
			fprintf(stream, "%.f%, ", (float)batCur/batMax*100);
			fprintf(stream, "estimated %.fmin left before fully charged\n",
				(float)(batMax - batCur) / -batAvg / 60);
		}
		else {
			fprintf(stream, "Charging: ");
			fprintf(stream, "%.f%\n", (float)batCur/batMax*100);
		}
	}
	else {
		if (batAvg) {
			fprintf(stream, "Discharging: ");
			fprintf(stream, "%.f%, ", (float)batCur/batMax*100);
			fprintf(stream, "estimated %.fmin left\n", (float)batCur / batAvg / 60);
		}
		else {
			fprintf(stream, "Discharging: ");
			fprintf(stream, "%.f%\n", (float)batCur/batMax*100);
		}
	}
}<commit_msg>Didn't escape % in string format<commit_after>#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include "battery.h"

int main(int argc, char *argv[])
{
	const int batMax = getValue(BAT_MAX);
	int batState = getValue(AC_CHRG);
	int batCur = 0;
	int batPre = 0;
	int batAvg = 0;
	int countIter = 0;
	FILE *fdout;

	if (argc == 3 && argv[1][1] == 'w') {
		fdout = fopen(argv[2], "w");
		if (fdout == NULL) {
			fprintf(stderr, "Error opening %s: ", argv[2]);
			perror("");
			exit(1);
		}
	}
	else {
		fdout = stdout;
	}

	while (1) {
		batCur = getValue(BAT_CUR);

		// On the first iteration, or after a
		// battery change, set batPre to the
		// current value
		if (countIter == 0) {
			batPre = batCur;
		}
		// Calculate average increase/decrease
		// of battery when defined iterations
		// are done
		else if (countIter % POLL_ITR == 0) {
			batAvg = (batPre - batCur) / POLL_AVG;
			batPre = batCur;
		}
		// If batState changed between discharging and charging
		// reset average counters
		if (batState != getValue(AC_CHRG)) {
			countIter = 0;
			batAvg = 0;
		}

		batState = getValue(AC_CHRG);
		printOutput(fdout, batCur, batMax, batAvg, batState);
		if (fdout != stdout) {
			if (ftruncate(fileno(fdout), 0) == -1) {
				fputs("Error truncating file", stderr);
				fclose(fdout);
				exit(1);
			}
			rewind(fdout);
			if (fflush(fdout) != 0) {
				perror("Couldn't flush buffer to file: ");
				exit(1);
			}
		}

		sleep(POLL_INT);
		countIter++;
	}

	fclose(fdout);
	return 0;
}

int getValue(const char *filename) {
	int fd;
	char line[MAX_LINE];
	ssize_t n;

	fd = open(filename, O_RDONLY);
	if (fd == -1) {
		fputs("Error opening file", stderr);
		exit(1);
	}

	n = read(fd, line, MAX_LINE);
	if (n == -1) {
		fputs("Error reading file", stderr);
		exit(1);
	}

	close(fd);
	return atoi(line);
}

void printOutput(FILE *stream, int batCur, int batMax, int batAvg, int batState)
{
	if (batState) {
		if (batAvg) {
			fprintf(stream, "Charging: ");
			fprintf(stream, "%.f%%, ", (float)batCur/batMax*100);
			fprintf(stream, "estimated %.fmin left before fully charged\n",
				(float)(batMax - batCur) / -batAvg / 60);
		}
		else {
			fprintf(stream, "Charging: ");
			fprintf(stream, "%.f%%\n", (float)batCur/batMax*100);
		}
	}
	else {
		if (batAvg) {
			fprintf(stream, "Discharging: ");
			fprintf(stream, "%.f%%, ", (float)batCur/batMax*100);
			fprintf(stream, "estimated %.fmin left\n", (float)batCur / batAvg / 60);
		}
		else {
			fprintf(stream, "Discharging: ");
			fprintf(stream, "%.f%%\n", (float)batCur/batMax*100);
		}
	}
}
{'fraction_non_alphanumeric': 0.1732375218489027, 'fraction_numerical': 0.015925422412118858, 'mean_word_length': 3.284525790349418, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 11, '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': '3242690', 'n_tokens_mistral': 2365, 'n_tokens_neox': 2091, 'n_words': 500}