Files
clang-p2996/flang/runtime/command.cpp
Diana Picus 6359a4cdbf Revert "[flang] GET_COMMAND_ARGUMENT(VALUE) runtime implementation"
This reverts commit 0446f1299f and
df6302311f.

There's a warning on flang-aarch64-latest-gcc related to strncpy using
the result of strlen as a bound. I'll recommit with a fix.
2021-09-28 12:07:27 +00:00

38 lines
1.2 KiB
C++

//===-- 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
//
//===----------------------------------------------------------------------===//
#include "flang/Runtime/command.h"
#include "environment.h"
#include <limits>
namespace Fortran::runtime {
std::int32_t RTNAME(ArgumentCount)() {
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;
}
std::int64_t RTNAME(ArgumentLength)(std::int32_t n) {
if (n < 0 || n >= executionEnvironment.argc) {
return 0;
}
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);
}
}
} // namespace Fortran::runtime