summaryrefslogtreecommitdiff
path: root/userspace
diff options
context:
space:
mode:
Diffstat (limited to 'userspace')
-rw-r--r--userspace/cat.c57
-rw-r--r--userspace/grep.c20
-rw-r--r--userspace/newcalls.h5
-rw-r--r--userspace/piper.c31
4 files changed, 57 insertions, 56 deletions
diff --git a/userspace/cat.c b/userspace/cat.c
index 94cbdaa..41768ea 100644
--- a/userspace/cat.c
+++ b/userspace/cat.c
@@ -3,34 +3,39 @@
int main(int argc, char **argv)
{
- printf("hmm\n");
- printf("pwd=%s\n",getenv("PWD"));
- FILE *f;
- if(argc>1){
- char buf[256];
- if(argv[1][0]!='/')
- {
- sprintf(buf,"%s/%s",getenv("PWD"),argv[1]);
- printf("cat %s\n",buf);
- f=fopen(buf,"r");
- }
- else
- {
- printf("cat %s\n",argv[1]);
- f=fopen(argv[1],"r");
- }
- }
- else f=stdin;
+ // Input (default stdin)
+ FILE *in=stdin;
+
+ // Output
+ FILE *out=stdout;
- setvbuf(stdin,NULL,_IONBF,0);
- setvbuf(stdout,NULL,_IONBF,0);
+ // In case a Filename was supplied
+ if(argc>1){
+ char buf[256];
- char c;
+ // Relative Path
+ if(argv[1][0]!='/')
+ {
+ sprintf(buf,"%s/%s",getenv("PWD"),argv[1]);
+ in=fopen(buf,"r");
+ }
- while(fread(&c,1,1,f))
- {
- printf("%c",c);
- }
+ // Absolute Path
+ else
+ {
+ in=fopen(argv[1],"r");
+ }
+ }
+
+ char buf[256];
+
+ while(1)
+ {
+ int l=fread(buf,1,255,in);
+ buf[l]=0;
+ if(l==0)break;
+ fwrite(buf,1,l,out);
+ }
- return 0;
+ return EXIT_SUCCESS;
}
diff --git a/userspace/grep.c b/userspace/grep.c
index c37392f..68d5b7a 100644
--- a/userspace/grep.c
+++ b/userspace/grep.c
@@ -3,15 +3,17 @@
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);
+ FILE *in=stdin;
+ FILE *out=stdout;
- }
+ while(1)
+ {
+ char buf[2];
+ int l=fread(buf,1,1,in);
+ if(l==0)break;
+ buf[l]=0;
+ fwrite(buf,1,l,out);
+ }
- return EXIT_SUCCESS;
+ return EXIT_SUCCESS;
}
diff --git a/userspace/newcalls.h b/userspace/newcalls.h
index 9bc55a5..4568711 100644
--- a/userspace/newcalls.h
+++ b/userspace/newcalls.h
@@ -3,6 +3,7 @@
// this syscall will be moved to newlib later!
#define SYSCALL_CLONE 83
#define SYSCALL_PIPE 84
+#define SYSCALL_DUP2 86
int _clone(void)
{
@@ -12,4 +13,8 @@ int _pipe(uint32_t fds[2])
{
return syscall(SYSCALL_PIPE,fds,0,0);
}
+int _dup2(uint32_t oldfd,uint32_t newfd)
+{
+ return syscall(SYSCALL_DUP2,oldfd,newfd,0);
+}
//
diff --git a/userspace/piper.c b/userspace/piper.c
index f45248e..80cbd27 100644
--- a/userspace/piper.c
+++ b/userspace/piper.c
@@ -1,38 +1,27 @@
#include <stdio.h>
#include "newcalls.h"
+extern **environ;
+
int main()
{
int fds[2];
_pipe(fds);
- printf("opened pipe / in fd=%d,out fd=%d\n",fds[0],fds[1]);
-
int pid=_fork();
if(pid)
{
- while(1) // read forever from our pipe and echo to stdout
- {
- char buf[256];
- int len=fread(buf,1,255,fds[0]);
- //int len=_read(fds[0],buf,255);
- buf[len]=0;
- printf("-\n");
- printf("%s\n",buf);
- printf("-\n");
- }
-
+ _close(fds[1]);
+ _dup2(fds[0],0); // replace stdin with the read-end of pipe
+ char *args[]={"grep",NULL};
+ _execve("/bin/grep",args,environ);
}
else
{
-
- // write to our pipe
- _write(fds[1],"Hello\n",fds[1]);
- _write(fds[1],"Bello\n",fds[1]);
-
- // hang forever
- while(1);
+ _close(fds[0]);
+ _dup2(fds[1],1); // replace stdout with the write-end of our pipe
+ char *args[]={"cat","hello.txt",0};
+ _execve("/bin/cat",args,environ);
}
-
}