<commit_before>#include <sys/types.h>

#include <err.h>
#include <libgen.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>

#include <pkg.h>

#include "utils.h"
#include "install.h"

void
usage_install(void)
{
	fprintf(stderr, "usage: pkg install [-ygxXf] <pkg-name> <...>\n\n");
	fprintf(stderr, "For more information see 'pkg help install'.\n");
}

int
exec_install(int argc, char **argv)
{
	struct pkg *pkg = NULL;
	struct pkgdb_it *it;
	struct pkgdb *db = NULL;
	struct pkg_jobs *jobs = NULL;
	int retcode = EPKG_OK;
	int i, ch, yes = 0;
	match_t match = MATCH_EXACT;

	while ((ch = getopt(argc, argv, "ygxXf")) != -1) {
		switch (ch) {
			case 'y':
				yes = 1;
				break;
			case 'g':
				match = MATCH_GLOB;
				break;
			case 'x':
				match = MATCH_REGEX;
				break;
			case 'X':
				match = MATCH_EREGEX;
				break;
			default:
				usage_install();
				return (EX_USAGE);
		}
	}
	argc -= optind;
	argv += optind;

	if (argc < 1) {
		usage_install();
		return (EX_USAGE);
	}

	if (geteuid() != 0) {
		warnx("installing packages can only be done as root");
		return (EX_NOPERM);
	}

	if (pkgdb_open(&db, PKGDB_REMOTE) != EPKG_OK) {
		return (EX_IOERR);
	}

	if (pkg_jobs_new(&jobs, PKG_JOBS_INSTALL, db) != EPKG_OK) {
		retcode = EPKG_FATAL;
		goto cleanup;
	}

	for (i = 0; i < argc; i++) {
		if ((it = pkgdb_rquery(db, argv[i], match, REPO_SEARCH_NAME)) == NULL) {
			retcode = EPKG_FATAL;
			goto cleanup;
		}

		while (( retcode = pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC)) == EPKG_OK) {
			pkg_jobs_add(jobs, pkgdb_query_remote(db, pkg_get(pkg, PKG_ORIGIN)));
		}
		
		pkg_free(pkg);
		pkg = NULL;
		pkgdb_it_free(it);
	}

	/* print a summary before applying the jobs */
	pkg = NULL;
	printf("The following packages will be installed:\n");
	while (pkg_jobs(jobs, &pkg) == EPKG_OK) {
		printf("\t%s-%s\n", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION));
	}
 
	if (yes == 0)
		yes = query_yesno("\nProceed with installing packages [y/N]: ");

	if (yes == 1)
		retcode = pkg_jobs_apply(jobs, 0);
<commit_msg>Do not free pkg to malloc it later.<commit_after>#include <sys/types.h>

#include <err.h>
#include <libgen.h>
#include <stdbool.h>
#include <stdio.h>
#include <string.h>
#include <sysexits.h>
#include <unistd.h>

#include <pkg.h>

#include "utils.h"
#include "install.h"

void
usage_install(void)
{
	fprintf(stderr, "usage: pkg install [-ygxXf] <pkg-name> <...>\n\n");
	fprintf(stderr, "For more information see 'pkg help install'.\n");
}

int
exec_install(int argc, char **argv)
{
	struct pkg *pkg = NULL;
	struct pkgdb_it *it;
	struct pkgdb *db = NULL;
	struct pkg_jobs *jobs = NULL;
	int retcode = EPKG_OK;
	int i, ch, yes = 0;
	match_t match = MATCH_EXACT;

	while ((ch = getopt(argc, argv, "ygxXf")) != -1) {
		switch (ch) {
			case 'y':
				yes = 1;
				break;
			case 'g':
				match = MATCH_GLOB;
				break;
			case 'x':
				match = MATCH_REGEX;
				break;
			case 'X':
				match = MATCH_EREGEX;
				break;
			default:
				usage_install();
				return (EX_USAGE);
		}
	}
	argc -= optind;
	argv += optind;

	if (argc < 1) {
		usage_install();
		return (EX_USAGE);
	}

	if (geteuid() != 0) {
		warnx("installing packages can only be done as root");
		return (EX_NOPERM);
	}

	if (pkgdb_open(&db, PKGDB_REMOTE) != EPKG_OK) {
		return (EX_IOERR);
	}

	if (pkg_jobs_new(&jobs, PKG_JOBS_INSTALL, db) != EPKG_OK) {
		retcode = EPKG_FATAL;
		goto cleanup;
	}

	for (i = 0; i < argc; i++) {
		if ((it = pkgdb_rquery(db, argv[i], match, REPO_SEARCH_NAME)) == NULL) {
			retcode = EPKG_FATAL;
			goto cleanup;
		}

		while (( retcode = pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC)) == EPKG_OK) {
			pkg_jobs_add(jobs, pkgdb_query_remote(db, pkg_get(pkg, PKG_ORIGIN)));
		}
		
		pkgdb_it_free(it);
	}
	pkg_free(pkg);

	/* print a summary before applying the jobs */
	pkg = NULL;
	printf("The following packages will be installed:\n");
	while (pkg_jobs(jobs, &pkg) == EPKG_OK) {
		printf("\t%s-%s\n", pkg_get(pkg, PKG_NAME), pkg_get(pkg, PKG_VERSION));
	}
 
	if (yes == 0)
		yes = query_yesno("\nProceed with installing packages [y/N]: ");

	if (yes == 1)
		retcode = pkg_jobs_apply(jobs, 0);

{'fraction_non_alphanumeric': 0.16534296028880865, 'fraction_numerical': 0.004332129963898917, 'mean_word_length': 3.5821389195148843, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 25, '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': '5344110', 'n_tokens_mistral': 1978, 'n_tokens_neox': 1753, 'n_words': 425}