Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

tools/nolibc: Add fread() to stdio.h

Add a very basic version of fread() like we already have for fwrite().

Signed-off-by: Daniel Palmer <daniel@thingy.jp>
Link: https://patch.msgid.link/20260105023629.1502801-2-daniel@thingy.jp
Signed-off-by: Thomas Weißschuh <linux@weissschuh.net>

authored by

Daniel Palmer and committed by
Thomas Weißschuh
edaf3074 6fe8360b

+33 -1
+33 -1
tools/include/nolibc/stdio.h
··· 170 170 } 171 171 172 172 173 - /* fwrite(), puts(), fputs(). Note that puts() emits '\n' but not fputs(). */ 173 + /* fwrite(), fread(), puts(), fputs(). Note that puts() emits '\n' but not fputs(). */ 174 174 175 175 /* internal fwrite()-like function which only takes a size and returns 0 on 176 176 * success or EOF on error. It automatically retries on short writes. ··· 202 202 s += size; 203 203 } 204 204 return written; 205 + } 206 + 207 + /* internal fread()-like function which only takes a size and returns 0 on 208 + * success or EOF on error. It automatically retries on short reads. 209 + */ 210 + static __attribute__((unused)) 211 + int _fread(void *buf, size_t size, FILE *stream) 212 + { 213 + int fd = fileno(stream); 214 + ssize_t ret; 215 + 216 + while (size) { 217 + ret = read(fd, buf, size); 218 + if (ret <= 0) 219 + return EOF; 220 + size -= ret; 221 + buf += ret; 222 + } 223 + return 0; 224 + } 225 + 226 + static __attribute__((unused)) 227 + size_t fread(void *s, size_t size, size_t nmemb, FILE *stream) 228 + { 229 + size_t nread; 230 + 231 + for (nread = 0; nread < nmemb; nread++) { 232 + if (_fread(s, size, stream) != 0) 233 + break; 234 + s += size; 235 + } 236 + return nread; 205 237 } 206 238 207 239 static __attribute__((unused))