#include <sys/types.h>
#include <assert.h>
#include <unistd.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>
#include <stdlib.h>

/*
 * Run this program as:
 *            ./exec <progname>
 *
 * progname is the name of the program that the child process will exec.
 */

int
main(int argc, char **argv)
{
	pid_t ret;

	assert(argc == 2);

	ret = fork();

	if(ret == 0)
	{
		printf("[%d] Hello, this is the child\n", getpid());
		printf("[%d] I will now call execlp to run: %s\n",
		       getpid(), argv[1]);
		if(execlp(argv[1], argv[1], 0))
		{
			perror("execlp");
			exit(EXIT_FAILURE);
		}
	}
	else if(ret > 0)
	{
		printf("[%d] Hello, this is the parent, child's pid is %d\n",
		       getpid(), ret);
	}
	else
	{
		fprintf(stderr, "fork(): %s\n", strerror(errno));
	}

	printf("[%d] Only the parent will print this. The child would have "
	       "switched to a different process.\n",
	       getpid());
}
