main()
{
	int fd, pfd[2], i;
	char *login, *getlogin();
	char buf[1024];

	/*
	 * Get user's login name.
	 */
	login = getlogin();

	/*
	 * Open the file.  This should be done before
	 * the fork.
	 */
	if ((fd = open("/etc/motd", 0)) < 0) {
		printf("Cannot open /etc/motd\en");
		exit(1);
	}

	/*
	 * Get a pipe.
	 */
	pipe(pfd);

	/*
	 * Get a fork.
	 */
	while ((pid = fork()) < 0) {
		printf("Cannot fork \- retrying...\en");
		sleep(5);
	}

	if (pid == 0) {		/* we're in the fork */
		/*
		 * Close standard input, set standard input 
		 * to the reading-end of the pipe, close
		 * the pipe descriptor.
		 */
		close(0);
		dup(pfd[0]);
		close(pfd[1]);
		close(pfd[0]);

		execlp("mail", "mail", login, 0);

		printf("Can't execute mail\en");

		/*
		 * Always put an exit in the child, in case
		 * the exec fails.
		 */
		exit(0);
	}

	/*
	 * Now instead of waiting on the fork, we 
	 * read from the file and write down the pipe to mail.
	 */
	while ((i = read(fd, buf, sizeof(buf)) > 0) {
		write(pfd[1], buf, i);
	}
}
