8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libbc / libc / stdio / sys5 / fopen.c
blobe156dfc876927980d765d9bc49c5c8d3ef823e98
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
22 /* Copyright (c) 1984 AT&T */
23 /* All Rights Reserved */
25 #pragma ident "%Z%%M% %I% %E% SMI"
27 /*LINTLIBRARY*/
29 #include <stdio.h>
30 #include <fcntl.h>
32 extern FILE *_findiop();
33 static FILE *_endopen();
35 FILE *
36 fopen(char *file, char *mode)
38 return (_endopen(file, mode, _findiop()));
41 FILE *
42 freopen(char *file, char *mode, FILE *iop)
44 (void) fclose(iop); /* doesn't matter if this fails */
45 return (_endopen(file, mode, iop));
48 static FILE *
49 _endopen(char *file, char *mode, FILE *iop)
51 int plus, oflag, fd;
53 if (iop == NULL || file == NULL || file[0] == '\0')
54 return (NULL);
55 plus = (mode[1] == '+');
56 switch (mode[0]) {
57 case 'w':
58 oflag = (plus ? O_RDWR : O_WRONLY) | O_TRUNC | O_CREAT;
59 break;
60 case 'a':
61 oflag = (plus ? O_RDWR : O_WRONLY) | O_APPEND | O_CREAT;
62 break;
63 case 'r':
64 oflag = plus ? O_RDWR : O_RDONLY;
65 break;
66 default:
67 return (NULL);
69 if ((fd = open(file, oflag, 0666)) < 0)
70 return (NULL);
71 iop->_cnt = 0;
72 iop->_file = fd;
73 iop->_flag = plus ? _IORW : (mode[0] == 'r') ? _IOREAD : _IOWRT;
74 if (mode[0] == 'a') {
75 if (!plus) {
76 /* if update only mode, move file pointer to the end
77 of the file */
78 if ((lseek(fd,0L,2)) < 0) {
79 (void) close(fd);
80 return NULL;
84 iop->_base = iop->_ptr = NULL;
85 iop->_bufsiz = 0;
86 return (iop);