Skip to content

Instantly share code, notes, and snippets.

@dduan
Last active August 29, 2015 14:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dduan/f6d359019db8b0b55962 to your computer and use it in GitHub Desktop.
Save dduan/f6d359019db8b0b55962 to your computer and use it in GitHub Desktop.
Unix 'cat' Command Implemented In Swift Without Foundation
#import <stdio.h>
let byLine = { (file : UnsafeMutablePointer<FILE>) in
anyGenerator({ () -> String? in
var input = UnsafeMutablePointer<Int8>()
var lim = 0
return getline(&input, &lim, file) > 0 ? String.fromCString(input) : nil
})
}
let cat = {(fd: UnsafeMutablePointer<FILE>) -> Void in
for line in byLine(fd) {
print(line, appendNewline: false)
}
}
if Process.arguments.count == 1 {
cat(stdin)
} else {
Process.arguments[1..<Process.arguments.count].map { f -> Void in
let fd = fopen(f, "r")
cat(fd)
fclose(fd)
}
}
SDKPATH = $(shell xcrun --show-sdk-path --sdk macosx)
CBRIDGEHEADER = bridge.h
TARGETS := cat
.PHONY : all $(TARGETS)
all: $(TARGETS)
$(TARGETS):
swiftc -sdk $(SDKPATH) $@.swift -import-objc-header $(CBRIDGEHEADER) -o $@
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment