[comp.sys.3b1] want workaround for "dup2"

kak@hico2.UUCP (Kris A. Kugel) (03/17/91)

Hummm... I don't seem to have "dup2" either.
I'm a little suprised, I'd expect that to
come with something that had its ancestry
from BSD :-)  I remember working around this
once, but I don't remember for what or what
I did about it.  Is there an easy way to
provide dup2?
                               Kris A. Kugel
                             ( 908 ) 842-2707
                      uunet!tsdiag.ccur.com!hico2!kak
                        {daver,ditka,zorch}!hico2!kak
                      internet: kak@hico2.westmark.com

pusateri@duke.cs.duke.edu (Thomas J. Pusateri) (03/18/91)

In article <1270@hico2.UUCP> kak@hico2.westmark.com writes:
>Is there an easy way to provide dup2?

I don't have my sysv manuals in front of me, but one can usually use
fcntl (at least the BSD version, which may not help!) 

To perform:
	dup2(old, new)
Try:
	close(new);		/*  guarentees new is not in use */
	fcntl(old, F_DUPFD, new);

If this isn't in unix-pc fcntl, then you still have a few options.
You just have to make sure that new is the next available slot
in the descriptor table. This could be accomplished with a bunch
on open()'s to fill unwanted slots, then a normal dup, and last a bunch
of close()'s which is kind of ugly, or if you are trying to fill a
stdio slot, then just close it before you do a dup. For instance,
to redirect stdin so it comes from a file, it is easiest to do the
following when you have dup2():
	new = open(...
	dup2(new, 0);
	close(new);

But this can be accomplished almost as easily by:

	new = open(...
	close(0);
	dup(new);
	close(new);

It depends on your application.

Tom Pusateri
Duke University

pusateri@nbsr.duke.edu

rmfowler@texrex.uucp (Rex Fowler) (03/18/91)

In article <1270@hico2.UUCP> kak@hico2.westmark.com writes:
>
>Hummm... I don't seem to have "dup2" either.
> ...
>Is there an easy way to provide dup2?
>
>                               Kris A. Kugel
>                             ( 908 ) 842-2707
>                      uunet!tsdiag.ccur.com!hico2!kak
>                        {daver,ditka,zorch}!hico2!kak
>                      internet: kak@hico2.westmark.com

#include<fcntl.h>
	...
	close(fd_to_dup);
	newfd=fcntl(fd, F_DUPFD, fd_to_dup);
	...

-- 
Rex Fowler <rmfowler%texrex@cirr.com>
UUCP:  egsner!texrex!rmfowler

Mariusz@fbits.ttank.com (Mariusz Stanczak) (03/18/91)

In article <1270@hico2.UUCP>, kak@hico2.UUCP (Kris A. Kugel) writes:
> 
> Hummm... I don't seem to have "dup2" either.
> [...]
> Is there an easy way to provide dup2?

The following version was included with GNU `make'

dup2 (old, new)
     int old, new;
{
  int newfd;

  (void) close (new);
  newfd = fcntl (old, F_DUPFD, new);
  if (newfd == -1)
    return -1;

  if (newfd != new)
    {
      (void) close (newfd);
      errno = EMFILE;
      return -1;
    }

  return newfd;
}

-Mariusz
-- 
INET: Mariusz@fbits.ttank.com
CIS : 71601.2430@compuserve.com
UUCP: ..!uunet!zardoz!ttank!fbits!Mariusz

john@chance.UUCP (John R. MacMillan) (03/19/91)

|>Is there an easy way to provide dup2?
|
|#include<fcntl.h>
|	...
    if (fd_to_dup != fd) {
|	close(fd_to_dup);
|	newfd=fcntl(fd, F_DUPFD, fd_to_dup);
    }
|	...

I'm not sure this is perfect, in particular I'm not sure errno will
agree with dup2(), but it's pretty close.  The check to see if the
fd's are equal isn't always necessary, but when it is...

dave@galaxia.Newport.RI.US (David H. Brierley) (03/25/91)

In article <90@fbits.ttank.com> Mariusz@fbits.ttank.com (Mariusz Stanczak) writes:
>
>The following version was included with GNU `make'

WARNING!!  WARNING!!  WARNING!!

If you use any portion of source code from any program that is covered by
the GNU GPL then your program in turn becomes covered by the GNU GPL.  If
you aware of this fact and aware of the consequences thereof and are willing
to accept those consequences, then go ahead and use this code.  I am not
saying that the GNU GPL is wrong or that it is evil, I just think people
should be aware of all the facts.  In fact, I believe that the GPL requires
you to notify people of this when you give them the source.
-- 
David H. Brierley
Home: dave@galaxia.newport.ri.us; Work: dhb@quahog.ssd.ray.com
Send comp.sources.3b1 submissions to comp-sources-3b1@galaxia.newport.ri.us
%% Can I be excused, my brain is full. **