24.01.2014 Views

Codice

Codice

Codice

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

354 volume VI os16<br />

Sorgenti della libreria generale 355<br />

3050016 | int digit;<br />

3050017 | //<br />

3050018 | bool flag_prefix_oct = 0;<br />

3050019 | bool flag_prefix_exa = 0;<br />

3050020 | bool flag_prefix_dec = 0;<br />

3050021 | //<br />

3050022 | // Check base and string.<br />

3050023 | //<br />

3050024 | if (base < 0<br />

3050025 | || base > 36<br />

3050026 | || base == 1 // With base 1 cannot do anything.<br />

3050027 | || string == NULL<br />

3050028 | || string[0] == 0)<br />

3050029 | {<br />

3050030 | if (endptr != NULL) *endptr = string;<br />

3050031 | errset (EINVAL); // Invalid argument.<br />

3050032 | return ((long int) 0);<br />

3050033 | }<br />

3050034 | //<br />

3050035 | // Eat initial spaces.<br />

3050036 | //<br />

3050037 | for (i = 0; isspace (string[i]); i++)<br />

3050038 | {<br />

3050039 | ;<br />

3050040 | }<br />

3050041 | //<br />

3050042 | // Check sign.<br />

3050043 | //<br />

3050044 | if (string[i] == ’+’)<br />

3050045 | {<br />

3050046 | sign = +1;<br />

3050047 | i++;<br />

3050048 | }<br />

3050049 | else if (string[i] == ’-’)<br />

3050050 | {<br />

3050051 | sign = -1;<br />

3050052 | i++;<br />

3050053 | }<br />

3050054 | //<br />

3050055 | // Check for prefix.<br />

3050056 | //<br />

3050057 | if (string[i] == ’0’)<br />

3050058 | {<br />

3050059 | if (string[i+1] == ’x’ || string[i+1] == ’X’)<br />

3050060 | {<br />

3050061 | flag_prefix_exa = 1;<br />

3050062 | }<br />

3050063 | else if (isoctal (string[i+1]))<br />

3050064 | {<br />

3050065 | flag_prefix_oct = 1;<br />

3050066 | }<br />

3050067 | else<br />

3050068 | {<br />

3050069 | flag_prefix_dec = 1;<br />

3050070 | }<br />

3050071 | }<br />

3050072 | else if (isdigit (string[i]))<br />

3050073 | {<br />

3050074 | flag_prefix_dec = 1;<br />

3050075 | }<br />

3050076 | //<br />

3050077 | // Check compatibility with requested base.<br />

3050078 | //<br />

3050079 | if (flag_prefix_exa)<br />

3050080 | {<br />

3050081 | //<br />

3050082 | // At the moment, there is a zero and a ‘x’. Might be<br />

3050083 | // exadecimal, or might be a number base 33 or more.<br />

3050084 | //<br />

3050085 | if (base == 0)<br />

3050086 | {<br />

3050087 | base = 16;<br />

3050088 | }<br />

3050089 | else if (base == 16)<br />

3050090 | {<br />

3050091 | ; // Ok.<br />

3050092 | }<br />

3050093 | else if (base >= 33)<br />

3050094 | {<br />

3050095 | ; // Ok.<br />

3050096 | }<br />

3050097 | else<br />

3050098 | {<br />

3050099 | //<br />

3050100 | // Incompatible sequence: only the initial zero is reported.<br />

3050101 | //<br />

3050102 | if (endptr != NULL) *endptr = &string[i+1];<br />

3050103 | return ((long int) 0);<br />

3050104 | }<br />

3050105 | //<br />

3050106 | // Move on, after the ‘0x’ prefix.<br />

3050107 | //<br />

3050108 | i += 2;<br />

3050109 | }<br />

3050110 | //<br />

3050111 | if (flag_prefix_oct)<br />

3050112 | {<br />

3050113 | //<br />

3050114 | // There is a zero and a digit.<br />

3050115 | //<br />

3050116 | if (base == 0)<br />

3050117 | {<br />

3050118 | base = 8;<br />

3050119 | }<br />

3050120 | //<br />

3050121 | // Move on, after the ‘0’ prefix.<br />

3050122 | //<br />

3050123 | i += 1;<br />

3050124 | }<br />

3050125 | //<br />

3050126 | if (flag_prefix_dec)<br />

3050127 | {<br />

3050128 | if (base == 0)<br />

3050129 | {<br />

3050130 | base = 10;<br />

3050131 | }<br />

3050132 | }<br />

3050133 | //<br />

3050134 | // Scan the string.<br />

3050135 | //<br />

3050136 | for (number = 0; string[i] != 0; i++)<br />

3050137 | {<br />

3050138 | if (string[i] >= ’0’ && string[i] = ’A’ && string[i] = ’a’ && string[i] (digit * sign))<br />

3050165 | {<br />

3050166 | //<br />

3050167 | // Check if the current digit can be safely computed.<br />

3050168 | //<br />

3050169 | previous = number;<br />

3050170 | number *= base;<br />

3050171 | number += digit;<br />

3050172 | if (number / base != previous)<br />

3050173 | {<br />

3050174 | //<br />

3050175 | // Out of range.<br />

3050176 | //<br />

3050177 | if (endptr != NULL) *endptr = &string[i+1];<br />

3050178 | errset (ERANGE); // Result too large.<br />

3050179 | if (sign > 0)<br />

3050180 | {<br />

3050181 | return (LONG_MAX);<br />

3050182 | }<br />

3050183 | else<br />

3050184 | {<br />

3050185 | return (LONG_MIN);<br />

3050186 | }<br />

3050187 | }<br />

3050188 | }<br />

3050189 | else<br />

3050190 | {<br />

3050191 | if (endptr != NULL) *endptr = &string[i];<br />

3050192 | return (number);<br />

3050193 | }<br />

3050194 | }<br />

3050195 | //<br />

3050196 | // The string is finished.<br />

3050197 | //<br />

3050198 | if (endptr != NULL) *endptr = &string[i];<br />

3050199 | //<br />

3050200 | return (number);<br />

3050201 |}<br />

105.10.19 lib/stdlib/strtoul.c<br />

Si veda la sezione 98.121.<br />

3060001 |#include <br />

3060002 |#include <br />

3060003 |#include <br />

3060004 |#include <br />

3060005 |//----------------------------------------------------------------------<br />

3060006 |// A really poor implementation. ,-(<br />

3060007 |//<br />

3060008 |unsigned long int<br />

3060009 |strtoul (const char *restrict string, char **restrict endptr, int base)<br />

3060010 |{<br />

3060011 | return ((unsigned long int) strtol (string, endptr, base));<br />

3060012 |}<br />

105.10.20 lib/stdlib/unsetenv.c<br />

Si veda la sezione 98.94.<br />

3070001 |#include <br />

3070002 |#include <br />

3070003 |#include <br />

3070004 |//----------------------------------------------------------------------<br />

3070005 |extern char *_environment[];<br />

3070006 |extern char *_environment_table[];<br />

3070007 |//----------------------------------------------------------------------<br />

3070008 |int<br />

3070009 |unsetenv (const char *name)<br />

3070010 |{<br />

3070011 | int e; // First index: environment table items.<br />

3070012 | int f; // Second index: environment string scan.<br />

3070013 | //<br />

3070014 | // Check if the input is empty. No error is reported.<br />

3070015 | //<br />

3070016 | if (name == NULL || strlen (name) == 0)<br />

3070017 | {<br />

3070018 | return (0);<br />

3070019 | }<br />

3070020 | //<br />

3070021 | // Check if the input is valid: error here is reported.<br />

3070022 | //<br />

«<br />

«

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!