jehanne/sys/src/lib/jehanne/9sys/dup.c

41 lines
1008 B
C

/*
* This file is part of Jehanne.
*
* Copyright (C) 2017 Giacomo Tesio <giacomo@tesio.it>
*
* Jehanne is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 2 of the License.
*
* Jehanne is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Jehanne. If not, see <http://www.gnu.org/licenses/>.
*/
#include <u.h>
#include <libc.h>
int
jehanne_dup(int oldfd, int newfd)
{
FdPair in, out;
long f;
in.fd[0] = oldfd;
in.fd[1] = newfd;
f = sys_create("#d/new", -1, in.aslong);
if(f == -1)
return -1;
if(f >= 0){
/* this should never happen */
sys_close(f);
return -1;
}
out.aslong = ~f;
return out.fd[1];
}