Posts Tagged C

My First Line of OS Code (Writing an OS Part 1)

In the third after-school robotics meeting of the year, I ventured into that frightening and mysterious land of operating system development. It’s the hardest thing you’ll ever fail to do – and I started it while listening suggestions for the name of this OS. I think Freddy ultimately decided on “Sloth”. Great day. Interestingly enough, the only hard-core programmer who I told about this project who did not inform me that it would be an utter failure was also the only person I know who has ever worked on writing his own OS (although it wasn’t very successfull). So I’m hopeful – probably more so than I have a right to be.

Briefly, I can now start up the computer and print an ‘A’ to the screen. That means that I can claim to be 3 months ahead of schedule, since I wasn’t planning on starting this project until early January (I got bored). Of course, when I tried to print an ‘A’ to the second line, I got a blue rectangle instead. But hey, that’s pretty cool too!

Now for the long story. Walking in today, I had made progress: I had acquired several computers to be used for this project and some other (all the meantime fighting back the greed of my bosses, who actually wanted to use them for things that were – god forbid – practical *shudder*), I had acquired a floppy disk on which to write the OS, and… well yeah, that’s about it. I also knew nothing about how to write an OS.

First, I set up grub. It turned out to be pretty simple. Next, I started figuring out how to write the OS to the disk (no, I didn’t have an OS yet – did I mention that I’ve never done this before?). At this point, Freddy pointed out that I could just store the file on the hard drive, and have grub read it. Oh. Right. Thanks. And I spent 45 minutes acquiring that floppy disk why?

Next I went to my favorite wiki, and looked up the boilerplate OS code. Pretty simple, so I copied it onto qweffor and ran it. Well, actually, I told qweffor to reboot, but I was logged on via SSH, so I had to walk over to the room with qweffor in order to watch what was happening. Freddy had already started up the right OS. So, yeah, it had printed out an ‘A’. Grub had also printed out some stuff. I specified that grub should be ‘quiet’, and retried, but that didn’t fix it. So I got a message like:

Aooting from (hd0,0)...

I decided to let that message appear, and print out the rest of the stuff to the following lines. So instead of just setting the first character to be ‘A’, I set it to be a newline, and set the second character to be ‘A’. Result: a blue square with a white circle in the middle. Then I had to catch my bus.

Result: its time to actually read the documentation. And for those of you who were planning to look on, you didn’t miss much.

Oh, and today was the first day of training for freshmen. Not that that’s unimportant, but it was somewhat less than exciting…

Tags: , , , ,

No Comments

Writing a Search Engine – Part 2

Note: this article is a continuation of a previous article on search engines, and has been continued with part 3.

After a bit over a week coding (and learning various Perl libraries), I have completed stages 1 and 2 of the search, although stage 2, the indexer, could do with a little improvement. Both are written in perl, and as usual, the complete code listings are below. I decided to write the entire spider and indexer in perl and optimize as necessary later on, so that I could get done with the thing and not get bogged down in C code. If the perl turns out not to be fast enough as the site grows, then I plan to port to C. Likewise, the actual search part (stage 3) will be written in PHP to save time. If the PHP is not fast enough, I’ll rewrite it in C – but I expect there to be no problems.

Read the rest of this entry »

Tags: , , , , , , ,

No Comments

Writing a Search Engine – Part 1

I decided that the website had grown to the point where it needed a search engine. I didn’t want to use a google search or an embedded yahoo search – they look disgusting. I also didn’t want to use any of the third party searching scripts, since most of them were costly and all of them had commercial licenses. I like free software. So I set out to write my own.

General Considerations

Let me start off by saying that what I have below is not a magic, easy solution to writing a search engine. If you are planning to write the world’s “next google”, I have a recommendation: go to http://bing.com – Microsoft’s “next google”. Notice how “copycatted” it looks. Then search around (on google, please) to find out exactly how popular it is. Hint: not very. Microsoft tried and failed. Don’t waste your time. My problem is to build an internal search engine, which only needs to deal with a small number of pages, and is low traffic so it doesn’t have to be super fast. When I told my co-working friends about the project, the responses I got varied from “maniac” to “shoot yourself now rather than afterwards – save some time”. And that’s with a highly simplified version of the problem.

Read the rest of this entry »

Tags: , , , , , , ,

1 Comment

How to Get a Directory Listing in C (POSIX)

For the minification scripts mentioned here, I had to write a program that would go through every file of a certain type in a directory and process (in this case, minify) that file. At the heart of this problem was the need to get a directory listing.

The obvious way to get a directory listing in C is to use the system call, as in system("ls");. But this is not only lame, it does not work on all systems, and the result cannot be used very easily. The better way to get a directory listing is to use system calls to the filesystem. A complete example is shown at the bottom of this post, as usual.

The functions used in getting a directory listing are opendir and readdir (note: these functions also exist in php, and the code to get a directory listing is in fact nearly the same in php and c). opendir() can be seen as kind of like a fopen() for directories. Following this analogy, readdir() is like a fgetc() for directories. To get a directory listing, we first open the directory with opendir("/dir/name/here"), and then read the name of each file in the directory with a loop of readdir(directory_handle)s. Read the rest of this entry »

Tags: , , , , , , , , ,

No Comments