Return type should match what we declare in dumb.h. Temporary variable should be long, because A4 returns a long here. Setting/checking errno to return -1 when we can't read anything, but returning number of bytes read if we read at least a few bytes. Consider clarifying the getnc spec what we should return when the caller wishes for 20 bytes, we can only read 10, then encounter an error. We should probably return 10 then.
Unified · Split
| @@ -147,7 +147,7 @@ Returns as `dumb_ssize_t`: · 146 unmodified lines | ||
| 147 | 147 | * the number of bytes successfully read, if it was possible to read at least |
| 148 | 148 | one byte. |
| 149 | 149 | |
| 150 | −* `-1` on error. | |
| 150 | +* `-1` on error, when it was not possible to read even a single byte. | |
| 151 | 151 | |
| 152 | 152 | This function shall bytes from the file `f` and store them in sequence in the |
| 153 | 153 | buffer beginning at `ptr`. It shall read fewer than `n` bytes if end of file |
| @@ -59,12 +59,15 @@ static int dumb_packfile_getc(void *f) { · 58 unmodified lines | ||
| 59 | 59 | return c; |
| 60 | 60 | } |
| 61 | 61 | |
| 62 | −static size_t dumb_packfile_getnc(char *ptr, size_t n, void *f) { | |
| 62 | +static dumb_ssize_t dumb_packfile_getnc(char *ptr, size_t n, void *f) { | |
| 63 | 63 | dumb_packfile *file = (dumb_packfile *)f; |
| 64 | − int nr = pack_fread(ptr, n, file->p); | |
| 65 | − if (nr > 0) | |
| 64 | + errno = 0; | |
| 65 | + long nr = pack_fread(ptr, n, file->p); | |
| 66 | + if (nr > 0) { | |
| 66 | 67 | file->pos += nr; |
| 67 | − return nr; | |
| 68 | + return nr; | |
| 69 | + } | |
| 70 | + return errno != 0 ? -1 : 0; | |
| 68 | 71 | } |
| 69 | 72 | |
| 70 | 73 | static void dumb_packfile_close(void *f) { |