I always dreamed of a stream opening routine that is able to open ordinary files, URLs and Unix pipes at the same time. I started to write such a routine just for fun.
Here are some examples:
extern void *kopen(const char*, int*);
extern int kclose(void*);
void *ko[4];
int i, fd[4];
ko[0] = kopen("file.txt", &fd[0]);
ko[1] = kopen("http://website.com/index.html", &fd[1]);
ko[2] = kopen("ftp://ftpsite.edu/file.txt", &fd[2]);
ko[3] = kopen("<grep test file.txt", &fd[3]);
for (i = 0; i < 4; ++i) {
close(fd[i]);
kclose(ko[i]);
}
As is showed, this library currently supports file/http/ftp/pipe. It returns an object to keep internal states and a file descriptor. You may use “fdopen()”, “gzdopen()” or my generic stream buffer for buffered I/O. For now, it does not work in Windows. I believe in Windows, a socket descriptor is not equivalent to a file descriptor as in Unix.
This routine is certainly imperfect. Its network related code is not robust and not optimized. There may be a memory leak to call other executables without using a shell. But the idea is there.
If you are interested, you may download kopen.c, compile with “gcc -D_KO_MAIN -O2 kopen.c -o kopen” and try:
./kopen file.txt ./kopen https://attractivechaos.wordpress.com/ ./kopen "<grep man /etc/man.conf"
You see, this kopen is basically a more versatile “cat” (to some extend).
brilliant idea!
shall the third example be like this?
grep man /etc/man.conf | ./kopen –