History

tools/nolibc/stdio: add fwrite() to stdio

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.

Willy Tarreau committed 4 years ago

Commit e3e19051 parent 99b037c1 file · +28 −7Browse files

Unified · Split

tools/include/nolibc/stdio.h+28−7
@@ -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 */
89static __attribute__((unused))92static __attribute__((unused))
90int fputs(const char *s, FILE *stream)93int _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
113static __attribute__((unused))
114size_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
126static __attribute__((unused))
127int fputs(const char *s, FILE *stream)
128{
129 return _fwrite(s, strlen(s), stream);
130}
131
111static __attribute__((unused))132static __attribute__((unused))
112int puts(const char *s)133int puts(const char *s)
113{134{