Last active
June 26, 2018 17:31
-
-
Save motiejus/02d8cc5c2e78433572d020cd9f708ef6 to your computer and use it in GitHub Desktop.
read a named socket
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
.PHONY: test | |
test: f read_c read_go | |
bash -c 'while true; do echo yaddda | head -2 >> f; echo written; done' & \ | |
./read_c f; \ | |
./read_go f; \ | |
./read.py f; \ | |
pkill -f "echo yaddda" | |
f: | |
mkfifo f | |
read_c: read.c | |
$(CC) -Wall -O2 $< -o $@ | |
read_go: read.go | |
go build -o $@ $< |
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
#include <stdio.h> | |
int main(int argc, char **argv) { | |
FILE *fp = fopen(argv[1], "r"); | |
char str[0xff]; | |
if(fp == NULL) { | |
perror("Error opening file"); | |
return(-1); | |
} | |
if(fgets(str, 0xff, fp) != NULL) { | |
printf("from c: %s", str); | |
} | |
fclose(fp); | |
} |
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
package main | |
import ( | |
"bufio" | |
"fmt" | |
"log" | |
"os" | |
) | |
func main() { | |
fn := os.Args[1] | |
f, err := os.Open(fn) | |
if err != nil { | |
log.Fatalf("failed to open %s: %s", fn, err) | |
} | |
read1(f) | |
} | |
func read1(f *os.File) { | |
rd := bufio.NewReader(f) | |
line, _, err := rd.ReadLine() | |
if err != nil { | |
log.Fatalf("failed to read line: %s", err) | |
} | |
fmt.Printf("from go: %s\n", line) | |
} |
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
#!/usr/bin/env python3 | |
import sys | |
line = None | |
with open(sys.argv[1], "r") as f: | |
line = f.readline() | |
print("from py: %s" % line) |
Author
motiejus
commented
Jun 20, 2018
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment