~jscott/which

abaf6c819e02f9815726afd81bb44980d8eedb4a — John Scott 6 months ago ff4d25e
Delete the dumb test and slightly tweak our copyright/license statements
2 files changed, 2 insertions(+), 89 deletions(-)

D test.c
M which.c
D test.c => test.c +0 -87
@@ 1,87 0,0 @@
#define _POSIX_C_SOURCE 200809L
#include <locale.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/wait.h>
#include <unistd.h>

/* Invoke the which executable passed on the command-line
 * to try and find the shell 'sh'. */
int main(int argc, char *argv[static argc+1]) {
	if(!setlocale(LC_ALL, "")) {
		fputs("Failed to enable default locale\n", stderr);
		abort();
	}
	if(argc < 2) {
		fputs("Missing argument\n", stderr);
		abort();
	}

	if(getenv("MESON_EXE_WRAPPER")) {
		/* We don't support this right now. */
		exit(77);
	}

	const size_t n = confstr(_CS_PATH, NULL, 0);
	char *path = malloc(n);
	if(!path) {
		perror("Failed to allocate memory");
		abort();
	}
	confstr(_CS_PATH, path, n);

	if(setenv("PATH", path, /* overwrite? */ true) == -1) {
		perror("Failed to set environment variable");
		abort();
	}
	free(path);

	char *cmd;
	FILE *cmdstream = open_memstream(&cmd, &(size_t){0});
	if(!cmdstream) {
		perror("Failed to create memory stream");
		abort();
	}
	/* If which is not at an absolute path, we'll need a leading './'
	 * for the shell to find it. */
	if(argv[1][0] != '/' && fputs("./", cmdstream) == EOF) {
		perror("Failed to print to memory stream");
		abort();
	}
	if(fputs(argv[1], cmdstream) == EOF || fputs (" sh", cmdstream) == EOF) {
		perror("Failed to print to memory stream");
		abort();
	}
	if(fclose(cmdstream) == EOF) {
		perror("Failed to close memory stream");
		abort();
	}

	FILE *pip = popen(cmd, "r");
	if(!pip) {
		perror("Failed to create pipe");
		abort();
	}
	free(cmd);

	char *line = NULL;
	const ssize_t z = getdelim(&line, &(size_t){0}, '\0', pip);
	if(ferror(pip)) {
		perror("Failed to read from which");
		abort();
	}
	if(z == -1) {
		fputs("Failed to read from which\n", stderr);
		abort();
	}
	fputs(line, stdout);
	free(line);

	int s = pclose(pip);
	if(!WIFEXITED(s) || WEXITSTATUS(s) != EXIT_SUCCESS) {
		fputs("which terminated abnormally\n", stderr);
		abort();
	}
}

M which.c => which.c +2 -2
@@ 1,5 1,5 @@
/* SPDX-FileCopyrightText: 2021-2022 John Scott <jscott@posteo.net>
 * SPDX-License-Identifier: GPL-3.0-or-later */
/* 🄯 SPDX-FileCopyrightText: 2021-2022 John Scott <jscott@posteo.net>
 * © SPDX-License-Identifier: GPL-3.0-or-later */

/* We do not support the obsolete extension where an
 * omitted directory name is interpreted as the current