Plan 9 from Bell Labs’s /usr/web/sources/contrib/steve/root/sys/src/cmd/refer/error.c

Copyright © 2021 Plan 9 Foundation.
Distributed under the MIT License.
Download the Plan 9 distribution.


#include <stdarg.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "error.h"

static	void	(*errfunc)(void);

/*
 * These conventions follow those at Toronto.
 */

char	*progname;

/*
 * Return a plausible program name for assignment to ``progname'',
 * given argv[0].
 */
char *
mkprogname(char *cp)
{
	char *lp;

	return(cp == 0 || (lp = strrchr(cp, '/'))==0? cp: lp+1);
}

/*
 * Error accepts a printf format and arguments, and prints
 * an error message on the standard error stream,
 * possibly preceded by a program name (as formed by mkprogname).
 */
void
error(char *fmt, ...)
{
	va_list args;
	char *cp;
	__ERRFN f;

	if ((cp = progname) != 0 && *cp != '\0')
		fprintf(stderr, "%s: ", cp);
	va_start(args, fmt);
	vfprintf(stderr, fmt, args);
	va_end(args);
	if (strrchr(cp, '\n') == 0)
		fprintf(stderr, "\n");
	if ((f = errfunc) != 0) {
		errfunc = 0;
		(*f)();
	}
	exit(1);
}

FILE*
efopen(char *file, char *mode)
{
	FILE *fp;

	if ((fp = fopen(file, mode)) == NULL)
		error("can't %s %s\n", *mode == 'r'? "open": "create", file);
	return fp;
}

FILE*
efreopen(char *file, char *mode, FILE *fp)
{
	if ((fp = freopen(file, mode, fp)) == NULL)
		error("can't %s %s\n", *mode == 'r'? "open": "create", file);
	return(fp);
}

/*
 * Local to York:
 */

/*
 * Set an error handling function; return its previous value.
 */
void
(*errcleanup(__ERRFN f))(void)
{
	__ERRFN oldf;

	oldf = errfunc;
	errfunc = f;
	return(oldf);
}

Bell Labs OSI certified Powered by Plan 9

(Return to Plan 9 Home Page)

Copyright © 2021 Plan 9 Foundation. All Rights Reserved.
Comments to [email protected].