By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,569 Members | 1,358 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,569 IT Pros & Developers. It's quick & easy.

using execvp/fork to catenate a file

P: 4
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.

Expand|Select|Wrap|Line Numbers
  1. #include <stdio.h>
  2. #include <unistd.h>
  3. #include <sys/types.h>
  4. #include <string.h>
  5.  
  6. int main(int argc, char *argv[])
  7. {
  8.     //VARIABLE DECLARATIONS
  9.  
  10.     char command[50];                //Command line input
  11.  
  12.     //tokens for strtok() funtion
  13.     char * com;                        //Primary command
  14.     char * arg1;                    //First argument of command (usually a file name)
  15.     char * arg2;                    //2nd argument of command (usually a file name)
  16.  
  17.     int args;                        //Number of arguments entered
  18.  
  19.     //FUNCTION DECLARATIONS
  20.  
  21.     void typefile(char * filename);
  22.  
  23.     printf("***************************************************\n");
  24.     printf("Welcome to O-Shell! A UNIX shell by O Haqqi\n\n");
  25.     printf("Type 'help' for list of commands, 'exit' to exit\n");
  26.     printf("***************************************************\n");
  27.  
  28.     for(;;)
  29.     {
  30.  
  31.     printf("o-shell>");                        //user prompts for o-shell
  32.  
  33.     fgets(command, 1024, stdin);        //command line input from user
  34.  
  35.     com = strtok(command, " \r\n\t");
  36.  
  37.     args = 0;
  38.     arg1 = strtok(NULL, " \r\n\t");
  39.     if(arg1) args++;                        //to see if arguments being tracked
  40.     arg2 = strtok(NULL, " \r\n\t");
  41.     if(arg2) args++;                        //to see if arguments being tracked
  42.  
  43.     if(com != NULL)            //Can only execute commands if a command is entered
  44.                             //Otherwise it keeps looping and printing prompt
  45.     {
  46.     printf("command entered: %s\n", com);
  47.     printf("There are %d arguments\n", args);
  48.  
  49.     if ((strcmp(com,"help")) == 0 || (strcmp(com,"Help")) == 0)
  50.     {
  51.         printf("***************************************************\n");
  52.         printf("\nO-Shell Help Menu:\n");
  53.         printf("\nhelp - Print help menu\n");
  54.         printf("\nexit - Exit O-Shell\n");
  55.         printf("\ntype <file> - Print contents of <file> to terminal\n");
  56.         printf("\ncopy <file1> <file2> - Copy contents of <file1> to <file2>.");
  57.         printf("\n     <file2> must be a non-existent file and will be created\n");
  58.         printf("\ndelete <file> - Delete <file> from system\n");
  59.         printf("***************************************************\n");
  60.     }
  61.  
  62.     else if ((strcmp(com,"exit")) == 0 || (strcmp(com,"Exit")) == 0)
  63.     {
  64.         printf("Exiting O-Shell...Goodbye!\n");
  65.  
  66.         //if exit command is entered, exit() system call is performed
  67.         exit(1);
  68.     }
  69.  
  70.     else if ((strcmp(com,"type")) == 0 || (strcmp(com,"Type")) == 0)
  71.     {
  72.         if((arg1 == NULL) || (arg2 != NULL))        //arg1 must exist, arg2 must be empty
  73.         {
  74.             printf("Incorrect number of arguments! Type 'help' for correct format.\n");
  75.         }
  76.  
  77.         else
  78.         printf("Printing contents of file: %s\n", arg1);
  79.         printf("***************************************************\n");
  80.  
  81.         typefile(arg1);            //call typefile function (see below)
  82.     }
  83.  
  84.     else
  85.     printf("Invalid command!!! Type 'help' for list of valid commands.\n");
  86.  
  87.     }
  88.  
  89.     }
  90.  
  91.     return 0;
  92. }
  93.  
  94. /************************************************************************************************
  95. Function typefile() prints contents of a file to the terminal
  96. Format: type <file1>
  97. ************************************************************************************************/
  98.  
  99. void typefile(char * filename)
  100. {
  101.     int pid, status;
  102.  
  103.     const char cat[] = "/bin/cat";
  104.  
  105.     pid = fork();
  106.  
  107.     if(pid < 0)
  108.     printf("ERROR: Child pid = %i FORK FAILED! Please try again.\n", pid);
  109.  
  110.     else if(pid > 0)
  111.     printf("ERROR: Child pid = %i Parent process executing, FORK FAILED!\n", pid);
  112.  
  113.     else if(pid == 0)                //If the process id is zero, fork() was successful
  114.     {
  115.         printf("Child pid = %i FORK SUCCESSFUL...\n");
  116.  
  117.         execvp(cat, filename);
  118.     }
  119.  
  120. }
Jul 2 '07 #1
Share this Question
Share on Google+
1 Reply


P: 4
I finally got the cat command to work..I'm having a problem with the child process creation now

I have testfile.txt to verify that the cat command works. It does work, but the fork() command is assigning a pid other than 0 to the child, or it just looks that way:

o-shell>type testfile.txt
command entered: type
There are 1 arguments
Printing contents of file: testfile.txt
************************************************** *
ERROR: Child pid = 15966 FORK FAILED! Please try again.

Child pid = 0 FORK SUCCESSFUL...

o-shell>This is a test file for the o-shell terminal. Hit <ENTER> to continue.



Expand|Select|Wrap|Line Numbers
  1. void typefile(char * filename)
  2. {
  3.     int status;
  4.     char *arg[] = {"cat", filename, NULL};
  5.  
  6.     pid_t pid = fork();
  7.  
  8.  
  9.     if(pid == 0)            //pid=0 indicates child process created, fork() was successful
  10.     {
  11.         printf("Child pid = %i FORK SUCCESSFUL...\n\n", pid);
  12.  
  13.         status = execvp("cat", arg);
  14.  
  15.         while (wait(&status) != pid)        //parent process waits for child process to complete
  16.         ;
  17.  
  18.     }
  19.  
  20.     else
  21.     printf("ERROR: Child pid = %i FORK FAILED! Please try again.\n\n", pid);
  22.  
  23. }
Does something glare out and say "this is why the non-zero pid is showing up!!" ?
Jul 2 '07 #2

Post your reply

Sign in to post your reply or Sign up for a free account.