Commit | Line | Data |
---|---|---|
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) |
8 | static char sccsid[] = "@(#)fopen.c 5.2 (Berkeley) %G%"; | |
9 | #endif LIBC_SCCS and not lint | |
586c39b1 | 10 | |
41e01b3e S |
11 | #include <sys/types.h> |
12 | #include <sys/file.h> | |
13 | #include <stdio.h> | |
9a5d9b6d BJ |
14 | |
15 | FILE * | |
16 | fopen(file, mode) | |
41e01b3e S |
17 | char *file; |
18 | register char *mode; | |
9a5d9b6d | 19 | { |
9a5d9b6d | 20 | register FILE *iop; |
41e01b3e S |
21 | register f, rw, oflags; |
22 | extern FILE *_findiop(); | |
9a5d9b6d | 23 | |
41e01b3e S |
24 | iop = _findiop(); |
25 | if (iop == NULL) | |
26 | return (NULL); | |
d8af6b8b | 27 | |
41e01b3e | 28 | rw = (mode[1] == '+'); |
d8af6b8b | 29 | |
41e01b3e S |
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); | |
9a5d9b6d | 45 | if (f < 0) |
41e01b3e S |
46 | return (NULL); |
47 | ||
48 | if (*mode == 'a') | |
49 | lseek(f, (off_t)0, L_XTND); | |
50 | ||
9a5d9b6d BJ |
51 | iop->_cnt = 0; |
52 | iop->_file = f; | |
41e01b3e | 53 | iop->_bufsiz = 0; |
d8af6b8b | 54 | if (rw) |
41e01b3e S |
55 | iop->_flag = _IORW; |
56 | else if (*mode == 'r') | |
57 | iop->_flag = _IOREAD; | |
9a5d9b6d | 58 | else |
41e01b3e S |
59 | iop->_flag = _IOWRT; |
60 | iop->_base = iop->_ptr = NULL; | |
61 | return (iop); | |
9a5d9b6d | 62 | } |