diff options
author | Elliott Hughes <enh@google.com> | 2016-08-01 16:35:17 -0700 |
---|---|---|
committer | Elliott Hughes <enh@google.com> | 2016-08-01 18:06:38 -0700 |
commit | 70715da453670b40701ea7d02ea9f702d10d783b (patch) | |
tree | a8e6c37cf0d36012df03ac0d68f395cb2d6e2f0d /libc/stdio | |
parent | a86e11f529aca06b2f4d5141ea838f2bc5ae9a73 (diff) |
More stdio one-liners.
This actually turns up a bug in fmemopen, so I guess that's what I'll
look at next...
Change-Id: I2971ecd1b5a3a3c7f43c22d985f88e389af89e97
Diffstat (limited to 'libc/stdio')
-rw-r--r-- | libc/stdio/local.h | 10 | ||||
-rw-r--r-- | libc/stdio/stdio.cpp | 95 |
2 files changed, 94 insertions, 11 deletions
diff --git a/libc/stdio/local.h b/libc/stdio/local.h index f25e90213..7be5a7cb9 100644 --- a/libc/stdio/local.h +++ b/libc/stdio/local.h @@ -246,19 +246,9 @@ int __vfwscanf(FILE * __restrict, const wchar_t * __restrict, __va_list); #define NO_PRINTF_PERCENT_N /* OpenBSD exposes these in <stdio.h>, but we only want them exposed to the implementation. */ -#define __sfeof(p) (((p)->_flags & __SEOF) != 0) #define __sferror(p) (((p)->_flags & __SERR) != 0) #define __sclearerr(p) ((void)((p)->_flags &= ~(__SERR|__SEOF))) -#if !defined(__cplusplus) #define __sgetc(p) (--(p)->_r < 0 ? __srget(p) : (int)(*(p)->_p++)) -static __inline int __sputc(int _c, FILE* _p) { - if (--_p->_w >= 0 || (_p->_w >= _p->_lbfsize && (char)_c != '\n')) { - return (*_p->_p++ = _c); - } else { - return (__swbuf(_c, _p)); - } -} -#endif /* OpenBSD declares these in fvwrite.h but we want to ensure they're hidden. */ struct __suio; diff --git a/libc/stdio/stdio.cpp b/libc/stdio/stdio.cpp index d218ee6d7..53b3faeb1 100644 --- a/libc/stdio/stdio.cpp +++ b/libc/stdio/stdio.cpp @@ -53,6 +53,13 @@ #define NDYNAMIC 10 /* add ten more whenever necessary */ +#define PRINTF_IMPL(expr) \ + va_list ap; \ + va_start(ap, fmt); \ + int result = (expr); \ + va_end(ap); \ + return result; + #define std(flags, file) \ {0,0,0,flags,file,{0,0},0,__sF+file,__sclose,__sread,nullptr,__swrite, \ {(unsigned char *)(__sFext+file), 0},nullptr,0,{0},{0},{0,0},0,0} @@ -412,7 +419,7 @@ void clearerr(FILE* fp) { } int feof_unlocked(FILE* fp) { - return __sfeof(fp); + return ((fp->_flags & __SEOF) != 0); } int feof(FILE* fp) { @@ -640,6 +647,14 @@ char* ctermid(char* s) { return s ? strcpy(s, _PATH_TTY) : const_cast<char*>(_PATH_TTY); } +int dprintf(int fd, const char* fmt, ...) { + PRINTF_IMPL(vdprintf(fd, fmt, ap)); +} + +int fprintf(FILE* fp, const char* fmt, ...) { + PRINTF_IMPL(vfprintf(fp, fmt, ap)); +} + int fgetc(FILE* fp) { return getc(fp); } @@ -648,6 +663,35 @@ int fputc(int c, FILE* fp) { return putc(c, fp); } +int fscanf(FILE* fp, const char* fmt, ...) { + PRINTF_IMPL(vfscanf(fp, fmt, ap)); +} + +int fwprintf(FILE* fp, const wchar_t* fmt, ...) { + PRINTF_IMPL(vfwprintf(fp, fmt, ap)); +} + +int fwscanf(FILE* fp, const wchar_t* fmt, ...) { + PRINTF_IMPL(vfwscanf(fp, fmt, ap)); +} + +int getc(FILE* fp) { + ScopedFileLock sfl(fp); + return getc_unlocked(fp); +} + +int getc_unlocked(FILE* fp) { + return __sgetc(fp); +} + +int getchar_unlocked() { + return getc_unlocked(stdin); +} + +int getchar() { + return getc(stdin); +} + ssize_t getline(char** buf, size_t* len, FILE* fp) { return getdelim(buf, len, '\n', fp); } @@ -660,6 +704,35 @@ wint_t getwchar() { return fgetwc(stdin); } +int printf(const char* fmt, ...) { + PRINTF_IMPL(vfprintf(stdout, fmt, ap)); +} + +int putc(int c, FILE* fp) { + ScopedFileLock sfl(fp); + return putc_unlocked(c, fp); +} + +int putc_unlocked(int c, FILE* fp) { + if (cantwrite(fp)) { + errno = EBADF; + return EOF; + } + _SET_ORIENTATION(fp, -1); + if (--fp->_w >= 0 || (fp->_w >= fp->_lbfsize && c != '\n')) { + return (*fp->_p++ = c); + } + return (__swbuf(c, fp)); +} + +int putchar(int c) { + return putc(c, stdout); +} + +int putchar_unlocked(int c) { + return putc_unlocked(c, stdout); +} + wint_t putwc(wchar_t wc, FILE* fp) { return fputwc(wc, fp); } @@ -674,6 +747,10 @@ void rewind(FILE* fp) { clearerr_unlocked(fp); } +int scanf(const char* fmt, ...) { + PRINTF_IMPL(vfscanf(stdin, fmt, ap)); +} + void setbuf(FILE* fp, char* buf) { setbuffer(fp, buf, BUFSIZ); } @@ -686,6 +763,14 @@ int setlinebuf(FILE* fp) { return setvbuf(fp, nullptr, _IOLBF, 0); } +int swprintf(wchar_t* s, size_t n, const wchar_t* fmt, ...) { + PRINTF_IMPL(vswprintf(s, n, fmt, ap)); +} + +int swscanf(const wchar_t* s, const wchar_t* fmt, ...) { + PRINTF_IMPL(vswscanf(s, fmt, ap)); +} + int vprintf(const char* fmt, va_list ap) { return vfprintf(stdout, fmt, ap); } @@ -701,3 +786,11 @@ int vwprintf(const wchar_t* fmt, va_list ap) { int vwscanf(const wchar_t* fmt, va_list ap) { return vfwscanf(stdin, fmt, ap); } + +int wprintf(const wchar_t* fmt, ...) { + PRINTF_IMPL(vfwprintf(stdout, fmt, ap)); +} + +int wscanf(const wchar_t* fmt, ...) { + PRINTF_IMPL(vfwscanf(stdin, fmt, ap)); +} |