/*
 * Copyright (c) 2005, 2011 Kamo Hiroyasu
 *
 * Permission to use, copy, modify, and distribute this software for any
 * purpose with or without fee is hereby granted, provided that the above
 * copyright notice and this permission notice appear in all copies.
 *
 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
 */

/*
 *  call 'pwd' command and then call `ls' command
 *  Author: Kamo Hiroyasu <wd@ics.nara-wu.ac.jp>
 */

#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>

int
main(int argc, char *argv[])
{
	pid_t	pid1, pid2;
	int	status1, status2;

	if ((pid1 = fork()) == 0) {
		execl("/bin/pwd", "pwd", NULL);
		execl("/usr/bin/pwd", "pwd", NULL);
		_exit(1);
	}
	if (pid1 == -1) {
		perror(NULL);
		exit(1);
	}
	waitpid(pid1, &status1, 0);
	if (!WIFEXITED(status1) || WEXITSTATUS(status1) != 0) {
		fprintf(stderr, "pwd failed\n");
		exit(1);
	}

	if ((pid2 = fork()) == 0) {
		execl("/bin/ls", "ls", "-l", NULL);
		execl("/usr/bin/ls", "ls", "-l", NULL);
		_exit(1);
	}
	if (pid2 == -1) {
		perror(NULL);
		exit(1);
	}
	waitpid(pid2, &status2, 0);
	if (!WIFEXITED(status2) || WEXITSTATUS(status2) != 0) {
		fprintf(stderr, "ls failed\n");
		exit(1);
	}
	return 0;
}
