atexit()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Steve Lambert

    #1

    atexit()

    Hi,

    Is it possible for the functions registered with atexit() to have access to
    parameter to exit i.e EXIT_SUCCESS or EXIT_FAILURE?

    I'd like to register a set of routines to be executed on exit(EXIT_FAILU RE)
    and a different set of routines on exit(EXIT_SUCCE SS).

    Any way of doing this?

    Cheers

    Steve


  • Mike Wahler

    #2
    Re: atexit()


    "Steve Lambert" <steve.lambert@ ntlworld.com> wrote in message
    news:TmZ6d.740$ UK2.428@newsfe1-gui.ntli.net...[color=blue]
    > Hi,
    >
    > Is it possible for the functions registered with atexit() to have access[/color]
    to[color=blue]
    > parameter to exit i.e EXIT_SUCCESS or EXIT_FAILURE?
    >
    > I'd like to register a set of routines to be executed on[/color]
    exit(EXIT_FAILU RE)[color=blue]
    > and a different set of routines on exit(EXIT_SUCCE SS).
    >
    > Any way of doing this?[/color]

    My first thought was to pass this info ('failure' or 'success')
    via an argument to the registered function. BUT:

    =============== =============== ============
    From the C standard:

    7.20.4.2 The atexit function

    Synopsis

    1 #include <stdlib.h>
    int atexit(void (*func)(void));

    Description

    2 The atexit function registers the function pointed to by func,
    to be called without arguments at normal program termination.

    Environmental limits

    3 The implementation shall support the registration of at least
    32 functions.

    Returns

    4 The atexit function returns zero if the registration succeeds,
    nonzero if it fails.

    =============== =============== ============


    So, as much as I dislike using globals, it seems that would
    be the way to pass this info: set a global to EXIT_SUCCESS
    or EXIT_FAILURE (or whatever other value you find useful),
    and have a 'controlling' registered function inspect that
    value, and invoke one of a set of other functions which
    performs the desired actions.

    -Mike


    Comment

    • Peter Shaggy Haywood

      #3
      Re: atexit()

      Groovy hepcat Mike Wahler was jivin' on Thu, 30 Sep 2004 23:21:34 GMT
      in comp.lang.c.
      Re: atexit()'s a cool scene! Dig it!
      [color=blue]
      >"Steve Lambert" <steve.lambert@ ntlworld.com> wrote in message
      >news:TmZ6d.740 $UK2.428@newsfe 1-gui.ntli.net...[color=green]
      >>
      >> Is it possible for the functions registered with atexit() to have access[/color]
      >to[color=green]
      >> parameter to exit i.e EXIT_SUCCESS or EXIT_FAILURE?
      >>
      >> I'd like to register a set of routines to be executed on[/color]
      >exit(EXIT_FAIL URE)[color=green]
      >> and a different set of routines on exit(EXIT_SUCCE SS).
      >>
      >> Any way of doing this?[/color][/color]

      [Snip.]
      [color=blue]
      >So, as much as I dislike using globals, it seems that would
      >be the way to pass this info: set a global to EXIT_SUCCESS
      >or EXIT_FAILURE (or whatever other value you find useful),
      >and have a 'controlling' registered function inspect that
      >value, and invoke one of a set of other functions which
      >performs the desired actions.[/color]

      That's what I would have suggested if you hadn't beaten me to it. :)
      But I would add this.
      Make the so called global variable static and put it in its own
      translation unit. Also put in this translation unit an exit() wrapper
      that sets this variable and a function to read its value. When exiting
      the program, call the wrapper instead of exit() directly, and call
      the other function from the function registered with atexit().

      --

      Dig the even newer still, yet more improved, sig!


      "Ain't I'm a dog?" - Ronny Self, Ain't I'm a Dog, written by G. Sherry & W. Walker.
      I know it's not "technicall y correct" English; but since when was rock & roll "technicall y correct"?

      Comment

      Working...