What I've done is that I've removed the possibility to send program arguments to my userspace programs. This means that instead of the usual:
Code: Select all
unsigned int main(int argc, char **argv)
{
return 0;
}
Code: Select all
void main()
{
}
To illustrate what I mean lets take the program cat. Normally you would give cat some arguments saying what files it should concatenate. My implementation of cat does not have any notion of what files it should work on (it does not get any arguments with the filenames), all it needs to know is that it can read data from some predetermined file descriptor like stdin and print it to stdout. This is pretty much the same as a normal pipe.
An simple implementation could look like this:
Code: Select all
void main()
{
char buffer[0x1000];
unsigned int count;
count = fread(3, 0x1000, buffer);
fwrite(STDOUT, count, buffer);
count = fread(4, 0x1000, buffer);
fwrite(STDOUT, count, buffer);
}
I like this way of writing programs because it almost forces programs to do one thing and do it well which is what the UNIX philosophy is about (I've heard). If a program does not have options a program can't really do much else than one thing. Many programs on modern UNIXes have a shitload of different parameters and you often don't remember or use half of them anyway. Without arguments you need to have many smaller programs to do all the things one typical program could do but at the same time you only use a subset of that normal program anyway.
Another good thing about not having arguments is that when calling execute() I don't need to copy the arguments from the parent's memory space to the child which I would have to do otherwise. This saves a few lines of code so it's nothing important but it separates the processes even more which I like because now I don't need to add stuff to the stack or the heap or wherever you might store the arguments.
Also it makes the implementation of a shell quite interesting. Given an input string like this:
Code: Select all
$ cat myfile.txt anotherfile.txt
Well... that was all I had. Does it make any sense what so ever?