summaryrefslogtreecommitdiff
path: root/src/fd.c
blob: ad208129949e67d5e254164ba800d66fede93cf4 (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
#include "fd.h"

#include <fcntl.h>

/**
	@file fd.c
	@autor djb
	@source qmail
	@brief file descriptor manipulation
*/

int close(int __fd); /* we won't use unistd.h here */

int fd_copy(int to, int from)
{
  if (to == from) return 0;
  if (fcntl(from, F_GETFL, 0) == -1) return -1;
  close(to);
  if (fcntl(from, F_DUPFD, to) == -1) return -1;
  return 0;
}

int fd_move(int to, int from)
{
  if (to == from) return 0;
  if (fd_copy(to, from) == -1) return -1;
  close(from);
  return 0;
}

int fd_coe(int fd)
{
  return fcntl(fd, F_SETFD, 1);
}