/*
 * Copyright (c) 2006 Claus Assmann
 *
 * By using this file, you agree to the terms and conditions set
 * forth in the license/LICENSE.3C file which can be found at the
 * top level of this source code distribution.
 */

#include "sm/generic.h"
SM_RCSID("@(#)$Id: t-exit2txt-0.c,v 1.3 2006/10/21 01:44:38 ca Exp $")

#include "sm/error.h"
#include "sm/wait.h"
#include "sm/sysexits.h"
#include "sm/signal.h"
#include "sm/io.h"
#include "sm/test.h"

#define BLEN	256
static int Verbose = 0;

/* not defined on OSF/1 and some SunOS versions? */
#ifndef W_EXITCODE
# define W_EXITCODE(ret, sig)	((ret) << 8 | (sig))
#endif

static sm_ret_T
t_exit2err(void)
{
	int status, i, r;
	int excode[] = {
		  EX_USAGE
		, EX_NOINPUT
		, EX_NOUSER
		, EX_NOHOST
		, EX_UNAVAILABLE
		, EX_SOFTWARE
		, EX_OSERR
		, EX_OSFILE
		, EX_TEMPFAIL
		, EX_NOPERM
		, EX_CONFIG
		};
	char buf[BLEN] /* , txt[BLEN] */;

	r = SM_SUCCESS;
	for (i = 0; i < SM_ARRAY_SIZE(excode); i++)
	{
		status = W_EXITCODE(excode[i], 0);
		r = exit2txt_r(status, buf, sizeof(buf));
		SM_TEST(SM_SUCCESS == r);
/*
sm_io_fprintf(smioerr, "status=%x, buf=\"%s\"\n", status, buf);
		sm_snprintf(txt, sizeof(txt), "exit=%d", excode[i]);
		SM_TEST(strcmp(buf, txt) == 0);
sm_io_fprintf(smioerr, "buf=\"%s\", txt=\"%s\"\n", buf, txt);
*/
	}

	status = 0x8b;
	r = exit2txt_r(status, buf, sizeof(buf));
	SM_TEST(SM_SUCCESS == r);
	if (Verbose > 0)
		sm_io_fprintf(smioerr, "status=%x, buf=\"%s\"\n", status, buf);

	return r;
}

extern int optind;

int
main(int argc, char *argv[])
{
	int c;
	bool interactive;

	interactive = false;
	while ((c = getopt(argc, argv, "iV")) != -1)
	{
		switch (c)
		{
		  case 'i':
			interactive = true;
			break;
		  case 'V':
			++Verbose;
			break;
		  default:
			return EX_USAGE;
		}
	}
	sm_test_begin(argc, argv, "test exit2err");
	argc -= optind;
	argv += optind;

	if (interactive)
	{
		int i;
		char buf[BLEN];

		for (i = 0; i < argc; i++)
		{
			c = (int) strtol(argv[i], (char **)NULL, 0);
			exit2txt_r(c, buf, sizeof(buf));
			sm_io_fprintf(smioerr, "status=%x, buf=\"%s\"\n", c, buf);
		}
	}
	else
		t_exit2err();
	return sm_test_end();
}


syntax highlighted by Code2HTML, v. 0.9.1