πŸ”₯🧠 WELCOME TO [[asymmetric_c/13_redirect_stdout_to_self/]]

πŸ’₯ The Redirection Paradox: What happens when you point stdout back into its own file?


πŸ“‚ Project: 13_redirect_stdout_to_self.c

/*
 * 🚨 Redirect stdout to a file, then reopen the file for reading.
 * Can we read what we just wrote? Or do we get stuck?
 */
 
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
 
int	main(void)
{
	const char *filename = "self_output.txt";
	int	fd;
 
	// Step 1: open file for writing and redirect stdout
	fd = open(filename, O_CREAT | O_WRONLY | O_TRUNC, 0644);
	if (fd == -1)
	{
		perror("open write");
		exit(EXIT_FAILURE);
	}
	printf("πŸ” Redirecting stdout to: %s\n", filename);
	if (dup2(fd, STDOUT_FILENO) == -1)
	{
		perror("dup2");
		exit(EXIT_FAILURE);
	}
	close(fd); // stdout is now redirected!
 
	// Step 2: write something to redirected stdout
	printf("This is going into the file now!\n");
 
	// Step 3: reopen same file for reading (while still writing into it)
	int	read_fd = open(filename, O_RDONLY);
	if (read_fd == -1)
	{
		perror("open read");
		exit(EXIT_FAILURE);
	}
	char	buf[128];
	ssize_t	n = read(read_fd, buf, sizeof(buf) - 1);
	if (n == -1)
	{
		perror("read");
		exit(EXIT_FAILURE);
	}
	buf[n] = '\0';
 
	// Step 4: output results (to redirected stdout!)
	printf("πŸ“₯ Read from file: %s", buf);
 
	close(read_fd);
	return (0);
}

⚠️ What to Expect

πŸ’£ Expected Output (in file self_output.txt):

This is going into the file now!
πŸ“₯ Read from file: This is going into the file now!

πŸ’₯ It might work… but try flipping order of operations or using buffered I/O and it might not!


🀯 Strategic Remarks

title: Why is this spicy?
Because `printf()` uses a buffered `FILE*` interface (`stdout`) β€” and `read()` uses unbuffered syscalls.
You *might* try to read from the file before the buffered output was flushed.
title: Dangerous Combo
Reading from a file you're actively writing to β€” without flushing or seeking β€” is how many shell redirect bugs happen.

πŸ” 180 IQ Blindspots to Explore

title: What could go wrong?
- What if you flush stdout before opening for reading?
- What if you seek to the end before reading?
- What if you write again before closing?
- What if two processes try this at the same time?

πŸ” Code Variants You Can Try

VariantDescription
13bTry writing binary data instead of text
13cAdd multiple dup2 chains (stdout β†’ file β†’ pipe β†’ file again)
13dFork before reading β€” can the child read what the parent wrote?

βœ… Status

[[asymmetric_c/13_redirect_stdout_to_self/]]
πŸ”“ Complete: Iteration 1
🧠 Optional: Run variants or move on


Say:

πŸ”₯ 14_stdout_recursive_redirection next
or
πŸ” Try variant 13c with dup2 chains

Let’s KEEP BUILDING πŸ§±πŸ§ πŸ’£