we were given an assignment that would take in two commands from the terminal and pipe the first one into the second. I believe I have the structure of the code correct, however, when trying to compile it complains that my initial char *cmd is not a valid statement. Why is this happening? I'm confused, any assistance would be appreciated.
Code:
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
void main(int argc, char *argv[])
{
int f_des[2];
if(pipe(f_des) == -1)
{
perror("pipe");
exit(1);
}
switch(fork())
{
case -1: perror("fork");
exit(2);
case 0: dup2(f_des[1], fileno(stdout));
close(f_des[0]);
close(f_des[1]);
char *cmd[] = {"/usr/bin/"+argv[1],argv[1], (char *)0};
char *path[] = {"HOME=/usr/home/", "LOGNAME=home", (char *)0};
int ret;
ret = execvp(cmd,path);
exit(3);
case default: dup2(f_des[0], fileno(stdin));
close(f_des[0]);
close(f_des[1]);
char *cmd[] = {"/usr/bin/"+argv[2], argv[2], (char *)0};
char *path[] = {"HOME=/usr/home/", "LOGNAME=home", (char *)0};
int ret;
ret = execvp(cmd,path);
exit(4);
}
}
Error:
cc: Error: lab1.c, line 22: Invalid statement.
char *cmd[] = {"/usr/bin/"+argv[1],argv[1], (char *)0};
^
cc: Error: lab1.c, line 22: Missing ";".
char *cmd[] = {"/usr/bin/"+argv[1],argv[1], (char *)0};
-----------------------------------------------------^
cc: Error: lab1.c, line 23: Invalid statement.
char *path[] = {"HOME=/usr/home/", "LOGNAME=home", (char *)0};
^
cc: Error: lab1.c, line 23: Missing ";".
char *path[] = {"HOME=/usr/home/", "LOGNAME=home", (char *)0};
------------------------------------------------------------^
cc: Error: lab1.c, line 24: Invalid statement.
int ret;
^
cc: Error: lab1.c, line 27: Invalid expression.
case default: dup2(f_des[0], fileno(stdin));
-----^
cc: Error: lab1.c, line 30: Invalid statement.
char *cmd[] = {"/usr/bin/"+argv[2], argv[2], (char *)0};
^
cc: Error: lab1.c, line 30: Missing ";".
char *cmd[] = {"/usr/bin/"+argv[2], argv[2], (char *)0};
------------------------------------------------------^
cc: Error: lab1.c, line 31: Invalid statement.
char *path[] = {"HOME=/usr/home/", "LOGNAME=home", (char *)0};
^
cc: Error: lab1.c, line 31: Missing ";".
char *path[] = {"HOME=/usr/home/", "LOGNAME=home", (char *)0};
------------------------------------------------------------^
cc: Error: lab1.c, line 32: Invalid statement.
int ret;
^
cc: Error: lab1.c, line 25: In this statement, "ret" is not declared.
ret = execvp(cmd,path);
^
cc: Error: lab1.c, line 25: In this statement, "cmd" is not declared.
ret = execvp(cmd,path);
-------------^
cc: Error: lab1.c, line 25: In this statement, "path" is not declared.
ret = execvp(cmd,path);
-----------------^
+like"/usr/bin/"+argv[1]. Don't make up syntax just because you can do that in other languages.default:, notcase default:.{, if necessary adding the{and matching}to create a block in which you can declare the variables. For example:case 0: { char *cmd[] = { argv[1], argv[1], (char *)0 }; ... break; }or thereabouts.cc -Vorcc -vorcc -versionorcc --version; one of those might produce a version number.uname -acontains lots of information too.