piping/reading stdin quirks.

Discussion in 'C Programming' started by chad kline, Oct 10, 2003.

  1. chad kline

    chad kline Guest

    FBSD 4.8/GCC

    ////////
    C-CODE:
    ////////

    char c;

    while ( 1 ) {

    read(0, &c, 1); printf("%02x ", c);
    close(0); open("/dev/stdin", O_RDWR, S_IRUSR|S_IWUSR);
    }

    //////////
    Question:
    //////////

    this is confusing to me. 'open' doesn't reopen 'stdin'
    ('errno' keeps getting set, and no new characters are
    ever read - after the 1st one).

    i have a function that opens, reads, and closes a file.
    i would like to use this function to read a file piped
    from 'stdin' as well as from disk. the problem is i
    can't re-open 'stdin' after the file reading function
    closes '/dev/stdin'.

    is there any way to "reset" /dev/stdin so that
    it functions normally after being close(2)'d?
     
    chad kline, Oct 10, 2003
    #1
    1. Advertising

  2. On 10 Oct 2003 13:06:23 -0700, (chad kline) wrote:

    >FBSD 4.8/GCC
    >
    >////////
    >C-CODE:
    >////////
    >
    >char c;
    >
    >while ( 1 ) {
    >
    > read(0, &c, 1); printf("%02x ", c);
    > close(0); open("/dev/stdin", O_RDWR, S_IRUSR|S_IWUSR);
    >}
    >

    Unfortunately, it is not standard c. Neither open nor close are
    defined in the standard library.


    <<Remove the del for email>>
     
    Barry Schwarz, Oct 11, 2003
    #2
    1. Advertising

  3. chad kline

    chad kline Guest

    (chad kline) wrote in message news:<>...
    > FBSD 4.8/GCC


    actually, i have a better example of what i am trying to do.
    i have searched "everywhere" for the solution, and have seen
    the question asked in many places, but without any clear answers.

    #include <errno.h>
    #include <stdio.h>
    #include <unistd.h>
    ////////////////////////////////////////////////////
    int main(int argc, char* argv[]) {

    char c, fd2, err;

    fd2 = dup(0);
    while ((err = read(0, &c, 1))) { // read pipe.
    printf("R: %i:%i:%02x\n", errno, err, c);
    } errno=0;
    err = dup2(fd2, 0);
    printf("2: %i:%i:%02x\n", errno, err, c);
    errno=0;
    while ((err = read(0, &c, 1))) { // read keyb.
    printf("R: %i:%i:%02x\n", errno, err, c);
    }

    exit(0);}
    ////////////////////////////////////////////////////

    this doesn't work - what needs to be done to make it work?
    if i avoid all 'dup's and just continue to read stdin after
    reading the pipe, the 2nd read just keeps reading the last
    char of the piped file (generating errno's).

    i want to make stdin read the keyboard after reading the pipe.
    i don't want to use 'streams' (fopen, freopen, etc).
     
    chad kline, Oct 11, 2003
    #3
  4. chad kline

    Jack Klein Guest

    On 10 Oct 2003 19:34:35 -0700, (chad kline) wrote
    in comp.lang.c:

    > (chad kline) wrote in message news:<>...
    > > FBSD 4.8/GCC

    >
    > actually, i have a better example of what i am trying to do.
    > i have searched "everywhere" for the solution, and have seen
    > the question asked in many places, but without any clear answers.
    >
    > #include <errno.h>
    > #include <stdio.h>
    > #include <unistd.h>

    ^^^^^^^^^^^^^^^^^^^^^

    Stop right there, this header is NOT part of C. The minute you
    include it, you're off-topic here. Ask in a group that supports your
    particular compiler/OS combination.

    --
    Jack Klein
    Home: http://JK-Technology.Com
    FAQs for
    comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
    comp.lang.c++ http://www.parashift.com/c -faq-lite/
    alt.comp.lang.learn.c-c++ ftp://snurse-l.org/pub/acllc-c /faq
     
    Jack Klein, Oct 11, 2003
    #4
  5. chad kline

    chad kline Guest

    (chad kline) wrote in message news:<>...
    > FBSD 4.8/GCC


    this defines much better the problem
    i am trying to solve. it would be
    better if the '/dev/tty' stuff
    wasn't necessary.

    #include <errno.h>
    #include <stdio.h>
    #include <fcntl.h>
    #include <unistd.h>
    /////////////////////////////////////////
    int main(int argc, char* argv[]) {

    char c; int n, fd;

    while ((n = read(0, &c, 1))) // read pipe.
    printf("PIPE: %02x:%i\n", c, n);

    fd=open("/dev/tty", O_RDWR, 0600);
    printf("OPEN: %i:%i\n", errno, fd);

    dup2(fd,0); close(fd);

    while ((n = read(0, &c, 1)) < 1 ); // wait keyb.
    printf("KEYB: %02x:%i\n", c, n);

    exit(0);}
    /////////////////////////////////////////
     
    chad kline, Oct 11, 2003
    #5
  6. chad kline

    chad kline Guest

    (chad kline) wrote in message news:<>...
    > FBSD 4.8/GCC

    i apologize for the several posts,
    but since it takes 10 hours or so
    to post, i was able to figure some
    things out, and think it's good
    knowledge to have in the DB. it
    seems to be a question without
    many good answers (not that the
    one i found is "good", not sure).

    #include <errno.h>
    #include <stdio.h>
    #include <fcntl.h>
    #include <unistd.h>
    ///////////////////////////////////////////////////
    int main(int argc, char* argv[]) {

    char c; int n;

    while ((n = read(0, &c, 1))) // read pipe.
    printf("PIPE: %02x:%i\n", c, n);

    dup2(1,0);

    while ((n = read(0, &c, 1)) < 1 ); // wait keyb.
    printf("KEYB: %02x:%i\n", c, n);

    exit(0);}
    ///////////////////////////////////////////////////
     
    chad kline, Oct 11, 2003
    #6
  7. (chad kline) wrote:

    > (chad kline) wrote in message news:<>...
    >> FBSD 4.8/GCC

    >i apologize for the several posts,
    >but since it takes 10 hours or so
    >to post, i was able to figure some
    >things out, and think it's good
    >knowledge to have in the DB. it
    >seems to be a question without
    >many good answers (not that the
    >one i found is "good", not sure).
    >
    >#include <errno.h>
    >#include <stdio.h>
    >#include <fcntl.h>
    >#include <unistd.h>


    But still it's hopelessly off-topic in comp.lang.c, as you are dealing
    with non-standard extensions to the C language.

    To echo Jack Klein:
    Ask in a group that supports your particular compiler/OS combination.

    <SNIP>

    Regards
    --
    Irrwahn
    ()
     
    Irrwahn Grausewitz, Oct 11, 2003
    #7
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. DaLoverhino

    Some quirks in InputStream.read() ?

    DaLoverhino, Jan 23, 2005, in forum: Java
    Replies:
    6
    Views:
    532
    Rogan Dawes
    Jan 24, 2005
  2. kayodeok
    Replies:
    20
    Views:
    853
    Toby A Inkster
    Nov 1, 2003
  3. Charlie Zender

    Reading stdin once confuses second stdin read

    Charlie Zender, Jun 19, 2004, in forum: C Programming
    Replies:
    6
    Views:
    815
    Dan Pop
    Jun 21, 2004
  4. Replies:
    9
    Views:
    113
    Dodger
    Jul 7, 2006
  5. Stefano Sabatini
    Replies:
    6
    Views:
    311
    Stefano Sabatini
    Jul 29, 2007
Loading...

Share This Page