using fork/execvp to catenate a file

ohaqqi's Avatar
Light Poster
Hi guys,
I'm still working on my shell. I'm trying to implement a function typefile that will take a command line input as follows:

> type <file1>

This command will implement a catenation of file1, equal to the command cat <file1>

I need to use execvp() and fork() system calls to create a new process that will type/cat any text file. In my code below, the execvp() call in the function at the bottom gets me the following:

o-shell.c:128: warning: passing arg 2 of `execvp' from incompatible pointer type

I know for sure i'm calling execvp incorrectly, any tips/pointers would be great. I want to use /bin/cat followed by filename as the argument.

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

int main(int argc, char *argv[])

	char command[50];				//Command line input

	//tokens for strtok() funtion
	char * com;						//Primary command
	char * arg1;					//First argument of command (usually a file name)
	char * arg2;					//2nd argument of command (usually a file name)

	int args;						//Number of arguments entered


	void typefile(char * filename);

	printf("Welcome to O-Shell! A UNIX shell by O Haqqi\n\n");
	printf("Type 'help' for list of commands, 'exit' to exit\n");


	printf("o-shell>");						//user prompts for o-shell

	fgets(command, 1024, stdin);		//command line input from user

	com = strtok(command, " \r\n\t");

	args = 0;
	arg1 = strtok(NULL, " \r\n\t");
	if(arg1) args++;						//to see if arguments being tracked
	arg2 = strtok(NULL, " \r\n\t");
	if(arg2) args++;						//to see if arguments being tracked

	if(com != NULL)			//Can only execute commands if a command is entered
							//Otherwise it keeps looping and printing prompt
	printf("command entered: %s\n", com);
	printf("There are %d arguments\n", args);

	if ((strcmp(com,"help")) == 0 || (strcmp(com,"Help")) == 0)
		printf("\nO-Shell Help Menu:\n");
		printf("\nhelp - Print help menu\n");
		printf("\nexit - Exit O-Shell\n");
		printf("\ntype <file> - Print contents of <file> to terminal\n");
		printf("\ncopy <file1> <file2> - Copy contents of <file1> to <file2>.");
		printf("\n     <file2> must be a non-existent file and will be created\n");
		printf("\ndelete <file> - Delete <file> from system\n");

	else if ((strcmp(com,"exit")) == 0 || (strcmp(com,"Exit")) == 0)
		printf("Exiting O-Shell...Goodbye!\n");

		//if exit command is entered, exit() system call is performed

	else if ((strcmp(com,"type")) == 0 || (strcmp(com,"Type")) == 0)
		if((arg1 == NULL) || (arg2 != NULL))		//arg1 must exist, arg2 must be empty
			printf("Incorrect number of arguments! Type 'help' for correct format.\n");

		printf("Printing contents of file: %s\n", arg1);

		typefile(arg1);			//call typefile function (see below)

	printf("Invalid command!!! Type 'help' for list of valid commands.\n");



	return 0;

Function typefile() prints contents of a file to the terminal
Format: type <file1>

void typefile(char * filename)
	int pid, status;

	const char cat[] = "/bin/cat";

	pid = fork();

	if(pid < 0)
	printf("ERROR: Child pid = %i FORK FAILED! Please try again.\n", pid);

	else if(pid > 0)
	printf("ERROR: Child pid = %i Parent process executing, FORK FAILED!\n", pid);

	else if(pid == 0)				//If the process id is zero, fork() was successful
		printf("Child pid = %i FORK SUCCESSFUL...\n");

		execvp(cat, filename);

DaWei's Avatar, Join Date: Dec 2006
Team Leader
Originally Posted by man page
The prototype: int execvp( const char *file, char *const argv[]);

The exect, execv, and execvp functions provide an array of
pointers to null-terminated strings that represent the
argument list available to the new program.

The first argument, by convention, should point to the file name
associated with the file being executed. The array of
pointers must be terminated by a NULL pointer.
Do you see yourself conforming to that?