using fork/execvp to catenate a file

Discussion in 'C' started by ohaqqi, Jul 2, 2007.

  1. ohaqqi

    ohaqqi New Member

    Joined:
    Jun 30, 2007
    Messages:
    6
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    hardware engineer
    Location:
    maryland, USA
    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.

    Code:
    #include <stdio.h>
    #include <unistd.h>
    #include <sys/types.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
    	//VARIABLE DECLARATIONS
    
    	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
    
    	//FUNCTION DECLARATIONS
    
    	void typefile(char * filename);
    
    	printf("***************************************************\n");
    	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("***************************************************\n");
    
    	for(;;)
    	{
    
    	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("***************************************************\n");
    		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");
    		printf("***************************************************\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
    		exit(1);
    	}
    
    	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");
    		}
    
    		else
    		printf("Printing contents of file: %s\n", arg1);
    		printf("***************************************************\n");
    
    		typefile(arg1);			//call typefile function (see below)
    	}
    
    	else
    	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);
    	}
    
    }
     
  2. DaWei

    DaWei New Member

    Joined:
    Dec 6, 2006
    Messages:
    835
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Semi-retired EE
    Location:
    Texan now in Central NY
    Home Page:
    http://www.daweidesigns.com
    Do you see yourself conforming to that?
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice