[libc-commits] [libc] [libc] Implement fileno on Linux (PR #85628)

Shourya Goel via libc-commits libc-commits at lists.llvm.org
Mon Mar 18 09:16:31 PDT 2024


https://github.com/Sh0g0-1758 updated https://github.com/llvm/llvm-project/pull/85628

>From 9b289311091c741674d270009f9557e9d9ac4760 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Mon, 18 Mar 2024 16:08:15 +0530
Subject: [PATCH 01/10] implement fileno

---
 libc/config/linux/x86_64/entrypoints.txt |  1 +
 libc/src/stdio/CMakeLists.txt            | 11 +++++++++++
 libc/src/stdio/fileno.h                  | 21 +++++++++++++++++++++
 libc/src/stdio/generic/CMakeLists.txt    | 12 ++++++++++++
 libc/src/stdio/generic/fileno.cpp        | 23 +++++++++++++++++++++++
 libc/test/src/stdio/fileop_test.cpp      |  1 +
 6 files changed, 69 insertions(+)
 create mode 100644 libc/src/stdio/fileno.h
 create mode 100644 libc/src/stdio/generic/fileno.cpp

diff --git a/libc/config/linux/x86_64/entrypoints.txt b/libc/config/linux/x86_64/entrypoints.txt
index 7e13a7c5793c30..32da6be77e5883 100644
--- a/libc/config/linux/x86_64/entrypoints.txt
+++ b/libc/config/linux/x86_64/entrypoints.txt
@@ -208,6 +208,7 @@ set(TARGET_LIBC_ENTRYPOINTS
     libc.src.stdio.sscanf
     libc.src.stdio.scanf
     libc.src.stdio.fscanf
+    libc.src.stdio.fileno
 
     # sys/epoll.h entrypoints
     libc.src.sys.epoll.epoll_wait
diff --git a/libc/src/stdio/CMakeLists.txt b/libc/src/stdio/CMakeLists.txt
index bb8e41606c5dfb..58dca404f93723 100644
--- a/libc/src/stdio/CMakeLists.txt
+++ b/libc/src/stdio/CMakeLists.txt
@@ -236,6 +236,17 @@ add_entrypoint_object(
     libc.src.stdio.printf_core.vfprintf_internal
 )
 
+add_stdio_entrypoint_object(
+  fileno
+  SRCS
+    fileno.cpp
+  HDRS
+    fileno.h
+  DEPENDS
+    libc.src.__support.arg_list
+    libc.src.stdio.fileno
+)
+
 add_subdirectory(printf_core)
 add_subdirectory(scanf_core)
 
diff --git a/libc/src/stdio/fileno.h b/libc/src/stdio/fileno.h
new file mode 100644
index 00000000000000..c0d67baa20f9a7
--- /dev/null
+++ b/libc/src/stdio/fileno.h
@@ -0,0 +1,21 @@
+//===-- Implementation header of fileno --------------------------*- C++
+//-*-===//
+//
+// 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
+//
+//===----------------------------------------------------------------------===//
+
+#ifndef LLVM_LIBC_SRC_STDIO_FILENO_H
+#define LLVM_LIBC_SRC_STDIO_FILENO_H
+
+#include <stdio.h>
+
+namespace LIBC_NAMESPACE {
+
+int fileno(::FILE *f);
+
+} // namespace LIBC_NAMESPACE
+
+#endif // LLVM_LIBC_SRC_STDIO_FILENO_H
diff --git a/libc/src/stdio/generic/CMakeLists.txt b/libc/src/stdio/generic/CMakeLists.txt
index 4e4a709e94061b..0aa213caba7b8a 100644
--- a/libc/src/stdio/generic/CMakeLists.txt
+++ b/libc/src/stdio/generic/CMakeLists.txt
@@ -70,6 +70,18 @@ add_entrypoint_object(
     libc.src.__support.File.platform_file
 )
 
+add_entrypoint_object(
+  fileno
+  SRCS
+    fileno.cpp
+  HDRS
+    ../fileno.h
+  DEPENDS
+    libc.include.stdio
+    libc.src.__support.File.file
+    libc.src.__support.File.platform_file
+)
+
 add_entrypoint_object(
   fflush
   SRCS
diff --git a/libc/src/stdio/generic/fileno.cpp b/libc/src/stdio/generic/fileno.cpp
new file mode 100644
index 00000000000000..81011c4bfe6fe3
--- /dev/null
+++ b/libc/src/stdio/generic/fileno.cpp
@@ -0,0 +1,23 @@
+//===-- Implementation of fileno
+//-------------------------------------------===//
+//
+// 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 "src/stdio/fileno.h"
+#include "src/__support/File/file.h"
+
+#include <stdio.h>
+
+namespace LIBC_NAMESPACE {
+
+LLVM_LIBC_FUNCTION(int, fileno, (::FILE * stream)) {
+  int result =
+      reinterpret_cast<LIBC_NAMESPACE::File *>(stream)->get_fileno(stream);
+  return result;
+}
+
+} // namespace LIBC_NAMESPACE
\ No newline at end of file
diff --git a/libc/test/src/stdio/fileop_test.cpp b/libc/test/src/stdio/fileop_test.cpp
index f5dbc49818390d..2f2e63eaf75d4c 100644
--- a/libc/test/src/stdio/fileop_test.cpp
+++ b/libc/test/src/stdio/fileop_test.cpp
@@ -30,6 +30,7 @@ TEST(LlvmLibcFILETest, SimpleFileOperations) {
   constexpr char FILENAME[] = "testdata/simple_operations.test";
   ::FILE *file = LIBC_NAMESPACE::fopen(FILENAME, "w");
   ASSERT_FALSE(file == nullptr);
+  ASSERT_EQ(LIBC_NAMESPACE::fileno(file), 3);
   constexpr char CONTENT[] = "1234567890987654321";
   ASSERT_EQ(sizeof(CONTENT) - 1,
             LIBC_NAMESPACE::fwrite(CONTENT, 1, sizeof(CONTENT) - 1, file));

>From 36355995bf1b6c57f2815fb998a9b62ed0e7e7f9 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Mon, 18 Mar 2024 16:25:03 +0530
Subject: [PATCH 02/10] Updated Param

---
 libc/src/stdio/generic/fileno.cpp | 3 +--
 1 file changed, 1 insertion(+), 2 deletions(-)

diff --git a/libc/src/stdio/generic/fileno.cpp b/libc/src/stdio/generic/fileno.cpp
index 81011c4bfe6fe3..9b32a7b28cbe60 100644
--- a/libc/src/stdio/generic/fileno.cpp
+++ b/libc/src/stdio/generic/fileno.cpp
@@ -15,8 +15,7 @@
 namespace LIBC_NAMESPACE {
 
 LLVM_LIBC_FUNCTION(int, fileno, (::FILE * stream)) {
-  int result =
-      reinterpret_cast<LIBC_NAMESPACE::File *>(stream)->get_fileno(stream);
+  int result = get_fileno(reinterpret_cast<LIBC_NAMESPACE::File *>(stream));
   return result;
 }
 

>From 38bc9a363145939af51852e02e353312ad73bd30 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Mon, 18 Mar 2024 16:26:05 +0530
Subject: [PATCH 03/10] Added Space

---
 libc/src/stdio/generic/fileno.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/stdio/generic/fileno.cpp b/libc/src/stdio/generic/fileno.cpp
index 9b32a7b28cbe60..881f570216b85c 100644
--- a/libc/src/stdio/generic/fileno.cpp
+++ b/libc/src/stdio/generic/fileno.cpp
@@ -19,4 +19,4 @@ LLVM_LIBC_FUNCTION(int, fileno, (::FILE * stream)) {
   return result;
 }
 
-} // namespace LIBC_NAMESPACE
\ No newline at end of file
+} // namespace LIBC_NAMESPACE

>From b9fd99ba9143f5d89624b161035dc261f4ed9e53 Mon Sep 17 00:00:00 2001
From: Sh0g0-1758 <shouryagoel10000 at gmail.com>
Date: Mon, 18 Mar 2024 21:14:24 +0530
Subject: [PATCH 04/10] minor changes

---
 libc/src/stdio/fileno.h           | 2 +-
 libc/src/stdio/generic/fileno.cpp | 5 ++---
 2 files changed, 3 insertions(+), 4 deletions(-)

diff --git a/libc/src/stdio/fileno.h b/libc/src/stdio/fileno.h
index c0d67baa20f9a7..b87239529cad0e 100644
--- a/libc/src/stdio/fileno.h
+++ b/libc/src/stdio/fileno.h
@@ -10,7 +10,7 @@
 #ifndef LLVM_LIBC_SRC_STDIO_FILENO_H
 #define LLVM_LIBC_SRC_STDIO_FILENO_H
 
-#include <stdio.h>
+#include <include/llvm-libc-types/FILE.h>
 
 namespace LIBC_NAMESPACE {
 
diff --git a/libc/src/stdio/generic/fileno.cpp b/libc/src/stdio/generic/fileno.cpp
index 881f570216b85c..6646c7d4768aad 100644
--- a/libc/src/stdio/generic/fileno.cpp
+++ b/libc/src/stdio/generic/fileno.cpp
@@ -10,13 +10,12 @@
 #include "src/stdio/fileno.h"
 #include "src/__support/File/file.h"
 
-#include <stdio.h>
+#include <include/llvm-libc-types/FILE.h>
 
 namespace LIBC_NAMESPACE {
 
 LLVM_LIBC_FUNCTION(int, fileno, (::FILE * stream)) {
-  int result = get_fileno(reinterpret_cast<LIBC_NAMESPACE::File *>(stream));
-  return result;
+  return get_fileno(reinterpret_cast<LIBC_NAMESPACE::File *>(stream));
 }
 
 } // namespace LIBC_NAMESPACE

>From a8de23eaa32d5870dd35e43f361f5d4fad35e79d Mon Sep 17 00:00:00 2001
From: Shourya Goel <shouryagoel10000 at gmail.com>
Date: Mon, 18 Mar 2024 21:33:34 +0530
Subject: [PATCH 05/10] Update fileno.h

---
 libc/src/stdio/fileno.h | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/stdio/fileno.h b/libc/src/stdio/fileno.h
index b87239529cad0e..d41f112226c512 100644
--- a/libc/src/stdio/fileno.h
+++ b/libc/src/stdio/fileno.h
@@ -10,7 +10,7 @@
 #ifndef LLVM_LIBC_SRC_STDIO_FILENO_H
 #define LLVM_LIBC_SRC_STDIO_FILENO_H
 
-#include <include/llvm-libc-types/FILE.h>
+#include "include/llvm-libc-types/FILE.h"
 
 namespace LIBC_NAMESPACE {
 

>From ad6278bbff0e4eaa915bb2985189adfe582938b7 Mon Sep 17 00:00:00 2001
From: Shourya Goel <shouryagoel10000 at gmail.com>
Date: Mon, 18 Mar 2024 21:34:06 +0530
Subject: [PATCH 06/10] Update fileno.cpp

---
 libc/src/stdio/generic/fileno.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/stdio/generic/fileno.cpp b/libc/src/stdio/generic/fileno.cpp
index 6646c7d4768aad..783c1b35b65b49 100644
--- a/libc/src/stdio/generic/fileno.cpp
+++ b/libc/src/stdio/generic/fileno.cpp
@@ -10,7 +10,7 @@
 #include "src/stdio/fileno.h"
 #include "src/__support/File/file.h"
 
-#include <include/llvm-libc-types/FILE.h>
+#include "include/llvm-libc-types/FILE.h"
 
 namespace LIBC_NAMESPACE {
 

>From 9f29fd31a7070a8748c07babe836e131cd5fc792 Mon Sep 17 00:00:00 2001
From: Shourya Goel <shouryagoel10000 at gmail.com>
Date: Mon, 18 Mar 2024 21:37:07 +0530
Subject: [PATCH 07/10] Update fileno.cpp

---
 libc/src/stdio/generic/fileno.cpp | 5 ++---
 1 file changed, 2 insertions(+), 3 deletions(-)

diff --git a/libc/src/stdio/generic/fileno.cpp b/libc/src/stdio/generic/fileno.cpp
index 783c1b35b65b49..b0694c12dc42a9 100644
--- a/libc/src/stdio/generic/fileno.cpp
+++ b/libc/src/stdio/generic/fileno.cpp
@@ -7,10 +7,9 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "src/stdio/fileno.h"
-#include "src/__support/File/file.h"
-
 #include "include/llvm-libc-types/FILE.h"
+#include "src/__support/File/file.h"
+#include "src/stdio/fileno.h"
 
 namespace LIBC_NAMESPACE {
 

>From ce01b87300243661881885452a6ed9e865cf265e Mon Sep 17 00:00:00 2001
From: Shourya Goel <shouryagoel10000 at gmail.com>
Date: Mon, 18 Mar 2024 21:38:10 +0530
Subject: [PATCH 08/10] Update fileno.cpp

---
 libc/src/stdio/generic/fileno.cpp | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/libc/src/stdio/generic/fileno.cpp b/libc/src/stdio/generic/fileno.cpp
index b0694c12dc42a9..86cfbf44900ac3 100644
--- a/libc/src/stdio/generic/fileno.cpp
+++ b/libc/src/stdio/generic/fileno.cpp
@@ -8,8 +8,8 @@
 //===----------------------------------------------------------------------===//
 
 #include "include/llvm-libc-types/FILE.h"
-#include "src/__support/File/file.h"
 #include "src/stdio/fileno.h"
+#include "src/__support/File/file.h"
 
 namespace LIBC_NAMESPACE {
 

>From 94f009a4b054fd8a39565a5cdc87569f724709c9 Mon Sep 17 00:00:00 2001
From: Shourya Goel <shouryagoel10000 at gmail.com>
Date: Mon, 18 Mar 2024 21:45:23 +0530
Subject: [PATCH 09/10] Update libc/src/stdio/generic/fileno.cpp

Co-authored-by: Nick Desaulniers <nickdesaulniers at users.noreply.github.com>
---
 libc/src/stdio/generic/fileno.cpp | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/libc/src/stdio/generic/fileno.cpp b/libc/src/stdio/generic/fileno.cpp
index 86cfbf44900ac3..663ba926637622 100644
--- a/libc/src/stdio/generic/fileno.cpp
+++ b/libc/src/stdio/generic/fileno.cpp
@@ -7,8 +7,9 @@
 //
 //===----------------------------------------------------------------------===//
 
-#include "include/llvm-libc-types/FILE.h"
 #include "src/stdio/fileno.h"
+
+#include "include/llvm-libc-types/FILE.h"
 #include "src/__support/File/file.h"
 
 namespace LIBC_NAMESPACE {

>From 0bad035be8deb3176d09eaed2cdc88306f0d1c74 Mon Sep 17 00:00:00 2001
From: Shourya Goel <shouryagoel10000 at gmail.com>
Date: Mon, 18 Mar 2024 21:46:21 +0530
Subject: [PATCH 10/10] Update CMakeLists.txt

---
 libc/src/stdio/CMakeLists.txt | 1 -
 1 file changed, 1 deletion(-)

diff --git a/libc/src/stdio/CMakeLists.txt b/libc/src/stdio/CMakeLists.txt
index 58dca404f93723..ece93fd56ef0c5 100644
--- a/libc/src/stdio/CMakeLists.txt
+++ b/libc/src/stdio/CMakeLists.txt
@@ -243,7 +243,6 @@ add_stdio_entrypoint_object(
   HDRS
     fileno.h
   DEPENDS
-    libc.src.__support.arg_list
     libc.src.stdio.fileno
 )
 



More information about the libc-commits mailing list