2021-08-25 15:51:48 +08:00
|
|
|
//===-- runtime/command.cpp -----------------------------------------------===//
|
|
|
|
//
|
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2021-09-02 07:00:53 +08:00
|
|
|
#include "flang/Runtime/command.h"
|
2021-08-25 15:51:48 +08:00
|
|
|
#include "environment.h"
|
2021-09-03 17:42:04 +08:00
|
|
|
#include <limits>
|
2021-08-25 15:51:48 +08:00
|
|
|
|
|
|
|
namespace Fortran::runtime {
|
2021-09-03 17:42:04 +08:00
|
|
|
std::int32_t RTNAME(ArgumentCount)() {
|
2021-08-25 15:51:48 +08:00
|
|
|
int argc{executionEnvironment.argc};
|
|
|
|
if (argc > 1) {
|
|
|
|
// C counts the command name as one of the arguments, but Fortran doesn't.
|
|
|
|
return argc - 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2021-09-03 17:42:04 +08:00
|
|
|
|
2021-09-28 20:02:52 +08:00
|
|
|
std::int64_t RTNAME(ArgumentLength)(std::int32_t n) {
|
|
|
|
if (n < 0 || n >= executionEnvironment.argc) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-09-03 17:42:04 +08:00
|
|
|
std::size_t length{std::strlen(executionEnvironment.argv[n])};
|
|
|
|
if constexpr (sizeof(std::size_t) <= sizeof(std::int64_t)) {
|
|
|
|
return static_cast<std::int64_t>(length);
|
|
|
|
} else {
|
|
|
|
std::size_t max{std::numeric_limits<std::int64_t>::max()};
|
|
|
|
return length > max ? 0 // Just fail.
|
|
|
|
: static_cast<std::int64_t>(length);
|
|
|
|
}
|
|
|
|
}
|
2021-08-25 15:51:48 +08:00
|
|
|
} // namespace Fortran::runtime
|