| 1689 | */ |
| 1690 | |
| 1691 | ssize_t /* O - Number of bytes read or -1 on error */ |
| 1692 | cupsFileRead(cups_file_t *fp, /* I - CUPS file */ |
| 1693 | char *buf, /* O - Buffer */ |
| 1694 | size_t bytes) /* I - Number of bytes to read */ |
| 1695 | { |
| 1696 | size_t total; /* Total bytes read */ |
| 1697 | ssize_t count; /* Bytes read */ |
| 1698 | |
| 1699 | |
| 1700 | DEBUG_printf(("2cupsFileRead(fp=%p, buf=%p, bytes=" CUPS_LLFMT ")", (void *)fp, (void *)buf, CUPS_LLCAST bytes)); |
| 1701 | |
| 1702 | /* |
| 1703 | * Range check input... |
| 1704 | */ |
| 1705 | |
| 1706 | if (!fp || !buf || (fp->mode != 'r' && fp->mode != 's')) |
| 1707 | return (-1); |
| 1708 | |
| 1709 | if (bytes == 0) |
| 1710 | return (0); |
| 1711 | |
| 1712 | if (fp->eof) |
| 1713 | { |
| 1714 | DEBUG_puts("5cupsFileRead: End-of-file!"); |
| 1715 | return (-1); |
| 1716 | } |
| 1717 | |
| 1718 | /* |
| 1719 | * Loop until all bytes are read... |
| 1720 | */ |
| 1721 | |
| 1722 | total = 0; |
| 1723 | while (bytes > 0) |
| 1724 | { |
| 1725 | if (fp->ptr >= fp->end) |
| 1726 | if (cups_fill(fp) <= 0) |
| 1727 | { |
| 1728 | DEBUG_printf(("4cupsFileRead: cups_fill() returned -1, total=" |
| 1729 | CUPS_LLFMT, CUPS_LLCAST total)); |
| 1730 | |
| 1731 | if (total > 0) |
| 1732 | return ((ssize_t)total); |
| 1733 | else |
| 1734 | return (-1); |
| 1735 | } |
| 1736 | |
| 1737 | count = (ssize_t)(fp->end - fp->ptr); |
| 1738 | if (count > (ssize_t)bytes) |
| 1739 | count = (ssize_t)bytes; |
| 1740 | |
| 1741 | memcpy(buf, fp->ptr,(size_t) count); |
| 1742 | fp->ptr += count; |
| 1743 | fp->pos += count; |
| 1744 | |
| 1745 | DEBUG_printf(("4cupsFileRead: pos=" CUPS_LLFMT, CUPS_LLCAST fp->pos)); |
| 1746 | |
| 1747 | /* |
| 1748 | * Update the counts for the last read... |