2002-03-25 Roland McGrath <roland@frob.com>
[platform/upstream/glibc.git] / hurd / fopenport.c
1 /* Copyright (C) 1994,95,97,2000,01 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 Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the 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    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, write to the Free
16    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17    02111-1307 USA.  */
18
19 #include <hurd.h>
20 #include <stdio.h>
21 #include <fcntl.h>
22 #include <string.h>
23
24 /* Read up to N chars into BUF from COOKIE.
25    Return how many chars were read, 0 for EOF or -1 for error.  */
26 static ssize_t
27 readio (void *cookie, char *buf, size_t n)
28 {
29   mach_msg_type_number_t nread;
30   error_t err;
31   char *bufp = buf;
32
33   nread = n;
34   if (err = __io_read ((io_t) cookie, &bufp, &nread, -1, n))
35     return __hurd_fail (err);
36
37   if (bufp != buf)
38     {
39       memcpy (buf, bufp, nread);
40       __vm_deallocate (__mach_task_self (),
41                        (vm_address_t) bufp, (vm_size_t) nread);
42     }
43
44   return nread;
45 }
46
47 /* Write up to N chars from BUF to COOKIE.
48    Return how many chars were written or -1 for error.  */
49 static ssize_t
50 writeio (void *cookie, const char *buf, size_t n)
51 {
52   mach_msg_type_number_t wrote;
53   error_t err;
54
55   if (err = __io_write ((io_t) cookie, buf, n, -1, &wrote))
56     return __hurd_fail (err);
57
58   return wrote;
59 }
60
61 /* Move COOKIE's file position *POS bytes, according to WHENCE.
62    The current file position is stored in *POS.
63    Returns zero if successful, nonzero if not.  */
64 static int
65 seekio (void *cookie,
66 #ifdef USE_IN_LIBIO
67         _IO_off64_t *pos,
68 #else
69         fpos_t *pos,
70 #endif
71         int whence)
72 {
73   off_t res;
74   error_t error;
75
76   /* XXX We don't really support large files on the Hurd.  So if POS
77      doesn't fit in an `off_t', we'll return `-1' and set errno.  EOVERFLOW
78      probably isn't the right error value, but seems appropriate here.  */
79   if ((off_t) *pos != *pos)
80     {
81       __set_errno (EOVERFLOW);
82       return -1;
83     }
84
85   error = __io_seek ((file_t) cookie, *pos, whence, &res);
86   if (error)
87     return __hurd_fail (error);
88   *pos = res;
89   return 0;
90 }
91
92 /* Close the file associated with COOKIE.
93    Return 0 for success or -1 for failure.  */
94 static int
95 closeio (void *cookie)
96 {
97   error_t error = __mach_port_deallocate (__mach_task_self (),
98                                           (mach_port_t) cookie);
99   if (error)
100     return __hurd_fail (error);
101   return 0;
102 }
103
104 #ifdef USE_IN_LIBIO
105 #include "../libio/libioP.h"
106 #define fopencookie _IO_fopencookie
107 #else
108 #define cookie_io_functions_t __io_functions
109 #endif
110 static const cookie_io_functions_t funcsio =
111 { readio, writeio, seekio, closeio };
112 \f
113
114 /* Open a stream on PORT.  MODE is as for fopen.  */
115
116 FILE *
117 __fopenport (mach_port_t port, const char *mode)
118 {
119   int pflags;
120   int needflags;
121   error_t err;
122
123   const char *m = mode;
124
125   switch (*m++)
126     {
127     case 'r':
128       needflags = O_READ;
129       break;
130     case 'w':
131       needflags = O_WRITE;
132       break;
133     case 'a':
134       needflags = O_WRITE|O_APPEND;
135       break;
136     default:
137       return NULL;
138   }
139   if (m[0] == '+' || (m[0] == 'b' && m[1] == '+'))
140     needflags |= O_RDWR;
141
142   /* Verify the PORT is valid allows the access MODE specifies.  */
143
144   if (err = __io_get_openmodes (port, &pflags))
145     return __hurd_fail (err), NULL;
146
147   /* Check the access mode.  */
148   if ((pflags & needflags) != needflags)
149     {
150       errno = EBADF;
151       return NULL;
152     }
153
154   return fopencookie ((void *) port, mode, funcsio);
155 }
156 weak_alias (__fopenport, fopenport)