summaryrefslogtreecommitdiff
path: root/userspace
diff options
context:
space:
mode:
Diffstat (limited to 'userspace')
-rw-r--r--userspace/Makefile1
-rw-r--r--userspace/file.txt82
-rw-r--r--userspace/foolshell.c54
-rw-r--r--userspace/grep.c67
-rw-r--r--userspace/number.c22
5 files changed, 218 insertions, 8 deletions
diff --git a/userspace/Makefile b/userspace/Makefile
index b3c962b..f2ca823 100644
--- a/userspace/Makefile
+++ b/userspace/Makefile
@@ -42,6 +42,7 @@ ext2.img: $(PROGS)
@mkdir -p mnt/home/miguel
@mkdir -p mnt/boot
@echo "Welcome to FoolOs\nWe hope you will enjoy your stay." > mnt/home/miguel/hello.txt
+ @cp file.txt mnt/home/miguel/file.txt
@mkdir -p mnt/bin
@mkdir -p mnt/doc/test
@mkdir -p mnt/sys # mountpoint for sysfs
diff --git a/userspace/file.txt b/userspace/file.txt
new file mode 100644
index 0000000..8cdd524
--- /dev/null
+++ b/userspace/file.txt
@@ -0,0 +1,82 @@
+Sometimes someone would speak in a boat. But most
+of the boats were silent except for the dip of the
+oars. They spread apart after they were out of the
+mouth of the harbour and each one headed for the
+part of the ocean where he hoped to find fish. The
+old man knew he was going far out and he left the
+smell of the land behind and rowed out into the
+clean early morning smell of the ocean. He saw the
+phosphorescence of the Gulf weed in the water as
+he rowed over the part of the ocean that the
+fishermen called the great well because there was
+a sudden deep of seven hundred fathoms where all
+sorts of fish congregated because of the swirl the
+current made against the steep walls of the floor
+of the ocean. Here there were concentrations of
+shrimp and bait fish and sometimes schools of
+squid in the deepest holes and these rose close to
+the surface at night where all the wandering fish
+fed on them.
+
+In the dark the old man could feel the morning
+coming and as he rowed he heard the trembling
+sound as flying fish left the water and the
+hissing that their stiff set wings made as they
+soared away in the darkness. He was very fond of
+flying fish as they were his principal friends on
+the ocean. He was sorry for the birds, especially
+the small delicate dark terns that were always
+flying and looking and almost never finding, and
+he thought, "The birds have a harder life than we
+do except for the robber birds and the heavy
+strong ones. Why did they make birds so delicate
+and fine as those sea swallows when the ocean can
+be so cruel? She is kind and very beautiful. But
+she can be so cruel and it comes so suddenly and
+such birds that fly, dipping and hunting, with
+their small sad voices are made too delicately for
+the sea."
+
+He always thought of the sea as la mar which is
+what people call her in Spanish when they love
+her. Sometimes those who love her say bad things
+of her but they are always said as though she were
+a woman. Some of the younger fishermen, those who
+used buoys as floats for their lines and had
+motorboats, bought when the shark livers had
+brought much money, spoke of her as el mar which
+is masculine. They spoke of her as a contestant or
+a place or even an enemy. But the old man always
+thought of her as feminine and as something that
+gave or withheld great favours, and if she did
+wild or wicked things it was because she could not
+help them. The moon affects her as it does a
+woman, he thought.
+
+He was rowing steadily and it was no effort for
+him since he kept well within his speed and the
+surface of the ocean was flat except for the
+occasional swirls of the current. He was letting
+the current do a third of the work and as it
+started to be light he saw he was already further
+out than he had hoped to be at this hour.
+
+I worked the deep wells for a week and did
+nothing, he thought. Today I'll work out where the
+schools of bonita and albacore are and maybe there
+will be a big one with them.
+
+Before it was really light he had his baits out
+and was drifting with the current. One bait was
+down forty fathoms. The second was at seventy-five
+and the third and fourth were down in the blue
+water at one hundred and one hundred and
+twenty-five fathoms. Each bait hung head down with
+the shank of the hook inside the bait fish, tied
+and sewed solid and all the projecting part of the
+hook, the curve and the point, was covered with
+fresh sardines. Each sardine was hooked through
+both eyes so that they made a half-garland on the
+projecting steel. There was no part of the hook
+that a great fish could feel which was not sweet
+smelling and good tasting.
diff --git a/userspace/foolshell.c b/userspace/foolshell.c
index e8b384e..4d011da 100644
--- a/userspace/foolshell.c
+++ b/userspace/foolshell.c
@@ -15,9 +15,13 @@
#include <errno.h>
#include <string.h>
+
+#include "newcalls.h"
+
extern char **environ;
bool process(char *buf);
+bool metaprocess(char *buf);
bool cd(char *path);
void version()
@@ -96,7 +100,7 @@ int main(int argc, char **argv)
}
}
- if(!process(buf))break; // process input and return if exit
+ if(!metaprocess(buf))break; // process input and return if exit
}
return 0;
@@ -142,6 +146,52 @@ char **tokenize(char *buf,char chr)
return token;
}
+bool metaprocess2(char *buf);
+bool metaprocess(char *buf)
+{
+ char **token=tokenize(buf,'|');
+ if(token[0]==0 || token[1]==0)return process(buf); // no pipes
+
+ int pid=_fork();
+
+ if(pid==0)
+ {
+ metaprocess2(buf);
+ exit(1);
+ }
+
+ _wait(pid);
+ return true;
+}
+
+bool metaprocess2(char *buf)
+{
+ char **token=tokenize(buf,'|');
+ if(token[0]==0 || token[1]==0)return process(buf); // no pipes
+
+ int fds[2];
+ _pipe(fds);
+
+ int pid=_fork();
+
+ if(pid==0)
+ {
+ // first child shall write to the pipe
+ _close(fds[0]);
+ _dup2(fds[1],1); // replace stdout with the write-end of our pipe
+ process(token[0]);
+ exit(1);
+ }
+
+ _close(fds[1]); // and we read from it
+ _dup2(fds[0],0);
+ metaprocess2(strchr(buf,'|')+1);
+
+ _wait(pid);
+
+ return true;
+}
+
bool process(char *buf)
{
char **token=tokenize(buf,' ');
@@ -179,7 +229,7 @@ bool process(char *buf)
else sprintf(buf,"%s/%s",getenv("PATH"),token[0]);
_execve(buf,token,environ);
printf("foolshell: %s (errno: %d)\n",strerror(errno),errno);
- return false;
+ exit(1);
}
if(token[1]==NULL||strcmp(token[1],"&"))_wait(pid);
diff --git a/userspace/grep.c b/userspace/grep.c
index 68d5b7a..058f1f8 100644
--- a/userspace/grep.c
+++ b/userspace/grep.c
@@ -1,18 +1,73 @@
#include <stdio.h>
#include <stdlib.h>
+// https://www.programmingsimplified.com/c/source-code/c-program-insert-substring-into-string
+//
+char *substring(char *string, int position, int length)
+{
+ char *pointer;
+ int c;
+
+ pointer = malloc(length+1);
+
+ if( pointer == NULL )
+ exit(EXIT_FAILURE);
+
+ for( c = 0 ; c < length ; c++ )
+ *(pointer+c) = *((string+position-1)+c);
+
+ *(pointer+c) = '\0';
+
+ return pointer;
+}
+
+
+void insert_substring(char *a, char *b, int position)
+{
+ char *f, *e;
+ int length;
+
+ length = strlen(a);
+
+ f = substring(a, 1, position - 1 );
+ e = substring(a, position, length-position+1);
+
+ strcpy(a, "");
+ strcat(a, f);
+ free(f);
+ strcat(a, b);
+ strcat(a, e);
+ free(e);
+}
+
int main(int argc, char **argv)
{
+ char col_start[]="\033[31;40m";
+ char col_end[]="\033[37;40m";
+
FILE *in=stdin;
FILE *out=stdout;
- while(1)
+ int i=1;
+ while(!feof(in))
{
- char buf[2];
- int l=fread(buf,1,1,in);
- if(l==0)break;
- buf[l]=0;
- fwrite(buf,1,l,out);
+ char buf[256];
+ char buf2[255];
+ fgets(buf,255,in);
+
+ char *pos=strstr(buf,argv[1]);
+ if(pos)
+ {
+ int p=pos-buf+1;
+ int l=strlen(argv[1])+strlen(col_start);
+
+ insert_substring(buf,col_start,p);
+ insert_substring(buf,col_end,p+l);
+
+ printf("%s",buf);
+ }
+
+ i++;
}
return EXIT_SUCCESS;
diff --git a/userspace/number.c b/userspace/number.c
new file mode 100644
index 0000000..b10bccb
--- /dev/null
+++ b/userspace/number.c
@@ -0,0 +1,22 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+int main(int argc, char **argv)
+{
+ char col_start[]="\033[34;40m";
+ char col_end[]="\033[37;40m";
+
+ FILE *in=stdin;
+ FILE *out=stdout;
+
+ int i=1;
+ char buf[256];
+ while(NULL!=fgets(buf,255,in))
+ {
+ printf("line %s%i%s: %s",col_start,i,col_end,buf);
+ //printf("line %i : %s",i,buf);
+ i++;
+ }
+
+ return EXIT_SUCCESS;
+}