1 /*
2  * Copyright (c) 1990 Regents of the University of California.
3  * All rights reserved.
4  *
5  * %sccs.include.redist.c%
6  */
7 
8 /*
9 FUNCTION
10 <<exit>>---end program execution
11 
12 INDEX
13 	exit
14 
15 SYNOPSIS
16 	#include <stdlib.h>
17 	void exit(int <[code]>);
18 
19 DESCRIPTION
20 Use <<exit>> to return control from a program to the host operating
21 environment.  Use the argument <[code]> to pass an exit status to the
22 operating environment: two particular values, <<EXIT_SUCCESS>> and
23 <<EXIT_FAILURE>>, are defined in `<<stdlib.h>>' to indicate success or
24 failure in a portable fashion.
25 
26 <<exit>> does two kinds of cleanup before ending execution of your
27 program.  First, it calls all application-defined cleanup functions
28 you have enrolled with <<atexit>>.  Second, files and streams are
29 cleaned up: any pending output is delivered to the host system, each
30 open file or stream is closed, and files created by <<tmpfile>> are
31 deleted.
32 
33 RETURNS
34 <<exit>> does not return to its caller.
35 
36 PORTABILITY
37 ANSI C requires <<exit>>, and specifies that <<EXIT_SUCCESS>> and
38 <<EXIT_FAILURE>> must be defined.
39 
40 Supporting OS subroutines required: <<_exit>>.
41 */
42 
43 #define _DEFAULT_SOURCE
44 #include <stdlib.h>
45 #include <unistd.h>	/* for _exit() declaration */
46 #ifndef TINY_STDIO
47 #include "../stdio/local.h"
48 #include <stdio.h>
49 #endif
50 #include "atexit.h"
51 
52 /*
53  * Exit, flushing stdio buffers if necessary.
54  */
55 
56 void
exit(int code)57 exit (int code)
58 {
59 #ifdef _LITE_EXIT
60   /* Refer to comments in __atexit.c for more details of lite exit.  */
61   void __call_exitprocs (int, void *) __attribute__((weak));
62   if (__call_exitprocs)
63 #endif
64     __call_exitprocs (code, NULL);
65 
66 #ifndef TINY_STDIO
67   if (_REENT_CLEANUP(_GLOBAL_REENT))
68     (*_REENT_CLEANUP(_GLOBAL_REENT)) ();
69   if (__stdio_exit_handler != NULL)
70     (*__stdio_exit_handler) ();
71 #endif
72   _exit (code);
73 }
74