int main(void) { if (isatty(0)) { fprintf(stderr,"Hit Ctrl-d to initialise stdin\n"); } else { fprintf(stderr,"Initialising stdin\n"); } char data[4096]; fread(data,sizeof(data),1,stdin); if (isatty(1)) { fprintf(stdout,"Initialising stdout\n"); } else { fprintf(stdout,"Initialising stdout\n"); fprintf(stderr,"Initialising stdout\n"); } fprintf(stderr,"Initialising stderr\n"); //redundant int i; for (i=0; i<3; i++) { fprintf(stderr,"%6s: tty=%d, lb=%d, size=%d\n", fileno2name(i), isatty(i), __flbf(fileno2FILE(i))?1:0, __fbufsize(fileno2FILE(i))); } return EXIT_SUCCESS; }
TEST(stdio_ext, __fbufsize) { FILE* fp = fopen("/proc/version", "r"); // Initially, there's no buffer in case the first thing you do is disable buffering. ASSERT_EQ(0U, __fbufsize(fp)); // A read forces a buffer to be created. char buf[128]; fgets(buf, sizeof(buf), fp); ASSERT_EQ(1024U, __fbufsize(fp)); ASSERT_EQ(0, setvbuf(fp, buf, _IOFBF, 1)); ASSERT_EQ(1U, __fbufsize(fp)); ASSERT_EQ(0, setvbuf(fp, buf, _IOFBF, 8)); ASSERT_EQ(8U, __fbufsize(fp)); fclose(fp); }
void testValues() { f = 2; FILE* file = VALID_FILE; if (file != NULL) { __fbufsize(file); } //@ assert f == 2; //@ assert vacuous: \false; }
/* LWP_WaitForKeystroke(Unix) :Wait until a key has been struck or time (secconds) * runs out and return to caller. The Unix version will actually wait until * a <cr> has been entered before returning. * Input: * seconds: wait for <seconds> seconds before returning. If seconds < 0, * wait infinitely. * Return Value: * 1: Keyboard input available * 0: seconds elapsed. Timeout. */ int LWP_WaitForKeystroke(int seconds) { fd_set rdfds; int code; struct timeval twait; struct timeval *tp = NULL; #if defined(HAVE_STDIO_EXT_H) if (__fbufsize(stdin) > 0) return 1; #elif defined(AFS_LINUX20_ENV) if (stdin->_IO_read_ptr < stdin->_IO_read_end) return 1; #elif (defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV)) && defined(AFS_DFBSD_ENV) struct appx_sbuf { unsigned char *_base; int _size; }; struct APPX_FILE { struct __FILE_public pub; struct appx_sbuf _bf; /* the buffer (at least 1 byte, if !NULL) */ }; struct APPX_FILE *appx_stdin = (struct APPX_FILE *) stdin; if (appx_stdin->_bf._size > 0) return 1; #elif defined(AFS_DARWIN_ENV) || defined(AFS_XBSD_ENV) if (stdin->_bf._size > 0) return 1; #else if (stdin->_cnt > 0) return 1; #endif FD_ZERO(&rdfds); FD_SET(fileno(stdin), &rdfds); if (seconds >= 0) { twait.tv_sec = seconds; twait.tv_usec = 0; tp = &twait; } #ifdef AFS_PTHREAD_ENV code = select(1 + fileno(stdin), &rdfds, NULL, NULL, tp); #else code = IOMGR_Select(1 + fileno(stdin), &rdfds, NULL, NULL, tp); #endif return (code == 1) ? 1 : 0; }
void runSuccess() { FILE* file = VALID_FILE; if (file != NULL) { __fbufsize(file); } }
void runFailure() { __fbufsize(NULL); }
static gboolean gst_file_sink_open_file (GstFileSink * sink) { gint mode; /* open the file */ if (sink->filename == NULL || sink->filename[0] == '\0') goto no_filename; if (sink->append) sink->file = gst_fopen (sink->filename, "ab"); else sink->file = gst_fopen (sink->filename, "wb"); if (sink->file == NULL) goto open_failed; /* see if we are asked to perform a specific kind of buffering */ if ((mode = sink->buffer_mode) != -1) { guint buffer_size; /* free previous buffer if any */ g_free (sink->buffer); if (mode == _IONBF) { /* no buffering */ sink->buffer = NULL; buffer_size = 0; } else { /* allocate buffer */ sink->buffer = g_malloc (sink->buffer_size); buffer_size = sink->buffer_size; } /* Cygwin does not have __fbufsize */ #if defined(HAVE_STDIO_EXT_H) && !defined(__CYGWIN__) GST_DEBUG_OBJECT (sink, "change buffer size %u to %u, mode %d", (guint) __fbufsize (sink->file), buffer_size, mode); #else GST_DEBUG_OBJECT (sink, "change buffer size to %u, mode %d", sink->buffer_size, mode); #endif if (setvbuf (sink->file, sink->buffer, mode, buffer_size) != 0) { GST_WARNING_OBJECT (sink, "warning: setvbuf failed: %s", g_strerror (errno)); } } sink->current_pos = 0; /* try to seek in the file to figure out if it is seekable */ sink->seekable = gst_file_sink_do_seek (sink, 0); GST_DEBUG_OBJECT (sink, "opened file %s, seekable %d", sink->filename, sink->seekable); return TRUE; /* ERRORS */ no_filename: { GST_ELEMENT_ERROR (sink, RESOURCE, NOT_FOUND, (_("No file name specified for writing.")), (NULL)); return FALSE; } open_failed: { GST_ELEMENT_ERROR (sink, RESOURCE, OPEN_WRITE, (_("Could not open file \"%s\" for writing."), sink->filename), GST_ERROR_SYSTEM); return FALSE; } }
int fbufmode (FILE *fp) { /* Most systems provide FILE as a struct and the necessary bitmask in <stdio.h>, because they need it for implementing getc() and putc() as fast macros. */ #if defined _IO_ftrylockfile || __GNU_LIBRARY__ == 1 /* GNU libc, BeOS, Haiku, Linux libc5 */ # if HAVE___FLBF /* glibc >= 2.2 */ if (__flbf (fp)) return _IOLBF; # else if (fp->_flags & _IO_LINE_BUF) return _IOLBF; # endif if (fp->_flags & _IO_UNBUFFERED) return _IONBF; return _IOFBF; #elif defined __sferror || defined __DragonFly__ || defined __ANDROID__ /* FreeBSD, NetBSD, OpenBSD, DragonFly, Mac OS X, Cygwin, Android */ if (fp_->_flags & __SLBF) return _IOLBF; if (fp_->_flags & __SNBF) return _IONBF; return _IOFBF; #elif defined __EMX__ /* emx+gcc */ return fp->_flags & (_IOLBF | _IONBF | _IOFBF); #elif defined __minix /* Minix */ return fp->_flags & (_IOLBF | _IONBF | _IOFBF); #elif defined _IOERR /* AIX, HP-UX, IRIX, OSF/1, Solaris, OpenServer, mingw, NonStop Kernel */ # if HAVE___FLBF /* Solaris >= 7 */ if (__flbf (fp)) return _IOLBF; # else if (fp->_flag & _IOLBF) return _IOLBF; # endif if (fp_->_flag & _IONBF) return _IONBF; return _IOFBF; #elif defined __UCLIBC__ /* uClibc */ if (fp->__modeflags & __FLAG_LBF) return _IOLBF; if (fp->__modeflags & __FLAG_NBF) return _IONBF; return _IOFBF; #elif defined __QNX__ /* QNX */ if (fp->_Mode & 0x400 /* _MLBF */) return _IOLBF; if (fp->_Mode & 0x800 /* _MNBF */) return _IONBF; return _IOFBF; #elif defined __MINT__ /* Atari FreeMiNT */ if (fp->__linebuf) return _IOLBF; return (fp->__bufsize > 0 ? _IOFBF : _IONBF); #elif HAVE___FLBF && HAVE___FBUFSIZE /* musl libc */ if (__flbf (fp)) return _IOLBF; return (__fbufsize (fp) > 0 ? _IOFBF : _IONBF); #elif defined EPLAN9 /* Plan9 */ if (fp->flags & 2 /* LINEBUF */) return _IOLBF; if (fp->bufl) return _IOFBF; return _IONBF; #else # error "Please port gnulib fbufmode.c to your platform! Look at the setvbuf implementation." #endif }