Remove unnecessary #includes.
Preserve ostream flags.
Take more care with catching exceptions.
Handle $USER being NULL.
Don't pass NULL to WARNING if $SHELL is NULL.