Rev 4921 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 4921 | Rev 6536 | ||
---|---|---|---|
1 | /* read.c -- read bytes from a input device. |
1 | /* read.c -- read bytes from a input device. |
2 | * |
2 | * |
3 | * Copyright (c) 1995 Cygnus Support |
3 | * Copyright (c) 1995 Cygnus Support |
4 | * |
4 | * |
5 | * The authors hereby grant permission to use, copy, modify, distribute, |
5 | * The authors hereby grant permission to use, copy, modify, distribute, |
6 | * and license this software and its documentation for any purpose, provided |
6 | * and license this software and its documentation for any purpose, provided |
7 | * that existing copyright notices are retained in all copies and that this |
7 | * that existing copyright notices are retained in all copies and that this |
8 | * notice is included verbatim in any distributions. No written agreement, |
8 | * notice is included verbatim in any distributions. No written agreement, |
9 | * license, or royalty fee is required for any of the authorized uses. |
9 | * license, or royalty fee is required for any of the authorized uses. |
10 | * Modifications to this software may be copyrighted by their authors |
10 | * Modifications to this software may be copyrighted by their authors |
11 | * and need not follow the licensing terms described here, provided that |
11 | * and need not follow the licensing terms described here, provided that |
12 | * the new terms are clearly indicated on the first page of each file where |
12 | * the new terms are clearly indicated on the first page of each file where |
13 | * they apply. |
13 | * they apply. |
14 | */ |
14 | */ |
15 | #include |
- | |
16 | #include |
15 | #include |
17 | #include |
- | |
18 | #include |
16 | #include |
19 | #include |
- | |
20 | #include |
- | |
21 | #include "glue.h" |
- | |
22 | #include "io.h" |
17 | #include "io.h" |
23 | 18 | ||
24 | #undef erro |
19 | #undef erro |
25 | extern int errno; |
20 | extern int errno; |
26 | 21 | ||
27 | ssize_t read(int fd, void *buf, size_t cnt) |
22 | ssize_t read(int fd, void *buf, size_t cnt) |
28 | { |
23 | { |
29 | _ssize_t ret; |
24 | _ssize_t ret; |
30 | 25 | ||
31 | _ssize_t read_len, total_len; |
26 | _ssize_t read_len, total_len; |
32 | unsigned reduce_idx, finish_idx; |
27 | unsigned reduce_idx, finish_idx; |
33 | unsigned iomode_flags; |
28 | unsigned iomode_flags; |
34 | char *buffer = buf; |
29 | char *buffer = buf; |
35 | int rc; |
30 | int rc; |
36 | int h; |
31 | int h; |
37 | unsigned amount_read; |
32 | unsigned amount_read; |
38 | int err; |
33 | int err; |
39 | 34 | ||
40 | __io_handle *ioh; |
35 | __io_handle *ioh; |
41 | 36 | ||
42 | if( (fd < 0) || (fd >=64) ) |
37 | if( (fd < 0) || (fd >=64) ) |
43 | { |
38 | { |
44 | errno = EBADF; |
39 | errno = EBADF; |
45 | return -1; |
40 | return -1; |
46 | }; |
41 | }; |
47 | 42 | ||
48 | ioh = &__io_tab[fd]; |
43 | ioh = &__io_tab[fd]; |
49 | 44 | ||
50 | iomode_flags = ioh->mode; |
45 | iomode_flags = ioh->mode; |
51 | 46 | ||
52 | if( iomode_flags == 0 ) |
47 | if( iomode_flags == 0 ) |
53 | { |
48 | { |
54 | errno = EBADF; |
49 | errno = EBADF; |
55 | return( -1 ); |
50 | return( -1 ); |
56 | } |
51 | } |
57 | 52 | ||
58 | if( !(iomode_flags & _READ) ) |
53 | if( !(iomode_flags & _READ) ) |
59 | { |
54 | { |
60 | errno = EACCES; |
55 | errno = EACCES; |
61 | return( -1 ); |
56 | return( -1 ); |
62 | } |
57 | } |
63 | 58 | ||
64 | if( iomode_flags & _BINARY ) /* if binary mode */ |
59 | if( iomode_flags & _BINARY ) /* if binary mode */ |
65 | { |
60 | { |
66 | err = read_file(ioh->name, buffer, ioh->offset, cnt, &amount_read); |
61 | err = ioh->read(ioh->name, buffer, ioh->offset, cnt, &amount_read); |
67 | ioh->offset+= amount_read; |
62 | ioh->offset+= amount_read; |
68 | total_len = amount_read; |
63 | total_len = amount_read; |
69 | 64 | ||
70 | if(err) |
65 | if(err) |
71 | if ( amount_read == 0) |
66 | if ( amount_read == 0) |
72 | return (-1); |
67 | return (-1); |
73 | } |
68 | } |
74 | else |
69 | else |
75 | { |
70 | { |
76 | total_len = 0; |
71 | total_len = 0; |
77 | read_len = cnt; |
72 | read_len = cnt; |
78 | do |
73 | do |
79 | { |
74 | { |
80 | err=read_file(ioh->name,buffer, ioh->offset, cnt, &amount_read); |
75 | err=ioh->read(ioh->name,buffer, ioh->offset, cnt, &amount_read); |
81 | ioh->offset+=amount_read; |
76 | ioh->offset+=amount_read; |
82 | 77 | ||
83 | if( amount_read == 0 ) |
78 | if( amount_read == 0 ) |
84 | break; /* EOF */ |
79 | break; /* EOF */ |
85 | 80 | ||
86 | reduce_idx = 0; |
81 | reduce_idx = 0; |
87 | finish_idx = reduce_idx; |
82 | finish_idx = reduce_idx; |
88 | for( ; reduce_idx < amount_read; ++reduce_idx ) |
83 | for( ; reduce_idx < amount_read; ++reduce_idx ) |
89 | { |
84 | { |
90 | if( buffer[ reduce_idx ] == 0x1a ) /* EOF */ |
85 | if( buffer[ reduce_idx ] == 0x1a ) /* EOF */ |
91 | { |
86 | { |
92 | lseek(fd, ((long)reduce_idx - (long)amount_read)+1L, SEEK_CUR ); |
87 | lseek(fd, ((long)reduce_idx - (long)amount_read)+1L, SEEK_CUR ); |
93 | total_len += finish_idx; |
88 | total_len += finish_idx; |
94 | return( total_len ); |
89 | return( total_len ); |
95 | } |
90 | } |
96 | if( buffer[ reduce_idx ] != '\r' ) |
91 | if( buffer[ reduce_idx ] != '\r' ) |
97 | { |
92 | { |
98 | buffer[ finish_idx++ ] = buffer[ reduce_idx ]; |
93 | buffer[ finish_idx++ ] = buffer[ reduce_idx ]; |
99 | }; |
94 | }; |
100 | } |
95 | } |
101 | 96 | ||
102 | total_len += finish_idx; |
97 | total_len += finish_idx; |
103 | buffer += finish_idx; |
98 | buffer += finish_idx; |
104 | read_len -= finish_idx; |
99 | read_len -= finish_idx; |
105 | if( iomode_flags & _ISTTY ) |
100 | if( iomode_flags & _ISTTY ) |
106 | { |
101 | { |
107 | break; |
102 | break; |
108 | } |
103 | } |
109 | } while( read_len != 0 ); |
104 | } while( read_len != 0 ); |
110 | } |
105 | } |
111 | return( total_len ); |
106 | return( total_len ); |
112 | }>> |
107 | }>> |