4
0
mirror of git://sourceware.org/git/newlib-cygwin.git synced 2025-01-29 02:20:21 +08:00
Jeff Johnston dc824ef736 2002-05-15 Jeff Johnston <jjohnstn@redhat.com>
* libc/include/stdlib.h: Add on_exit prototype.
        * libc/include/sys/reent.h (struct _atexit): Add argument array
        and bits to track type of exit routine to support both on_exit
        and atexit.
        (_REENT_INIT_PTR): Add missing fields that won't be zeroed out
        by default and change the setting of the atexit structure.
        (_REENT_INIT)[!_REENT_SMALL]: Remove extraneous end brace.
        * libc/stdlib/on_exit.c: New file.
        * libc/stdlib/Makefile.am: Add support for on_exit.
        * libc/stdlib/Makefile.in: Regenerated.
        * libc/stdlib/atexit.c: Change to initialize types field.
        * libc/stdlib/exit.c: Change to look at types field for each
        exit routine and either call an atexit-style or an on_exit-style
        routine accordingly.
2002-05-15 22:58:10 +00:00

84 lines
1.9 KiB
C

/*
* Copyright (c) 1990 Regents of the University of California.
* All rights reserved.
*
* %sccs.include.redist.c%
*/
/*
FUNCTION
<<exit>>---end program execution
INDEX
exit
ANSI_SYNOPSIS
#include <stdlib.h>
void exit(int <[code]>);
TRAD_SYNOPSIS
#include <stdlib.h>
void exit(<[code]>)
int <[code]>;
DESCRIPTION
Use <<exit>> to return control from a program to the host operating
environment. Use the argument <[code]> to pass an exit status to the
operating environment: two particular values, <<EXIT_SUCCESS>> and
<<EXIT_FAILURE>>, are defined in `<<stdlib.h>>' to indicate success or
failure in a portable fashion.
<<exit>> does two kinds of cleanup before ending execution of your
program. First, it calls all application-defined cleanup functions
you have enrolled with <<atexit>>. Second, files and streams are
cleaned up: any pending output is delivered to the host system, each
open file or stream is closed, and files created by <<tmpfile>> are
deleted.
RETURNS
<<exit>> does not return to its caller.
PORTABILITY
ANSI C requires <<exit>>, and specifies that <<EXIT_SUCCESS>> and
<<EXIT_FAILURE>> must be defined.
Supporting OS subroutines required: <<_exit>>.
*/
#include <stdlib.h>
#include <unistd.h> /* for _exit() declaration */
#include <reent.h>
#ifndef _REENT_ONLY
/*
* Exit, flushing stdio buffers if necessary.
*/
void
_DEFUN (exit, (code),
int code)
{
register struct _atexit *p;
register int n;
int i = 1;
#ifdef _REENT_SMALL
for (p = &_REENT->_atexit, n = p->_ind-1, i = (n>=0) ? (1<<n) : 0;
n >= 0; --n, i >>= 1)
#else
for (p = _REENT->_atexit; p; p = p->_next)
for (n = p->_ind - 1, i = (n >= 0) ? (1 << n) : 0; n >= 0; --n, i >>= 1)
#endif
if (p->_fntypes & i)
(*((void (*)(int, void *))p->_fns[n]))(code, p->_fnargs[n]);
else
(*p->_fns[n]) ();
if (_REENT->__cleanup)
(*_REENT->__cleanup) (_REENT);
_exit (code);
}
#endif