OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [rtos/] [ecos-3.0/] [packages/] [io/] [fileio/] [current/] [src/] [io.cxx] - Blame information for rev 867

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

Line No. Rev Author Line
1 786 skrzyp
//==========================================================================
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 Free Software Foundation, Inc.
12
//
13
// eCos is free software; you can redistribute it and/or modify it under    
14
// the terms of the GNU General Public License as published by the Free     
15
// Software Foundation; either version 2 or (at your option) any later      
16
// version.                                                                 
17
//
18
// eCos is distributed in the hope that it will be useful, but WITHOUT      
19
// ANY 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        
24
// along with eCos; if not, write to the Free Software Foundation, Inc.,    
25
// 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.            
26
//
27
// As a special exception, if other files instantiate templates or use      
28
// macros or inline functions from this file, or you compile this file      
29
// and link it with other works to produce a work based on this file,       
30
// this file does not by itself cause the resulting work to be covered by   
31
// the GNU General Public License. However the source code for this file    
32
// must still be made available in accordance with section (3) of the GNU   
33
// General Public License v2.                                               
34
//
35
// This exception does not invalidate any other reasons why a work based    
36
// on this file might be covered by the GNU General Public License.         
37
// -------------------------------------------                              
38
// ####ECOSGPLCOPYRIGHTEND####                                              
39
//==========================================================================
40
//#####DESCRIPTIONBEGIN####
41
//
42
// Author(s):           nickg
43
// Contributors:        nickg
44
// Date:                2000-05-25
45
// Purpose:             Fileio IO operations
46
// Description:         These are the functions that operate on open files,
47
//                      such as read(), write(), fstat() etc.
48
//              
49
//              
50
//
51
//####DESCRIPTIONEND####
52
//
53
//==========================================================================
54
 
55
#include <pkgconf/hal.h>
56
#include <pkgconf/io_fileio.h>
57
 
58
#include <cyg/infra/cyg_trac.h>        // tracing macros
59
#include <cyg/infra/cyg_ass.h>         // assertion macros
60
 
61
#include <stdarg.h>                     // for fcntl()
62
 
63
#include "fio.h"                       // Private header
64
 
65
 
66
//==========================================================================
67
// File object locking
68
 
69
#define LOCK_FILE( fp ) cyg_file_lock( fp, fp->f_syncmode )
70
 
71
#define UNLOCK_FILE( fp ) cyg_file_unlock( fp, fp->f_syncmode )
72
 
73
//==========================================================================
74
// Common wrapper for read/write using an iovec descriptor 
75
// 'direction' should be O_RDONLY for readv, O_WRONLY for writev
76
 
77
static ssize_t
78
readwritev( int fd, const cyg_iovec *_iov, int iov_len, int direction )
79
{
80
    FILEIO_ENTRY();
81
 
82
    CYG_CANCELLATION_POINT;
83
 
84
    ssize_t cnt, len;
85
    int ret, _idx;
86
    cyg_file *fp;
87
    cyg_iovec iov[CYGNUM_FILEIO_IOVEC_MAX];
88
 
89
    if( iov_len > CYGNUM_FILEIO_IOVEC_MAX )
90
        FILEIO_RETURN(EINVAL);
91
 
92
    // Copy 'iovec' structure since it's supposed to be "const"
93
    // and some lower level routines might want to change it.
94
    // Also accumulate the length of the total I/O request
95
    len = 0;
96
    for (_idx = 0;  _idx < iov_len;  _idx++) {
97
        len += _iov[_idx].iov_len;
98
        iov[_idx].iov_base = _iov[_idx].iov_base;
99
        iov[_idx].iov_len = _iov[_idx].iov_len;
100
    }
101
 
102
    if( len > SSIZE_MAX )
103
        FILEIO_RETURN(EINVAL);
104
 
105
    fp = cyg_fp_get( fd );
106
 
107
    if( fp == NULL )
108
        FILEIO_RETURN(EBADF);
109
 
110
    if( (fp->f_flag & direction) == 0 )
111
    {
112
        cyg_fp_free( fp );
113
        FILEIO_RETURN(EBADF);
114
    }
115
 
116
    cyg_uio uio;
117
    cyg_fileop_readwrite *op;
118
 
119
    uio.uio_iov         = iov;
120
    uio.uio_iovcnt      = iov_len;
121
    uio.uio_resid       = len;
122
    uio.uio_segflg      = UIO_USERSPACE;
123
 
124
    cnt = len;
125
 
126
    if( direction == O_RDONLY )
127
        uio.uio_rw = UIO_READ, op = fp->f_ops->fo_read;
128
    else
129
        uio.uio_rw = UIO_WRITE, op = fp->f_ops->fo_write;
130
 
131
    LOCK_FILE( fp );
132
 
133
    ret = op( fp, &uio );
134
 
135
    UNLOCK_FILE( fp );
136
 
137
    cnt -= uio.uio_resid;
138
 
139
    cyg_fp_free( fp );
140
 
141
    CYG_CANCELLATION_POINT;
142
 
143
    if( ret != 0 )
144
    {
145
        if ((ret == EWOULDBLOCK || ret == EAGAIN) && cnt)
146
            FILEIO_RETURN_VALUE(cnt);
147
        else
148
            FILEIO_RETURN(ret);
149
    }
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, ... )
254
{
255
    FILEIO_ENTRY();
256
 
257
    int ret;
258
    cyg_file *fp;
259
    va_list ap;
260
    CYG_ADDRWORD data;
261
 
262
    fp = cyg_fp_get( fd );
263
 
264
    if( fp == NULL )
265
        FILEIO_RETURN(EBADF);
266
 
267
    va_start(ap, com);
268
    data = va_arg(ap, CYG_ADDRWORD);
269
    va_end(ap);
270
 
271
    LOCK_FILE( fp );
272
 
273
    ret = fp->f_ops->fo_ioctl( fp, com, data );
274
 
275
    UNLOCK_FILE( fp );
276
 
277
    cyg_fp_free( fp );
278
 
279
    FILEIO_RETURN(ret);
280
}
281
 
282
//==========================================================================
283
// fsync
284
 
285
__externC int fsync( int fd )
286
{
287
    FILEIO_ENTRY();
288
 
289
    CYG_CANCELLATION_POINT;
290
 
291
    int ret;
292
    cyg_file *fp;
293
 
294
    fp = cyg_fp_get( fd );
295
 
296
    if( fp == NULL )
297
        FILEIO_RETURN(EBADF);
298
 
299
    LOCK_FILE( fp );
300
 
301
    ret = fp->f_ops->fo_fsync( fp, CYG_FSYNC );
302
 
303
    UNLOCK_FILE( fp );
304
 
305
    cyg_fp_free( fp );
306
 
307
    CYG_CANCELLATION_POINT;
308
 
309
    FILEIO_RETURN(ret);
310
}
311
 
312
//==========================================================================
313
// fdatasync()
314
 
315
__externC int fdatasync( int fd )
316
{
317
    FILEIO_ENTRY();
318
 
319
    CYG_CANCELLATION_POINT;
320
 
321
    int ret;
322
    cyg_file *fp;
323
 
324
    fp = cyg_fp_get( fd );
325
 
326
    if( fp == NULL )
327
        FILEIO_RETURN(EBADF);
328
 
329
    LOCK_FILE( fp );
330
 
331
    ret = fp->f_ops->fo_fsync( fp, CYG_FDATASYNC );
332
 
333
    UNLOCK_FILE( fp );
334
 
335
    cyg_fp_free( fp );
336
 
337
    CYG_CANCELLATION_POINT;
338
 
339
    FILEIO_RETURN(ret);
340
}
341
 
342
//==========================================================================
343
// fstat
344
 
345
__externC int fstat( int fd, struct stat *buf )
346
{
347
    FILEIO_ENTRY();
348
 
349
    int ret;
350
    cyg_file *fp;
351
 
352
    fp = cyg_fp_get( fd );
353
 
354
    if( fp == NULL )
355
        FILEIO_RETURN(EBADF);
356
 
357
    LOCK_FILE( fp );
358
 
359
    ret = fp->f_ops->fo_fstat( fp, buf );
360
 
361
    UNLOCK_FILE( fp );
362
 
363
    cyg_fp_free( fp );
364
 
365
    FILEIO_RETURN(ret);
366
}
367
 
368
//==========================================================================
369
// fpathconf
370
 
371
__externC long fpathconf( int fd, int name )
372
{
373
    FILEIO_ENTRY();
374
 
375
    int ret;
376
    cyg_file *fp;
377
 
378
    fp = cyg_fp_get( fd );
379
 
380
    if( fp == NULL )
381
        FILEIO_RETURN(EBADF);
382
 
383
    struct cyg_pathconf_info info;
384
 
385
    info.name = name;
386
    info.value = 0;
387
 
388
    LOCK_FILE( fp );
389
 
390
    ret = fp->f_ops->fo_getinfo( fp, FILE_INFO_CONF, (char *)&info, sizeof(info) );
391
 
392
    UNLOCK_FILE( fp );
393
 
394
    cyg_fp_free( fp );
395
 
396
    if( ret != 0 )
397
        FILEIO_RETURN(ret);
398
 
399
    FILEIO_RETURN_VALUE(info.value);
400
}
401
 
402
//==========================================================================
403
// fcntl
404
 
405
__externC int fcntl( int fd, int cmd, ... )
406
{
407
    FILEIO_ENTRY();
408
 
409
    CYG_CANCELLATION_POINT;
410
 
411
    int ret = 0;
412
    cyg_file *fp;
413
    va_list a;
414
 
415
    fp = cyg_fp_get( fd );
416
 
417
    if( fp == NULL )
418
        FILEIO_RETURN(EBADF);
419
 
420
    va_start( a, cmd );
421
 
422
    switch( cmd )
423
    {
424
    case F_DUPFD:
425
    {
426
        int fda = va_arg(a, int);
427
 
428
        if( fda < 0 || fda >= OPEN_MAX )
429
        {
430
            errno = EBADF;
431
            break;
432
        }
433
 
434
        int fd2 = cyg_fd_alloc( fda );
435
 
436
        if( fd2 == -1 )
437
        {
438
            ret = EMFILE;
439
            break;
440
        }
441
 
442
        cyg_fd_assign( fd2, fp );
443
 
444
        break;
445
    }
446
 
447
    default:
448
        ret = ENOTSUP;
449
        break;
450
    }
451
 
452
    va_end(a);
453
 
454
    cyg_fp_free( fp );
455
 
456
    CYG_CANCELLATION_POINT;
457
 
458
    FILEIO_RETURN(ret);
459
}
460
 
461
//==========================================================================
462
// isatty()
463
 
464
__externC int isatty( int fd )
465
{
466
    FILEIO_ENTRY();
467
 
468
    int ret = 0;
469
    struct stat buf;
470
    int err;
471
 
472
    err = fstat( fd, &buf );
473
 
474
    // Any error and we return zero. If the client wants to
475
    // they can always pick up the error code from errno.
476
    if( err < 0 )
477
        FILEIO_RETURN_VALUE(0);
478
 
479
    // For now we assume that all char devices are ttys.
480
    // In future we may need to have a special getinfo()
481
    // call to decide this more specifically.
482
 
483
    if( S_ISCHR( buf.st_mode ) )
484
        ret = 1;
485
 
486
    FILEIO_RETURN_VALUE(ret);
487
}
488
 
489
//==========================================================================
490
// File get info.
491
 
492
__externC int cyg_fs_fgetinfo( int fd, int key, void *buf, int len )
493
{
494
    FILEIO_ENTRY();
495
 
496
    int ret;
497
    cyg_file *fp;
498
 
499
    fp = cyg_fp_get( fd );
500
 
501
    if( fp == NULL )
502
        FILEIO_RETURN(EBADF);
503
 
504
    LOCK_FILE( fp );
505
 
506
    ret = fp->f_ops->fo_getinfo( fp, key, buf, len );
507
 
508
    UNLOCK_FILE( fp );
509
 
510
    cyg_fp_free( fp );
511
 
512
    FILEIO_RETURN(ret);
513
}
514
 
515
//==========================================================================
516
// File set info.
517
 
518
__externC int cyg_fs_fsetinfo( int fd, int key, void *buf, int len )
519
{
520
    FILEIO_ENTRY();
521
 
522
    int ret;
523
    cyg_file *fp;
524
 
525
    fp = cyg_fp_get( fd );
526
 
527
    if( fp == NULL )
528
        FILEIO_RETURN(EBADF);
529
 
530
    LOCK_FILE( fp );
531
 
532
    ret = fp->f_ops->fo_setinfo( fp, key, buf, len );
533
 
534
    UNLOCK_FILE( fp );
535
 
536
    cyg_fp_free( fp );
537
 
538
    FILEIO_RETURN(ret);
539
}
540
 
541
// -------------------------------------------------------------------------
542
// EOF io.cxx

powered by: WebSVN 2.1.0

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