1
0
mirror of https://github.com/adtools/clib2.git synced 2025-12-08 14:59:05 +00:00
Files
amiga-clib2/library/unistd_dup2.c
Olaf Barthel f2d1a3238d - Rewrote the code that allocates the file descriptor and file
buffer tables so that all the memory allocations are in one
  place and it's possible to specify exactly how many table
  entries are required at a time.

- Creation and initialization of semaphores now uses the AmigaOS4
  specific functions for this purpose, if available.


git-svn-id: file:///Users/olsen/Code/migration-svn-zu-git/logical-line-staging/clib2/trunk@14857 87f5fb63-7c3d-0410-a384-fd976d0f7a62
2005-03-04 09:07:09 +00:00

130 lines
3.5 KiB
C

/*
* $Id: unistd_dup2.c,v 1.8 2005-03-04 09:07:09 obarthel Exp $
*
* :ts=4
*
* Portable ISO 'C' (1994) runtime library for the Amiga computer
* Copyright (c) 2002-2005 by Olaf Barthel <olsen@sourcery.han.de>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* - Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
*
* - Neither the name of Olaf Barthel nor the names of contributors
* may be used to endorse or promote products derived from this
* software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef _UNISTD_HEADERS_H
#include "unistd_headers.h"
#endif /* _UNISTD_HEADERS_H */
/****************************************************************************/
/* The following is not part of the ISO 'C' (1994) standard. */
/****************************************************************************/
int
dup2(int file_descriptor1, int file_descriptor2)
{
struct fd * fd1;
int result = -1;
ENTER();
SHOWVALUE(file_descriptor1);
SHOWVALUE(file_descriptor2);
if(__check_abort_enabled)
__check_abort();
__stdio_lock();
assert( file_descriptor1 >= 0 && file_descriptor1 < __num_fd );
assert( __fd[file_descriptor1] != NULL );
assert( FLAG_IS_SET(__fd[file_descriptor1]->fd_Flags,FDF_IN_USE) );
fd1 = __get_file_descriptor(file_descriptor1);
if(fd1 == NULL)
{
__set_errno(EBADF);
goto out;
}
if(file_descriptor2 < 0)
{
/* Try to find a place to put the duplicate into. */
file_descriptor2 = __find_vacant_fd_entry();
if(file_descriptor2 < 0)
{
/* No free space, so let's grow the table. */
if(__grow_fd_table(0) < 0)
{
SHOWMSG("not enough memory for new file descriptor");
goto out;
}
file_descriptor2 = __find_vacant_fd_entry();
assert( file_descriptor2 >= 0 );
}
}
else if (file_descriptor1 != file_descriptor2)
{
/* Make sure the requested duplicate exists. */
if(__grow_fd_table(file_descriptor2 + 1) < 0)
goto out;
assert( file_descriptor2 >= 0 && file_descriptor2 < __num_fd );
assert( __fd[file_descriptor2] != NULL );
}
if(file_descriptor1 != file_descriptor2)
{
struct fd * fd2;
/* Have a look at the requested file descriptor. */
assert( 0 <= file_descriptor2 && file_descriptor2 < __num_fd );
fd2 = __fd[file_descriptor2];
assert( fd2 != NULL );
/* Make sure that the entry is cleaned up before we used it. */
if(FLAG_IS_SET(fd2->fd_Flags,FDF_IN_USE))
{
SHOWMSG("closing file descriptor #2");
if(close(file_descriptor2) < 0)
goto out;
}
__duplicate_fd(fd2,fd1);
}
result = file_descriptor2;
out:
__stdio_unlock();
RETURN(result);
return(result);
}