Daniel Duan

Building 'pwd' Under 1 Minute In Swift

I guess I’m starting a series of minute-long videos. In this first video, I built pwd out of getcwd. The code turned out shorter than I imagined (1 line). Which is a good demonstration of Swift’s great C-interop capability.

Some details are lost in this video, however. For example, the POSIX standard actually says getcwd’s 2nd argument being 0 is undefined. Luckily, both macOS and Linux define it the way we want (allocate as much memory as needed). Swift’s handling of char * aka UnsafeMutablePointer<CChar> in context of a var array is also very cool.