Rev 3758 | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 3758 | Rev 4338 | ||
---|---|---|---|
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; |
46 | FILE *stdin, *stdout, *stderr; |
47 | 47 | ||
48 | /** |
48 | /** |
49 | * Open a stream. |
49 | * Open a stream. |
50 | * |
50 | * |
51 | * @param file_name Name of the file to open. |
51 | * @param file_name Name of the file to open. |
52 | * @param mode Mode string, (r|w|a)[b|t][+]. |
52 | * @param mode Mode string, (r|w|a)[b|t][+]. |
53 | */ |
53 | */ |
54 | FILE *fopen(const char *file_name, const char *mode) |
54 | FILE *fopen(const char *file_name, const char *mode) |
55 | { |
55 | { |
56 | FILE *f; |
56 | FILE *f; |
57 | int flags; |
57 | int flags; |
58 | bool plus; |
58 | bool plus; |
59 | const char *mp; |
59 | const char *mp; |
60 | 60 | ||
61 | /* Parse mode except first character. */ |
61 | /* Parse mode except first character. */ |
62 | 62 | ||
63 | mp = mode; |
63 | mp = mode; |
64 | if (*mp++ == '\0') { |
64 | if (*mp++ == '\0') { |
65 | errno = EINVAL; |
65 | errno = EINVAL; |
66 | return NULL; |
66 | return NULL; |
67 | } |
67 | } |
68 | 68 | ||
69 | if (*mp == 'b' || *mp == 't') ++mp; |
69 | if (*mp == 'b' || *mp == 't') ++mp; |
70 | 70 | ||
71 | if (*mp == '+') { |
71 | if (*mp == '+') { |
72 | ++mp; |
72 | ++mp; |
73 | plus = true; |
73 | plus = true; |
74 | } else { |
74 | } else { |
75 | plus = false; |
75 | plus = false; |
76 | } |
76 | } |
77 | 77 | ||
78 | if (*mp != '\0') { |
78 | if (*mp != '\0') { |
79 | errno = EINVAL; |
79 | errno = EINVAL; |
80 | return NULL; |
80 | return NULL; |
81 | } |
81 | } |
82 | 82 | ||
83 | /* Parse first character of mode and determine flags for open(). */ |
83 | /* Parse first character of mode and determine flags for open(). */ |
84 | 84 | ||
85 | switch (mode[0]) { |
85 | switch (mode[0]) { |
86 | case 'r': |
86 | case 'r': |
87 | flags = plus ? O_RDWR : O_RDONLY; |
87 | flags = plus ? O_RDWR : O_RDONLY; |
88 | break; |
88 | break; |
89 | case 'w': |
89 | case 'w': |
90 | flags = (O_TRUNC | O_CREAT) | (plus ? O_RDWR : O_WRONLY); |
90 | flags = (O_TRUNC | O_CREAT) | (plus ? O_RDWR : O_WRONLY); |
91 | break; |
91 | break; |
92 | case 'a': |
92 | case 'a': |
93 | /* TODO: a+ must read from beginning, append to the end. */ |
93 | /* TODO: a+ must read from beginning, append to the end. */ |
94 | if (plus) { |
94 | if (plus) { |
95 | errno = ENOTSUP; |
95 | errno = ENOTSUP; |
96 | return NULL; |
96 | return NULL; |
97 | } |
97 | } |
98 | flags = (O_APPEND | O_CREAT) | (plus ? O_RDWR : O_WRONLY); |
98 | flags = (O_APPEND | O_CREAT) | (plus ? O_RDWR : O_WRONLY); |
99 | default: |
99 | default: |
100 | errno = EINVAL; |
100 | errno = EINVAL; |
101 | return NULL; |
101 | return NULL; |
102 | } |
102 | } |
103 | 103 | ||
104 | /* Open file. */ |
104 | /* Open file. */ |
105 | 105 | ||
106 | f = malloc(sizeof(FILE)); |
106 | f = malloc(sizeof(FILE)); |
107 | if (f == NULL) { |
107 | if (f == NULL) { |
108 | errno = ENOMEM; |
108 | errno = ENOMEM; |
109 | return NULL; |
109 | return NULL; |
110 | } |
110 | } |
111 | 111 | ||
112 | f->fd = open(file_name, flags, 0666); |
112 | f->fd = open(file_name, flags, 0666); |
113 | if (f->fd < 0) { |
113 | if (f->fd < 0) { |
114 | free(f); |
114 | free(f); |
115 | return NULL; /* errno was set by open() */ |
115 | return NULL; /* errno was set by open() */ |
116 | } |
116 | } |
117 | 117 | ||
118 | f->error = 0; |
118 | f->error = 0; |
119 | f->eof = 0; |
119 | f->eof = 0; |
120 | 120 | ||
121 | return f; |
121 | return f; |
122 | } |
122 | } |
123 | 123 | ||
124 | /** Close a stream. |
124 | /** Close a stream. |
125 | * |
125 | * |
126 | * @param f Pointer to stream. |
126 | * @param f Pointer to stream. |
127 | * @return 0 on success, EOF on error. |
127 | * @return 0 on success, EOF on error. |
128 | */ |
128 | */ |
129 | int fclose(FILE *f) |
129 | int fclose(FILE *f) |
130 | { |
130 | { |
131 | int rc; |
131 | int rc; |
132 | 132 | ||
133 | rc = close(f->fd); |
133 | rc = close(f->fd); |
134 | free(f); |
134 | free(f); |
135 | 135 | ||
136 | if (rc != 0) |
136 | if (rc != 0) |
137 | return EOF; /* errno was set by close() */ |
137 | return EOF; /* errno was set by close() */ |
138 | 138 | ||
139 | return 0; |
139 | return 0; |
140 | } |
140 | } |
141 | 141 | ||
142 | /** Read from a stream. |
142 | /** Read from a stream. |
143 | * |
143 | * |
144 | * @param buf Destination buffer. |
144 | * @param buf Destination buffer. |
145 | * @param size Size of each record. |
145 | * @param size Size of each record. |
146 | * @param nmemb Number of records to read. |
146 | * @param nmemb Number of records to read. |
147 | * @param f Pointer to the stream. |
147 | * @param f Pointer to the stream. |
148 | */ |
148 | */ |
149 | 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) |
150 | { |
150 | { |
151 | size_t left, done, n; |
151 | size_t left, done, n; |
152 | 152 | ||
153 | left = size * nmemb; |
153 | left = size * nmemb; |
154 | done = 0; |
154 | done = 0; |
155 | 155 | ||
156 | while (left > 0 && !f->error && !f->eof) { |
156 | while (left > 0 && !f->error && !f->eof) { |
157 | n = read(f->fd, buf + done, left); |
157 | n = read(f->fd, buf + done, left); |
158 | 158 | ||
159 | if (n < 0) { |
159 | if (n < 0) { |
160 | f->error = 1; |
160 | f->error = 1; |
161 | } else if (n == 0) { |
161 | } else if (n == 0) { |
162 | f->eof = 1; |
162 | f->eof = 1; |
163 | } else { |
163 | } else { |
164 | left -= n; |
164 | left -= n; |
165 | done += n; |
165 | done += n; |
166 | } |
166 | } |
167 | } |
167 | } |
168 | 168 | ||
169 | return done / size; |
169 | return done / size; |
170 | } |
170 | } |
171 | 171 | ||
172 | 172 | ||
173 | /** Write to a stream. |
173 | /** Write to a stream. |
174 | * |
174 | * |
175 | * @param buf Source buffer. |
175 | * @param buf Source buffer. |
176 | * @param size Size of each record. |
176 | * @param size Size of each record. |
177 | * @param nmemb Number of records to write. |
177 | * @param nmemb Number of records to write. |
178 | * @param f Pointer to the stream. |
178 | * @param f Pointer to the stream. |
179 | */ |
179 | */ |
180 | 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) |
181 | { |
181 | { |
182 | size_t left, done, n; |
182 | size_t left, done, n; |
183 | 183 | ||
184 | left = size * nmemb; |
184 | left = size * nmemb; |
185 | done = 0; |
185 | done = 0; |
186 | 186 | ||
187 | while (left > 0 && !f->error) { |
187 | while (left > 0 && !f->error) { |
188 | n = write(f->fd, buf + done, left); |
188 | n = write(f->fd, buf + done, left); |
189 | 189 | ||
190 | if (n <= 0) { |
190 | if (n <= 0) { |
191 | f->error = 1; |
191 | f->error = 1; |
192 | } else { |
192 | } else { |
193 | left -= n; |
193 | left -= n; |
194 | done += n; |
194 | done += n; |
195 | } |
195 | } |
196 | } |
196 | } |
197 | 197 | ||
198 | return done / size; |
198 | return done / size; |
199 | } |
199 | } |
200 | 200 | ||
201 | /** Return the end-of-file indicator of a stream. */ |
201 | /** Return the end-of-file indicator of a stream. */ |
202 | int feof(FILE *f) |
202 | int feof(FILE *f) |
203 | { |
203 | { |
204 | return f->eof; |
204 | return f->eof; |
205 | } |
205 | } |
206 | 206 | ||
207 | /** Return the error indicator of a stream. */ |
207 | /** Return the error indicator of a stream. */ |
208 | int ferror(FILE *f) |
208 | int ferror(FILE *f) |
209 | { |
209 | { |
210 | return f->error; |
210 | return f->error; |
211 | } |
211 | } |
212 | 212 | ||
213 | /** Clear the error and end-of-file indicators of a stream. */ |
213 | /** Clear the error and end-of-file indicators of a stream. */ |
214 | void clearerr(FILE *f) |
214 | void clearerr(FILE *f) |
215 | { |
215 | { |
216 | f->eof = 0; |
216 | f->eof = 0; |
217 | f->error = 0; |
217 | f->error = 0; |
218 | } |
218 | } |
219 | 219 | ||
220 | /** Read character from a stream. */ |
220 | /** Read character from a stream. */ |
221 | int fgetc(FILE *f) |
221 | int fgetc(FILE *f) |
222 | { |
222 | { |
223 | unsigned char c; |
223 | unsigned char c; |
224 | size_t n; |
224 | size_t n; |
225 | 225 | ||
226 | n = fread(&c, sizeof(c), 1, f); |
226 | n = fread(&c, sizeof(c), 1, f); |
227 | if (n < 1) return EOF; |
227 | if (n < 1) return EOF; |
228 | 228 | ||
229 | return (int) c; |
229 | return (int) c; |
230 | } |
230 | } |
231 | 231 | ||
232 | /** Write character to a stream. */ |
232 | /** Write character to a stream. */ |
233 | int fputc(int c, FILE *f) |
233 | int fputc(int c, FILE *f) |
234 | { |
234 | { |
235 | unsigned char cc; |
235 | unsigned char cc; |
236 | size_t n; |
236 | size_t n; |
237 | 237 | ||
238 | cc = (unsigned char) c; |
238 | cc = (unsigned char) c; |
239 | n = fwrite(&cc, sizeof(cc), 1, f); |
239 | n = fwrite(&cc, sizeof(cc), 1, f); |
240 | if (n < 1) return EOF; |
240 | if (n < 1) return EOF; |
241 | 241 | ||
242 | return (int) cc; |
242 | return (int) cc; |
243 | } |
243 | } |
244 | 244 | ||
245 | /** Write string to a stream. */ |
245 | /** Write string to a stream. */ |
246 | int fputs(const char *s, FILE *f) |
246 | int fputs(const char *s, FILE *f) |
247 | { |
247 | { |
248 | int rc; |
248 | int rc; |
249 | 249 | ||
250 | rc = 0; |
250 | rc = 0; |
251 | 251 | ||
252 | while (*s && rc >= 0) { |
252 | while (*s && rc >= 0) { |
253 | rc = fputc(*s++, f); |
253 | rc = fputc(*s++, f); |
254 | } |
254 | } |
255 | 255 | ||
256 | if (rc < 0) return EOF; |
256 | if (rc < 0) return EOF; |
257 | 257 | ||
258 | return 0; |
258 | return 0; |
259 | } |
259 | } |
260 | 260 | ||
261 | /** Seek to position in stream. */ |
261 | /** Seek to position in stream. */ |
262 | int fseek(FILE *f, long offset, int origin) |
262 | int fseek(FILE *f, long offset, int origin) |
263 | { |
263 | { |
264 | off_t rc; |
264 | off_t rc; |
265 | 265 | ||
266 | rc = lseek(f->fd, offset, origin); |
266 | rc = lseek(f->fd, offset, origin); |
267 | if (rc == (off_t) (-1)) { |
267 | if (rc == (off_t) (-1)) { |
268 | /* errno has been set by lseek. */ |
268 | /* errno has been set by lseek. */ |
269 | return -1; |
269 | return -1; |
270 | } |
270 | } |
271 | 271 | ||
272 | f->eof = 0; |
272 | f->eof = 0; |
273 | 273 | ||
274 | return 0; |
274 | return 0; |
275 | } |
275 | } |
276 | 276 | ||
277 | /** @} |
277 | /** @} |
278 | */ |
278 | */ |
279 | 279 |