Remove all usages of getcwd, chdir from library code

@Seldaek As you asked I opened followup of #503.