Rev 3757 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3757 | Rev 3758 | ||
---|---|---|---|
1 | /* |
1 | /* |
2 | * Copyright (c) 2008 Jiri Svoboda |
2 | * Copyright (c) 2008 Jiri Svoboda |
3 | * All rights reserved. |
3 | * All rights reserved. |
4 | * |
4 | * |
5 | * Redistribution and use in source and binary forms, with or without |
5 | * Redistribution and use in source and binary forms, with or without |
6 | * modification, are permitted provided that the following conditions |
6 | * modification, are permitted provided that the following conditions |
7 | * are met: |
7 | * are met: |
8 | * |
8 | * |
9 | * - Redistributions of source code must retain the above copyright |
9 | * - Redistributions of source code must retain the above copyright |
10 | * notice, this list of conditions and the following disclaimer. |
10 | * notice, this list of conditions and the following disclaimer. |
11 | * - Redistributions in binary form must reproduce the above copyright |
11 | * - Redistributions in binary form must reproduce the above copyright |
12 | * notice, this list of conditions and the following disclaimer in the |
12 | * notice, this list of conditions and the following disclaimer in the |
13 | * documentation and/or other materials provided with the distribution. |
13 | * documentation and/or other materials provided with the distribution. |
14 | * - The name of the author may not be used to endorse or promote products |
14 | * - The name of the author may not be used to endorse or promote products |
15 | * derived from this software without specific prior written permission. |
15 | * derived from this software without specific prior written permission. |
16 | * |
16 | * |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
27 | */ |
27 | */ |
28 | 28 | ||
29 | /** @addtogroup libc |
29 | /** @addtogroup libc |
30 | * @{ |
30 | * @{ |
31 | */ |
31 | */ |
32 | /** |
32 | /** |
33 | * @file |
33 | * @file |
34 | * @brief ANSI C Stream I/O. |
34 | * @brief ANSI C Stream I/O. |
35 | */ |
35 | */ |
36 | 36 | ||
37 | #include <stdlib.h> |
37 | #include <stdlib.h> |
38 | #include <sys/types.h> |
38 | #include <sys/types.h> |
39 | #include <sys/stat.h> |
39 | #include <sys/stat.h> |
40 | #include <fcntl.h> |
40 | #include <fcntl.h> |
41 | #include <unistd.h> |
41 | #include <unistd.h> |
42 | #include <errno.h> |
42 | #include <errno.h> |
43 | #include <bool.h> |
43 | #include <bool.h> |
44 | #include <stdio.h> |
44 | #include <stdio.h> |
45 | 45 | ||
- | 46 | FILE *stdin, *stdout, *stderr; |
|
- | 47 | ||
46 | /** |
48 | /** |
47 | * Open a stream. |
49 | * Open a stream. |
48 | * |
50 | * |
49 | * @param file_name Name of the file to open. |
51 | * @param file_name Name of the file to open. |
50 | * @param mode Mode string, (r|w|a)[b|t][+]. |
52 | * @param mode Mode string, (r|w|a)[b|t][+]. |
51 | */ |
53 | */ |
52 | FILE *fopen(const char *file_name, const char *mode) |
54 | FILE *fopen(const char *file_name, const char *mode) |
53 | { |
55 | { |
54 | FILE *f; |
56 | FILE *f; |
55 | int flags; |
57 | int flags; |
56 | bool plus; |
58 | bool plus; |
57 | const char *mp; |
59 | const char *mp; |
58 | 60 | ||
59 | /* Parse mode except first character. */ |
61 | /* Parse mode except first character. */ |
60 | 62 | ||
61 | mp = mode; |
63 | mp = mode; |
62 | if (*mp++ == '\0') { |
64 | if (*mp++ == '\0') { |
63 | errno = EINVAL; |
65 | errno = EINVAL; |
64 | return NULL; |
66 | return NULL; |
65 | } |
67 | } |
66 | 68 | ||
67 | if (*mp == 'b' || *mp == 't') ++mp; |
69 | if (*mp == 'b' || *mp == 't') ++mp; |
68 | 70 | ||
69 | if (*mp == '+') { |
71 | if (*mp == '+') { |
70 | ++mp; |
72 | ++mp; |
71 | plus = true; |
73 | plus = true; |
72 | } else { |
74 | } else { |
73 | plus = false; |
75 | plus = false; |
74 | } |
76 | } |
75 | 77 | ||
76 | if (*mp != '\0') { |
78 | if (*mp != '\0') { |
77 | errno = EINVAL; |
79 | errno = EINVAL; |
78 | return NULL; |
80 | return NULL; |
79 | } |
81 | } |
80 | 82 | ||
81 | /* Parse first character of mode and determine flags for open(). */ |
83 | /* Parse first character of mode and determine flags for open(). */ |
82 | 84 | ||
83 | switch (mode[0]) { |
85 | switch (mode[0]) { |
84 | case 'r': |
86 | case 'r': |
85 | flags = plus ? O_RDWR : O_RDONLY; |
87 | flags = plus ? O_RDWR : O_RDONLY; |
86 | break; |
88 | break; |
87 | case 'w': |
89 | case 'w': |
88 | flags = (O_TRUNC | O_CREAT) | (plus ? O_RDWR : O_WRONLY); |
90 | flags = (O_TRUNC | O_CREAT) | (plus ? O_RDWR : O_WRONLY); |
89 | break; |
91 | break; |
90 | case 'a': |
92 | case 'a': |
91 | /* TODO: a+ must read from beginning, append to the end. */ |
93 | /* TODO: a+ must read from beginning, append to the end. */ |
92 | if (plus) { |
94 | if (plus) { |
93 | errno = ENOTSUP; |
95 | errno = ENOTSUP; |
94 | return NULL; |
96 | return NULL; |
95 | } |
97 | } |
96 | flags = (O_APPEND | O_CREAT) | (plus ? O_RDWR : O_WRONLY); |
98 | flags = (O_APPEND | O_CREAT) | (plus ? O_RDWR : O_WRONLY); |
97 | default: |
99 | default: |
98 | errno = EINVAL; |
100 | errno = EINVAL; |
99 | return NULL; |
101 | return NULL; |
100 | } |
102 | } |
101 | 103 | ||
102 | /* Open file. */ |
104 | /* Open file. */ |
103 | 105 | ||
104 | f = malloc(sizeof(FILE)); |
106 | f = malloc(sizeof(FILE)); |
105 | if (f == NULL) { |
107 | if (f == NULL) { |
106 | errno = ENOMEM; |
108 | errno = ENOMEM; |
107 | return NULL; |
109 | return NULL; |
108 | } |
110 | } |
109 | 111 | ||
110 | f->fd = open(file_name, flags, 0666); |
112 | f->fd = open(file_name, flags, 0666); |
111 | if (f->fd < 0) { |
113 | if (f->fd < 0) { |
112 | free(f); |
114 | free(f); |
113 | return NULL; /* errno was set by open() */ |
115 | return NULL; /* errno was set by open() */ |
114 | } |
116 | } |
115 | 117 | ||
116 | f->error = 0; |
118 | f->error = 0; |
117 | f->eof = 0; |
119 | f->eof = 0; |
118 | 120 | ||
119 | return f; |
121 | return f; |
120 | } |
122 | } |
121 | 123 | ||
122 | /** Close a stream. |
124 | /** Close a stream. |
123 | * |
125 | * |
124 | * @param f Pointer to stream. |
126 | * @param f Pointer to stream. |
125 | * @return 0 on success, EOF on error. |
127 | * @return 0 on success, EOF on error. |
126 | */ |
128 | */ |
127 | int fclose(FILE *f) |
129 | int fclose(FILE *f) |
128 | { |
130 | { |
129 | int rc; |
131 | int rc; |
130 | 132 | ||
131 | rc = close(f->fd); |
133 | rc = close(f->fd); |
132 | free(f); |
134 | free(f); |
133 | 135 | ||
134 | if (rc != 0) |
136 | if (rc != 0) |
135 | return EOF; /* errno was set by close() */ |
137 | return EOF; /* errno was set by close() */ |
136 | 138 | ||
137 | return 0; |
139 | return 0; |
138 | } |
140 | } |
139 | 141 | ||
140 | /** Read from a stream. |
142 | /** Read from a stream. |
141 | * |
143 | * |
142 | * @param buf Destination buffer. |
144 | * @param buf Destination buffer. |
143 | * @param size Size of each record. |
145 | * @param size Size of each record. |
144 | * @param nmemb Number of records to read. |
146 | * @param nmemb Number of records to read. |
145 | * @param f Pointer to the stream. |
147 | * @param f Pointer to the stream. |
146 | */ |
148 | */ |
147 | size_t fread(void *buf, size_t size, size_t nmemb, FILE *f) |
149 | size_t fread(void *buf, size_t size, size_t nmemb, FILE *f) |
148 | { |
150 | { |
149 | size_t left, done, n; |
151 | size_t left, done, n; |
150 | 152 | ||
151 | left = size * nmemb; |
153 | left = size * nmemb; |
152 | done = 0; |
154 | done = 0; |
153 | 155 | ||
154 | while (left > 0 && !f->error && !f->eof) { |
156 | while (left > 0 && !f->error && !f->eof) { |
155 | n = read(f->fd, buf + done, left); |
157 | n = read(f->fd, buf + done, left); |
156 | 158 | ||
157 | if (n < 0) { |
159 | if (n < 0) { |
158 | f->error = 1; |
160 | f->error = 1; |
159 | } else if (n == 0) { |
161 | } else if (n == 0) { |
160 | f->eof = 1; |
162 | f->eof = 1; |
161 | } else { |
163 | } else { |
162 | left -= n; |
164 | left -= n; |
163 | done += n; |
165 | done += n; |
164 | } |
166 | } |
165 | } |
167 | } |
166 | 168 | ||
167 | return done / size; |
169 | return done / size; |
168 | } |
170 | } |
169 | 171 | ||
170 | 172 | ||
171 | /** Write to a stream. |
173 | /** Write to a stream. |
172 | * |
174 | * |
173 | * @param buf Source buffer. |
175 | * @param buf Source buffer. |
174 | * @param size Size of each record. |
176 | * @param size Size of each record. |
175 | * @param nmemb Number of records to write. |
177 | * @param nmemb Number of records to write. |
176 | * @param f Pointer to the stream. |
178 | * @param f Pointer to the stream. |
177 | */ |
179 | */ |
178 | size_t fwrite(const void *buf, size_t size, size_t nmemb, FILE *f) |
180 | size_t fwrite(const void *buf, size_t size, size_t nmemb, FILE *f) |
179 | { |
181 | { |
180 | size_t left, done, n; |
182 | size_t left, done, n; |
181 | 183 | ||
182 | left = size * nmemb; |
184 | left = size * nmemb; |
183 | done = 0; |
185 | done = 0; |
184 | 186 | ||
185 | while (left > 0 && !f->error) { |
187 | while (left > 0 && !f->error) { |
186 | n = write(f->fd, buf + done, left); |
188 | n = write(f->fd, buf + done, left); |
187 | 189 | ||
188 | if (n <= 0) { |
190 | if (n <= 0) { |
189 | f->error = 1; |
191 | f->error = 1; |
190 | } else { |
192 | } else { |
191 | left -= n; |
193 | left -= n; |
192 | done += n; |
194 | done += n; |
193 | } |
195 | } |
194 | } |
196 | } |
195 | 197 | ||
196 | return done / size; |
198 | return done / size; |
197 | } |
199 | } |
198 | 200 | ||
199 | /** Return the end-of-file indicator of a stream. */ |
201 | /** Return the end-of-file indicator of a stream. */ |
200 | int feof(FILE *f) |
202 | int feof(FILE *f) |
201 | { |
203 | { |
202 | return f->eof; |
204 | return f->eof; |
203 | } |
205 | } |
204 | 206 | ||
205 | /** Return the error indicator of a stream. */ |
207 | /** Return the error indicator of a stream. */ |
206 | int ferror(FILE *f) |
208 | int ferror(FILE *f) |
207 | { |
209 | { |
208 | return f->error; |
210 | return f->error; |
209 | } |
211 | } |
210 | 212 | ||
211 | /** Clear the error and end-of-file indicators of a stream. */ |
213 | /** Clear the error and end-of-file indicators of a stream. */ |
212 | void clearerr(FILE *f) |
214 | void clearerr(FILE *f) |
213 | { |
215 | { |
214 | f->eof = 0; |
216 | f->eof = 0; |
215 | f->error = 0; |
217 | f->error = 0; |
216 | } |
218 | } |
217 | 219 | ||
218 | /** Read character from a stream. */ |
220 | /** Read character from a stream. */ |
219 | int fgetc(FILE *f) |
221 | int fgetc(FILE *f) |
220 | { |
222 | { |
221 | unsigned char c; |
223 | unsigned char c; |
222 | size_t n; |
224 | size_t n; |
223 | 225 | ||
224 | n = fread(&c, sizeof(c), 1, f); |
226 | n = fread(&c, sizeof(c), 1, f); |
225 | if (n < 1) return EOF; |
227 | if (n < 1) return EOF; |
226 | 228 | ||
227 | return (int) c; |
229 | return (int) c; |
228 | } |
230 | } |
229 | 231 | ||
230 | /** Write character to a stream. */ |
232 | /** Write character to a stream. */ |
231 | int fputc(int c, FILE *f) |
233 | int fputc(int c, FILE *f) |
232 | { |
234 | { |
233 | unsigned char cc; |
235 | unsigned char cc; |
234 | size_t n; |
236 | size_t n; |
235 | 237 | ||
236 | cc = (unsigned char) c; |
238 | cc = (unsigned char) c; |
237 | n = fwrite(&cc, sizeof(cc), 1, f); |
239 | n = fwrite(&cc, sizeof(cc), 1, f); |
238 | if (n < 1) return EOF; |
240 | if (n < 1) return EOF; |
239 | 241 | ||
240 | return (int) cc; |
242 | return (int) cc; |
241 | } |
243 | } |
242 | 244 | ||
243 | /** Write string to a stream. */ |
245 | /** Write string to a stream. */ |
244 | int fputs(const char *s, FILE *f) |
246 | int fputs(const char *s, FILE *f) |
245 | { |
247 | { |
246 | int rc; |
248 | int rc; |
247 | 249 | ||
248 | rc = 0; |
250 | rc = 0; |
249 | 251 | ||
250 | while (*s && rc >= 0) { |
252 | while (*s && rc >= 0) { |
251 | rc = fputc(*s++, f); |
253 | rc = fputc(*s++, f); |
252 | } |
254 | } |
253 | 255 | ||
254 | if (rc < 0) return EOF; |
256 | if (rc < 0) return EOF; |
255 | 257 | ||
256 | return 0; |
258 | return 0; |
257 | } |
259 | } |
258 | 260 | ||
259 | /** Seek to position in stream. */ |
261 | /** Seek to position in stream. */ |
260 | int fseek(FILE *f, long offset, int origin) |
262 | int fseek(FILE *f, long offset, int origin) |
261 | { |
263 | { |
262 | off_t rc; |
264 | off_t rc; |
263 | 265 | ||
264 | rc = lseek(f->fd, offset, origin); |
266 | rc = lseek(f->fd, offset, origin); |
265 | if (rc == (off_t) (-1)) { |
267 | if (rc == (off_t) (-1)) { |
266 | /* errno has been set by lseek. */ |
268 | /* errno has been set by lseek. */ |
267 | return -1; |
269 | return -1; |
268 | } |
270 | } |
269 | 271 | ||
270 | f->eof = 0; |
272 | f->eof = 0; |
271 | 273 | ||
272 | return 0; |
274 | return 0; |
273 | } |
275 | } |
274 | 276 | ||
275 | /** @} |
277 | /** @} |
276 | */ |
278 | */ |
277 | 279 |