Section 8.2 - Read and Write¶
Question¶
Copy input to output by using read and write system calls.
/*copy input to output */
#include <sys/syscall.h>
#include <unistd.h>
#define BUFSIZ 1024
int main() /* copy input to output */
{
char buf[BUFSIZ];
int n;
while ((n = read(0, buf, BUFSIZ)) > 0)
write(1, buf, n);
return 0;
}
Explanation¶
This uses the read and write system calls to copy input to output.
# Compile the program
gcc copy.c -o copy
# Test 1: Echo a simple string
echo "Hello, World!" | ./copy
# Test 2: Multiple lines
cat << 'EOL' | ./copy
Line 1
Line 2
Line 3
EOL
# Test 3: Binary data (create a file with some null bytes)
dd if=/dev/zero bs=1024 count=1 2>/dev/null | ./copy > /dev/null