Reopening stdin

Chuck Musciano chuck at trantor.harris-atd.com
Wed Jan 9 00:13:26 AEST 1991


In article <8685 at star.cs.vu.nl>, maart at cs.vu.nl (Maarten Litmaath) writes:
> Does the following program fail too?
> 
> 	main()
> 	{
> 		int	fd, i;
> 
> 		close(0);
> 		fd = open("/etc/passwd", 0);
> 		dup2(fd, 0);
> 		i = system("cat");
> 		exit(i & 0377 ? i | 0200 : i >> 8);
> 	}
> 
> If so, it looks like a resurrection of the old dup2() bug: as stdin has
> been closed, the open() will return file descriptor 0, so the next line
> results in
> 		dup2(0, 0)

     I checked dup2(), and it is returning 0, as you would expect.  In the
case of dup2(0, 0), I tried closing stdin, calling open(), and verified
that I got 0 as the descriptor.  I then skipped the dup2 call, called
system(), and it still failed!

-- 

Chuck Musciano				ARPA  : chuck at trantor.harris-atd.com
Harris Corporation 			Usenet: ...!uunet!x102a!trantor!chuck
PO Box 37, MS 3A/1912			AT&T  : (407) 727-6131
Melbourne, FL 32902			FAX   : (407) 729-2537

A good newspaper is never good enough,
	but a lousy newspaper is a joy forever.		-- Garrison Keillor



More information about the Comp.unix.questions mailing list