| 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[] = "@(#)fopen.c 5.2 (Berkeley) %G%"; |
| 9 | #endif LIBC_SCCS and not lint |
| 10 | |
| 11 | #include <sys/types.h> |
| 12 | #include <sys/file.h> |
| 13 | #include <stdio.h> |
| 14 | |
| 15 | FILE * |
| 16 | fopen(file, mode) |
| 17 | char *file; |
| 18 | register char *mode; |
| 19 | { |
| 20 | register FILE *iop; |
| 21 | register f, rw, oflags; |
| 22 | extern FILE *_findiop(); |
| 23 | |
| 24 | iop = _findiop(); |
| 25 | if (iop == NULL) |
| 26 | return (NULL); |
| 27 | |
| 28 | rw = (mode[1] == '+'); |
| 29 | |
| 30 | switch (*mode) { |
| 31 | case 'a': |
| 32 | oflags = O_CREAT | (rw ? O_RDWR : O_WRONLY); |
| 33 | break; |
| 34 | case 'r': |
| 35 | oflags = rw ? O_RDWR : O_RDONLY; |
| 36 | break; |
| 37 | case 'w': |
| 38 | oflags = O_TRUNC | O_CREAT | (rw ? O_RDWR : O_WRONLY); |
| 39 | break; |
| 40 | default: |
| 41 | return (NULL); |
| 42 | } |
| 43 | |
| 44 | f = open(file, oflags, 0666); |
| 45 | if (f < 0) |
| 46 | return (NULL); |
| 47 | |
| 48 | if (*mode == 'a') |
| 49 | lseek(f, (off_t)0, L_XTND); |
| 50 | |
| 51 | iop->_cnt = 0; |
| 52 | iop->_file = f; |
| 53 | iop->_bufsiz = 0; |
| 54 | if (rw) |
| 55 | iop->_flag = _IORW; |
| 56 | else if (*mode == 'r') |
| 57 | iop->_flag = _IOREAD; |
| 58 | else |
| 59 | iop->_flag = _IOWRT; |
| 60 | iop->_base = iop->_ptr = NULL; |
| 61 | return (iop); |
| 62 | } |