| 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 | |
| 7 | #if defined(LIBC_SCCS) && !defined(lint) |
| 8 | static char sccsid[] = "@(#)fdopen.c 5.2 (Berkeley) %G%"; |
| 9 | #endif LIBC_SCCS and not lint |
| 10 | |
| 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 | |
| 17 | #include <sys/types.h> |
| 18 | #include <sys/file.h> |
| 19 | #include <stdio.h> |
| 20 | |
| 21 | FILE * |
| 22 | fdopen(fd, mode) |
| 23 | register char *mode; |
| 24 | { |
| 25 | extern FILE *_findiop(); |
| 26 | static int nofile = -1; |
| 27 | register FILE *iop; |
| 28 | |
| 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) |
| 37 | return (NULL); |
| 38 | |
| 39 | iop->_cnt = 0; |
| 40 | iop->_file = fd; |
| 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); |
| 56 | } |
| 57 | |
| 58 | if (mode[1] == '+') |
| 59 | iop->_flag = _IORW; |
| 60 | |
| 61 | return (iop); |
| 62 | } |