Path: utzoo!attcan!uunet!snorkelwacker!paperboy!meissner From: meissner@osf.org (Michael Meissner) Newsgroups: comp.lang.c Subject: Re: perror - (was Re: redirecting output) Message-ID: Date: 3 Jul 90 14:21:04 GMT References: <22931@dartvax.Dartmouth.EDU> <1990Jun29.172429.2818@zoo.toronto.edu> <1792@necisa.ho.necisa.oz> Sender: news@OSF.ORG Organization: Open Software Foundation Lines: 95 In-reply-to: boyd@necisa.ho.necisa.oz's message of 3 Jul 90 02:00:05 GMT In article <1792@necisa.ho.necisa.oz> boyd@necisa.ho.necisa.oz (Boyd Roberts) writes: | 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@necisa.ho.necisa.oz.au | | ``When the going gets wierd, the weird turn pro...'' You probably should use the ANSI name (strerror), and support the use of old UNIX with sys_errlst and new ANSI with strerror, via: #include #ifndef BSD_STRINGS_H #include #endif #ifndef HAVE_STRERROR char * strerror(e) { extern int errno; extern int sys_nerr; extern char *sys_errlist[]; if (e < 0 || e >= sys_nerr) return "Unknown error"; return sys_errlist[e]; } #endif void could_not(what, with) register char *what; register char *with; { fprintf(stderr, "%s: Could not %s \"%s\". %s\n", my_name, what, with, strerror(errno)); exit(1); /* NOTREACHED*/ } Not all non-UNIX systems have sys_errlist, and as internationalization becomes more important, I would expect that people will soon want the error messages to be presented in the user's native language. You also have the problem that old binaries using sys_errlist don't get new error message strings without relinking. -- Michael Meissner email: meissner@osf.org phone: 617-621-8861 Open Software Foundation, 11 Cambridge Center, Cambridge, MA Do apple growers tell their kids money doesn't grow on bushes?