summaryrefslogtreecommitdiffstats
path: root/lib/libc_r/uthread/uthread_dup.c
blob: e6c459835be7a8b9a2c992c6b2ce4d2751969469 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <unistd.h>
#ifdef _THREAD_SAFE
#include <pthread.h>
#include "pthread_private.h"

int
dup(int fd)
{
	int             ret;

	/* Lock the file descriptor: */
	if ((ret = _thread_fd_lock(fd, FD_RDWR, NULL, __FILE__, __LINE__)) == 0) {
		/* Perform the 'dup' syscall: */
		if ((ret = _thread_sys_dup(fd)) < 0) {
		}
		/* Initialise the file descriptor table entry: */
		else if (_thread_fd_table_init(ret) != 0) {
			/* Quietly close the file: */
			_thread_sys_close(ret);

			/* Reset the file descriptor: */
			ret = -1;
		} else {
			/*
			 * Save the file open flags so that they can be
			 * checked later: 
			 */
			_thread_fd_table[ret]->flags = _thread_fd_table[fd]->flags;
		}

		/* Unlock the file descriptor: */
		_thread_fd_unlock(fd, FD_RDWR);
	}
	/* Return the completion status: */
	return (ret);
}
#endif
OpenPOWER on IntegriCloud