If the target is a *nix system, why not just save yourself the trouble and use a
chgrp command in backticks, or via a
system() call. If you want to get fancy you could check the OS type or for the existence of a chgrp command in your path, and only chgrp if you know you can.
Want to support the EFF and FSF by buying cool stuff? Click
here.