CSE 127: gets behavior


Take a look at what happens with NULL characters with gets. (Note, the ASCII NUL character is entered as control-@.) You should be careful with NUL characters in interpreting the output of gets input, since all the stdio string output routines will stop outputting at the NUL and make it appear that gets did not actually transfer all characters (bytes) until a newline character. (Note the distinction between the ASCII character NUL and a NULL pointer. The former is a character, which fits in a byte; the latter is a pointer -- of arbitrary type -- which when viewed as an integer has the value zero.)
bash-2.03$ cat gets.c
#include 
int main(void)
{
        unsigned char buf[256];
        int i;
        (void) gets(buf);
        for (i = 0; i < 256; i++) {
                printf("%02x%c",buf[i],(i&0xf)==0xf?'\n':' ');
        }
        putchar('\n');
}
bash-2.03$ gcc -o gets gets.c
bash-2.03$ ./gets
foo^@bar
66 6f 6f 00 62 61 72 00 00 00 00 65 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 01 03 25 00 00 00 01 ec f1 fe 64
00 00 00 00 ec f2 13 54 00 01 03 25 00 00 00 00
ec f2 10 98 ec f2 10 98 00 00 00 06 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 ec f1 f4 dc
00 00 00 00 00 08 a9 dc 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 ec f1 f4 dc
00 00 00 00 ec ef ab 14 ec ea 5b 58 ec ea 37 00
00 00 00 00 00 00 00 00 ef ff f8 28 00 01 04 d0
00 00 00 03 ef ff f8 8c 00 00 00 04 ef ff f8 94

bash-2.03$ 

[ search CSE | CSE | bsy's home page | links | webster | MRQE | google | yahoo | citeseer | certserver ]
picture of bsy

bsy+cse127w02@cs.ucsd.edu, last updated Mon Mar 25 15:22:08 PST 2002. Copyright 2002 Bennet Yee.
email bsy.


Don't make me hand over my privacy keys!