From 1f467580eed557e70d6857b9ba472c0b30f9fbb7 Mon Sep 17 00:00:00 2001 From: Bananymous Date: Wed, 14 Feb 2024 17:23:26 +0200 Subject: [PATCH] Userspace: Add test for popen --- userspace/CMakeLists.txt | 1 + userspace/test-popen/CMakeLists.txt | 16 +++++++++++++++ userspace/test-popen/main.cpp | 30 +++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+) create mode 100644 userspace/test-popen/CMakeLists.txt create mode 100644 userspace/test-popen/main.cpp diff --git a/userspace/CMakeLists.txt b/userspace/CMakeLists.txt index facadf46..85e158a3 100644 --- a/userspace/CMakeLists.txt +++ b/userspace/CMakeLists.txt @@ -34,6 +34,7 @@ set(USERSPACE_PROJECTS test-framebuffer test-globals test-mouse + test-popen test-sort test-tcp test-udp diff --git a/userspace/test-popen/CMakeLists.txt b/userspace/test-popen/CMakeLists.txt new file mode 100644 index 00000000..02c38f23 --- /dev/null +++ b/userspace/test-popen/CMakeLists.txt @@ -0,0 +1,16 @@ +cmake_minimum_required(VERSION 3.26) + +project(test-popen CXX) + +set(SOURCES + main.cpp +) + +add_executable(test-popen ${SOURCES}) +target_compile_options(test-popen PUBLIC -O2 -g) +target_link_libraries(test-popen PUBLIC libc) + +add_custom_target(test-popen-install + COMMAND ${CMAKE_COMMAND} -E copy ${CMAKE_CURRENT_BINARY_DIR}/test-popen ${BANAN_BIN}/ + DEPENDS test-popen +) diff --git a/userspace/test-popen/main.cpp b/userspace/test-popen/main.cpp new file mode 100644 index 00000000..89d4cede --- /dev/null +++ b/userspace/test-popen/main.cpp @@ -0,0 +1,30 @@ +#include +#include + +int main(int argc, char** argv) +{ + if (argc != 2) + { + fprintf(stderr, "usage: %s COMMAND\n", argv[0]); + return 1; + } + + FILE* fp = popen(argv[1], "r"); + if (fp == nullptr) + { + perror("popen"); + return 1; + } + + char buffer[128]; + while (fgets(buffer, sizeof(buffer), fp) != NULL) + printf("%s", buffer); + + if (pclose(fp) == -1) + { + perror("pclose"); + return 1; + } + + return 0; +}