📄 libc.sgml
字号:
<!-- {{{ Banner --><!-- =============================================================== --><!-- --><!-- libc.sgml --><!-- --><!-- eCos C Library --><!-- --><!-- =============================================================== --><!-- ####COPYRIGHTBEGIN#### --><!-- --><!-- =============================================================== --><!-- Copyright (C) 1997, 1998, 1999, 2000, 2001, 2002 Red Hat, Inc. --><!-- This material may be distributed only subject to the terms --><!-- and conditions set forth in the Open Publication License, v1.0 --><!-- or later (the latest version is presently available at --><!-- http://www.opencontent.org/openpub/) --><!-- Distribution of the work or derivative of the work in any --><!-- standard (paper) book form is prohibited unless prior --><!-- permission obtained from the copyright holder --><!-- =============================================================== --><!-- --> <!-- ####COPYRIGHTEND#### --><!-- =============================================================== --><!-- #####DESCRIPTIONBEGIN#### --><!-- --><!-- ####DESCRIPTIONEND#### --><!-- =============================================================== --><!-- }}} --><PART id="libc"><TITLE>The ISO Standard C and Math Libraries</TITLE><CHAPTER id="c-and-math-library-overview"><TITLE><!-- <xref> -->C and math library overview</TITLE><anchor id="iso-c-and-math-libraries"><PARA><EMPHASIS>eCos</EMPHASIS> provides compatibility with the ISO 9899:1990 specification for the standard C library, which is essentially the same as the better-known ANSI C3.159-1989 specification (C-89).</PARA><PARA>There are three aspects of this compatibility supplied by <EMPHASIS>eCos</EMPHASIS>.First there is a <!-- <index></index> --><firstterm>C library</firstterm> whichimplements the functions defined by the ISO standard, except for themathematical functions. This is provided by the eCos C librarypackages. </PARA><PARA>Then <EMPHASIS>eCos</EMPHASIS> provides a <!-- <index></index> -->mathlibrary, which implements the mathematical functions from the ISOC library. This distinction between C and math libraries is frequentlydrawn — most standard C library implementations provideseparate linkable files for the two, and the math library containsall the functions from the <filename>math.h</filename> headerfile.</PARA><PARA>There is a third element to the ISO C library, which is theenvironment in which applications run when they use the standardC library. This environment is set up by the C library startup procedure(<xref linkend="c-library-startup">)and it provides (among other things) a <FUNCTION>main()</FUNCTION> entrypoint function, an <FUNCTION>exit()</FUNCTION> function thatdoes the cleanup required by the standard (including handlers registeredusing the <FUNCTION>atexit()</FUNCTION> function), and an environmentthat can be read with <FUNCTION>getenv()</FUNCTION>. </PARA><PARA>The description in this manual focuses on the <EMPHASIS>eCos</EMPHASIS>-specificaspects of the C library (mostly related to <EMPHASIS>eCos</EMPHASIS>'sconfigurability) as well as mentioning the omissions from the standardin this release. We do not attempt to define the semantics of eachfunction, since that information can be found in the ISO, ANSI,POSIX and IEEE standards, and the many good books that have beenwritten about the standard C library, that cover usage of thesefunctions in a more general and useful way.</PARA><SECT1 id="include-non-iso-functions"><TITLE>Included non-ISO functions</TITLE><PARA>The following functions from the <!-- <index></index> -->POSIX specificationare included for convenience: </PARA><PARA> <literal>extern char **</literal><EMPHASIS>environ</EMPHASIS> variable(for setting up the environment for use with <FUNCTION>getenv()</FUNCTION>)</PARA><PARA> <FUNCTION>_exit()</FUNCTION> </PARA><PARA> <FUNCTION>strtok_r()</FUNCTION> </PARA><PARA> <FUNCTION>rand_r()</FUNCTION> </PARA><PARA> <FUNCTION>asctime_r()</FUNCTION> </PARA><PARA> <FUNCTION>ctime_r()</FUNCTION> </PARA><PARA> <FUNCTION>localtime_r()</FUNCTION> </PARA><PARA> <FUNCTION>gmtime_r()</FUNCTION> </PARA><PARA><EMPHASIS>eCos</EMPHASIS> provides the following additionalimplementation-specific functions within the standard C libraryto adjust the date and time settings:</PARA><PROGRAMLISTING>void <FUNCTION>cyg_libc_time_setdst</FUNCTION>( cyg_libc_time_dst state);</PROGRAMLISTING><PARA>This function sets the state of Daylight Savings Time. Thevalues for state are:</PARA><PROGRAMLISTING>CYG_LIBC_TIME_DSTNA unknownCYG_LIBC_TIME_DSTOFF offCYG_LIBC_TIME_DSTON on</PROGRAMLISTING><PROGRAMLISTING>void <FUNCTION>cyg_libc_time_setzoneoffsets</FUNCTION>( time_t stdoffset, time_t dstoffset);</PROGRAMLISTING><PARA>This function sets the offsets from UTC used when DaylightSavings Time is enabled or disabled. The offsets are in time_t’s,which are seconds in the current inplementation.</PARA><PROGRAMLISTING><FUNCTION>Cyg_libc_time_dst cyg_libc_time_getzoneoffsets</FUNCTION>( time_t *stdoffset, time_t *dstoffset);</PROGRAMLISTING><PARA>This function retrieves the current setting for Daylight SavingsTime along with the offsets used for both STD and DST. The offsetsare both in time_t’s, which are seconds in thecurrent implementation.</PARA><PROGRAMLISTING><FUNCTION>cyg_bool cyg_libc_time_settime</FUNCTION>( time_t utctime);</PROGRAMLISTING><PARA>This function sets the current time for the system The timeis specified as a <type>time_t</type> in UTC.It returns non-zero on error.</PARA></SECT1><SECT1 id="math-library-compatibility-modes"><TITLE>Math library compatibility modes</TITLE><PARA>This math library is capable of being operated in severaldifferent compatibility modes. These options deal solely with howerrors are handled. </PARA><PARA>There are 4 compatibility modes: ANSI/POSIX 1003.1;IEEE-754; X/Open Portability Guide issue 3 (XPG3); andSystem V Interface Definition Edition 3. </PARA><PARA>In <!-- <index></index> -->IEEE mode, the <function>matherr()</function> function(see below) is never called, no warning messages are printed onthe stderr output stream, and errno is never set. </PARA><PARA>In <!-- <index></index> -->ANSI/POSIX mode, errno is set correctly,but <function>matherr()</function> is never called and no warning messagesare printed on the stderr output stream. </PARA><PARA>In <!-- <index></index> -->X/Open mode, errno is set correctly,<function>matherr()</function> is called, but no warning messages are printedon the stderr output stream. </PARA><PARA>In <!-- <index></index> -->SVID mode, functions which overflow returna value HUGE (defined in <filename>math.h</filename>), which is the maximumsingle precision floating point value (as opposed toHUGE_VAL which is meant to stand for infinity). errno isset correctly and <function>matherr()</function> is called. If<function>matherr()</function> returns 0, warning messages are printed onthe stderr output stream for some errors. </PARA><PARA>The mode can be compiled-in as IEEE-only, or any one of theabove methods settable at run-time. </PARA><NOTE><PARA>This math library assumes that the hardware (or software floatingpoint emulation) supports IEEE-754 style arithmetic, 32-bit 2'scomplement integer arithmetic, doubles are in 64-bit IEEE-754 format.</PARA></NOTE><SECT2><TITLE><!-- <index></index> --> matherr()</TITLE><PARA>As mentioned above, in X/Open or SVID modes, the user can supply a function <FUNCTION>matherr()</FUNCTION> of the form:</PARA><PROGRAMLISTING>int <FUNCTION>matherr</FUNCTION>( struct exception *e )</PROGRAMLISTING><PARA>where struct exception is defined as:</PARA><PROGRAMLISTING>struct exception { int type; char *name; double arg1, arg2, retval;}; </PROGRAMLISTING><PARA>type is the exception type and is one of:</PARA><VARIABLELIST><VARLISTENTRY><TERM>DOMAIN </TERM><LISTITEM><PARA>argument domain exception</PARA></LISTITEM></VARLISTENTRY><VARLISTENTRY><TERM>SING </TERM><LISTITEM><PARA>argument singularity</PARA></LISTITEM></VARLISTENTRY><VARLISTENTRY><TERM>OVERFLOW </TERM><LISTITEM><PARA>overflow range exception</PARA></LISTITEM></VARLISTENTRY><VARLISTENTRY><TERM>UNDERFLOW </TERM><LISTITEM><PARA>underflow range exception</PARA></LISTITEM></VARLISTENTRY><VARLISTENTRY><TERM>TLOSS </TERM><LISTITEM><PARA>total loss of significance</PARA></LISTITEM></VARLISTENTRY><VARLISTENTRY><TERM>PLOSS </TERM><LISTITEM><PARA>partial loss of significance</PARA></LISTITEM></VARLISTENTRY></VARIABLELIST><PARA><parameter>name</parameter> is a string containing the name of thefunction</PARA><PARA><parameter>arg1</parameter> and <parameter>arg2</parameter> are thearguments passed to the function</PARA><PARA><parameter>retval</parameter> is the default value that will be returnedby the function, and can be changed by <FUNCTION>matherr()</FUNCTION></PARA><NOTE><PARA>matherr must have “C” linkage, not “C++” linkage.</PARA></NOTE><PARA>If matherr returns zero, or the user doesn't supplytheir own matherr, then the following <EMPHASIS>usually</EMPHASIS> happensin SVID mode:</PARA><!-- FIXME: must convert this table --> <table><title>Behavior of math exception handling</title><tgroup cols="2"><thead><row> <entry>Type</entry> <entry>Behavior</entry</row></thead><tbody><row><entry>DOMAIN</entry><entry>0.0 returned,errno=EDOM, and a message printed on stderr</entry></row><row><entry>SING</entry><entry>HUGE of appropriatesign is returned, errno=EDOM, and a message is printedon stderr</entry></row><row><entry>OVERFLOW</entry><entry>HUGE ofappropriate sign is returned, and errno=ERANGE</entry></row><row><entry>UNDERFLOW</entry><entry>0.0 isreturned and errno=ERANGE</entry></row><row><entry>TLOSS</entry><entry>0.0 is returned,errno=ERANGE, and a message is printed on stderr</entry></row><row><entry>PLOSS</entry><entry>The currentimplementation doesn't return this type</entry></row></tbody></tgroup></table><!-- --><PARA>X/Open mode is similar except that the message isnot printed on stderr and HUGE_VAL is used in place ofHUGE</PARA></SECT2><SECT2><TITLE>Thread-safety and re-entrancy</TITLE><PARA>With the appropriate configuration options set below, themath library is fully thread-safe if:</PARA><ITEMIZEDLIST><LISTITEM><PARA>Depending on the compatibility mode, the setting of the errno variable from the C library is thread-safe</PARA></LISTITEM><LISTITEM><PARA>Depending on the compatibility mode, sending error messagesto the stderr output stream using the C library <FUNCTION>fputs()</FUNCTION> function is thread-safe </PARA></LISTITEM><LISTITEM><PARA>Depending on the compatibility mode, the user-supplied <FUNCTION>matherr()</FUNCTION> function and anything it depends on are thread-safe </PARA></LISTITEM></ITEMIZEDLIST><PARA>In addition, with the exception of the <FUNCTION>gamma*()</FUNCTION> and <FUNCTION>lgamma*()</FUNCTION> functions,the math library is reentrant (and thus safe to use from interrupt handlers)if the Math library is always in IEEE mode.</PARA></SECT2></SECT1><SECT1 id="libc-implementation-details"><TITLE>Some implementation details</TITLE>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -