mirror of
https://git.FreeBSD.org/src.git
synced 2024-12-18 10:35:55 +00:00
Preserve errno.
According to C99: "The functions atof, atoi, atol, and atoll need not affect the value of the integer expression errno on an error. If the value of the result cannot be represented, the behavior is undefined."
This commit is contained in:
parent
9dd4281db8
commit
b12990ca58
Notes:
svn2git
2020-12-20 02:59:44 +00:00
svn path=/head/; revision=88469
@ -29,18 +29,26 @@
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* $FreeBSD$
|
||||
*/
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)atof.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
|
||||
double
|
||||
atof(ascii)
|
||||
const char *ascii;
|
||||
{
|
||||
return (strtod(ascii, NULL));
|
||||
double r;
|
||||
int saverr;
|
||||
|
||||
saverr = errno;
|
||||
r = strtod(ascii, (char **)NULL);
|
||||
errno = saverr;
|
||||
return r;
|
||||
}
|
||||
|
@ -29,18 +29,25 @@
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* $FreeBSD$
|
||||
*/
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)atoi.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
#include <stddef.h>
|
||||
|
||||
int
|
||||
atoi(str)
|
||||
const char *str;
|
||||
{
|
||||
return((int)strtol(str, (char **)NULL, 10));
|
||||
int r, saverr;
|
||||
|
||||
saverr = errno;
|
||||
r = (int)strtol(str, (char **)NULL, 10);
|
||||
errno = saverr;
|
||||
return r;
|
||||
}
|
||||
|
@ -82,6 +82,13 @@ representation.
|
||||
It is equivalent to:
|
||||
.Pp
|
||||
.Dl "strtoll(nptr, (char **)NULL, 10);"
|
||||
.Sh ERRORS
|
||||
The
|
||||
.Fn atol
|
||||
function does not detect errors.
|
||||
The
|
||||
.Fn atoll
|
||||
function does not detect errors.
|
||||
.Sh SEE ALSO
|
||||
.Xr atof 3 ,
|
||||
.Xr atoi 3 ,
|
||||
@ -94,7 +101,6 @@ The
|
||||
function
|
||||
conforms to
|
||||
.St -isoC .
|
||||
.Pp
|
||||
The
|
||||
.Fn atoll
|
||||
function
|
||||
|
@ -29,18 +29,26 @@
|
||||
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
||||
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
||||
* SUCH DAMAGE.
|
||||
*
|
||||
* $FreeBSD$
|
||||
*/
|
||||
|
||||
#if defined(LIBC_SCCS) && !defined(lint)
|
||||
static char sccsid[] = "@(#)atol.c 8.1 (Berkeley) 6/4/93";
|
||||
#endif /* LIBC_SCCS and not lint */
|
||||
|
||||
#include <stddef.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
long
|
||||
atol(str)
|
||||
const char *str;
|
||||
{
|
||||
return(strtol(str, (char **)NULL, 10));
|
||||
long r;
|
||||
int saverr;
|
||||
|
||||
saverr = errno;
|
||||
r = strtol(str, (char **)NULL, 10);
|
||||
errno = saverr;
|
||||
return r;
|
||||
}
|
||||
|
@ -33,12 +33,18 @@
|
||||
* $FreeBSD$
|
||||
*/
|
||||
|
||||
#include <stddef.h>
|
||||
#include <errno.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
long long
|
||||
atoll(str)
|
||||
const char *str;
|
||||
{
|
||||
return(strtoll(str, (char **)NULL, 10));
|
||||
long long r;
|
||||
int saverr;
|
||||
|
||||
saverr = errno;
|
||||
r = strtoll(str, (char **)NULL, 10);
|
||||
errno = saverr;
|
||||
return r;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user