sort:multi-character tab in C error - c

I have error multi-character tab ` \' \' -k 10 -r -n log.txt'
int
main ()
{
const char *sort[] = { "sort", "-t ' ' -k 10 -r -n log.txt", 0 };
const char *awk[] = { "awk", "{print $10}", 0 };
struct command cmd [] = { {sort}, {awk} };
return fork_pipes (2, cmd);
}
How backspace escape or correct sort argument in C.
All code:
#include <unistd.h>
struct command
{
const char **argv;
};
int
spawn_proc (int in, int out, struct command *cmd)
{
pid_t pid;
if ((pid = fork ()) == 0)
{
if (in != 0)
{
dup2 (in, 0);
close (in);
}
if (out != 1)
{
dup2 (out, 1);
close (out);
}
return execvp (cmd->argv [0], (char * const *)cmd->argv);
}
return pid;
}
int
fork_pipes (int n, struct command *cmd)
{
int i;
pid_t pid;
int in, fd [2];
/* The first process should get its input from the original file descriptor 0. */
in = 0;
/* Note the loop bound, we spawn here all, but the last stage of the pipeline. */
for (i = 0; i < n - 1; ++i)
{
pipe (fd);
/* f [1] is the write end of the pipe, we carry `in` from the prev iteration. */
spawn_proc (in, fd [1], cmd + i);
/* No need for the write and of the pipe, the child will write here. */
close (fd [1]);
/* Keep the read end of the pipe, the next child will read from there. */
in = fd [0];
}
/* Last stage of the pipeline - set stdin be the read end of the previous pipe
and output to the original file descriptor 1. */
if (in != 0)
dup2 (in, 0);
/* Execute the last stage with the current process. */
return execvp (cmd [i].argv [0], (char * const *)cmd [i].argv);
}
int
main ()
{
const char *sort[] = { "sort", "-t ' ' -k 10 -r -n log.txt", 0 };
const char *awk[] = { "awk", "{print $10}", 0 };
struct command cmd [] = { {sort}, {awk} };
return fork_pipes (2, cmd);
}

I assume that the strings are passed as arguments to one of the exec functions? Then you can't put multiple arguments in a single string because exec will treat it as a single argument to the program.
The arguments to exec is what becomes the argv array in the called program, so for example "-t ' ' -k 10 -r -n log.txt" will become a single argument (e.g. argv[1]) in the called program. You need to separate the arguments:
const char *sort[] = { "sort", "-t", " ", "-k", "10", "-r", "-n", "log.txt", 0 };

Related

command hangs on read end of pipe

I've doing custom shell for an assignment, and I wanted to implement the pseudo code from emphemient here for multiple piping. However, my code is still hanging on the read end of the pipe (in my test command, I do echo hello | wc and it's hanging on wc).
Is my implementation incorrect based on the pseudocode? I think I close the pipes properly
void piped()
{
// test command
char args[] = "echo hello | wc";
int status;
pid_t pid;
int cmdsRun = 0;
int newfds[2];
int oldfds[2];
char *nextCmd;
char *prevCmd = NULL;
char *cmdToken;
char *cmdSavePter;
cmdToken = strtok_r(args, "|", &cmdSavePter);
while (cmdToken)
{
nextCmd = strtok_r(NULL, "|", &cmdSavePter);
fprintf(stderr, "cmdToken: %s, nextCmd: %s, prev: %s\n", cmdToken, nextCmd, prevCmd);
struct command *commands = parseW(cmdToken);
// if next command make new pipes
if (nextCmd)
pipe(newfds);
// fork
pid = fork();
if (pid == 0) // child
{
// if there was prev cmd
if (prevCmd)
{
dup2(oldfds[0], STDIN_FILENO);
close(oldfds[0]);
close(oldfds[1]);
}
// if next cmd
if (nextCmd)
{
close(newfds[0]);
dup2(newfds[1], STDOUT_FILENO);
close(newfds[1]);
}
// this function simply execvps the commands
runSimple(commands);
}
else if (pid < 0)
{
perror("error");
exit(EXIT_FAILURE);
}
else // parent
{
// if there was a prev command
if (prevCmd)
{
close(oldfds[0]);
close(oldfds[1]);
}
// if next command
if (nextCmd)
{
memcpy(oldfds, newfds, sizeof(oldfds));
}
}
waitpid(pid, &status, 0);
prevCmd = cmdToken;
cmdToken = nextCmd;
cmdsRun++;
}
// parent: if multiple cmds, close
if (cmdsRun > 1)
{
close(oldfds[0]);
close(oldfds[1]);
}
}

Redirection at the end of the pipe (C shell)

I'm trying to make ls | tr a b > text.txt
I have piping done, but I can't add STDOUT to the end of the pipe (STDOUT in my case can be only in the last argument)
I mark the part of the code, in which redirection should be done, I think that file should be opened, and dup2 method used, but I don't know in which way
Methods contains piping -
enum reqType { PIPE, STDOUT };
int spawn_proc (int in, int out, char** cmd) {
pid_t pid;
if ((pid = fork ()) == 0) {
if (in != 0) {
dup2 (in, 0);
close (in);
}
if (out != 1) {
dup2 (out, 1);
close (out);
}
return execvp (cmd[0], cmd);
}
return pid;
}
void fork_pipes (int n, char** cmd[], enum reqType type) {
int i;
pid_t pid;
int in, fd [2];
in = 0;
for (i = 0; i < n - 1; ++i) {
if(type == PIPE || i < n-2) {
pipe (fd);
spawn_proc (in, fd [1], cmd[i]);
close (fd [1]);
in = fd [0];
}
else if(type == STDOUT && i == n-2) {
///HOW TO IMPLEMENT THIS PART?
}
}
if (in != 0)
dup2 (in, 0);
execvp (cmd[i][0], cmd[i]);
}
EDIT
in the marked by /// place I wrote
pipe(fd);
int out = open(cmd[n-1][0],O_WRONLY|O_CREAT|O_TRUNC);
spawn_proc(in, out, cmd[i]);
close(fd[1]);
I think that file should be opened, and dup2 method used, but I don't know in which way
You are right about the mechanisms for implementing the redirection. It should be done on the process intended for tr, and before performing the overlay.
Let's go step by step:
ls | tr a b > text.txt
First create a pipe, then fork().
From now on, there are two processes running in parallel, both of them will be eventually overlaid by means of exec(): one with the ls program, the other with the tr program.
Process for ls:
Close the reading end of the pipe: this process will only write to the pipe.
dup2() the writing end of the pipe to STDOUT: what this process writes to STDOUT is being written to the pipe.
Perform the overlay: exec() with ls.
Process for tr:
Close the writing end of the pipe: this process will only read from the pipe.
dup2() the reading end of the pipe to STDIN: what this process reads from STDIN is coming from the pipe.
In order to perform the redirection to the text.txt file, first open() the file text.txt for writing and with the flags O_CREAT and O_TRUNC, then dup2() the obtained file descriptor to STDOUT.
Perform the overlay: exec() with tr.
Note that, if the command were appending to text.txt instead of truncating it (i.e.: using >> instead of >):
ls | tr a b >> text.txt
You would have to use the flag O_APPEND instead of O_TRUNC when open()ing the text.txt file.
Code Snippet
I've modified your code (also the interface of fork_pipes()). It's a minimal example that runs, I hope it helps.
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
int spawn_proc (int in, int out, char** cmd) {
pid_t pid;
if ((pid = fork ()) == 0) {
if (in != 0) {
dup2 (in, 0);
close (in);
}
if (out != 1) {
dup2 (out, 1);
close (out);
}
return execvp (cmd[0], cmd);
}
return pid;
}
void fork_pipes (char** cmd[], const char *redirection) {
int i, n;
int in, out, fd[2];
in = 0;
// obtain n from the NULL terminated cmd array
for (n = 0; cmd[n]; ++n)
;
// process all but the last elemet of the pipe
for (i = 0; i < n-1; ++i) {
pipe(fd);
spawn_proc(in, fd[1], cmd[i]);
close(fd [1]);
in = fd [0];
}
// process the last element of the pipe
if (redirection) {
out = open(redirection, O_WRONLY | O_CREAT | O_TRUNC);
fchmod(out, 0666);
} else
out = STDOUT_FILENO;
if (in != 0)
dup2(in, 0);
spawn_proc(in, out, cmd[i]);
}
int main()
{
char *cmd1[] = {"ls", NULL};
char *cmd2[] = {"tr", "a", "b", NULL};
char **cmd[] = { cmd1, cmd2, NULL};
// redirected to text.txt
fork_pipes(cmd, "text.txt");
// no redirection
fork_pipes(cmd, NULL);
// another example with a longer pipe
{
char *cmd1[] = {"echo", "hello world", NULL};
char *cmd2[] = {"tee", NULL};
char *cmd3[] = {"tee", NULL};
char *cmd4[] = {"tr", "lo", "10", NULL};
char **cmd[] = {cmd1, cmd2, cmd3, cmd4, NULL};
// redirected to redirection.txt
fork_pipes(cmd, "redirection.txt");
// no redirected
fork_pipes(cmd, NULL);
}
return 0;
}
As already pointed out in this comment. You just need to call pipe() once in your example: The pipe() system call only needs to be called once for each pipe operator (i.e.: the | character) found in the compound command. For example, in the following command:
cmd1 | cmd2 | cmd3 | cmd4
pipe() must be called exactly four times, since there are four pipe operators.

How to handle errors in execvp?

I've written a small program (with code from SO) that facilitates printenv | sort | less. Now I want to implement error-handling and I start with execvp. Is it just to check the return value and what more? AFAIK I just check the return value if it was 0 in this function return execvp (cmd [i].argv [0], (char * const *)cmd [i].argv);. Is that correct?
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
struct command
{
const char **argv;
};
/* Helper function that spawns processes */
int spawn_proc (int in, int out, struct command *cmd) {
pid_t pid;
if ((pid = fork ()) == 0) {
if (in != 0) {
dup2 (in, 0);
close (in);
}
if (out != 1) {
dup2 (out, 1);
close (out);
}
return execvp (cmd->argv [0], (char * const *)cmd->argv);
}
return pid;
}
/* Helper function that forks pipes */
int fork_pipes (int n, struct command *cmd) {
int i;
int in, fd [2];
for (i = 0; i < n - 1; ++i) {
pipe (fd);
spawn_proc (in, fd [1], cmd + i);
close (fd [1]);
in = fd [0];
}
dup2 (in, 0);
return execvp (cmd [i].argv [0], (char * const *)cmd [i].argv);
}
int main (int argc, char ** argv) {
int i;
if (argc == 1) { /* There were no arguments */
const char *printenv[] = { "printenv", 0};
const char *sort[] = { "sort", 0 };
const char *less[] = { "less", 0 };
struct command cmd [] = { {printenv}, {sort}, {less} };
return fork_pipes (3, cmd);
}
if (argc > 1) { /* I'd like an argument */
if (strncmp(argv[1], "cd", 2) && strncmp(argv[1], "exit", 2)) {
char *tmp;
int len = 1;
for( i=1; i<argc; i++)
{
len += strlen(argv[i]) + 2;
}
tmp = (char*) malloc(len);
tmp[0] = '\0';
int pos = 0;
for( i=1; i<argc; i++)
{
pos += sprintf(tmp+pos, "%s%s", (i==1?"":"|"), argv[i]);
}
const char *printenv[] = { "printenv", 0};
const char *grep[] = { "grep", "-E", tmp, NULL};
const char *sort[] = { "sort", 0 };
const char *less[] = { "less", 0 };
struct command cmd [] = { {printenv}, {grep}, {sort}, {less} };
return fork_pipes (4, cmd);
free(tmp);
} else if (! strncmp(argv[1], "cd", 2)) { /* change directory */
printf("change directory to %s\n" , argv[2]);
chdir(argv[2]);
} else if (! strncmp(argv[1], "exit", 2)) { /* change directory */
printf("exit\n");
exit(0);
}
}
exit(0);
}
Look at the manual : execvp change your process's program image to another. So if there is a return value at all, it will be obviously -1, because it will have obviously failed.
As for any syscall or functions calling syscall, execvp with set errno for an appropriate error code, that you can use with perror to know why your function failed. (By the way, you should do the same to any syscall: fork, pipe and dup2 at least.)
Then, if by "error handling", you mean handle errors from the program that you launched, you can make it with wait or waitpid. It will allow you to know how the program ended, it return value, if it ended by a signal, if yes wich one, etc.

Executing a command-line pipeline in C with fork() and exec()

I asked about my code and the answer was that it is incorrect.
perror usage in this case
Now I wonder how I can adjust and improve so that I no longer will have these errors?
execvp does not return, except if an error occurs, so if all works,
the enclosing function will never return.
the value 'i' is already past the end of the array in 'cmd' due to the
prior loop, so 'cmd[i].argv[0] is not correct.
cmd is not an array, of struct command, so should not be indexed
the first entry in cmd.argv is a pointer to an array where the last
entry is NULL. the execvp will work on that (and only that) array so
all other pointers to arrays will be ignored
There are a large number of errors in the code. for instance, the
first time through the loop in fork_pipe() 'in' contains garbage. the
second parameter passed to execvp() needs to be a pointer to character
strings, with a final NULL pointer. That final NULL pointer is
missing, There are plenty more problems
#include <sys/types.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
struct command
{
const char **argv;
};
/* Helper function that spawns processes */
int spawn_proc (int in, int out, struct command *cmd) {
pid_t pid;
if ((pid = fork ()) == 0) {
if (in != 0) {
/*if (dup2(in, 0) == -1) {
perror("dup2 failed");
exit(1);
}*/
dup2 (in, 0);
close (in);
}
if (out != 1) {
dup2 (out, 1);
close (out);
}
if (execvp(cmd->argv [0], (char * const *)cmd->argv) < 0) {
perror("execvp failed");
exit(1);
}
} else if (pid < 0) {
perror("fork failed");
exit(1);
}
return pid;
}
/* Helper function that forks pipes */
int fork_pipes (int n, struct command *cmd) {
int i;
int in, fd [2];
for (i = 0; i < n - 1; ++i) {
pipe (fd);
spawn_proc (in, fd [1], cmd + i);
close (fd [1]);
in = fd [0];
}
dup2 (in, 0);
/*return execvp (cmd [i].argv [0], (char * const *)cmd [i].argv);*/
if (execvp (cmd [i].argv [0], (char * const *)cmd [i].argv) < 0) {
perror("execvp failed");
exit(1);
} else {
return execvp (cmd [i].argv [0], (char * const *)cmd [i].argv);
}
}
int main (int argc, char ** argv) {
int i;
if (argc == 1) { /* There were no arguments */
const char *printenv[] = { "printenv", 0};
const char *sort[] = { "sort", 0 };
const char *less[] = { "less", 0 };
struct command cmd [] = { {printenv}, {sort}, {less} };
return fork_pipes (3, cmd);
}
if (argc > 1) { /* I'd like an argument */
if (strncmp(argv[1], "cd", 2) && strncmp(argv[1], "exit", 2)) {
char *tmp;
int len = 1;
for( i=1; i<argc; i++)
{
len += strlen(argv[i]) + 2;
}
tmp = (char*) malloc(len);
tmp[0] = '\0';
int pos = 0;
for( i=1; i<argc; i++)
{
pos += sprintf(tmp+pos, "%s%s", (i==1?"":"|"), argv[i]);
}
const char *printenv[] = { "printenv", 0};
const char *grep[] = { "grep", "-E", tmp, NULL};
const char *sort[] = { "sort", 0 };
const char *less[] = { "less", 0 };
struct command cmd [] = { {printenv}, {grep}, {sort}, {less} };
return fork_pipes (4, cmd);
free(tmp);
} else if (! strncmp(argv[1], "cd", 2)) { /* change directory */
printf("change directory to %s\n" , argv[2]);
chdir(argv[2]);
} else if (! strncmp(argv[1], "exit", 2)) { /* change directory */
printf("exit\n");
exit(0);
}
}
exit(0);
}
Transferring comments into (part of) an answer.
You don't need the test on execvp() (if it returns, it failed), but you do need the error reporting and exit call after it.
The 'cmd is not an array' comment seems to be bogus; inside fork_pipes(), it is an array. It is not used as an array inside spawn_proc().
I think the comment 'The first entry in cmd.argv is a pointer to an array where the last entry is NULL. The execvp will work on that (and only that) array so all other pointers to arrays will be ignored' is bogus too. I think they overlooked that you're creating an array of struct command's.
I think the comment 'the value i is already past the end of the array in cmd due to the prior loop, so cmd[i].argv[0] is not correct' is incorrect because the loop is for (i = 0; i < n - 1; i++) so i is n-1 after the loop, and the array cmd has elements 0..n-1 to address.
However, the value of in in the first call to spawn_proc() is indeed garbage. Probably you can simply set it to 0 (STDIN_FILENO) and be OK, but you need to verify that. But the comment about the second argument to execvp() is peculiar — the cast should be absent, but otherwise the code looks OK to me. I should add that I've not yet run a compiler over any of this, so anything I've said so far stands to be corrected by a compiler. But I'm not doing the analysis casually either… Are you compiling with your compiler set fussy: gcc -Wall -Wextra -Werror as a minimum (I use more options!)?
In fork_pipes(), the if test on execvp() and the else clause are weird. You just need calls to execvp(), perror() and exit().
These comments above stand basically accurate. Here is some modified code, but the modifications are mostly cosmetic. The err_syserr() function is based on what I use for reporting errors. It is a varargs function that also reports the system error. It is better than perror() because (a) it can format more comprehensively and (b) it exits.
I was getting compilation warnings like:
ft13.c: In function ‘spawn_proc’:
ft13.c:45:9: error: passing argument 2 of ‘execvp’ from incompatible pointer type [-Werror]
execvp(cmd->argv[0], cmd->argv);
^
In file included from ft13.c:6:0:
/usr/include/unistd.h:440:6: note: expected ‘char * const*’ but argument is of type ‘const char **’
int execvp(const char *, char * const *);
The easiest way to fix these is to place the const in the correct place in struct command and to remove the const from the argument lists for the various commands.
Other changes are more cosmetic than really substantive (the uninitialized in was the only serious bug to fix). I've use my error reporting code, and checked some extra system calls (the dup2() ones, for example), and cleaned up the execvp() and error reporting. I moved the tests for exit and cd ahead of the general code to avoid repeating the tests. Also, you were using strncmp() and the test for exit was only looking at ex, but ex is a system command… Use strcmp(). I use strcmp(x, y) == 0 in the condition; the relational operator in use mimics the relational operation I'm testing (so strcmp(x, y) >= 0 tests for x greater than or equal to y, etc.).
Modern POSIX does not require #include <sys/types.h> as an include. The other headers include it as necessary.
Source: ft13.c
Compiled to ft13.
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
struct command
{
char * const *argv;
};
static _Noreturn void err_syserr(char *fmt, ...)
{
int errnum = errno;
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
if (errnum != 0)
fprintf(stderr, "(%d: %s)\n", errnum, strerror(errnum));
exit(EXIT_FAILURE);
}
/* Helper function that spawns processes */
static int spawn_proc(int in, int out, struct command *cmd)
{
pid_t pid;
if ((pid = fork()) == 0)
{
if (in != 0)
{
if (dup2(in, 0) < 0)
err_syserr("dup2() failed on stdin for %s: ", cmd->argv[0]);
close(in);
}
if (out != 1)
{
if (dup2(out, 1) < 0)
err_syserr("dup2() failed on stdout for %s: ", cmd->argv[0]);
close(out);
}
fprintf(stderr, "%d: executing %s\n", (int)getpid(), cmd->argv[0]);
execvp(cmd->argv[0], cmd->argv);
err_syserr("failed to execute %s: ", cmd->argv[0]);
}
else if (pid < 0)
err_syserr("fork failed: ");
return pid;
}
/* Helper function that forks pipes */
static void fork_pipes(int n, struct command *cmd)
{
int i;
int in = 0;
int fd[2];
for (i = 0; i < n - 1; ++i)
{
pipe(fd);
spawn_proc(in, fd[1], cmd + i);
close(fd[1]);
in = fd[0];
}
if (dup2(in, 0) < 0)
err_syserr("dup2() failed on stdin for %s: ", cmd[i].argv[0]);
fprintf(stderr, "%d: executing %s\n", (int)getpid(), cmd[i].argv[0]);
execvp(cmd[i].argv[0], cmd[i].argv);
err_syserr("failed to execute %s: ", cmd[i].argv[0]);
}
int main(int argc, char **argv)
{
int i;
if (argc == 1) /* There were no arguments */
{
char *printenv[] = { "printenv", 0};
char *sort[] = { "sort", 0 };
char *less[] = { "less", 0 };
struct command cmd[] = { {printenv}, {sort}, {less} };
fork_pipes(3, cmd);
}
else
{
if (strcmp(argv[1], "cd") == 0) /* change directory */
{
printf("change directory to %s\n", argv[2]);
chdir(argv[2]);
}
else if (strcmp(argv[1], "exit") == 0)
{
printf("exit\n");
exit(0);
}
else
{
char *tmp;
int len = 1;
for (i = 1; i < argc; i++)
{
len += strlen(argv[i]) + 2;
}
tmp = (char *) malloc(len);
tmp[0] = '\0';
int pos = 0;
for (i = 1; i < argc; i++)
{
pos += sprintf(tmp + pos, "%s%s", (i == 1 ? "" : "|"), argv[i]);
}
char *printenv[] = { "printenv", 0};
char *grep[] = { "grep", "-E", tmp, NULL};
char *sort[] = { "sort", 0 };
char *less[] = { "less", 0 };
struct command cmd[] = { {printenv}, {grep}, {sort}, {less} };
fork_pipes(4, cmd);
free(tmp);
}
}
return(0);
}
Example runs
Sample 1:
$ ./ft13 | cat
1733: executing less
1735: executing printenv
1736: executing sort
Apple_PubSub_Socket_Render=/private/tmp/com.apple.launchd.sl7NmyZPgI/Render
BASH_ENV=/Users/jleffler/.bashrc
CDPATH=:/Users/jleffler:/Users/jleffler/src:/Users/jleffler/src/perl:/Users/jleffler/src/sqltools:/Users/jleffler/lib:/Users/jleffler/doc:/Users/jleffler/work:/Users/jleffler/ids
CLICOLOR=1
…lots of environment omitted…
VISUAL=vim
XPC_FLAGS=0x0
XPC_SERVICE_NAME=0
_=./ft13
__CF_USER_TEXT_ENCODING=0x1F7:0x0:0x0
$
Sample 2:
$ ./ft13 PATH | cat
1739: executing printenv
1737: executing less
1740: executing grep
1741: executing sort
CDPATH=:/Users/jleffler:/Users/jleffler/src:/Users/jleffler/src/perl:/Users/jleffler/src/sqltools:/Users/jleffler/lib:/Users/jleffler/doc:/Users/jleffler/work:/Users/jleffler/ids
DYLD_LIBRARY_PATH=/usr/lib:/usr/informix/11.70.FC6/lib:/usr/informix/11.70.FC6/lib/esql:/usr/informix/11.70.FC6/lib/cli
GOPATH=/Users/jleffler/Software/go-1.2
LD_LIBRARY_PATH=/usr/lib:/usr/gnu/lib:/usr/gcc/v4.9.1/lib
MANPATH=/Users/jleffler/man:/Users/jleffler/share/man:/usr/local/mysql/man:/usr/gcc/v4.9.1/share/man:/Users/jleffler/perl/v5.20.1/man:/usr/local/man:/usr/local/share/man:/opt/local/man:/opt/local/share/man:/usr/share/man:/usr/gnu/man:/usr/gnu/share/man
PATH=/Users/jleffler/bin:/usr/informix/11.70.FC6/bin:.:/usr/local/mysql/bin:/usr/gcc/v4.9.1/bin:/Users/jleffler/perl/v5.20.1/bin:/usr/local/go/bin:/Users/jleffler/Software/go-1.2/bin:/usr/local/bin:/opt/local/bin:/usr/bin:/bin:/usr/gnu/bin:/usr/sbin:/sbin
$

Why is extended grep not working?

When I try extended grep like this it does not work.
const char *grep[] = { "grep", "-E", "'JOBS|COMPIZ'" };
If I do it just for one string without single quotes then it works. Why? Why can't I build up arguments to extended grep like above? The following with just one string is working.
const char *grep[] = { "grep", "-E", "JOBS" };
My program should do printenv | sort | grep <parameter-list> | less and if no arguments to main then the program should do printenv | sort | less. I already achieved the latter functionality and now I need to achieve the grep of parameter list but I can't seem to do extended grep from within C code.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
struct command
{
const char **argv;
};
int
spawn_proc (int in, int out, struct command *cmd)
{
pid_t pid;
if ((pid = fork ()) == 0)
{
if (in != 0)
{
dup2 (in, 0);
close (in);
}
if (out != 1)
{
dup2 (out, 1);
close (out);
}
return execvp (cmd->argv [0], (char * const *)cmd->argv);
}
return pid;
}
int
fork_pipes (int n, struct command *cmd)
{
int i;
pid_t pid;
int in, fd [2];
/* The first process should get its input from the original file descriptor 0. */
in = 0;
/* Note the loop bound, we spawn here all, but the last stage of the pipeline. */
for (i = 0; i < n - 1; ++i)
{
pipe (fd);
/* f [1] is the write end of the pipe, we carry `in` from the prev iteration. */
spawn_proc (in, fd [1], cmd + i);
/* No need for the write and of the pipe, the child will write here. */
close (fd [1]);
/* Keep the read end of the pipe, the next child will read from there. */
in = fd [0];
}
/* Last stage of the pipeline - set stdin be the read end of the previous pipe
and output to the original file descriptor 1. */
if (in != 0)
dup2 (in, 0);
/* Execute the last stage with the current process. */
return execvp (cmd [i].argv [0], (char * const *)cmd [i].argv);
}
int
main (int argc, char ** argv)
{
printf("in main...");
int i;
if (argc == 1) {
const char *printenv[] = { "printenv", 0};
const char *sort[] = { "sort", 0 };
const char *less[] = { "less", 0 };
struct command cmd [] = { {printenv}, {sort}, {less} };
return fork_pipes (3, cmd);
}
if (argc > 1) {
/*char *tmp = argv[1];
sprintf(tmp, "%s%s", "'", tmp);*/
for( i=1; i<argc-1; i++)
{
/* tmp = "%s%s%s", tmp, "\\|", argv[i];
printf("tmp:%s", tmp);
sprintf(tmp, "%s%s%s", tmp, "|", argv[i]);
sprintf(tmp, "%s%s", tmp, "'");*/
}
const char *printenv[] = { "printenv", 0};
const char *grep[] = { "grep", "-E", "JOB" };
const char *sort[] = { "sort", 0 };
const char *less[] = { "less", 0 };
struct command cmd [] = { {printenv}, {grep}, {sort}, {less} };
return fork_pipes (4, cmd);
}
}
Don't use single quotes in the argument. They are needed only on the command line to prevent shell from interpreting the vertical bar.

Resources