From 6a42c8de66e3b2dc7293ddeadaa3ee396db2624d Mon Sep 17 00:00:00 2001 From: Luke Shumaker Date: Sat, 12 Oct 2013 13:47:42 -0400 Subject: initial commit --- public/fd_printf.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 public/fd_printf.md (limited to 'public/fd_printf.md') diff --git a/public/fd_printf.md b/public/fd_printf.md new file mode 100644 index 0000000..5e1098e --- /dev/null +++ b/public/fd_printf.md @@ -0,0 +1,37 @@ +`fd_printf`: print formatted text directly to a file descriptor +=============================================================== +:copyright 2013 Luke Shumaker +:license WTFPL-2 + +I wrote this while debugging some code, and thought it might be useful +to others: + + #define _GNU_SOURCE /* vasprintf() */ + #include /* va_start()/va_end() */ + #include /* vasprintf() */ + #include /* free() */ + #include /* write() */ + + int + fd_printf(int fd, const char *format, ...) + { + va_list arg; + int len; + char *str; + + va_start(arg, format); + len = vasprintf(&str, format, arg); + va_end(arg); + + write(fd, str, len); + + free(str); + return len; + } + + +It is a version of `printf` that prints to a file descriptor—where +`fprintf` prints to a `FILE*` data structure. + +The appeal of this is that `FILE*` I/O is buffered—which means mixing +it with raw file descriptor I/O is going to produce weird results. -- cgit v1.2.3