Changeset 8b5fb5e in mainline for uspace/lib/posix/stdio.c
- Timestamp:
- 2011-06-28T20:51:10Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- 4ec6820
- Parents:
- 244d6fd
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/lib/posix/stdio.c
r244d6fd r8b5fb5e 41 41 #include "internal/common.h" 42 42 #include "stdio.h" 43 #include "libc/io/printf_core.h" 43 44 #include "string.h" 45 #include "libc/str.h" 44 46 45 47 /* not the best of solutions, but freopen will eventually … … 48 50 #include "../c/generic/private/stdio.h" 49 51 52 /** Clears the stream's error and end-of-file indicators. 53 * 54 * @param stream 55 */ 56 void posix_clearerr(FILE *stream) 57 { 58 stream->error = 0; 59 stream->eof = 0; 60 } 61 62 /** 63 * 64 * @param s 65 * @return 66 */ 67 char *posix_ctermid(char *s) 68 { 69 /* Currently always returns an error value (empty string). */ 70 // TODO: return a real terminal path 71 72 static char dummy_path[L_ctermid] = {'\0'}; 73 74 if (s == NULL) { 75 return dummy_path; 76 } 77 78 s[0] = '\0'; 79 return s; 80 } 81 50 82 /** 51 83 * … … 55 87 */ 56 88 int posix_ungetc(int c, FILE *stream) 89 { 90 // TODO 91 not_implemented(); 92 } 93 94 ssize_t posix_getdelim(char **restrict lineptr, size_t *restrict n, 95 int delimiter, FILE *restrict stream) 96 { 97 // TODO 98 not_implemented(); 99 } 100 101 ssize_t posix_getline(char **restrict lineptr, size_t *restrict n, 102 FILE *restrict stream) 57 103 { 58 104 // TODO … … 113 159 } 114 160 161 FILE *posix_fmemopen(void *restrict buf, size_t size, 162 const char *restrict mode) 163 { 164 // TODO 165 not_implemented(); 166 } 167 168 FILE *posix_open_memstream(char **bufp, size_t *sizep) 169 { 170 // TODO 171 not_implemented(); 172 } 173 115 174 /** 116 175 * … … 119 178 void posix_perror(const char *s) 120 179 { 121 // TODO 122 not_implemented(); 180 if (s == NULL || s[0] == '\0') { 181 fprintf(stderr, "%s\n", posix_strerror(errno)); 182 } else { 183 fprintf(stderr, "%s: %s\n", s, posix_strerror(errno)); 184 } 185 } 186 187 struct _posix_fpos { 188 off64_t offset; 189 }; 190 191 /** Restores stream a to position previously saved with fgetpos(). 192 * 193 * @param stream Stream to restore 194 * @param pos Position to restore 195 * @return Zero on success, non-zero (with errno set) on failure 196 */ 197 int posix_fsetpos(FILE *stream, const posix_fpos_t *pos) 198 { 199 return fseek(stream, pos->offset, SEEK_SET); 200 } 201 202 /** Saves the stream's position for later use by fsetpos(). 203 * 204 * @param stream Stream to save 205 * @param pos Place to store the position 206 * @return Zero on success, non-zero (with errno set) on failure 207 */ 208 int posix_fgetpos(FILE *restrict stream, posix_fpos_t *restrict pos) 209 { 210 off64_t ret = ftell(stream); 211 if (ret == -1) { 212 return errno; 213 } 214 pos->offset = ret; 215 return 0; 123 216 } 124 217 … … 130 223 * @return 131 224 */ 225 int posix_fseek(FILE *stream, long offset, int whence) 226 { 227 return fseek(stream, (off64_t) offset, whence); 228 } 229 230 /** 231 * 232 * @param stream 233 * @param offset 234 * @param whence 235 * @return 236 */ 132 237 int posix_fseeko(FILE *stream, posix_off_t offset, int whence) 133 238 { 134 // TODO 135 not_implemented(); 239 return fseek(stream, (off64_t) offset, whence); 240 } 241 242 /** 243 * 244 * @param stream 245 * @return 246 */ 247 long posix_ftell(FILE *stream) 248 { 249 return (long) ftell(stream); 136 250 } 137 251 … … 143 257 posix_off_t posix_ftello(FILE *stream) 144 258 { 145 // TODO 146 not_implemented(); 259 return (posix_off_t) ftell(stream); 260 } 261 262 int posix_dprintf(int fildes, const char *restrict format, ...) 263 { 264 va_list list; 265 va_start(list, format); 266 int result = posix_vdprintf(fildes, format, list); 267 va_end(list); 268 return result; 269 } 270 271 static int _dprintf_str_write(const char *str, size_t size, void *fd) 272 { 273 ssize_t wr = write(*(int *) fd, str, size); 274 return str_nlength(str, wr); 275 } 276 277 static int _dprintf_wstr_write(const wchar_t *str, size_t size, void *fd) 278 { 279 size_t offset = 0; 280 size_t chars = 0; 281 size_t sz; 282 char buf[4]; 283 284 while (offset < size) { 285 sz = 0; 286 if (chr_encode(str[chars], buf, &sz, sizeof(buf)) != EOK) { 287 break; 288 } 289 290 if (write(*(int *) fd, buf, sz) != (ssize_t) sz) { 291 break; 292 } 293 294 chars++; 295 offset += sizeof(wchar_t); 296 } 297 298 return chars; 299 } 300 301 int posix_vdprintf(int fildes, const char *restrict format, va_list ap) 302 { 303 printf_spec_t spec = { 304 .str_write = _dprintf_str_write, 305 .wstr_write = _dprintf_wstr_write, 306 .data = &fildes 307 }; 308 309 return printf_core(format, &spec, ap); 147 310 } 148 311 … … 156 319 int posix_sprintf(char *s, const char *format, ...) 157 320 { 158 // TODO 159 not_implemented(); 160 } 161 162 /** 163 * 164 * @param s 321 va_list list; 322 va_start(list, format); 323 int result = posix_vsprintf(s, format, list); 324 va_end(list); 325 return result; 326 } 327 328 /** 329 * 330 * @param s 331 * @param format 332 * @param ap 333 * @return 334 */ 335 int posix_vsprintf(char *s, const char *format, va_list ap) 336 { 337 return vsnprintf(s, STR_NO_LIMIT, format, ap); 338 } 339 340 /** 341 * 342 * @param stream 165 343 * @param format 166 344 * @param ... 167 345 * @return 168 346 */ 169 int posix_vsprintf(char *s, const char *format, va_list ap) 347 int posix_fscanf(FILE *restrict stream, const char *restrict format, ...) 348 { 349 va_list list; 350 va_start(list, format); 351 int result = posix_vfscanf(stream, format, list); 352 va_end(list); 353 return result; 354 } 355 356 int posix_vfscanf(FILE *restrict stream, const char *restrict format, va_list arg) 357 { 358 // TODO 359 not_implemented(); 360 } 361 362 /** 363 * 364 * @param format 365 * @param ... 366 * @return 367 */ 368 int posix_scanf(const char *restrict format, ...) 369 { 370 va_list list; 371 va_start(list, format); 372 int result = posix_vscanf(format, list); 373 va_end(list); 374 return result; 375 } 376 377 /** 378 * 379 * @param format 380 * @param arg 381 * @return 382 */ 383 int posix_vscanf(const char *restrict format, va_list arg) 384 { 385 return posix_vfscanf(stdin, format, arg); 386 } 387 388 /** 389 * 390 * @param s 391 * @param format 392 * @param ... 393 * @return 394 */ 395 int posix_sscanf(const char *s, const char *format, ...) 396 { 397 va_list list; 398 va_start(list, format); 399 int result = posix_vsscanf(s, format, list); 400 va_end(list); 401 return result; 402 } 403 404 /** 405 * 406 * @param s 407 * @param format 408 * @param arg 409 * @return 410 */ 411 int posix_vsscanf( 412 const char *restrict s, const char *restrict format, va_list arg) 413 { 414 // TODO 415 not_implemented(); 416 } 417 418 void posix_flockfile(FILE *file) 419 { 420 /* dummy */ 421 } 422 423 int posix_ftrylockfile(FILE *file) 424 { 425 /* dummy */ 426 return 0; 427 } 428 429 void posix_funlockfile(FILE *file) 430 { 431 /* dummy */ 432 } 433 434 int posix_getc_unlocked(FILE *stream) 435 { 436 return getc(stream); 437 } 438 439 int posix_getchar_unlocked(void) 440 { 441 return getchar(); 442 } 443 444 int posix_putc_unlocked(int c, FILE *stream) 445 { 446 return putc(c, stream); 447 } 448 449 int posix_putchar_unlocked(int c) 450 { 451 return putchar(c); 452 } 453 454 /** 455 * 456 * @param path 457 * @return 458 */ 459 int posix_remove(const char *path) 460 { 461 // FIXME: unlink() and rmdir() seem to be equivalent at the moment, 462 // but that does not have to be true forever 463 return unlink(path); 464 } 465 466 /** 467 * 468 * @param s 469 * @return 470 */ 471 char *posix_tmpnam(char *s) 170 472 { 171 473 // TODO: low priority, just a compile-time dependency of binutils … … 173 475 } 174 476 175 /**176 *177 * @param s178 * @param format179 * @param ...180 * @return181 */182 int posix_sscanf(const char *s, const char *format, ...)183 {184 // TODO185 not_implemented();186 }187 188 /**189 *190 * @param path191 * @return192 */193 int posix_remove(const char *path)194 {195 // TODO: low priority, just a compile-time dependency of binutils196 not_implemented();197 }198 199 /**200 *201 * @param s202 * @return203 */204 char *posix_tmpnam(char *s)205 {206 // TODO: low priority, just a compile-time dependency of binutils207 not_implemented();208 }209 210 477 /** @} 211 478 */
Note:
See TracChangeset
for help on using the changeset viewer.