|
| 1 | +//===-- Implementation of perror ------------------------------------------===// |
| 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 "src/stdio/perror.h" |
| 10 | +#include "src/__support/CPP/string_view.h" |
| 11 | +#include "src/__support/File/file.h" |
| 12 | +#include "src/__support/StringUtil/error_to_string.h" |
| 13 | +#include "src/__support/macros/config.h" |
| 14 | +#include "src/errno/libc_errno.h" |
| 15 | + |
| 16 | +namespace LIBC_NAMESPACE_DECL { |
| 17 | + |
| 18 | +namespace { |
| 19 | + |
| 20 | +// TODO: this is copied from `puts`, it should be moved to a shared utility. |
| 21 | +// Simple helper to unlock the file once destroyed. |
| 22 | +struct ScopedLock { |
| 23 | + ScopedLock(LIBC_NAMESPACE::File *stream) : stream(stream) { stream->lock(); } |
| 24 | + ~ScopedLock() { stream->unlock(); } |
| 25 | + |
| 26 | +private: |
| 27 | + LIBC_NAMESPACE::File *stream; |
| 28 | +}; |
| 29 | + |
| 30 | +int write_out(cpp::string_view str_view, File *f) { |
| 31 | + if (str_view.size() > 0) { |
| 32 | + auto result = f->write_unlocked(str_view.data(), str_view.size()); |
| 33 | + if (result.has_error()) |
| 34 | + return result.error; |
| 35 | + } |
| 36 | + return 0; |
| 37 | +} |
| 38 | + |
| 39 | +} // namespace |
| 40 | + |
| 41 | +// TODO: this seems like there should be some sort of queue system to |
| 42 | +// deduplicate this code. |
| 43 | +LLVM_LIBC_FUNCTION(void, perror, (const char *str)) { |
| 44 | + const char empty_str[1] = {'\0'}; |
| 45 | + if (str == nullptr) |
| 46 | + str = empty_str; |
| 47 | + cpp::string_view str_view(str); |
| 48 | + |
| 49 | + auto err_str = get_error_string(libc_errno); |
| 50 | + |
| 51 | + // We need to lock the stream to ensure the newline is always appended. |
| 52 | + ScopedLock lock(LIBC_NAMESPACE::stderr); |
| 53 | + int write_err; |
| 54 | + |
| 55 | + // FORMAT: |
| 56 | + // if str != nullptr and doesn't start with a null byte: |
| 57 | + // "[str]: [strerror(errno)]\n" |
| 58 | + // else |
| 59 | + // "[strerror(errno)]\n" |
| 60 | + if (str_view.size() > 0) { |
| 61 | + write_err = write_out(str_view, LIBC_NAMESPACE::stderr); |
| 62 | + if (write_err != 0) { |
| 63 | + libc_errno = write_err; |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + write_err = write_out(": ", LIBC_NAMESPACE::stderr); |
| 68 | + if (write_err != 0) { |
| 69 | + libc_errno = write_err; |
| 70 | + return; |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + write_err = write_out(err_str, LIBC_NAMESPACE::stderr); |
| 75 | + if (write_err != 0) { |
| 76 | + libc_errno = write_err; |
| 77 | + return; |
| 78 | + } |
| 79 | + |
| 80 | + write_err = write_out("\n", LIBC_NAMESPACE::stderr); |
| 81 | + if (write_err != 0) { |
| 82 | + libc_errno = write_err; |
| 83 | + return; |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +} // namespace LIBC_NAMESPACE_DECL |
0 commit comments