Redirecting file descriptor 3 - SUMMARY OF RESPONSES (and thanks!)

msk at afinitc.UUCP msk at afinitc.UUCP
Fri Dec 7 03:41:05 AEST 1984


I have received these seven responses to my original question on how
to redirect output to/from file descriptors other than 0 (stdin),
1 (stdout), and 2 (stderr).

Thank you all for your helpful information!

---------------
ark at grigg says:
---------------
#include <stdio.h>
	FILE *file3;
	file3 = fdopen (3, "w");
	if (file3 == NULL) { /* complain */ }
	fprintf (file3, ...);
------------------
jeff at alberta says:
------------------
Try the dup2(2) system call.  That should do it.
----------------
mike at whuxl says:
----------------
You can use any system calls directly on them.  This includes raw
I/O (read, write), dup, ioctl, fcntl, lseek, close, etc.  Just say
write(3, "hello", 5); for instance.

If you want to use the normal stdio routines, create a FILE pointer
to it using fdopen():
	FILE *fp;
	fp = fdopen(3, "w");
	fprintf(fd, "blick is %d\n", blick);
	...
If you use "... 3< file ..." on the command line, use "r" as the type
on fdopen.

... the shell allows only single-digit fd's ...
-------------------
keesan at bbncca says:
-------------------
Simply do I/O to the desired file descriptor as if you had opened it within
your program.  E.g.

main(){write(3,"Hello, world\n",13);}

If you want to use stdio, then just insert

FILE *fp3;
fp3 = fdopen(3,"w");

into your program and use fwrite or putc to FILE fp3.

What is happening is that when you say

command 3>foo

to the shell, the shell opens file "foo" for writing, with file descriptor 3,
and then executes the command as a child process, which inherits all of the
open files belonging to its parent (the shell).
---------------
ado at elsie says:
---------------
If you're on a Berkeley system, the function call
	fdopen(3, "w");
returns a FILE pointer that can be used to write to the file associated with
file descriptor 3.  See the manual page on "fopen" in section three of the
Programmer's Manual for details.

If you're not on a Berkeley system, I don't know of a portable way to do it.
Here's a version of "fdopen" that might work on most systems, though:

	#include <stdio.h>

	FILE *	fdopen(fildes, type)
	char *	type;
	{
		register FILE *	fp;

		fp = fopen("/dev/null", type);
		if (fp == NULL)
			return NULL;
		close(fp->_file);
		fp->_file = fildes;
		return fp;
	}
-------------------
davidt at ttidca says:
-------------------
Use Bourne shell, not C shell.  Just something like
prog 3>/dev/tty to shell and write(3, "hello!!!", 8); in C program
works fine.
---------------
dpg at busch says:
---------------
file descriptors are preserved across forks and execs, so all you need to
insure is that the descriptor for your file has a specific value. The dup2(2)
call does this, so

	int fd, pid

	if ((fd=open("myfile", mode)) < 0)
		terminate();
	switch (pid=fork()) {
	case -1:
		terminate();
	case 0:		/* child */
		dup2(fd, 3)
		exec(program, ...);
		terminate();	/* couldn't exec */
	default:	/* parent */
		break;
	}
-- 
--  From the terminal of Morris Kahn  (...ihnp4!wucs!afinitc!msk)
--  Affinitec, Corp., 2252 Welsch Ind. Ct., St. Louis, MO 63146 (314)569-3450



More information about the Comp.unix mailing list