view loadtools/ltexit.c @ 465:003e48f8ebe1

rvinterf/etmsync/fsnew.c: cast 0 to (char *) for execl sentinel I generally don't use NULL and use plain 0 instead, based on a "NULL considered harmful" discussion on the classiccmp mailing list many aeons ago (I couldn't find it, and I reason that it must have been 2005 or earlier), but a recent complaint by a packager sent me searching, and I found this: https://ewontfix.com/11/ While I don't give a @#$% about "modern" systems and code-nazi tools, I realized that passing a plain 0 as a pointer sentinel in execl is wrong because it will break on systems where pointers are longer than the plain int type. Again, I don't give a @#$% about the abomination of x86_64 and the like, but if anyone ever manages to port my code to something like a PDP-11 (16-bit int, 32-bit long and pointers), then passing a plain 0 as a function argument where a pointer is expected most definitely won't work: if the most natural stack slot and SP alignment unit is 16 bits, fitting an int, with longs and pointers taking up two such slots, then the call stack will be totally wrong with a plain 0 passed for a pointer. Casting the 0 to (char *) ought to be the most kosher solution for the most retro systems possible.
author Mychaela Falconia <falcon@freecalypso.org>
date Mon, 11 Feb 2019 00:00:19 +0000
parents 96332d875fc9
children aba969153d20
line wrap: on
line source

/*
 * This module implements the loadtool exit command, along with its
 * options for jump-reboot and Iota power-off.
 */

#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdlib.h>

static void
exit_bare(rc)
{
	exit(rc);
}

static void
exit_gta02_cutpwr(rc)
{
#ifdef GTA0x_AP_BUILD
	set_gta_modem_power_ctrl(0);
#endif
	exit(rc);
}

static void
exit_iotaoff(rc)
{
	static char *poweroff_argv[2] = {"poweroff", 0};

	tpinterf_make_cmd(poweroff_argv);
	tpinterf_send_cmd();
	exit(rc);
}

static void
exit_jump0(rc)
{
	static char *jump0_argv[3] = {"jump", "0", 0};

	tpinterf_make_cmd(jump0_argv);
	tpinterf_send_cmd();
	exit(rc);
}

void (*default_exit)() = exit_bare;

static struct kwtab {
	char *kw;
	void (*func)();
} exit_modes[] = {
	{"bare", exit_bare},
	{"gta02-cutpwr", exit_gta02_cutpwr},
	{"iota-off", exit_iotaoff},
	{"jump0", exit_jump0},
	{0, 0}
};

cmd_exit(argc, argv)
	char **argv;
{
	struct kwtab *tp;

	if (argc < 2)
		default_exit(0);
	for (tp = exit_modes; tp->kw; tp++)
		if (!strcmp(tp->kw, argv[1]))
			break;
	if (!tp->func) {
		fprintf(stderr,
			"error: \"%s\" is not an understood exit mode\n",
			argv[1]);
		return(-1);
	}
	tp->func(0);
}

/* called from hwparam.c config file parser */
void
set_default_exit_mode(arg, filename_for_errs, lineno_for_errs)
	char *arg;
	char *filename_for_errs;
	int lineno_for_errs;
{
	char *cp;
	struct kwtab *tp;

	while (isspace(*arg))
		arg++;
	if (!*arg) {
		fprintf(stderr,
		"%s line %d: exit-mode setting requires an argument\n",
			filename_for_errs, lineno_for_errs);
		exit(1);
	}
	for (cp = arg; *cp && !isspace(*cp); cp++)
		;
	*cp = '\0';
	for (tp = exit_modes; tp->kw; tp++)
		if (!strcmp(tp->kw, arg))
			break;
	if (!tp->func) {
		fprintf(stderr,
			"%s line %d: \"%s\" is not an understood exit mode\n",
			filename_for_errs, lineno_for_errs, arg);
		exit(1);
	}
	default_exit = tp->func;
}