xref: /llvm-project/libc/src/stdlib/atoi.cpp (revision 5ff3ff33ff930e4ec49da7910612d8a41eb068cb)
1bad3168bSMichael Jones //===-- Implementation of atoi --------------------------------------------===//
2bad3168bSMichael Jones //
3bad3168bSMichael Jones // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4bad3168bSMichael Jones // See https://llvm.org/LICENSE.txt for license information.
5bad3168bSMichael Jones // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6bad3168bSMichael Jones //
7bad3168bSMichael Jones //===----------------------------------------------------------------------===//
8bad3168bSMichael Jones 
9bad3168bSMichael Jones #include "src/stdlib/atoi.h"
10bad3168bSMichael Jones #include "src/__support/common.h"
11*5ff3ff33SPetr Hosek #include "src/__support/macros/config.h"
1231d797f4SMichael Jones #include "src/__support/str_to_integer.h"
1304a9c625SMichael Jones #include "src/errno/libc_errno.h"
14bad3168bSMichael Jones 
15*5ff3ff33SPetr Hosek namespace LIBC_NAMESPACE_DECL {
16bad3168bSMichael Jones 
17bad3168bSMichael Jones LLVM_LIBC_FUNCTION(int, atoi, (const char *str)) {
18ce4d6717SMichael Jones   // This is done because the standard specifies that atoi is identical to
19ce4d6717SMichael Jones   // (int)(strtol).
20ce4d6717SMichael Jones   auto result = internal::strtointeger<long>(str, 10);
2174da5e6cSMichael Jones   if (result.has_error())
2204a9c625SMichael Jones     libc_errno = result.error;
2374da5e6cSMichael Jones 
24ce4d6717SMichael Jones   return static_cast<int>(result);
25bad3168bSMichael Jones }
26bad3168bSMichael Jones 
27*5ff3ff33SPetr Hosek } // namespace LIBC_NAMESPACE_DECL
28