1 //===-- runtime/command.cpp -----------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "flang/Runtime/command.h"
10 #include "environment.h"
11 #include <limits>
12 
13 namespace Fortran::runtime {
14 std::int32_t RTNAME(ArgumentCount)() {
15   int argc{executionEnvironment.argc};
16   if (argc > 1) {
17     // C counts the command name as one of the arguments, but Fortran doesn't.
18     return argc - 1;
19   }
20   return 0;
21 }
22 
23 std::int64_t RTNAME(ArgumentLength)(std::int32_t n) {
24   if (n < 0 || n >= executionEnvironment.argc) {
25     return 0;
26   }
27 
28   std::size_t length{std::strlen(executionEnvironment.argv[n])};
29   if constexpr (sizeof(std::size_t) <= sizeof(std::int64_t)) {
30     return static_cast<std::int64_t>(length);
31   } else {
32     std::size_t max{std::numeric_limits<std::int64_t>::max()};
33     return length > max ? 0 // Just fail.
34                         : static_cast<std::int64_t>(length);
35   }
36 }
37 } // namespace Fortran::runtime
38