We'll use it to write substrings. It relies on a simpler _fwrite() that only takes one size. fputs() was also modified to rely on it.
Unified · Split
| @@ -84,12 +84,14 @@ int putchar(int c) · 83 unmodified lines | |||
| 84 | } | 84 | } |
| 85 | 85 | ||
| 86 | 86 | ||
| 87 | /* puts(), fputs(). Note that puts() emits '\n' but not fputs(). */ | 87 | /* fwrite(), puts(), fputs(). Note that puts() emits '\n' but not fputs(). */ |
| 88 | 88 | ||
| 89 | /* internal fwrite()-like function which only takes a size and returns 0 on | ||
| 90 | * success or EOF on error. It automatically retries on short writes. | ||
| 91 | */ | ||
| 89 | static __attribute__((unused)) | 92 | static __attribute__((unused)) |
| 90 | int fputs(const char *s, FILE *stream) | 93 | int _fwrite(const void *buf, size_t size, FILE *stream) |
| 91 | { | 94 | { |
| 92 | size_t len = strlen(s); | ||
| 93 | ssize_t ret; | 95 | ssize_t ret; |
| 94 | int fd; | 96 | int fd; |
| 95 | 97 | ||
| @@ -98,16 +100,35 @@ int fputs(const char *s, FILE *stream) · 2 unmodified lines | |||
| 98 | 100 | ||
| 99 | fd = 3 + (long)stream; | 101 | fd = 3 + (long)stream; |
| 100 | 102 | ||
| 101 | while (len > 0) { | 103 | while (size) { |
| 102 | ret = write(fd, s, len); | 104 | ret = write(fd, buf, size); |
| 103 | if (ret <= 0) | 105 | if (ret <= 0) |
| 104 | return EOF; | 106 | return EOF; |
| 105 | s += ret; | 107 | size -= ret; |
| 106 | len -= ret; | 108 | buf += ret; |
| 107 | } | 109 | } |
| 108 | return 0; | 110 | return 0; |
| 109 | } | 111 | } |
| 110 | 112 | ||
| 113 | static __attribute__((unused)) | ||
| 114 | size_t fwrite(const void *s, size_t size, size_t nmemb, FILE *stream) | ||
| 115 | { | ||
| 116 | size_t written; | ||
| 117 | |||
| 118 | for (written = 0; written < nmemb; written++) { | ||
| 119 | if (_fwrite(s, size, stream) != 0) | ||
| 120 | break; | ||
| 121 | s += size; | ||
| 122 | } | ||
| 123 | return written; | ||
| 124 | } | ||
| 125 | |||
| 126 | static __attribute__((unused)) | ||
| 127 | int fputs(const char *s, FILE *stream) | ||
| 128 | { | ||
| 129 | return _fwrite(s, strlen(s), stream); | ||
| 130 | } | ||
| 131 | |||
| 111 | static __attribute__((unused)) | 132 | static __attribute__((unused)) |
| 112 | int puts(const char *s) | 133 | int puts(const char *s) |
| 113 | { | 134 | { |