I think I would be happier if the size of scanbuf is added as an argument to this function. It will make it less error prone and easier to follow.