Last active
August 29, 2015 14:24
-
-
Save dduan/f6d359019db8b0b55962 to your computer and use it in GitHub Desktop.
Unix 'cat' Command Implemented In Swift Without Foundation
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#import <stdio.h> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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