main()
{
	int fd, pid;
	char *login, *getlogin();

	/*
	 * 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 fork.
	 */
	while ((pid = fork()) < 0) {
		printf("Cannot fork \- retrying...\en");
		sleep(5);
	}

	if (pid == 0) { 	/* we're in the fork */
		/*
		 * Set the file to be standard input.
		 */
		dup2(fd, 0);

		/*
		 * Execute mail.
		 */
		execlp("mail", "mail", login, 0);

		/*
		 * If the exec fails, we get this line.
		 */
		printf("Cannot execute mail.\en");

		/*
		 * Always put an exit in the child, so it
		 * doesn't run on forever if the exec fails.
		 */
		exit(0);
	}

	/*
	 * The parent waits for the fork to finish.
	 */
	while (wait( (int *) 0) != pid);
}
