473,890 Members | 1,948 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

using execvp/fork to catenate a file

4 New Member
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
1 8275
ohaqqi
4 New Member
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

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

Similar topics

7
2569
by: mikeSpindler | last post by:
I've seen it addressed in searching, but not specifically what is recommended to get around it. On Linux where I did my development all was fine. But then going to Solaris for the target I found I couldn't resolve the errors wrt execvp(). It's apparently not syntax, but I could be wrong. Any other thoughts on how to get execvp() working and/or how to cat a file in a new window without bringing tkinter into the discussion? My...
2
2242
by: George | last post by:
Hi, Does anyone have any experience with GnuPGInterface? I'm having a problem with decrypting files through a cron job. The below job works great when I run it manually from the command line, but it blows up whenever I try to run it through cron, and I can't really figure out why. I've been trying to learn python, and I'm at the point where I can get things working in small scripts (you know, just enough to be dangerous). If anybody could...
27
6007
by: steve | last post by:
I was given the following code, and asked what the possible outputs could be. We're learning about processes and forking. int value; int main(){ int pid, number = 1; value = 2; pid = fork(); value = value + 2; number = number + 2;
0
3348
by: mohitp | last post by:
HI I am trying to compile WSDLs on a unix box, it generates the .obj file, but when creating .lib it throws this error execvp: ar: Arg list too long Any pointers to this error? Thanks Mohit
2
13189
by: noelloen | last post by:
hi, I have the following code, int ret; char ** vector; //vector = "ls" vector ="-al" ..... //fork a child ......
3
2056
by: Thomas Guettler | last post by:
Hi, I noticed, that sys.stout does not get flushed before the process is replaced. The last print statements (before execvp()) disappear. It only happens, if the output is redirected to a file (if sys.stdout is not line buffered). """#testexec.py import os
0
6708
by: maheshnew2007 | last post by:
Hi, I have simple Ant's build.xml file, which contains fork="yes". build.xml file: <?xml version="1.0"?> <project name="test" default="compile" basedir="."> <property name="src" value="."/>
3
5691
by: CMorgan | last post by:
Hi everybody, I am experiencing an annoying problem with fork() and execv(). In my program I need to launch the "pppd" from a thread, so, I create a new process with fork and then in the child process I launch with execv the pppd to connect an embedded system to internet. The problem is that pppd fails. I have made two test program just to discard bugs (my applications is really big) and the result is that when I call execv from a...
2
6864
by: thanhnh | last post by:
Hi. I have to write a simple Shell with History feature. First, I get a string from command line. The command line is split into tokens. And then, I call fork ( ) to create a new process. The child process will call execvp(args, args) in order to execute the string command line. When user press < Ctrl + C > ( signal SIGINT ) , I have to read a file and print out 10 last commands. My problem is: when I embed small code to catch < Ctrl + C >...
0
11215
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10801
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10904
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10449
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9616
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8007
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5834
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
6034
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3266
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.