patches from bug-gnu-utils to support more architectures
[glibc.git] / stdio / fopen.c
blobfea227482f1409fb8659b946361295ad0ead399b
1 /* Copyright (C) 1991, 1992, 1993, 1996 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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. */
19 #include <ctype.h>
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
26 #define badmode() return ((__set_errno (EINVAL)), 0)
28 /* Dissect the given mode string into an __io_mode. */
29 int
30 __getmode (mode, mptr)
31 const char *mode;
32 __io_mode *mptr;
34 register unsigned char i;
36 if (mode == NULL)
37 badmode ();
39 memset ((void *) mptr, 0, sizeof (*mptr));
41 switch (*mode)
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 ();
56 for (i = 1; i < 3; ++i)
58 ++mode;
59 if (*mode == '\0')
60 break;
61 switch (*mode)
63 case '+':
64 mptr->__read = mptr->__write = 1;
65 break;
66 case 'b':
67 mptr->__binary = 1;
68 break;
72 if (!mptr->__read && !mptr->__write)
73 badmode ();
75 mptr->__exclusive = *mode == 'x';
77 return 1;
80 /* Open a new stream on the given file. */
81 FILE *
82 fopen (filename, mode)
83 const char *filename;
84 const char *mode;
86 FILE *stream;
87 __io_mode m;
89 if (filename == NULL)
91 __set_errno (EINVAL);
92 return NULL;
95 if (!__getmode (mode, &m))
96 return NULL;
98 stream = __newstream ();
99 if (stream == NULL)
100 return NULL;
102 if (__stdio_open (filename, m, &stream->__cookie))
104 int save = errno;
105 (void) fclose (stream);
106 __set_errno (save);
107 return NULL;
110 stream->__mode = m;
112 return stream;