Userspace: Add test for popen

This commit is contained in:
Bananymous 2024-02-14 17:23:26 +02:00
parent 1ba883719a
commit 1f467580ee
3 changed files with 47 additions and 0 deletions

View File

@ -34,6 +34,7 @@ set(USERSPACE_PROJECTS
test-framebuffer
test-globals
test-mouse
test-popen
test-sort
test-tcp
test-udp

View File

@ -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
)

View File

@ -0,0 +1,30 @@
#include <stdio.h>
#include <unistd.h>
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;
}