]> sourceware.org Git - glibc.git/blob - stdio/fopen.c
update from main archive
[glibc.git] / stdio / fopen.c
1 /* Copyright (C) 1991, 1992, 1993, 1996 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
3
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
8
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, USA. */
18
19 #include <ctype.h>
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25
26 #define badmode() return ((__set_errno (EINVAL)), 0)
27
28 /* Dissect the given mode string into an __io_mode. */
29 int
30 __getmode (mode, mptr)
31 const char *mode;
32 __io_mode *mptr;
33 {
34 register unsigned char i;
35
36 if (mode == NULL)
37 badmode ();
38
39 memset ((void *) mptr, 0, sizeof (*mptr));
40
41 switch (*mode)
42 {
43 case 'a':
44 mptr->__write = mptr->__create = mptr->__append = 1;
45 break;
46 case 'w':
47 mptr->__write = mptr->__create = mptr->__truncate = 1;
48 break;
49 case 'r':
50 mptr->__read = 1;
51 break;
52 default:
53 badmode ();
54 }
55
56 for (i = 1; i < 3; ++i)
57 {
58 ++mode;
59 if (*mode == '\0')
60 break;
61 switch (*mode)
62 {
63 case '+':
64 mptr->__read = mptr->__write = 1;
65 break;
66 case 'b':
67 mptr->__binary = 1;
68 break;
69 }
70 }
71
72 if (!mptr->__read && !mptr->__write)
73 badmode ();
74
75 mptr->__exclusive = *mode == 'x';
76
77 return 1;
78 }
79 \f
80 /* Open a new stream on the given file. */
81 FILE *
82 fopen (filename, mode)
83 const char *filename;
84 const char *mode;
85 {
86 FILE *stream;
87 __io_mode m;
88
89 if (filename == NULL)
90 {
91 __set_errno (EINVAL);
92 return NULL;
93 }
94
95 if (!__getmode (mode, &m))
96 return NULL;
97
98 stream = __newstream ();
99 if (stream == NULL)
100 return NULL;
101
102 if (__stdio_open (filename, m, &stream->__cookie))
103 {
104 int save = errno;
105 (void) fclose (stream);
106 __set_errno (save);
107 return NULL;
108 }
109
110 stream->__mode = m;
111
112 return stream;
113 }
This page took 0.037868 seconds and 5 git commands to generate.