Userspace: Add test for popen
This commit is contained in:
parent
1ba883719a
commit
1f467580ee
|
@ -34,6 +34,7 @@ set(USERSPACE_PROJECTS
|
|||
test-framebuffer
|
||||
test-globals
|
||||
test-mouse
|
||||
test-popen
|
||||
test-sort
|
||||
test-tcp
|
||||
test-udp
|
||||
|
|
|
@ -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
|
||||
)
|
|
@ -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;
|
||||
}
|
Loading…
Reference in New Issue