Commit | Line | Data |
---|---|---|
411867e7 KB |
1 | /*- |
2 | * Copyright (c) 1990 The Regents of the University of California. | |
3 | * All rights reserved. | |
4 | * | |
5 | * This code is derived from software contributed to Berkeley by | |
6 | * Chris Torek. | |
7 | * | |
8 | * %sccs.include.redist.c% | |
586c39b1 DF |
9 | */ |
10 | ||
2ce81398 | 11 | #if defined(LIBC_SCCS) && !defined(lint) |
9a38e1e7 | 12 | static char sccsid[] = "@(#)fopen.c 5.5 (Berkeley) %G%"; |
411867e7 | 13 | #endif /* LIBC_SCCS and not lint */ |
586c39b1 | 14 | |
41e01b3e | 15 | #include <sys/types.h> |
411867e7 | 16 | #include <sys/stat.h> |
9a38e1e7 | 17 | #include <fcntl.h> |
41e01b3e | 18 | #include <stdio.h> |
411867e7 KB |
19 | #include <errno.h> |
20 | #include "local.h" | |
9a5d9b6d BJ |
21 | |
22 | FILE * | |
23 | fopen(file, mode) | |
d25ccb19 CT |
24 | const char *file; |
25 | const char *mode; | |
9a5d9b6d | 26 | { |
411867e7 KB |
27 | register FILE *fp; |
28 | register int f; | |
29 | int flags, oflags; | |
9a5d9b6d | 30 | |
411867e7 | 31 | if ((flags = __sflags(mode, &oflags)) == 0) |
41e01b3e | 32 | return (NULL); |
411867e7 | 33 | if ((fp = __sfp()) == NULL) |
41e01b3e | 34 | return (NULL); |
411867e7 KB |
35 | if ((f = open(file, oflags, DEFFILEMODE)) < 0) { |
36 | fp->_flags = 0; /* release */ | |
41e01b3e | 37 | return (NULL); |
411867e7 KB |
38 | } |
39 | fp->_file = f; | |
40 | fp->_flags = flags; | |
41 | fp->_cookie = fp; | |
42 | fp->_read = __sread; | |
43 | fp->_write = __swrite; | |
44 | fp->_seek = __sseek; | |
45 | fp->_close = __sclose; | |
9a38e1e7 CT |
46 | |
47 | /* | |
48 | * When opening in append mode, even though we use O_APPEND, | |
49 | * we need to seek to the end so that ftell() gets the right | |
50 | * answer. If the user then alters the seek pointer, or | |
51 | * the file extends, this will fail, but there is not much | |
52 | * we can do about this. (We could set __SAPP and check in | |
53 | * fseek and ftell.) | |
54 | */ | |
55 | if (oflags & O_APPEND) | |
56 | (void) __sseek((void *)fp, (fpos_t)0, SEEK_END); | |
411867e7 | 57 | return (fp); |
9a5d9b6d | 58 | } |