Non blocking write long (note 74)

Recommended for you: Get network issues from WhatsUp Gold. Not end users.
#include "apue.h"
#include <errno.h>
#include <fcntl.h>
#include <syslog.h>

char	buf[500];
int		log_to_stderr;

void
set_fl(int fd, int flags)	/* flags are file status to turn on */
{
	int		val;

	if ((val = fcntl(fd, F_GETFL, 0)) < 0)
	{
		printf("fcntl F_GETFL error");
		exit(1);
	}

	val |= flags;	/* turn on flags */

	if (fcntl(fd, F_SETFL, val) < 0)
	{
		printf("fcntl F_SETFL error");
		exit(1);
	}
}

void
clr_fl(int fd, int flags)
{
	int		val;

	if ((val = fcntl(fd, F_GETFL, 0)) == -1)
	{
		syslog(LOG_ERR, __FILE__, __LINE__, "fcntl() error: %s", strerror(errno));
		exit(1);
	}

	val &= ~flags;	/* turn flags off */

	if (fcntl(fd, F_SETFL, val) == -1)
	{
		syslog(LOG_ERR, __FILE__, __LINE__, "fcntl() error: %s", strerror(errno));
		exit(1);
	}
	return;
}

int
main(void)
{
	int		ntowrite, nwrite;
	char	*ptr;
	
	set_fl(STDIN_FILENO, O_NONBLOCK);	/* set nonblocking */

	ntowrite = read(STDIN_FILENO, buf, sizeof(buf));
	fprintf(stderr, "read %d bytes\n", ntowrite);

	set_fl(STDOUT_FILENO, O_NONBLOCK);	/* set nonblocking */

	ptr = buf;
	while (ntowrite > 0) {
		errno = 0;
		nwrite = write(STDOUT_FILENO, ptr, ntowrite);
		fprintf(stderr, "nwrite = %d, errno = %d\n", nwrite, errno);

		if (nwrite > 0) {
			ptr += nwrite;
			ntowrite -= nwrite;
		}
	}
	clr_fl(STDOUT_FILENO, O_NONBLOCK);	/* clear nonblocking */
	clr_fl(STDIN_FILENO, O_NONBLOCK);	/* clear nonblocking */
	exit(0);
}
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download

Posted by Carol at December 28, 2013 - 7:15 PM