1da2e3ebdSchin /***********************************************************************
2da2e3ebdSchin * *
3da2e3ebdSchin * This software is part of the ast package *
4*3e14f97fSRoger A. Faulkner * Copyright (c) 1985-2010 AT&T Intellectual Property *
5da2e3ebdSchin * and is licensed under the *
6da2e3ebdSchin * Common Public License, Version 1.0 *
77c2fbfb3SApril Chin * by AT&T Intellectual Property *
8da2e3ebdSchin * *
9da2e3ebdSchin * A copy of the License is available at *
10da2e3ebdSchin * http://www.opensource.org/licenses/cpl1.0.txt *
11da2e3ebdSchin * (with md5 checksum 059e8cd6165cb4c31e351f2b69388fd9) *
12da2e3ebdSchin * *
13da2e3ebdSchin * Information and Software Systems Research *
14da2e3ebdSchin * AT&T Research *
15da2e3ebdSchin * Florham Park NJ *
16da2e3ebdSchin * *
17da2e3ebdSchin * Glenn Fowler <gsf@research.att.com> *
18da2e3ebdSchin * David Korn <dgk@research.att.com> *
19da2e3ebdSchin * Phong Vo <kpv@research.att.com> *
20da2e3ebdSchin * *
21da2e3ebdSchin ***********************************************************************/
22da2e3ebdSchin #include "sfhdr.h"
23da2e3ebdSchin
24da2e3ebdSchin /* Change the file descriptor
25da2e3ebdSchin **
26da2e3ebdSchin ** Written by Kiem-Phong Vo.
27da2e3ebdSchin */
28da2e3ebdSchin
29da2e3ebdSchin #if __STD_C
_sfdup(int fd,int newfd)307c2fbfb3SApril Chin static int _sfdup(int fd, int newfd)
31da2e3ebdSchin #else
32da2e3ebdSchin static int _sfdup(fd,newfd)
337c2fbfb3SApril Chin int fd;
347c2fbfb3SApril Chin int newfd;
35da2e3ebdSchin #endif
36da2e3ebdSchin {
37da2e3ebdSchin reg int dupfd;
38da2e3ebdSchin
39da2e3ebdSchin #ifdef F_DUPFD /* the simple case */
40da2e3ebdSchin while((dupfd = sysfcntlf(fd,F_DUPFD,newfd)) < 0 && errno == EINTR)
41da2e3ebdSchin errno = 0;
42da2e3ebdSchin return dupfd;
43da2e3ebdSchin
44da2e3ebdSchin #else /* do it the hard way */
45da2e3ebdSchin if((dupfd = sysdupf(fd)) < 0 || dupfd >= newfd)
46da2e3ebdSchin return dupfd;
47da2e3ebdSchin
48da2e3ebdSchin /* dup() succeeded but didn't get the right number, recurse */
49da2e3ebdSchin newfd = _sfdup(fd,newfd);
50da2e3ebdSchin
51da2e3ebdSchin /* close the one that didn't match */
52da2e3ebdSchin CLOSE(dupfd);
53da2e3ebdSchin
54da2e3ebdSchin return newfd;
55da2e3ebdSchin #endif
56da2e3ebdSchin }
57da2e3ebdSchin
58da2e3ebdSchin #if __STD_C
sfsetfd(Sfio_t * f,int newfd)597c2fbfb3SApril Chin int sfsetfd(Sfio_t* f, int newfd)
60da2e3ebdSchin #else
61da2e3ebdSchin int sfsetfd(f,newfd)
627c2fbfb3SApril Chin Sfio_t *f;
637c2fbfb3SApril Chin int newfd;
64da2e3ebdSchin #endif
65da2e3ebdSchin {
66da2e3ebdSchin reg int oldfd;
677c2fbfb3SApril Chin SFMTXDECL(f);
68da2e3ebdSchin
697c2fbfb3SApril Chin SFMTXENTER(f, -1);
70da2e3ebdSchin
71da2e3ebdSchin if(f->flags&SF_STRING)
72da2e3ebdSchin SFMTXRETURN(f, -1);
73da2e3ebdSchin
74da2e3ebdSchin if((f->mode&SF_INIT) && f->file < 0)
75da2e3ebdSchin { /* restoring file descriptor after a previous freeze */
76da2e3ebdSchin if(newfd < 0)
77da2e3ebdSchin SFMTXRETURN(f, -1);
78da2e3ebdSchin }
79da2e3ebdSchin else
80da2e3ebdSchin { /* change file descriptor */
81da2e3ebdSchin if((f->mode&SF_RDWR) != f->mode && _sfmode(f,0,0) < 0)
82da2e3ebdSchin SFMTXRETURN(f, -1);
83da2e3ebdSchin SFLOCK(f,0);
84da2e3ebdSchin
85da2e3ebdSchin oldfd = f->file;
86da2e3ebdSchin if(oldfd >= 0)
87da2e3ebdSchin { if(newfd >= 0)
88da2e3ebdSchin { if((newfd = _sfdup(oldfd,newfd)) < 0)
89da2e3ebdSchin { SFOPEN(f,0);
90da2e3ebdSchin SFMTXRETURN(f, -1);
91da2e3ebdSchin }
92da2e3ebdSchin CLOSE(oldfd);
93da2e3ebdSchin }
94da2e3ebdSchin else
95da2e3ebdSchin { /* sync stream if necessary */
96da2e3ebdSchin if(((f->mode&SF_WRITE) && f->next > f->data) ||
97da2e3ebdSchin (f->mode&SF_READ) || f->disc == _Sfudisc)
98da2e3ebdSchin { if(SFSYNC(f) < 0)
99da2e3ebdSchin { SFOPEN(f,0);
100da2e3ebdSchin SFMTXRETURN(f, -1);
101da2e3ebdSchin }
102da2e3ebdSchin }
103da2e3ebdSchin
104da2e3ebdSchin if(((f->mode&SF_WRITE) && f->next > f->data) ||
105da2e3ebdSchin ((f->mode&SF_READ) && f->extent < 0 &&
106da2e3ebdSchin f->next < f->endb) )
107da2e3ebdSchin { SFOPEN(f,0);
108da2e3ebdSchin SFMTXRETURN(f, -1);
109da2e3ebdSchin }
110da2e3ebdSchin
111da2e3ebdSchin #ifdef MAP_TYPE
112da2e3ebdSchin if((f->bits&SF_MMAP) && f->data)
113da2e3ebdSchin { SFMUNMAP(f,f->data,f->endb-f->data);
114da2e3ebdSchin f->data = NIL(uchar*);
115da2e3ebdSchin }
116da2e3ebdSchin #endif
117da2e3ebdSchin
118da2e3ebdSchin /* make stream appears uninitialized */
119da2e3ebdSchin f->endb = f->endr = f->endw = f->data;
120da2e3ebdSchin f->extent = f->here = 0;
121da2e3ebdSchin f->mode = (f->mode&SF_RDWR)|SF_INIT;
122da2e3ebdSchin f->bits &= ~SF_NULL; /* off /dev/null handling */
123da2e3ebdSchin }
124da2e3ebdSchin }
125da2e3ebdSchin
126da2e3ebdSchin SFOPEN(f,0);
127da2e3ebdSchin }
128da2e3ebdSchin
129da2e3ebdSchin /* notify changes */
130da2e3ebdSchin if(_Sfnotify)
1317c2fbfb3SApril Chin (*_Sfnotify)(f, SF_SETFD, (void*)((long)newfd));
132da2e3ebdSchin
133da2e3ebdSchin f->file = newfd;
134da2e3ebdSchin
135da2e3ebdSchin SFMTXRETURN(f,newfd);
136da2e3ebdSchin }
137