I am reading APUE (Advanced Programming in the UNIX Environment) and I am testing a sample program whose output produced on OpenBSD differs from the output shown in the book.
Here is the program:
Code:
#include "apue.h"
#define BSZ 48
int
main(void)
{
FILE *fp;
char buf[BSZ];
memset(buf, 'a', BSZ-2);
buf[BSZ-2] = '\0';
buf[BSZ-1] = 'X';
if ((fp = fmemopen(buf, BSZ, "w+")) == NULL)
err_sys("fmemopen failed");
printf("initial buffer contents: %s\n", buf);
fprintf(fp, "hello, world");
printf("before flush: %s\n", buf);
fflush(fp);
printf("after fflush: %s\n", buf);
printf("len of string in buf = %ld\n", (long)strlen(buf));
memset(buf, 'b', BSZ-2);
buf[BSZ-2] = '\0';
buf[BSZ-1] = 'X';
fprintf(fp, "hello, world");
fseek(fp, 0, SEEK_SET);
printf("after fseek: %s\n", buf);
printf("len of string in buf = %ld\n", (long)strlen(buf));
memset(buf, 'c', BSZ-2);
buf[BSZ-2] = '\0';
buf[BSZ-1] = 'X';
fprintf(fp, "hello, world");
fclose(fp);
printf("after fclose: %s\n", buf);
printf("len of string in buf = %ld\n", (long)strlen(buf));
return(0);
}
This is what it prints in OpenBSD:
Code:
initial buffer contents:
before flush:
after fflush: hello, worldaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
len of string in buf = 46
after fseek: bbbbbbbbbbbbhello, worldbbbbbbbbbbbbbbbbbbbbbb
len of string in buf = 46
after fclose: hello, worldcccccccccccccccccccccccccccccccccc
len of string in buf = 46
This is what the book shows:
Code:
initial buffer contents:
before flush:
after fflush: hello, world
len of string in buf = 12
after fseek: bbbbbbbbbbbbhello, world
len of string in buf = 24
after fclose: hello, worldcccccccccccccccccccccccccccccccccc
len of string in buf = 46
Is there something different in OpenBSD implementation of memory streams?
It was supposed to write a `'\0'` (nul character) on the two first writings after `"hello, world"`, but this nul character is not written. And only the nul character I explicitly wrote with the statement `buf[BSZ-2] = '\0';` is considered by the printf(3)s.
But the nul character that should be written at opening the memory stream with fmemopen(3) is correctly written at the beginning of the buffer, as you can see in the line `before flush:`.