Thread: buffered vs unbuffered question

  1. #1
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329

    buffered vs unbuffered question

    I just don't spend enough time "tinkering" with *nix. Anyhow when I
    read from an unbuffered input and type something in, I get two prompts
    after I hit return.

    m-net% more tty.c
    Code:
    #include <stdio.h> 
    
    
    int main(void) 
    { 
     char c; 
    
    
     return(read(0, &c, 1) == 1) ? (unsigned char)c : EOF; 
    
    
     return 0; 
    
    
    }
    m-net% ./tty
    c
    m-net%
    m-net%

    Now when I buffer the input, and type something in, I only get one
    prompt after I hit return.


    m-net% more tty2.c
    Code:
    #include <stdio.h> 
    
    
    int main(void) 
    { 
     static char buf[BUFSIZ]; 
     static char *bufp = buf; 
     static int n = 0; 
    
    
     if(n == 0) { 
      n = read(0,buf, sizeof buf); 
      bufp = buf; 
     } 
     return (--n >= 0) ? (unsigned char) *bufp++ : EOF; 
    
    
    }
    m-net% ./tty2
    c h a d
    m-net%

    Why is this?

  2. #2
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Just a guess - perhaps the raw read() version is not "consuming" the <return>, so the shell does.

    gg

  3. #3
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    That might be.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    With buffered input, the run-time library will have consumed the return, even if you didn't.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    Code:
    m-net% gdb tty2
    GNU gdb 6.1.1 [FreeBSD]
    Copyright 2004 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB.  Type "show warranty" for details.
    This GDB was configured as "i386-marcel-freebsd"...
    (gdb) list
    1       #include <stdio.h>
    2
    3       int main(void)
    4       {
    5        static char buf[BUFSIZ];
    6        static char *bufp = buf;
    7        static int n = 0;
    8
    9        if(n == 0) {
    10        n = read(0,buf, sizeof buf);
    (gdb) br 6
    Breakpoint 1 at 0x8048514: file tty2.c, line 6.
    (gdb) br 9
    Note: breakpoint 1 also set at pc 0x8048514.
    Breakpoint 2 at 0x8048514: file tty2.c, line 9.
    (gdb) run
    Starting program: /home/guest/cdalten/tty2
    
    Breakpoint 1, main () at tty2.c:9
    9        if(n == 0) {
    (gdb) display buf
    1: buf = '\0' <repeats 1023 times>
    (gdb) step
    10        n = read(0,buf, sizeof buf);
    1: buf = '\0' <repeats 1023 times>
    (gdb) step
    c h a d
    11        bufp = buf;
    1: buf = "c h a d\n", '\0' <repeats 1015 times>
    (gdb) step
    13       return (--n >= 0) ? (unsigned char) *bufp++ : EOF;
    1: buf = "c h a d\n", '\0' <repeats 1015 times>
    (gdb) step
    14      }
    1: buf = "c h a d\n", '\0' <repeats 1015 times>
    (gdb) step
    0x08048442 in _start ()
    1: buf = "c h a d\n", '\0' <repeats 1015 times>
    (gdb) step
    Single stepping until exit from function _start,
    which has no line number information.
    
    Program exited with code 0143.
    (gdb) q
    Code:
    m-net% gdb tty
    GNU gdb 6.1.1 [FreeBSD]
    Copyright 2004 Free Software Foundation, Inc.
    GDB is free software, covered by the GNU General Public License, and you are
    welcome to change it and/or distribute copies of it under certain conditions.
    Type "show copying" to see the conditions.
    There is absolutely no warranty for GDB.  Type "show warranty" for details.
    This GDB was configured as "i386-marcel-freebsd"...
    (gdb) list
    1       #include <stdio.h>
    2
    3       int main(void)
    4       {
    5        char c;
    6
    7        return(read(0, &c, 1) == 1) ? (unsigned char)c : EOF;
    8
    9        return 0;
    10      }
    (gdb) br 7
    Breakpoint 1 at 0x8048514: file tty.c, line 7.
    (gdb) run
    Starting program: /home/guest/cdalten/tty
    
    Breakpoint 1, main () at tty.c:7
    7        return(read(0, &c, 1) == 1) ? (unsigned char)c : EOF;
    (gdb) display c
    1: c = 0 '\0'
    (gdb) step
    c
    10      }
    1: c = 99 'c'
    (gdb)
    0x08048442 in _start ()
    (gdb)

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Was that meant to be a question?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Banned
    Join Date
    May 2007
    Location
    Berkeley, CA
    Posts
    329
    No. It was just a brain fart.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Debugging question
    By o_0 in forum C Programming
    Replies: 9
    Last Post: 10-10-2004, 05:51 PM
  2. Question about pointers #2
    By maxhavoc in forum C++ Programming
    Replies: 28
    Last Post: 06-21-2004, 12:52 PM
  3. Question...
    By TechWins in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 07-28-2003, 09:47 PM
  4. Question about linked lists.
    By cheeisme123 in forum C++ Programming
    Replies: 6
    Last Post: 02-25-2003, 01:36 PM
  5. Question, question!
    By oskilian in forum A Brief History of Cprogramming.com
    Replies: 5
    Last Post: 12-24-2001, 01:47 AM