<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html><head><meta http-equiv="Content-Type" content="text/html;charset=iso-8859-1"> <title>UbixOS V2: src/sys/lib/strtol.c Source File</title> <link href="doxygen.css" rel="stylesheet" type="text/css"> <link href="tabs.css" rel="stylesheet" type="text/css"> </head><body> <!-- Generated by Doxygen 1.4.7 --> <div class="tabs"> <ul> <li><a href="main.html"><span>Main Page</span></a></li> <li><a href="classes.html"><span>Data Structures</span></a></li> <li id="current"><a href="files.html"><span>Files</span></a></li> <li><a href="dirs.html"><span>Directories</span></a></li> <li> <form action="search.php" method="get"> <table cellspacing="0" cellpadding="0" border="0"> <tr> <td><label> <u>S</u>earch for </label></td> <td><input type="text" name="query" value="" size="20" accesskey="s"/></td> </tr> </table> </form> </li> </ul></div> <div class="tabs"> <ul> <li><a href="files.html"><span>File List</span></a></li> <li><a href="globals.html"><span>Globals</span></a></li> </ul></div> <div class="nav"> <a class="el" href="dir_897b6a2d7bab147dd1db58381aad3984.html">src</a> » <a class="el" href="dir_832905b1f7f5feaf61a306b40c0ac817.html">sys</a> » <a class="el" href="dir_d9dcf62a8e8b4cc91cbf2445d76a799b.html">lib</a></div> <h1>strtol.c</h1><a href="strtol_8c.html">Go to the documentation of this file.</a><div class="fragment"><pre class="fragment"><a name="l00001"></a>00001 <span class="preprocessor">#include <<a class="code" href="cdefs_8h.html">sys/cdefs.h</a>></span> <a name="l00002"></a>00002 <span class="comment">//#include <limits.h></span> <a name="l00003"></a>00003 <span class="comment">//#include <ctype.h></span> <a name="l00004"></a>00004 <span class="comment">//#include <stdlib.h></span> <a name="l00005"></a>00005 <a name="l00006"></a><a class="code" href="strtol_8c.html#e8a44c5a7436466221e0f3859d02420f">00006</a> <span class="preprocessor">#define LONG_MIN (-0x7fffffffL - 1)</span> <a name="l00007"></a><a class="code" href="strtol_8c.html#50fece4db74f09568b2938db583c5655">00007</a> <span class="preprocessor"></span><span class="preprocessor">#define LONG_MAX 0x7fffffffL</span> <a name="l00008"></a>00008 <span class="preprocessor"></span> <a name="l00009"></a>00009 <a name="l00010"></a>00010 <a name="l00011"></a>00011 <span class="keywordtype">long</span> <a name="l00012"></a><a class="code" href="strtol_8c.html#23a80e470a9dae66d16e7d25fbba122a">00012</a> <a class="code" href="string_8h.html#23a80e470a9dae66d16e7d25fbba122a">strtol</a>(<span class="keyword">const</span> <span class="keywordtype">char</span> * __restrict nptr, <span class="keywordtype">char</span> ** __restrict endptr, <span class="keywordtype">int</span> base) <a name="l00013"></a>00013 { <a name="l00014"></a>00014 <span class="keyword">const</span> <span class="keywordtype">char</span> *s; <a name="l00015"></a>00015 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> acc; <a name="l00016"></a>00016 <span class="keywordtype">char</span> c = 0x0; <span class="comment">/* to remove warning */</span> <a name="l00017"></a>00017 <span class="keywordtype">unsigned</span> <span class="keywordtype">long</span> cutoff; <a name="l00018"></a>00018 <span class="keywordtype">int</span> neg, any, cutlim; <a name="l00019"></a>00019 <a name="l00020"></a>00020 <span class="comment">/*</span> <a name="l00021"></a>00021 <span class="comment"> * Skip white space and pick up leading +/- sign if any.</span> <a name="l00022"></a>00022 <span class="comment"> * If base is 0, allow 0x for hex and 0 for octal, else</span> <a name="l00023"></a>00023 <span class="comment"> * assume decimal; if base is already 16, allow 0x.</span> <a name="l00024"></a>00024 <span class="comment"> */</span> <a name="l00025"></a>00025 s = nptr; <a name="l00026"></a>00026 <span class="comment">/*</span> <a name="l00027"></a>00027 <span class="comment"> do {</span> <a name="l00028"></a>00028 <span class="comment"> c = *s++;</span> <a name="l00029"></a>00029 <span class="comment"> } while (isspace((unsigned char)c));</span> <a name="l00030"></a>00030 <span class="comment"> */</span> <a name="l00031"></a>00031 <span class="keywordflow">if</span> (c == <span class="charliteral">'-'</span>) { <a name="l00032"></a>00032 neg = 1; <a name="l00033"></a>00033 c = *s++; <a name="l00034"></a>00034 } <span class="keywordflow">else</span> { <a name="l00035"></a>00035 neg = 0; <a name="l00036"></a>00036 <span class="keywordflow">if</span> (c == <span class="charliteral">'+'</span>) <a name="l00037"></a>00037 c = *s++; <a name="l00038"></a>00038 } <a name="l00039"></a>00039 <span class="keywordflow">if</span> ((base == 0 || base == 16) && <a name="l00040"></a>00040 c == <span class="charliteral">'0'</span> && (*s == <span class="charliteral">'x'</span> || *s == <span class="charliteral">'X'</span>)) { <a name="l00041"></a>00041 c = s[1]; <a name="l00042"></a>00042 s += 2; <a name="l00043"></a>00043 base = 16; <a name="l00044"></a>00044 } <a name="l00045"></a>00045 <span class="keywordflow">if</span> (base == 0) <a name="l00046"></a>00046 base = c == <span class="charliteral">'0'</span> ? 8 : 10; <a name="l00047"></a>00047 acc = any = 0; <a name="l00048"></a>00048 <span class="keywordflow">if</span> (base < 2 || base > 36) <a name="l00049"></a>00049 <span class="keywordflow">goto</span> noconv; <a name="l00050"></a>00050 <a name="l00051"></a>00051 <span class="comment">/*</span> <a name="l00052"></a>00052 <span class="comment"> * Compute the cutoff value between legal numbers and illegal</span> <a name="l00053"></a>00053 <span class="comment"> * numbers. That is the largest legal value, divided by the</span> <a name="l00054"></a>00054 <span class="comment"> * base. An input number that is greater than this value, if</span> <a name="l00055"></a>00055 <span class="comment"> * followed by a legal input character, is too big. One that</span> <a name="l00056"></a>00056 <span class="comment"> * is equal to this value may be valid or not; the limit</span> <a name="l00057"></a>00057 <span class="comment"> * between valid and invalid numbers is then based on the last</span> <a name="l00058"></a>00058 <span class="comment"> * digit. For instance, if the range for longs is</span> <a name="l00059"></a>00059 <span class="comment"> * [-2147483648..2147483647] and the input base is 10,</span> <a name="l00060"></a>00060 <span class="comment"> * cutoff will be set to 214748364 and cutlim to either</span> <a name="l00061"></a>00061 <span class="comment"> * 7 (neg==0) or 8 (neg==1), meaning that if we have accumulated</span> <a name="l00062"></a>00062 <span class="comment"> * a value > 214748364, or equal but the next digit is > 7 (or 8),</span> <a name="l00063"></a>00063 <span class="comment"> * the number is too big, and we will return a range error.</span> <a name="l00064"></a>00064 <span class="comment"> *</span> <a name="l00065"></a>00065 <span class="comment"> * Set 'any' if any `digits' consumed; make it negative to indicate</span> <a name="l00066"></a>00066 <span class="comment"> * overflow.</span> <a name="l00067"></a>00067 <span class="comment"> */</span> <a name="l00068"></a>00068 cutoff = neg ? (<span class="keywordtype">unsigned</span> long)-(<a class="code" href="strtol_8c.html#e8a44c5a7436466221e0f3859d02420f">LONG_MIN</a> + <a class="code" href="strtol_8c.html#50fece4db74f09568b2938db583c5655">LONG_MAX</a>) + <a class="code" href="strtol_8c.html#50fece4db74f09568b2938db583c5655">LONG_MAX</a> <a name="l00069"></a>00069 : <a class="code" href="strtol_8c.html#50fece4db74f09568b2938db583c5655">LONG_MAX</a>; <a name="l00070"></a>00070 cutlim = cutoff % base; <a name="l00071"></a>00071 cutoff /= base; <a name="l00072"></a>00072 <span class="keywordflow">for</span> ( ; ; c = *s++) { <a name="l00073"></a>00073 <span class="keywordflow">if</span> (c >= <span class="charliteral">'0'</span> && c <= <span class="charliteral">'9'</span>) <a name="l00074"></a>00074 c -= <span class="charliteral">'0'</span>; <a name="l00075"></a>00075 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (c >= <span class="charliteral">'A'</span> && c <= <span class="charliteral">'Z'</span>) <a name="l00076"></a>00076 c -= <span class="charliteral">'A'</span> - 10; <a name="l00077"></a>00077 <span class="keywordflow">else</span> <span class="keywordflow">if</span> (c >= <span class="charliteral">'a'</span> && c <= <span class="charliteral">'z'</span>) <a name="l00078"></a>00078 c -= <span class="charliteral">'a'</span> - 10; <a name="l00079"></a>00079 <span class="keywordflow">else</span> <a name="l00080"></a>00080 <span class="keywordflow">break</span>; <a name="l00081"></a>00081 <span class="keywordflow">if</span> (c >= base) <a name="l00082"></a>00082 <span class="keywordflow">break</span>; <a name="l00083"></a>00083 <span class="keywordflow">if</span> (any < 0 || acc > cutoff || (acc == cutoff && c > cutlim)) <a name="l00084"></a>00084 any = -1; <a name="l00085"></a>00085 <span class="keywordflow">else</span> { <a name="l00086"></a>00086 any = 1; <a name="l00087"></a>00087 acc *= base; <a name="l00088"></a>00088 acc += c; <a name="l00089"></a>00089 } <a name="l00090"></a>00090 } <a name="l00091"></a>00091 <span class="keywordflow">if</span> (any < 0) { <a name="l00092"></a>00092 acc = neg ? <a class="code" href="strtol_8c.html#e8a44c5a7436466221e0f3859d02420f">LONG_MIN</a> : <a class="code" href="strtol_8c.html#50fece4db74f09568b2938db583c5655">LONG_MAX</a>; <a name="l00093"></a>00093 <span class="comment">//errno = ERANGE;</span> <a name="l00094"></a>00094 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (!any) { <a name="l00095"></a>00095 noconv: <a name="l00096"></a>00096 <span class="comment">//errno = EINVAL;</span> <a name="l00097"></a>00097 cutoff = 0x0;<span class="comment">//UBU</span> <a name="l00098"></a>00098 } <span class="keywordflow">else</span> <span class="keywordflow">if</span> (neg) <a name="l00099"></a>00099 acc = -acc; <a name="l00100"></a>00100 <span class="keywordflow">if</span> (endptr != <a class="code" href="types_8h.html#070d2ce7b6bb7e5c05602aa8c308d0c4">NULL</a>) <a name="l00101"></a>00101 *endptr = (<span class="keywordtype">char</span> *)(any ? s - 1 : nptr); <a name="l00102"></a>00102 <span class="keywordflow">return</span> (acc); <a name="l00103"></a>00103 } </pre></div><hr size="1"><address style="align: right;"><small>Generated on Fri Dec 15 11:18:55 2006 for UbixOS V2 by <a href="http://www.doxygen.org/index.html"> <img src="doxygen.png" alt="doxygen" align="middle" border="0"></a> 1.4.7 </small></address> </body> </html>