try commenting out your while loops. I'm not that familiar with vanilla c,, but it almost seems as though it's treating all of your while loops as do-whiles, minus the printing out. If you comment out those loops, and it works right, try putting in error print messages in your while loops to see if it is triggering them.
Also, do you have all the libraries included that you need? I know several unix compilers if they don;t see the correct library will sometimes sub in "implied" headers, but not the implimentations, and allow them to compile right.... just a thought.