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[])
-
{
-
//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);
-
}
-
-
}
1 8066
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. - void typefile(char * filename)
-
{
-
int status;
-
char *arg[] = {"cat", filename, NULL};
-
-
pid_t pid = fork();
-
-
-
if(pid == 0) //pid=0 indicates child process created, fork() was successful
-
{
-
printf("Child pid = %i FORK SUCCESSFUL...\n\n", pid);
-
-
status = execvp("cat", arg);
-
-
while (wait(&status) != pid) //parent process waits for child process to complete
-
;
-
-
}
-
-
else
-
printf("ERROR: Child pid = %i FORK FAILED! Please try again.\n\n", pid);
-
-
}
Does something glare out and say "this is why the non-zero pid is showing up!!" ?
Sign in to post your reply or Sign up for a free account.
Similar topics
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...
|
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...
|
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();...
|
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
|
by: noelloen |
last post by:
hi,
I have the following code,
int ret;
char ** vector;
//vector = "ls" vector ="-al"
.....
//fork a child
......
|
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...
|
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"...
|
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...
|
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...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: erikbower65 |
last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps:
1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal.
2. Connect to...
|
by: linyimin |
last post by:
Spring Startup Analyzer generates an interactive Spring application startup report that lets you understand what contributes to the application startup time and helps to optimize it. Support for...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: DJRhino1175 |
last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this -
If...
|
by: Rina0 |
last post by:
I am looking for a Python code to find the longest common subsequence of two strings. I found this blog post that describes the length of longest common subsequence problem and provides a solution in...
|
by: DJRhino |
last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer)
If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _
310030356 Or 310030359 Or 310030362 Or...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: Mushico |
last post by:
How to calculate date of retirement from date of birth
| |