From aeefdb37d1fc1c0eb7953b9c196cab09460bc167 Mon Sep 17 00:00:00 2001 From: Miguel Date: Thu, 20 Sep 2018 20:51:57 +0200 Subject: we are now prepared for piping with _pipe and _dup2 --- userspace/cat.c | 3 +++ userspace/grep.c | 17 +++++++++++++++++ userspace/piper.c | 12 ++++++++---- 3 files changed, 28 insertions(+), 4 deletions(-) create mode 100644 userspace/grep.c (limited to 'userspace') diff --git a/userspace/cat.c b/userspace/cat.c index d0b018b..94cbdaa 100644 --- a/userspace/cat.c +++ b/userspace/cat.c @@ -1,7 +1,10 @@ #include +#include int main(int argc, char **argv) { + printf("hmm\n"); + printf("pwd=%s\n",getenv("PWD")); FILE *f; if(argc>1){ char buf[256]; diff --git a/userspace/grep.c b/userspace/grep.c new file mode 100644 index 0000000..c37392f --- /dev/null +++ b/userspace/grep.c @@ -0,0 +1,17 @@ +#include +#include + +int main(int argc, char **argv) +{ + while(1) + { + char buf[256]; + int l=fread(buf,1,255,stdin); + if(l==0)break; + buf[l]=0; + printf("grep: %s",buf); + + } + + return EXIT_SUCCESS; +} diff --git a/userspace/piper.c b/userspace/piper.c index e433a5c..f45248e 100644 --- a/userspace/piper.c +++ b/userspace/piper.c @@ -1,5 +1,6 @@ #include #include "newcalls.h" + int main() { int fds[2]; @@ -15,7 +16,11 @@ int main() { char buf[256]; int len=fread(buf,1,255,fds[0]); - printf("%s",buf); + //int len=_read(fds[0],buf,255); + buf[len]=0; + printf("-\n"); + printf("%s\n",buf); + printf("-\n"); } } @@ -23,9 +28,8 @@ int main() { // write to our pipe - fwrite("Hello\n",1,6,fds[1]); - fwrite("Bello\n",1,6,fds[1]); - fwrite("Gello\n",1,6,fds[1]); + _write(fds[1],"Hello\n",fds[1]); + _write(fds[1],"Bello\n",fds[1]); // hang forever while(1); -- cgit v1.2.3