Skip to content

Instantly share code, notes, and snippets.

@dduan dduan/Makefile
Last active Aug 29, 2015

Embed
What would you like to do?
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
You can’t perform that action at this time.