<commit_before>#define _GNU_SOURCE	/* linux */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <err.h>
#include <sys/types.h>
#include <sys/wait.h>

#include <sqlite3.h>

#include "hgd.h"

struct hgd_playlist_item	*next_track; /* not need pass as cb arg */

char				*hgd_dir = NULL;
char				*db_path = NULL;
char				*filestore_path;
sqlite3				*db = NULL;

uint8_t				exit_ok = 0;

/*
 * clean up, exit. if exit_ok = 0, an error (signal/error)
 */
void
hgd_exit_nicely()
{
	if (db)
		sqlite3_close(db);
	if (hgd_dir)
		free(hgd_dir);
	if (db_path)
		free (db_path);
	if (filestore_path)
		free(filestore_path);

	if (exit_ok)
		exit (EXIT_SUCCESS);
	else
		exit (EXIT_FAILURE);
}

void
hgd_play_track(struct hgd_playlist_item *t)
{
	int			status = 0, sql_res;
	char			*q, *sql_err;

	DPRINTF("%s: playing '%s' for '%s'\n", __func__, t->filename, t->user);

	/* mark it as playing in the database */
	xasprintf(&q, "UPDATE playlist SET playing=1 WHERE id=%d", t->id);
	sql_res = sqlite3_exec(db, q, NULL, NULL, &sql_err);
	free(q);

	if (sql_res != SQLITE_OK) {
		fprintf(stderr, "%s: set track playing in sql: %s\n",
		    __func__, sqlite3_errmsg(db));
		sqlite3_free(sql_err);
		hgd_exit_nicely();
	}


	if (!fork()) {
		/* child - your the d00d who will play this track */
		execlp("mplayer", "mplayer", "-quiet",
		    t->filename, (char *) NULL);

		/* if we get here, the shit hit the fan with execlp */
		warn("execlp() failed");
		hgd_exit_nicely();
	} else {
		wait(&status);
	}

	DPRINTF("%s: finished playing (exit %d)\n", __func__, status);

	/* mark it as finished in the database */
	xasprintf(&q,
	    "UPDATE playlist SET playing=0, finished=1 WHERE id=%d", t->id);
	sql_res = sqlite3_exec(db, q, NULL, NULL, &sql_err);
	if (sql_res != SQLITE_OK) {
		fprintf(stderr, "%s: can't initialise db: %s\n",
		    __func__, sqlite3_errmsg(db));
		sqlite3_free(sql_err);
	}

	free(q);

	hgd_free_playlist_item(t);
}<commit_msg>have playd write away the mplayer pid<commit_after>#define _GNU_SOURCE	/* linux */
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <err.h>
#include <sys/types.h>
#include <sys/wait.h>

#include <sqlite3.h>

#include "hgd.h"

struct hgd_playlist_item	*next_track; /* not need pass as cb arg */

char				*hgd_dir = NULL;
char				*db_path = NULL;
char				*filestore_path;
sqlite3				*db = NULL;

uint8_t				exit_ok = 0;

/*
 * clean up, exit. if exit_ok = 0, an error (signal/error)
 */
void
hgd_exit_nicely()
{
	if (db)
		sqlite3_close(db);
	if (hgd_dir)
		free(hgd_dir);
	if (db_path)
		free (db_path);
	if (filestore_path)
		free(filestore_path);

	if (exit_ok)
		exit (EXIT_SUCCESS);
	else
		exit (EXIT_FAILURE);
}

void
hgd_play_track(struct hgd_playlist_item *t)
{
	int			status = 0, sql_res, pid;
	char			*q, *sql_err;
	char			*pid_path;
	FILE			*pid_file;

	DPRINTF("%s: playing '%s' for '%s'\n", __func__, t->filename, t->user);

	/* mark it as playing in the database */
	xasprintf(&q, "UPDATE playlist SET playing=1 WHERE id=%d", t->id);
	sql_res = sqlite3_exec(db, q, NULL, NULL, &sql_err);
	free(q);

	if (sql_res != SQLITE_OK) {
		fprintf(stderr, "%s: set track playing in sql: %s\n",
		    __func__, sqlite3_errmsg(db));
		sqlite3_free(sql_err);
		hgd_exit_nicely();
	}


	pid = fork();
	if (!pid) {
		/* child - your the d00d who will play this track */
		execlp("mplayer", "mplayer", "-quiet",
		    t->filename, (char *) NULL);

		/* if we get here, the shit hit the fan with execlp */
		warn("execlp() failed");
		hgd_exit_nicely();
	} else {
		/* we will write away child pid */
		xasprintf(&pid_path, "%s/%s", hgd_dir, HGD_MPLAYER_PID_NAME);

		pid_file = fopen(pid_path, "w");
		if (pid_file == NULL) {
			warn("%s: can't open '%s'", __func__, pid_path);
			free(pid_path);
			hgd_exit_nicely();
		}


		fprintf(pid_file, "%d", pid);
		fclose(pid_file);
		wait(&status);
		if (unlink(pid_path) < 0) {
			warn("%s: can't unlink '%s'", __func__, pid_path);
			free(pid_path);
			hgd_exit_nicely();
		}
		free(pid_path);
	}

	DPRINTF("%s: finished playing (exit %d)\n", __func__, status);

	/* mark it as finished in the database */
	xasprintf(&q,
	    "UPDATE playlist SET playing=0, finished=1 WHERE id=%d", t->id);
	sql_res = sqlite3_exec(db, q, NULL, NULL, &sql_err);
	if (sql_res != SQLITE_OK) {
		fprintf(stderr, "%s: can't initialise db: %s\n",
		    __func__, sqlite3_errmsg(db));
		sqlite3_free(sql_err);
	}

	free(q);

	hgd_free_playlist_item(t);
}
{'fraction_non_alphanumeric': 0.16504639858594786, 'fraction_numerical': 0.008174988952717632, 'mean_word_length': 3.862513426423201, 'pattern_counts': {'":': 0, '<': 22, '<?xml version=': 0, '>': 31, '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': 2, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2077659', 'n_tokens_mistral': 2035, 'n_tokens_neox': 1887, 'n_words': 471}