... or, as above, refactor the functions to which you're passing tmp so that they (or a subordinate function you and they can call) take a FILE, then use a tmpfile() here.