| 1 |
//===-- runtime/extensions.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 |
// These C-coded entry points with Fortran-mangled names implement legacy |
| 10 |
// extensions that will eventually be implemented in Fortran. |
| 11 |
|
| 12 |
#include "flang/Runtime/extensions.h" |
| 13 |
#include "terminator.h" |
| 14 |
#include "tools.h" |
| 15 |
#include "flang/Runtime/command.h" |
| 16 |
#include "flang/Runtime/descriptor.h" |
| 17 |
#include "flang/Runtime/entry-names.h" |
| 18 |
#include "flang/Runtime/io-api.h" |
| 19 |
#include <chrono> |
| 20 |
#include <cstring> |
| 21 |
#include <ctime> |
| 22 |
#include <signal.h> |
| 23 |
#include <thread> |
| 24 |
|
| 25 |
#ifdef _WIN32 |
| 26 |
#define WIN32_LEAN_AND_MEAN |
| 27 |
#define NOMINMAX |
| 28 |
#include <windows.h> |
| 29 |
|
| 30 |
#include <synchapi.h> |
| 31 |
|
| 32 |
inline void CtimeBuffer(char *buffer, size_t bufsize, const time_t cur_time, |
| 33 |
Fortran::runtime::Terminator terminator) { |
| 34 |
int error{ctime_s(buffer, bufsize, &cur_time)}; |
| 35 |
RUNTIME_CHECK(terminator, error == 0); |
| 36 |
} |
| 37 |
#elif _POSIX_C_SOURCE >= 1 || _XOPEN_SOURCE || _BSD_SOURCE || _SVID_SOURCE || \ |
| 38 |
defined(_POSIX_SOURCE) |
| 39 |
inline void CtimeBuffer(char *buffer, size_t bufsize, const time_t cur_time, |
| 40 |
Fortran::runtime::Terminator terminator) { |
| 41 |
const char *res{ctime_r(&cur_time, buffer)}; |
| 42 |
RUNTIME_CHECK(terminator, res != nullptr); |
| 43 |
} |
| 44 |
#else |
| 45 |
inline void CtimeBuffer(char *buffer, size_t bufsize, const time_t cur_time, |
| 46 |
Fortran::runtime::Terminator terminator) { |
| 47 |
buffer[0] = '\0'; |
| 48 |
terminator.Crash("fdate is not supported."); |
| 49 |
} |
| 50 |
#endif |
| 51 |
|
| 52 |
#ifndef _WIN32 |
| 53 |
// posix-compliant and has getlogin_r and F_OK |
| 54 |
#include <unistd.h> |
| 55 |
#endif |
| 56 |
|
| 57 |
extern "C" { |
| 58 |
|
| 59 |
namespace Fortran::runtime { |
| 60 |
|
| 61 |
void GetUsernameEnvVar(const char *envName, char *arg, std::int64_t length) { |
| 62 |
Descriptor name{*Descriptor::Create( |
| 63 |
1, std::strlen(envName) + 1, const_cast<char *>(envName), 0)}; |
| 64 |
Descriptor value{*Descriptor::Create(1, length, arg, 0)}; |
| 65 |
|
| 66 |
RTNAME(GetEnvVariable) |
| 67 |
(name, &value, nullptr, false, nullptr, __FILE__, __LINE__); |
| 68 |
} |
| 69 |
namespace io { |
| 70 |
// SUBROUTINE FLUSH(N) |
| 71 |
// FLUSH N |
| 72 |
// END |
| 73 |
void FORTRAN_PROCEDURE_NAME(flush)(const int &unit) { |
| 74 |
Cookie cookie{IONAME(BeginFlush)(unit, __FILE__, __LINE__)}; |
| 75 |
IONAME(EndIoStatement)(cookie); |
| 76 |
} |
| 77 |
} // namespace io |
| 78 |
|
| 79 |
// CALL FDATE(DATE) |
| 80 |
void FORTRAN_PROCEDURE_NAME(fdate)(char *arg, std::int64_t length) { |
| 81 |
// Day Mon dd hh:mm:ss yyyy\n\0 is 26 characters, e.g. |
| 82 |
// Tue May 26 21:51:03 2015\n\0 |
| 83 |
char str[26]; |
| 84 |
// Insufficient space, fill with spaces and return. |
| 85 |
if (length < 24) { |
| 86 |
std::memset(arg, ' ', length); |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
Terminator terminator{__FILE__, __LINE__}; |
| 91 |
std::time_t current_time; |
| 92 |
std::time(¤t_time); |
| 93 |
CtimeBuffer(str, sizeof(str), current_time, terminator); |
| 94 |
|
| 95 |
// Pad space on the last two byte `\n\0`, start at index 24 included. |
| 96 |
CopyAndPad(arg, str, length, 24); |
| 97 |
} |
| 98 |
|
| 99 |
// RESULT = IARGC() |
| 100 |
std::int32_t FORTRAN_PROCEDURE_NAME(iargc)() { return RTNAME(ArgumentCount)(); } |
| 101 |
|
| 102 |
// CALL GETARG(N, ARG) |
| 103 |
void FORTRAN_PROCEDURE_NAME(getarg)( |
| 104 |
std::int32_t &n, char *arg, std::int64_t length) { |
| 105 |
Descriptor value{*Descriptor::Create(1, length, arg, 0)}; |
| 106 |
(void)RTNAME(GetCommandArgument)( |
| 107 |
n, &value, nullptr, nullptr, __FILE__, __LINE__); |
| 108 |
} |
| 109 |
|
| 110 |
// CALL GETLOG(USRNAME) |
| 111 |
void FORTRAN_PROCEDURE_NAME(getlog)(char *arg, std::int64_t length) { |
| 112 |
#if _REENTRANT || _POSIX_C_SOURCE >= 199506L |
| 113 |
if (length >= 1 && getlogin_r(arg, length) == 0) { |
| 114 |
auto loginLen{std::strlen(arg)}; |
| 115 |
std::memset( |
| 116 |
arg + loginLen, ' ', static_cast<std::size_t>(length) - loginLen); |
| 117 |
return; |
| 118 |
} |
| 119 |
#endif |
| 120 |
#if _WIN32 |
| 121 |
GetUsernameEnvVar("USERNAME", arg, length); |
| 122 |
#else |
| 123 |
GetUsernameEnvVar("LOGNAME", arg, length); |
| 124 |
#endif |
| 125 |
} |
| 126 |
|
| 127 |
std::int64_t RTNAME(Signal)(std::int64_t number, void (*handler)(int)) { |
| 128 |
// using auto for portability: |
| 129 |
// on Windows, this is a void * |
| 130 |
// on POSIX, this has the same type as handler |
| 131 |
auto result = signal(number, handler); |
| 132 |
|
| 133 |
// GNU defines the intrinsic as returning an integer, not a pointer. So we |
| 134 |
// have to reinterpret_cast |
| 135 |
return static_cast<int64_t>(reinterpret_cast<std::uintptr_t>(result)); |
| 136 |
} |
| 137 |
|
| 138 |
// CALL SLEEP(SECONDS) |
| 139 |
void RTNAME(Sleep)(std::int64_t seconds) { |
| 140 |
// ensure that conversion to unsigned makes sense, |
| 141 |
// sleep(0) is an immidiate return anyway |
| 142 |
if (seconds < 1) { |
| 143 |
return; |
| 144 |
} |
| 145 |
#if _WIN32 |
| 146 |
Sleep(seconds * 1000); |
| 147 |
#else |
| 148 |
sleep(seconds); |
| 149 |
#endif |
| 150 |
} |
| 151 |
|
| 152 |
// TODO: not supported on Windows |
| 153 |
#ifndef _WIN32 |
| 154 |
std::int64_t FORTRAN_PROCEDURE_NAME(access)(const char *name, |
| 155 |
std::int64_t nameLength, const char *mode, std::int64_t modeLength) { |
| 156 |
std::int64_t ret{-1}; |
| 157 |
if (nameLength <= 0 || modeLength <= 0 || !name || !mode) { |
| 158 |
return ret; |
| 159 |
} |
| 160 |
|
| 161 |
// ensure name is null terminated |
| 162 |
char *newName{nullptr}; |
| 163 |
if (name[nameLength - 1] != '\0') { |
| 164 |
newName = static_cast<char *>(std::malloc(nameLength + 1)); |
| 165 |
std::memcpy(newName, name, nameLength); |
| 166 |
newName[nameLength] = '\0'; |
| 167 |
name = newName; |
| 168 |
} |
| 169 |
|
| 170 |
// calculate mode |
| 171 |
bool read{false}; |
| 172 |
bool write{false}; |
| 173 |
bool execute{false}; |
| 174 |
bool exists{false}; |
| 175 |
int imode{0}; |
| 176 |
|
| 177 |
for (std::int64_t i = 0; i < modeLength; ++i) { |
| 178 |
switch (mode[i]) { |
| 179 |
case 'r': |
| 180 |
read = true; |
| 181 |
break; |
| 182 |
case 'w': |
| 183 |
write = true; |
| 184 |
break; |
| 185 |
case 'x': |
| 186 |
execute = true; |
| 187 |
break; |
| 188 |
case ' ': |
| 189 |
exists = true; |
| 190 |
break; |
| 191 |
default: |
| 192 |
// invalid mode |
| 193 |
goto cleanup; |
| 194 |
} |
| 195 |
} |
| 196 |
if (!read && !write && !execute && !exists) { |
| 197 |
// invalid mode |
| 198 |
goto cleanup; |
| 199 |
} |
| 200 |
|
| 201 |
if (!read && !write && !execute) { |
| 202 |
imode = F_OK; |
| 203 |
} else { |
| 204 |
if (read) { |
| 205 |
imode |= R_OK; |
| 206 |
} |
| 207 |
if (write) { |
| 208 |
imode |= W_OK; |
| 209 |
} |
| 210 |
if (execute) { |
| 211 |
imode |= X_OK; |
| 212 |
} |
| 213 |
} |
| 214 |
ret = access(name, imode); |
| 215 |
|
| 216 |
cleanup: |
| 217 |
if (newName) { |
| 218 |
free(newName); |
| 219 |
} |
| 220 |
return ret; |
| 221 |
} |
| 222 |
#endif |
| 223 |
|
| 224 |
} // namespace Fortran::runtime |
| 225 |
} // extern "C" |