make mkdtemp and mkstemp family leave template unchanged on fail

also refactor mkdtemp based on new shared temp code, removing
dependency on the deprecated mktemp, whose behavior made this logic
more difficult.