|
| 1 | +//===---------- Linux implementation of the ioctl function ----------------===// |
| 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/sys/ioctl/ioctl.h" |
| 10 | + |
| 11 | +#include "src/__support/OSUtil/syscall.h" // For internal syscall function. |
| 12 | +#include "src/__support/common.h" |
| 13 | +#include "src/errno/libc_errno.h" |
| 14 | +#include <stdarg.h> |
| 15 | +#include <sys/syscall.h> // For syscall numbers. |
| 16 | + |
| 17 | +namespace LIBC_NAMESPACE_DECL { |
| 18 | + |
| 19 | +LLVM_LIBC_FUNCTION(int, ioctl, (int fd, unsigned long request, ...)) { |
| 20 | + va_list vargs; |
| 21 | + va_start(vargs, request); |
| 22 | + void *data_pointer = va_arg(vargs, void *); |
| 23 | + int ret = |
| 24 | + LIBC_NAMESPACE::syscall_impl<int>(SYS_ioctl, fd, request, data_pointer); |
| 25 | + va_end(vargs); |
| 26 | + |
| 27 | + // From `man ioctl`: |
| 28 | + // "Usually, on success zero is returned. A few ioctl() operations |
| 29 | + // use the return value as an output parameter and return a |
| 30 | + // nonnegative value on success. On error, -1 is returned, and errno |
| 31 | + // is set to indicate the error." |
| 32 | + if (ret < 0) { |
| 33 | + libc_errno = -ret; |
| 34 | + return -1; |
| 35 | + } |
| 36 | + |
| 37 | + return ret; |
| 38 | +} |
| 39 | + |
| 40 | +} // namespace LIBC_NAMESPACE_DECL |
0 commit comments