Using select() and read();

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Martin Holm Pedersen

    #1

    Using select() and read();

    Hey All..
    Im having a bit of a problem with my program that i wrote for linux in c. I
    use select() to monitor if the user has pressed a key and reads the key
    with read(). It works fine om my IBM laptop but once i move the program to
    my dell laptop it seems like it doesn't even recognize the select-function.
    That is, it doesn't use the timeout assigned at all. I don't get any errors
    when i compile on either computer. I run debian/testing on both laptops and
    i use CVS for version-control.. I have tried to compile it on the IBM and
    run it on the dell.. But nothing seems to work..

    - Martin

    The code is something along the lines of:

    #include <sys/types.h>
    #include <sys/stat.h>
    #include <asm/io.h>
    #include <fcntl.h>
    #include <termios.h>
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <unistd.h>
    #include <limits.h>
    #include <time.h>
    #include <termio.h>
    #include <sys/ioctl.h>

    struct termio OldTerm;
    struct termio NewTerm;
    struct timeval tv1;

    void SetRaw(int);
    void TermRestore(int );

    int main(void) {
    int fd=0, res=0, i=0;
    char keystroke = 0;
    fd_set rfds1;

    SetRaw(fd);
    FD_ZERO(&rfds1) ;
    FD_SET(fileno(s tdin), &rfds1);
    while(1){

    tv1.tv_sec=0;
    tv1.tv_usec=1;

    /* PRINT THE MENU */
    res=select(fd+1 , &rfds1,NULL,NUL L,&tv1);
    if(res){
    read(fd, &keystroke, 1);

    if(keystroke==' 1'){

    }

    if(keystroke==' 2'){

    }

    if(keystroke==' 3'){

    }

    if(keystroke==' 0'){
    printf("så slutter vi!\n");
    TermRestore(fd) ;
    exit(1);
    }
    }
    system("clear") ;
    }

    TermRestore(fd) ;
    return 0;
    }

    void SetRaw(int fd)
    {

    /*
    * Get terminal modes, and saves them in the OldTerm struct.
    */
    (void)ioctl(fd, TCGETA, &OldTerm);


    /*
    * Set the modes to the way we want them.
    */
    NewTerm.c_lflag &= ~(ICANON|ECHO|E CHOE|ECHOK|ECHO NL);
    NewTerm.c_oflag |= (OPOST|ONLCR|TA B3);
    NewTerm.c_oflag &= ~(OCRNL|ONOCR|O NLRET);
    NewTerm.c_cc[VMIN] = 1;
    NewTerm.c_cc[VTIME] = 0;
    (void)ioctl(fd, TCSETAW, &NewTerm);
    printf("fd is %d\n", fd);
    }

    void TermRestore(int fd)
    {
    /*
    * Restore saved modes.
    */
    (void)ioctl(fd, TCSETAW, &OldTerm);
    }



  • Nils Petter Vaskinn

    #2
    Re: Using select() and read();

    On Thu, 13 May 2004 11:40:55 +0200, Martin Holm Pedersen wrote:

    Hi This is probably not a topic for comp.lang.c but for
    comp.unix.progr ammer. Crossposted and followups set.
    [color=blue]
    > Hey All..
    > Im having a bit of a problem with my program that i wrote for linux in c. I
    > use select() to monitor if the user has pressed a key and reads the key
    > with read(). It works fine om my IBM laptop but once i move the program to
    > my dell laptop it seems like it doesn't even recognize the select-function.
    > That is, it doesn't use the timeout assigned at all. I don't get any errors
    > when i compile on either computer. I run debian/testing on both laptops and
    > i use CVS for version-control.. I have tried to compile it on the IBM and
    > run it on the dell.. But nothing seems to work..
    >
    > - Martin
    >
    > The code is something along the lines of:
    >
    > #include <sys/types.h>
    > #include <sys/stat.h>
    > #include <asm/io.h>
    > #include <fcntl.h>
    > #include <termios.h>
    > #include <stdio.h>
    > #include <string.h>
    > #include <stdlib.h>
    > #include <unistd.h>
    > #include <limits.h>
    > #include <time.h>
    > #include <termio.h>
    > #include <sys/ioctl.h>
    >
    > struct termio OldTerm;
    > struct termio NewTerm;
    > struct timeval tv1;
    >
    > void SetRaw(int);
    > void TermRestore(int );
    >
    > int main(void) {
    > int fd=0, res=0, i=0;
    > char keystroke = 0;
    > fd_set rfds1;
    >
    > SetRaw(fd);
    > FD_ZERO(&rfds1) ;
    > FD_SET(fileno(s tdin), &rfds1);
    > while(1){
    >
    > tv1.tv_sec=0;
    > tv1.tv_usec=1;
    >
    > /* PRINT THE MENU */
    > res=select(fd+1 , &rfds1,NULL,NUL L,&tv1);
    > if(res){
    > read(fd, &keystroke, 1);
    >
    > if(keystroke==' 1'){
    >
    > }
    >
    > if(keystroke==' 2'){
    >
    > }
    >
    > if(keystroke==' 3'){
    >
    > }
    >
    > if(keystroke==' 0'){
    > printf("så slutter vi!\n");
    > TermRestore(fd) ;
    > exit(1);
    > }
    > }
    > system("clear") ;
    > }
    >
    > TermRestore(fd) ;
    > return 0;
    > }
    >
    > void SetRaw(int fd)
    > {
    >
    > /*
    > * Get terminal modes, and saves them in the OldTerm struct.
    > */
    > (void)ioctl(fd, TCGETA, &OldTerm);[/color]

    Check return value
    [color=blue]
    >
    >
    > /*
    > * Set the modes to the way we want them.
    > */
    > NewTerm.c_lflag &= ~(ICANON|ECHO|E CHOE|ECHOK|ECHO NL);
    > NewTerm.c_oflag |= (OPOST|ONLCR|TA B3);
    > NewTerm.c_oflag &= ~(OCRNL|ONOCR|O NLRET);
    > NewTerm.c_cc[VMIN] = 1;
    > NewTerm.c_cc[VTIME] = 0;
    > (void)ioctl(fd, TCSETAW, &NewTerm);[/color]

    Check return value
    [color=blue]
    > printf("fd is %d\n", fd);
    > }
    >
    > void TermRestore(int fd)
    > {
    > /*
    > * Restore saved modes.
    > */
    > (void)ioctl(fd, TCSETAW, &OldTerm);[/color]

    Check return value
    [color=blue]
    > }[/color]

    --
    NPV

    "the large print giveth, and the small print taketh away"
    Tom Waits - Step right up

    Comment

    • Floyd L. Davidson

      #3
      Re: Using select() and read();

      Martin Holm Pedersen <mhpe03@kom.auc .dk> wrote:[color=blue]
      >
      >struct termio OldTerm;
      >struct termio NewTerm;[/color]

      ....
      [color=blue]
      >void SetRaw(int fd)
      >{
      >
      > /*
      > * Get terminal modes, and saves them in the OldTerm struct.
      > */
      > (void)ioctl(fd, TCGETA, &OldTerm);[/color]

      You'll want to initalize NewTerm also... :-)
      [color=blue]
      > /*
      > * Set the modes to the way we want them.
      > */
      > NewTerm.c_lflag &= ~(ICANON|ECHO|E CHOE|ECHOK|ECHO NL);
      > NewTerm.c_oflag |= (OPOST|ONLCR|TA B3);
      > NewTerm.c_oflag &= ~(OCRNL|ONOCR|O NLRET);
      > NewTerm.c_cc[VMIN] = 1;
      > NewTerm.c_cc[VTIME] = 0;
      > (void)ioctl(fd, TCSETAW, &NewTerm);[/color]

      It's an accident if it does anything at this point!
      [color=blue]
      > printf("fd is %d\n", fd);
      >}
      >
      >void TermRestore(int fd)
      >{
      > /*
      > * Restore saved modes.
      > */
      > (void)ioctl(fd, TCSETAW, &OldTerm);
      >}[/color]

      --
      Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
      Ukpeagvik (Barrow, Alaska) floyd@barrow.co m

      Comment

      • Martin Holm Pedersen

        #4
        Re: Using select() and read();

        Floyd L. Davidson wrote:
        [color=blue]
        > Martin Holm Pedersen <mhpe03@kom.auc .dk> wrote:[color=green]
        >>
        >>struct termio OldTerm;
        >>struct termio NewTerm;[/color]
        >
        > ...
        >[color=green]
        >>void SetRaw(int fd)
        >>{
        >>
        >> /*
        >> * Get terminal modes, and saves them in the OldTerm struct.
        >> */
        >> (void)ioctl(fd, TCGETA, &OldTerm);[/color]
        >
        > You'll want to initalize NewTerm also... :-)[/color]

        (void)ioctl(fd, TCGETA, &OldTerm);
        is for getting the existing settings and saving them in OldTerm?

        Do i have to initialize NewTerm more than:
        struct termio NewTerm; ??
        [color=blue]
        >[color=green]
        >> /*
        >> * Set the modes to the way we want them.
        >> */
        >> NewTerm.c_lflag &= ~(ICANON|ECHO|E CHOE|ECHOK|ECHO NL);
        >> NewTerm.c_oflag |= (OPOST|ONLCR|TA B3);
        >> NewTerm.c_oflag &= ~(OCRNL|ONOCR|O NLRET);
        >> NewTerm.c_cc[VMIN] = 1;
        >> NewTerm.c_cc[VTIME] = 0;
        >> (void)ioctl(fd, TCSETAW, &NewTerm);[/color]
        >
        > It's an accident if it does anything at this point!
        >[color=green]
        >> printf("fd is %d\n", fd);
        >>}
        >>
        >>void TermRestore(int fd)
        >>{
        >> /*
        >> * Restore saved modes.
        >> */
        >> (void)ioctl(fd, TCSETAW, &OldTerm);
        >>}[/color]
        >
        > --
        > Floyd L. Davidson <http://web.newsguy.com/floyd_davidson>
        > Ukpeagvik (Barrow, Alaska) floyd@barrow.co m[/color]

        Comment

        • Mark A. Odell

          #5
          Re: Using select() and read();

          Martin Holm Pedersen <mhpe03@kom.auc .dk> wrote in
          news:40a36e29$0 $3048$14726298@ news.sunsite.dk :
          [color=blue][color=green][color=darkred]
          >>> /*
          >>> * Get terminal modes, and saves them in the OldTerm struct.
          >>> */
          >>> (void)ioctl(fd, TCGETA, &OldTerm);[/color]
          >>
          >> You'll want to initalize NewTerm also... :-)[/color]
          >
          > (void)ioctl(fd, TCGETA, &OldTerm);
          > is for getting the existing settings and saving them in OldTerm?[/color]

          What's this have to do with the C language? Can't you ask this non-ISO C
          stuff in comp.unix.progr ammer?

          --
          - Mark ->
          --

          Comment

          • those who know me have no need of my name

            #6
            Re: Using select() and read();

            in comp.lang.c i read:
            [color=blue]
            >Im having a bit of a problem with my program that i wrote for linux in c. I
            >use select() to monitor if the user has pressed a key and reads the key
            >with read().[/color]

            these things are off-topic here. try comp.unix.progr ammer.

            --
            a signature

            Comment

            • Martin Holm Pedersen

              #7
              Re: Using select() and read();

              Mark A. Odell wrote:
              [color=blue]
              > Martin Holm Pedersen <mhpe03@kom.auc .dk> wrote in
              > news:40a36e29$0 $3048$14726298@ news.sunsite.dk :
              >[color=green][color=darkred]
              >>>> /*
              >>>> * Get terminal modes, and saves them in the OldTerm struct.
              >>>> */
              >>>> (void)ioctl(fd, TCGETA, &OldTerm);
              >>>
              >>> You'll want to initalize NewTerm also... :-)[/color]
              >>
              >> (void)ioctl(fd, TCGETA, &OldTerm);
              >> is for getting the existing settings and saving them in OldTerm?[/color]
              >
              > What's this have to do with the C language? Can't you ask this non-ISO C
              > stuff in comp.unix.progr ammer?
              >[/color]

              What it has to do with the C-language? It's written in C. I got it
              crosspostet. But then i got a reply here.. Ignoring it would be rude..

              - Martin

              Comment

              • Dan Pop

                #8
                Re: Using select() and read();

                In <40a37add$0$305 0$14726298@news .sunsite.dk> Martin Holm Pedersen <mhpe03@kom.auc .dk> writes:
                [color=blue]
                >Mark A. Odell wrote:
                >[color=green]
                >> Martin Holm Pedersen <mhpe03@kom.auc .dk> wrote in
                >> news:40a36e29$0 $3048$14726298@ news.sunsite.dk :
                >>[color=darkred]
                >>>>> /*
                >>>>> * Get terminal modes, and saves them in the OldTerm struct.
                >>>>> */
                >>>>> (void)ioctl(fd, TCGETA, &OldTerm);
                >>>>
                >>>> You'll want to initalize NewTerm also... :-)
                >>>
                >>> (void)ioctl(fd, TCGETA, &OldTerm);
                >>> is for getting the existing settings and saving them in OldTerm?[/color]
                >>
                >> What's this have to do with the C language? Can't you ask this non-ISO C
                >> stuff in comp.unix.progr ammer?[/color]
                >
                >What it has to do with the C-language? It's written in C.[/color]

                But it uses features beyond the scope of the C language, so it's unfit
                for this newsgroup.
                [color=blue]
                >I got it crosspostet.[/color]

                Very bad idea, as it belongs to only one newsgroup and that newsgroup is
                not comp.lang.c.
                [color=blue]
                >But then i got a reply here.. Ignoring it would be rude..[/color]

                Anything wrong with replying by email?

                Dan
                --
                Dan Pop
                DESY Zeuthen, RZ group
                Email: Dan.Pop@ifh.de

                Comment

                • Alan Balmer

                  #9
                  Re: Using select() and read();

                  On Thu, 13 May 2004 04:09:41 -0800, floyd@barrow.co m (Floyd L.
                  Davidson) wrote:
                  [color=blue]
                  >Martin Holm Pedersen <mhpe03@kom.auc .dk> wrote:[color=green]
                  >>[/color][/color]
                  Please don't answer off-topic questions here. refer the OP to the
                  appropriate newsgroup.

                  --
                  Al Balmer
                  Balmer Consulting
                  removebalmercon sultingthis@att .net

                  Comment

                  • Alan Balmer

                    #10
                    Re: Using select() and read();

                    On Thu, 13 May 2004 15:38:00 +0200, Martin Holm Pedersen
                    <mhpe03@kom.auc .dk> wrote:
                    [color=blue][color=green]
                    >> What's this have to do with the C language? Can't you ask this non-ISO C
                    >> stuff in comp.unix.progr ammer?
                    >>[/color]
                    >
                    >What it has to do with the C-language? It's written in C. I got it
                    >crosspostet. But then i got a reply here.. Ignoring it would be rude..[/color]

                    The *first* reply you got told you it was off-topic and suggested
                    alternates. Continuing the topic after that was rude to everyone here.
                    We would much prefer that you be rude to any idiot who answers your
                    off-topic question here.

                    --
                    Al Balmer
                    Balmer Consulting
                    removebalmercon sultingthis@att .net

                    Comment

                    • Keith Thompson

                      #11
                      Re: Using select() and read();

                      Martin Holm Pedersen <mhpe03@kom.auc .dk> writes:
                      [...][color=blue]
                      > What it has to do with the C-language? It's written in C. I got it
                      > crosspostet. But then i got a reply here.. Ignoring it would be rude..[/color]

                      Most of us who hang out here in comp.lang.c are not experts on the
                      system-specific things you're asking about. Because of that, we're
                      not competent to judge whether any answers you may get here are valid.
                      That's why we try to discourage answers to off-topic questions; we've
                      seen it lead to bad advice. If you post in comp.unix.progr ammer, any
                      answers you get will be checked by experts.

                      It's understandable to assume that, because your program is written in
                      C, it's topical in comp.lang.c, but in fact it isn't, because it uses
                      features that aren't defined by the C standard. (The features that
                      are defined in the C standard are more than enough to keep us busy
                      here.) The best way we can help you is to tell you where to go for
                      help.

                      --
                      Keith Thompson (The_Other_Keit h) kst-u@mib.org <http://www.ghoti.net/~kst>
                      San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
                      Schroedinger does Shakespeare: "To be *and* not to be"

                      Comment

                      Working...