perror - (was Re: redirecting output)

Boyd Roberts boyd at necisa.ho.necisa.oz
Tue Jul 3 12:00:05 AEST 1990


perror(3) is good, but this is better:

    char	*
    sysmess()
    {
	extern int	errno;
	extern int	sys_nerr;
	extern char	*sys_errlist[];

	if (errno < 0 || errno >= sys_nerr)
	    return "Unknown error";

	return sys_errlist[errno];
    }

    void
    could_not(what, with)
    register char    *what;
    register char    *with;
    {
	fprintf(stderr, "%s: Could not %s \"%s\". %s\n", my_name, what, with, sysmess());
	exit(1);
	/* NOTREACHED*/
    }

So then we go:

    if ((fd = open(file, O_RDONLY)) == -1)
    {
	could_not("open", file);
	/* NOTREACHED */
    }

Of course, my_name is:

char	*my_name;

And main() goes:

	if ((my_name = strrchr(argv[0], '/')) == NULLSTR || *++my_name == '\0')
		my_name = argv[0];


Boyd Roberts			boyd at necisa.ho.necisa.oz.au

``When the going gets wierd, the weird turn pro...''



More information about the Comp.lang.c mailing list