OpenCores
URL https://opencores.org/ocsvn/openrisc_2011-10-31/openrisc_2011-10-31/trunk

Subversion Repositories openrisc_2011-10-31

[/] [openrisc/] [trunk/] [rtos/] [ecos-2.0/] [packages/] [io/] [fileio/] [v2_0/] [src/] [io.cxx] - Blame information for rev 252

Go to most recent revision | Details | Compare with Previous | View Log

Line No. Rev Author Line
1 27 unneback
//==========================================================================
2
//
3
//      io.cxx
4
//
5
//      Fileio IO operations
6
//
7
//==========================================================================
8
//####ECOSGPLCOPYRIGHTBEGIN####
9
// -------------------------------------------
10
// This file is part of eCos, the Embedded Configurable Operating System.
11
// Copyright (C) 1998, 1999, 2000, 2001, 2002 Red Hat, Inc.
12
// Copyright (C) 2002 Gary Thomas
13
//
14
// eCos is free software; you can redistribute it and/or modify it under
15
// the terms of the GNU General Public License as published by the Free
16
// Software Foundation; either version 2 or (at your option) any later version.
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT ANY
19
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
20
// FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
21
// for more details.
22
//
23
// You should have received a copy of the GNU General Public License along
24
// with eCos; if not, write to the Free Software Foundation, Inc.,
25
// 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
26
//
27
// As a special exception, if other files instantiate templates or use macros
28
// or inline functions from this file, or you compile this file and link it
29
// with other works to produce a work based on this file, this file does not
30
// by itself cause the resulting work to be covered by the GNU General Public
31
// License. However the source code for this file must still be made available
32
// in accordance with section (3) of the GNU General Public License.
33
//
34
// This exception does not invalidate any other reasons why a work based on
35
// this file might be covered by the GNU General Public License.
36
//
37
// Alternative licenses for eCos may be arranged by contacting Red Hat, Inc.
38
// at http://sources.redhat.com/ecos/ecos-license/
39
// -------------------------------------------
40
//####ECOSGPLCOPYRIGHTEND####
41
//==========================================================================
42
//#####DESCRIPTIONBEGIN####
43
//
44
// Author(s):           nickg
45
// Contributors:        nickg
46
// Date:                2000-05-25
47
// Purpose:             Fileio IO operations
48
// Description:         These are the functions that operate on open files,
49
//                      such as read(), write(), fstat() etc.
50
//              
51
//              
52
//
53
//####DESCRIPTIONEND####
54
//
55
//==========================================================================
56
 
57
#include <pkgconf/hal.h>
58
#include <pkgconf/kernel.h>
59
#include <pkgconf/io_fileio.h>
60
 
61
#include <cyg/kernel/ktypes.h>         // base kernel types
62
#include <cyg/infra/cyg_trac.h>        // tracing macros
63
#include <cyg/infra/cyg_ass.h>         // assertion macros
64
 
65
#include <stdarg.h>                     // for fcntl()
66
 
67
#include "fio.h"                       // Private header
68
 
69
#include <cyg/kernel/mutex.hxx>        // mutex definitions
70
 
71
//==========================================================================
72
// File object locking
73
 
74
#define LOCK_FILE( fp ) cyg_file_lock( fp, fp->f_syncmode )
75
 
76
#define UNLOCK_FILE( fp ) cyg_file_unlock( fp, fp->f_syncmode )
77
 
78
//==========================================================================
79
// Common wrapper for read/write using an iovec descriptor 
80
// 'direction' should be O_RDONLY for readv, O_WRONLY for writev
81
 
82
static ssize_t
83
readwritev( int fd, const cyg_iovec *_iov, int iov_len, int direction )
84
{
85
    FILEIO_ENTRY();
86
 
87
    CYG_CANCELLATION_POINT;
88
 
89
    ssize_t cnt, len;
90
    int ret, _idx;
91
    cyg_file *fp;
92
    cyg_iovec iov[CYGNUM_FILEIO_IOVEC_MAX];
93
 
94
    if( iov_len > CYGNUM_FILEIO_IOVEC_MAX )
95
        FILEIO_RETURN(EINVAL);
96
 
97
    // Copy 'iovec' structure since it's supposed to be "const"
98
    // and some lower level routines might want to change it.
99
    // Also accumulate the length of the total I/O request
100
    len = 0;
101
    for (_idx = 0;  _idx < iov_len;  _idx++) {
102
        len += _iov[_idx].iov_len;
103
        iov[_idx].iov_base = _iov[_idx].iov_base;
104
        iov[_idx].iov_len = _iov[_idx].iov_len;
105
    }
106
 
107
    if( len > SSIZE_MAX )
108
        FILEIO_RETURN(EINVAL);
109
 
110
    fp = cyg_fp_get( fd );
111
 
112
    if( fp == NULL )
113
        FILEIO_RETURN(EBADF);
114
 
115
    if( (fp->f_flag & direction) == 0 )
116
    {
117
        cyg_fp_free( fp );
118
        FILEIO_RETURN(EBADF);
119
    }
120
 
121
    cyg_uio uio;
122
    cyg_fileop_readwrite *op;
123
 
124
    uio.uio_iov         = iov;
125
    uio.uio_iovcnt      = iov_len;
126
    uio.uio_resid       = len;
127
    uio.uio_segflg      = UIO_USERSPACE;
128
 
129
    cnt = len;
130
 
131
    if( direction == O_RDONLY )
132
        uio.uio_rw = UIO_READ, op = fp->f_ops->fo_read;
133
    else
134
        uio.uio_rw = UIO_WRITE, op = fp->f_ops->fo_write;
135
 
136
    LOCK_FILE( fp );
137
 
138
    ret = op( fp, &uio );
139
 
140
    UNLOCK_FILE( fp );
141
 
142
    cnt -= uio.uio_resid;
143
 
144
    cyg_fp_free( fp );
145
 
146
    CYG_CANCELLATION_POINT;
147
 
148
    if( ret != 0 )
149
        FILEIO_RETURN(ret);
150
 
151
    FILEIO_RETURN_VALUE(cnt);
152
}
153
 
154
//==========================================================================
155
// Read from file
156
 
157
__externC ssize_t read( int fd, void *buf, size_t len )
158
{
159
    cyg_iovec _iov;
160
 
161
    _iov.iov_base = buf;
162
    _iov.iov_len = len;
163
    return readwritev(fd, &_iov, 1, O_RDONLY);
164
}
165
 
166
//==========================================================================
167
// Write to a file
168
 
169
__externC ssize_t write( int fd, const void *buf, size_t len )
170
{
171
    cyg_iovec _iov;
172
 
173
    _iov.iov_base = (void *)buf;
174
    _iov.iov_len = len;
175
    return readwritev(fd, &_iov, 1, O_WRONLY);
176
}
177
 
178
//==========================================================================
179
// Read via an iovec
180
__externC ssize_t readv( int fd, const cyg_iovec *_iov, int iov_len )
181
{
182
    return readwritev(fd, _iov, iov_len, O_RDONLY);
183
}
184
 
185
//==========================================================================
186
// Write via an iovec
187
__externC ssize_t writev( int fd, const cyg_iovec *_iov, int iov_len )
188
{
189
    return readwritev(fd, _iov, iov_len, O_WRONLY);
190
}
191
 
192
 
193
//==========================================================================
194
// Close a file
195
 
196
__externC int close( int fd )
197
{
198
    FILEIO_ENTRY();
199
 
200
    CYG_CANCELLATION_POINT;
201
 
202
    int ret;
203
    cyg_file *fp;
204
 
205
    fp = cyg_fp_get( fd );
206
 
207
    if( fp == NULL )
208
        FILEIO_RETURN(EBADF);
209
 
210
    cyg_fp_free( fp );
211
 
212
    // The file's fo_close entry may be called as a side
213
    // effect of this operation...
214
    ret = cyg_fd_free( fd );
215
 
216
    CYG_CANCELLATION_POINT;
217
 
218
    FILEIO_RETURN(ret);
219
}
220
 
221
//==========================================================================
222
// Seek a file
223
 
224
__externC off_t lseek( int fd, off_t pos, int whence )
225
{
226
    FILEIO_ENTRY();
227
 
228
    int ret;
229
    cyg_file *fp;
230
 
231
    fp = cyg_fp_get( fd );
232
 
233
    if( fp == NULL )
234
        FILEIO_RETURN(EBADF);
235
 
236
    LOCK_FILE( fp );
237
 
238
    ret = fp->f_ops->fo_lseek( fp, &pos, whence );
239
 
240
    UNLOCK_FILE( fp );
241
 
242
    cyg_fp_free( fp );
243
 
244
    if( ret != 0 )
245
        FILEIO_RETURN(ret);
246
 
247
    FILEIO_RETURN_VALUE(pos);
248
}
249
 
250
//==========================================================================
251
// ioctl
252
 
253
__externC int ioctl( int fd, CYG_ADDRWORD com, CYG_ADDRWORD data )
254
{
255
    FILEIO_ENTRY();
256
 
257
    int ret;
258
    cyg_file *fp;
259
 
260
    fp = cyg_fp_get( fd );
261
 
262
    if( fp == NULL )
263
        FILEIO_RETURN(EBADF);
264
 
265
    LOCK_FILE( fp );
266
 
267
    ret = fp->f_ops->fo_ioctl( fp, com, data );
268
 
269
    UNLOCK_FILE( fp );
270
 
271
    cyg_fp_free( fp );
272
 
273
    FILEIO_RETURN(ret);
274
}
275
 
276
//==========================================================================
277
// fsync
278
 
279
__externC int fsync( int fd )
280
{
281
    FILEIO_ENTRY();
282
 
283
    CYG_CANCELLATION_POINT;
284
 
285
    int ret;
286
    cyg_file *fp;
287
 
288
    fp = cyg_fp_get( fd );
289
 
290
    if( fp == NULL )
291
        FILEIO_RETURN(EBADF);
292
 
293
    LOCK_FILE( fp );
294
 
295
    ret = fp->f_ops->fo_fsync( fp, CYG_FSYNC );
296
 
297
    UNLOCK_FILE( fp );
298
 
299
    cyg_fp_free( fp );
300
 
301
    CYG_CANCELLATION_POINT;
302
 
303
    FILEIO_RETURN(ret);
304
}
305
 
306
//==========================================================================
307
// fdatasync()
308
 
309
__externC int fdatasync( int fd )
310
{
311
    FILEIO_ENTRY();
312
 
313
    CYG_CANCELLATION_POINT;
314
 
315
    int ret;
316
    cyg_file *fp;
317
 
318
    fp = cyg_fp_get( fd );
319
 
320
    if( fp == NULL )
321
        FILEIO_RETURN(EBADF);
322
 
323
    LOCK_FILE( fp );
324
 
325
    ret = fp->f_ops->fo_fsync( fp, CYG_FDATASYNC );
326
 
327
    UNLOCK_FILE( fp );
328
 
329
    cyg_fp_free( fp );
330
 
331
    CYG_CANCELLATION_POINT;
332
 
333
    FILEIO_RETURN(ret);
334
}
335
 
336
//==========================================================================
337
// fstat
338
 
339
__externC int fstat( int fd, struct stat *buf )
340
{
341
    FILEIO_ENTRY();
342
 
343
    int ret;
344
    cyg_file *fp;
345
 
346
    fp = cyg_fp_get( fd );
347
 
348
    if( fp == NULL )
349
        FILEIO_RETURN(EBADF);
350
 
351
    LOCK_FILE( fp );
352
 
353
    ret = fp->f_ops->fo_fstat( fp, buf );
354
 
355
    UNLOCK_FILE( fp );
356
 
357
    cyg_fp_free( fp );
358
 
359
    FILEIO_RETURN(ret);
360
}
361
 
362
//==========================================================================
363
// fpathconf
364
 
365
__externC long fpathconf( int fd, int name )
366
{
367
    FILEIO_ENTRY();
368
 
369
    int ret;
370
    cyg_file *fp;
371
 
372
    fp = cyg_fp_get( fd );
373
 
374
    if( fp == NULL )
375
        FILEIO_RETURN(EBADF);
376
 
377
    struct cyg_pathconf_info info;
378
 
379
    info.name = name;
380
    info.value = 0;
381
 
382
    LOCK_FILE( fp );
383
 
384
    ret = fp->f_ops->fo_getinfo( fp, FILE_INFO_CONF, (char *)&info, sizeof(info) );
385
 
386
    UNLOCK_FILE( fp );
387
 
388
    cyg_fp_free( fp );
389
 
390
    if( ret != 0 )
391
        FILEIO_RETURN(ret);
392
 
393
    FILEIO_RETURN_VALUE(info.value);
394
}
395
 
396
//==========================================================================
397
// fcntl
398
 
399
__externC int fcntl( int fd, int cmd, ... )
400
{
401
    FILEIO_ENTRY();
402
 
403
    CYG_CANCELLATION_POINT;
404
 
405
    int ret = 0;
406
    cyg_file *fp;
407
    va_list a;
408
 
409
    fp = cyg_fp_get( fd );
410
 
411
    if( fp == NULL )
412
        FILEIO_RETURN(EBADF);
413
 
414
    va_start( a, cmd );
415
 
416
    switch( cmd )
417
    {
418
    case F_DUPFD:
419
    {
420
        int fda = va_arg(a, int);
421
 
422
        if( fda < 0 || fda >= OPEN_MAX )
423
        {
424
            errno = EBADF;
425
            break;
426
        }
427
 
428
        int fd2 = cyg_fd_alloc( fda );
429
 
430
        if( fd2 == -1 )
431
        {
432
            ret = EMFILE;
433
            break;
434
        }
435
 
436
        cyg_fd_assign( fd2, fp );
437
 
438
        break;
439
    }
440
 
441
    default:
442
        ret = ENOTSUP;
443
        break;
444
    }
445
 
446
    va_end(a);
447
 
448
    cyg_fp_free( fp );
449
 
450
    CYG_CANCELLATION_POINT;
451
 
452
    FILEIO_RETURN(ret);
453
}
454
 
455
//==========================================================================
456
// isatty()
457
 
458
__externC int isatty( int fd )
459
{
460
    FILEIO_ENTRY();
461
 
462
    int ret = 0;
463
    struct stat buf;
464
    int err;
465
 
466
    err = fstat( fd, &buf );
467
 
468
    // Any error and we return zero. If the client wants to
469
    // they can always pick up the error code from errno.
470
    if( err < 0 )
471
        FILEIO_RETURN_VALUE(0);
472
 
473
    // For now we assume that all char devices are ttys.
474
    // In future we may need to have a special getinfo()
475
    // call to decide this more specifically.
476
 
477
    if( S_ISCHR( buf.st_mode ) )
478
        ret = 1;
479
 
480
    FILEIO_RETURN_VALUE(ret);
481
}
482
 
483
// -------------------------------------------------------------------------
484
// EOF io.cxx

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.