2

I was looking at the manual for longjmp and in the Errors part it says this:

ERRORS

If the contents of the env are corrupted, or correspond to an environment that has already returned, the longjmp() routine calls the routine longjmperror(3). If longjmperror() returns, the program is aborted (see abort(3)). The default version of longjmperror() prints the message ``longjmp botch'' to standard error and returns. User programs wishing to exit more gracefully should write their own versions of longjmperror().

How would i write my own version of longjmperror? From what i know in C you can't override functions and i really need the long jump to exit in a specific way when it doesn't find the point to jump to.

Lee Duhem
  • 14,695
  • 3
  • 29
  • 47
Jean-Luc Nacif Coelho
  • 1,006
  • 3
  • 14
  • 30
  • You need to write the function, of course, and then you need to tell the linker to use it instead of the library function. The details of doing this will differ by platform. – Ernest Friedman-Hill Mar 01 '14 at 21:15
  • 1
    Clearly you haven't actually tried it. Do try it before you convince us that it cannot work. – Hans Passant Mar 01 '14 at 21:16
  • 1
    Maybe you should read http://stackoverflow.com/questions/1733649/excellent-setjmp-longjmp-tutorials – Peter M Mar 01 '14 at 21:19
  • I looked. There are no tutorials that teach you how to treat errors and google search returned 169 unrelated search results for longjmperror – Jean-Luc Nacif Coelho Mar 01 '14 at 21:25
  • 1
    The man page is saying that your implementation of `longjmperror()` shouldn't return if a more graceful exit than aborting is desired. So it should call `exit()` or `_exit()` instead of returning (for example). – jxh Mar 01 '14 at 21:27
  • I understand that. But long jump is a library function and i assume longjmperror is in the same library, so when i try to compile it will probably say longjmperror is a dublicated symbol. What i want to know is how to tell longjmp to use my longjmperror instead of the default one. There IS a default one. "The default version of longjmperror() prints the message ``longjmp botch'' to standard error and returns." – Jean-Luc Nacif Coelho Mar 02 '14 at 01:27

1 Answers1

1

On Mac OS X (10.9.2, Mavericks) at any rate, the prototype for longjmperror() is:

void longjmperror(void);

You write a function with that signature. It must not return (or, rather, if it does, the program will be abort()ed). What you do in that function is your business, but bear in mind that things have gone moderately catastrophically wrong for the function to be called at all). It might log an error to your log file, or just write a more meaningful message before exiting (instead of aborting and perhaps core dumping).

You link the object file containing the function ahead of the system library. You are normally not expected to replace system functions, but this is one you are intended to override.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278