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

Discussion in 'C Programming' started by E.U., Nov 1, 2004.

  1. E.U.

    E.U. Guest


    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?

    E.U., Nov 1, 2004
    1. Advertisements

  2. E.U.

    dandelion Guest

    Lots. Grep is a very usefull utility.
    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].
    dandelion, Nov 1, 2004
    1. Advertisements

  3. strstr() will be useful, I suspect.
    As well as what?
    (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>
    Christopher Benson-Manica, Nov 1, 2004
  4. E.U.

    CBFalconer Guest

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

    p3 word <story.txt

    My just released id2id-20 is an example of this.
    CBFalconer, Nov 1, 2004
    1. Advertisements

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 (here). After that, you can post your question and our members will help you out.