help needed with C grep program. About pipes and ls in shell

E

E.U.

Hello,

I need to program grep (like the one UNIX has but more simple)
For example if the programs name(the grep I will write) is p3 then if
I write in shell
p3 story.txt word

The output should be all lines where the word "word" appears.

The problem is that I am suppose to use this command in shell as well
ls | p3 word

The output should be the lines that the word "word" appears in with in
all files of the directory.

My question is what can I do with that? I mean ls is not my command
its a shell command. What kind of paramters do I get in order to
understand that I need all the files and how do I find all the files
in the directory. The first case isn't that clear either.

Any tutorial or something (like a *.h) files that can help?

Thanks
 
D

dandelion

E.U. said:
Hello,

I need to program grep (like the one UNIX has but more simple)

My question is what can I do with that?

Lots. Grep is a very usefull utility.
I mean ls is not my command its a shell command.

Ah... And the question is "How do I get at the command-line arguments" ?

Try this. It may give you a hint or two...

#include <stdlib.h>
#include <stdio.h>

int main(int argc, char* argv[], char* envp[])
{
printf("number of arguments = %d\n", argc);

for(i=0; i<argc, i++)
{
printf("argv[ %d] = %s\n", i, argv);
}

return EXIT_SUCCESS;
}

when compiled (into, for instance, foobar) type

"foobar this is an argument, too" and hit [RETURN].
 
C

Christopher Benson-Manica

E.U. said:
The output should be all lines where the word "word" appears.

strstr() will be useful, I suspect.
The problem is that I am suppose to use this command in shell as well
ls | p3 word

As well as what?
My question is what can I do with that? I mean ls is not my command
its a shell command. What kind of paramters do I get in order to
understand that I need all the files and how do I find all the files
in the directory. The first case isn't that clear either.

(caveats may apply)

<ot>The command you listed pipes the output of ls to the standard
input of p3. You can simply read lines from standard input and pass
them to fopen() and proceed from there.</ot>
 
C

CBFalconer

E.U. said:
I need to program grep (like the one UNIX has but more simple)
For example if the programs name(the grep I will write) is p3
then if I write in shell
p3 story.txt word

The output should be all lines where the word "word" appears.

The problem is that I am suppose to use this command in shell
as well ls | p3 word

The output should be the lines that the word "word" appears in
with in all files of the directory.

So write it as a filter. You run it with:

p3 word <story.txt

My just released id2id-20 is an example of this.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top