Mercurial > hg > toybox
comparison lib/lib.c @ 310:8b8116214b1c
Roberto Foglietta pointed out that readall() needs fdlength() to restore
the original position before exiting.
author | Rob Landley <rob@landley.net> |
---|---|
date | Fri, 18 Jul 2008 05:43:44 -0500 |
parents | 79a61cd58596 |
children | 962bbf7341f8 |
comparison
equal
deleted
inserted
replaced
309:79a61cd58596 | 310:8b8116214b1c |
---|---|
472 } | 472 } |
473 | 473 |
474 // Return how long the file at fd is, if there's any way to determine it. | 474 // Return how long the file at fd is, if there's any way to determine it. |
475 off_t fdlength(int fd) | 475 off_t fdlength(int fd) |
476 { | 476 { |
477 off_t bottom = 0, top = 0, pos; | 477 off_t bottom = 0, top = 0, pos, old; |
478 int size; | 478 int size; |
479 | 479 |
480 // If the ioctl works for this, return it. | 480 // If the ioctl works for this, return it. |
481 | 481 |
482 if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512L; | 482 if (ioctl(fd, BLKGETSIZE, &size) >= 0) return size*512L; |
483 | 483 |
484 // If not, do a binary search for the last location we can read. (Some | 484 // If not, do a binary search for the last location we can read. (Some |
485 // block devices don't do BLKGETSIZE right.) This should probably have | 485 // block devices don't do BLKGETSIZE right.) This should probably have |
486 // a CONFIG option... | 486 // a CONFIG option... |
487 | 487 |
488 old = lseek(fd, 0, SEEK_CUR); | |
488 do { | 489 do { |
489 char temp; | 490 char temp; |
490 | 491 |
491 pos = bottom + (top - bottom) / 2; | 492 pos = bottom + (top - bottom) / 2; |
492 | 493 |
503 if (!top) return 0; | 504 if (!top) return 0; |
504 bottom = top/2; | 505 bottom = top/2; |
505 } else top = pos; | 506 } else top = pos; |
506 } | 507 } |
507 } while (bottom + 1 != top); | 508 } while (bottom + 1 != top); |
509 | |
510 lseek(fd, old, SEEK_SET); | |
508 | 511 |
509 return pos + 1; | 512 return pos + 1; |
510 } | 513 } |
511 | 514 |
512 // This can return null (meaning file not found). It just won't return null | 515 // This can return null (meaning file not found). It just won't return null |