3
/* Change the file descriptor
5
** Written by Kiem-Phong Vo.
9
static int _sfdup(reg int fd, reg int newfd)
11
static int _sfdup(fd,newfd)
18
#ifdef F_DUPFD /* the simple case */
19
while((dupfd = fcntl(fd,F_DUPFD,newfd)) < 0 && errno == EINTR)
23
#else /* do it the hard way */
24
if((dupfd = dup(fd)) < 0 || dupfd >= newfd)
27
/* dup() succeeded but didn't get the right number, recurse */
28
newfd = _sfdup(fd,newfd);
30
/* close the one that didn't match */
38
int sfsetfd(reg Sfio_t* f, reg int newfd)
49
if(f->flags&SF_STRING)
52
if((f->mode&SF_INIT) && f->file < 0)
53
{ /* restoring file descriptor after a previous freeze */
58
{ /* change file descriptor */
59
if((f->mode&SF_RDWR) != f->mode && _sfmode(f,0,0) < 0)
66
{ if((newfd = _sfdup(oldfd,newfd)) < 0)
73
{ /* sync stream if necessary */
74
if(((f->mode&SF_WRITE) && f->next > f->data) ||
75
(f->mode&SF_READ) || f->disc == _Sfudisc)
82
if(((f->mode&SF_WRITE) && f->next > f->data) ||
83
((f->mode&SF_READ) && f->extent < 0 &&
90
if((f->bits&SF_MMAP) && f->data)
91
{ SFMUNMAP(f,f->data,f->endb-f->data);
92
f->data = NIL(uchar*);
96
/* make stream appears uninitialized */
97
f->endb = f->endr = f->endw = f->data;
98
f->extent = f->here = 0;
99
f->mode = (f->mode&SF_RDWR)|SF_INIT;
100
f->bits &= ~SF_NULL; /* off /dev/null handling */
109
(*_Sfnotify)(f,SF_SETFD,newfd);
113
SFMTXRETURN(f,newfd);