Stdio buffer flushing

Leslie Mikesell les at chinet.chi.il.us
Fri Jul 6 08:00:09 AEST 1990


Does the putc macro have any strange side effects (such as flushing
an unfilled buffer) under SysV when setbuf is used?  I'm seeing
occasional lines in a logfile that should be:
    line1
    line2
       become:
    line1line2
    blank_line
The relevant code is:  (from smail 3.1, incidentally...)
static char logbuf[BUFSIZ]; /* stdio buffer to avoid mallocs */
fd = open(log_fn, O_CREAT|O_APPEND|O_WRONLY, log_mode);
logfile = fdopen(fd, "a");
(void) setbuf(logfile, logbuf);	/* associate stream and buffer */
	...
(void)vfprintf(logfile, fmt, ap);
putc('\n', logfile);	/* log messages don't come with \n */
(void)fflush(logfile);

Apparently the putc is performing a flush *before* adding the \n, so
that a concurrent process can write it's entry first.  Is this a
bug or are you not supposed to mix different forms of buffered output?

Les Mikesell
  les at chinet.chi.il.us



More information about the Comp.unix.questions mailing list