write system call violates POSIX standard

Nicolas Bourdaud nicolas.bourdaud at gmail.com
Wed Feb 15 14:36:16 UTC 2012

[resent since the signature messed my previous message on the mailing
list archive]

Hi all,

When a write() cannot transfer as many bytes as requested (because of a
file limit), it fails instead of transferring as many bytes as there is
room to write.

This is a violation of the POSIX standard:

I have provided a small test to verify the problem (fsize-lim.c).

I have also created a bug report but I have been advised to post on this
mailing list anyway:

Best regards


test file: fsize-lim.c:

#include <unistd.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>
#include <string.h>

#define TARGETSIZE	80000
#define LIMSIZE		60000
#define PATTSIZE	27

int main(void)
	struct rlimit lim;
	int fd;
	ssize_t retc;
	size_t count = 0;
	const char pattern[PATTSIZE] = "Hello world!";
	signal(SIGXFSZ, SIG_IGN);
	getrlimit(RLIMIT_FSIZE, &lim);
	lim.rlim_cur = LIMSIZE;
	setrlimit(RLIMIT_FSIZE, &lim);

	fd = open("result.txt", O_WRONLY|O_CREAT|O_TRUNC, S_IRUSR|S_IWUSR);

	while (count < TARGETSIZE) {
		retc = write(fd, pattern, PATTSIZE);

		if (retc < PATTSIZE && retc > 0)
			        "added %zi bytes instead of %u bytes after %zu bytes\n",
				retc, PATTSIZE, count);
		else if (retc < 0) {
			        "failed when adding %u bytes after %zu bytes (error: %s)\n",
				PATTSIZE, count, strerror(errno));
		count += retc;


	return 0;

More information about the freebsd-standards mailing list