sockaddr's now require length (K. Sklower);
[unix-history] / usr / src / lib / libc / stdio / fdopen.c
CommitLineData
586c39b1
DF
1/*
2 * Copyright (c) 1980 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
2ce81398
DS
7#if defined(LIBC_SCCS) && !defined(lint)
8static char sccsid[] = "@(#)fdopen.c 5.2 (Berkeley) %G%";
9#endif LIBC_SCCS and not lint
586c39b1 10
39195eb1
BJ
11/*
12 * Unix routine to do an "fopen" on file descriptor
13 * The mode has to be repeated because you can't query its
14 * status
15 */
16
41e01b3e
S
17#include <sys/types.h>
18#include <sys/file.h>
19#include <stdio.h>
39195eb1
BJ
20
21FILE *
22fdopen(fd, mode)
41e01b3e 23 register char *mode;
39195eb1 24{
41e01b3e
S
25 extern FILE *_findiop();
26 static int nofile = -1;
39195eb1 27 register FILE *iop;
39195eb1 28
41e01b3e
S
29 if (nofile < 0)
30 nofile = getdtablesize();
31
32 if (fd < 0 || fd >= nofile)
33 return (NULL);
34
35 iop = _findiop();
36 if (iop == NULL)
c72d2806 37 return (NULL);
41e01b3e 38
39195eb1
BJ
39 iop->_cnt = 0;
40 iop->_file = fd;
41e01b3e
S
41 iop->_bufsiz = 0;
42 iop->_base = iop->_ptr = NULL;
43
44 switch (*mode) {
45 case 'r':
46 iop->_flag = _IOREAD;
47 break;
48 case 'a':
49 lseek(fd, (off_t)0, L_XTND);
50 /* fall into ... */
51 case 'w':
52 iop->_flag = _IOWRT;
53 break;
54 default:
55 return (NULL);
d8af6b8b 56 }
41e01b3e
S
57
58 if (mode[1] == '+')
59 iop->_flag = _IORW;
60
61 return (iop);
39195eb1 62}