[comp.lang.c] Reporting errors from local libraries

bks@ALFA.berkeley.edu ( Brad Sherman ) (06/14/89)

>In <186@cbnewsd.ATT.COM>, tainter@cbnewsd.ATT.COM (johnathan.tainter) writes:
>> In <1795@auspex.auspex.com> guy@auspex.auspex.com (Guy Harris) writes:
>> >I.e., if you compile a file containing
>> >	extern int foo;
>> >	main() {}
>> >and link that *with no other object files and with no libraries that
>> >define "foo"*, will the link fail?
> ...
>I have since learned that this is explicitly legal in ANSI C.
> ...				--Andrew Koenig

(For some reason the above thread seems relevant to my question.)

Where is the actual space for "errno" allocated? (Metaspace along with
argc, argv, envp?)

If one has N libraries and all functions in these libraries return
0 on success and -1 on failure, and one wishes, say, to set an (extern)
integer "Localerrno" to provide additional info on failure, what is the
guruish way to proceed?  That is, where exactly should the single instance
of "int Localerrno" appear?

	Brad Sherman (bks@ALFA.Berkeley.EDU)

diamond@diamond.csl.sony.junet (Norman Diamond) (06/14/89)

In article <25447@agate.BERKELEY.EDU> bks@ALFA.berkeley.edu ( Brad Sherman ) writes:

>Where is the actual space for "errno" allocated? (Metaspace along with
>argc, argv, envp?)

Something like crt0.o would be a fine place.

--
Norman Diamond, Sony Computer Science Lab (diamond%csl.sony.co.jp@relay.cs.net)
 The above opinions are my own.  However, if you see this at Waterloo, Stanford,
 or Anterior, then their administrators must have approved of these opinions.

karl@haddock.ima.isc.com (Karl Heuer) (06/15/89)

In article <25447@agate.BERKELEY.EDU> bks@ALFA.berkeley.edu ( Brad Sherman ) writes:
>If one has N libraries and all functions in these libraries return
>0 on success and -1 on failure, and one wishes, say, to set an (extern)
>integer "Localerrno" to provide additional info on failure, what is the
>guruish way to proceed?  That is, where exactly should the single instance
>of "int Localerrno" appear?

In a separate module.  I.e., create "Localerrno.c" containing the single line
"int Localerrno = 0;" and hope that your linker is smart enough to handle it.

(Unfortunately, if it isn't smart enough, it's probably for what the vendor
believes is a good reason, and so a bug report is likely to be ignored.)

Karl W. Z. Heuer (ima!haddock!karl or karl@haddock.isc.com), The Walking Lint

gwyn@smoke.BRL.MIL (Doug Gwyn) (06/17/89)

In article <25447@agate.BERKELEY.EDU> bks@ALFA.berkeley.edu ( Brad Sherman ) writes:
>Where is the actual space for "errno" allocated?

The usual implementation is for the C library to contain a module that
defines storage for it, for example what you get when you compile:

	/* errno.c -- C library source for the "errno" data module */
	int errno;

(Actually, the module usually also contains an secret run-time support
function that is used to log an error into errno and return a -1 value
from the failed system call.)

>If one has N libraries and all functions in these libraries return
>0 on success and -1 on failure, and one wishes, say, to set an (extern)
>integer "Localerrno" to provide additional info on failure, what is the
>guruish way to proceed?

The "errno" approach is a mistake, and I strongly recommend not returning
values via external global variables.  Use a better function interface,
for example return 0 on success and non-zero error code on failure.