rweatherly,
Don't worry, i'm not gonna tell you to download something, I understand you want to write it yourself

I've written a program to do exactly what yer talkign about, it's not fancy, but it gets the job done. My file format of choice was DXF though, but basically my program loads the DXF into my Model class, which is composed only of Triangulated Polygons, which sounds like what a STL file has, just triangulated polygons. For a while i was going to support both quads and tri's but realized there was no benefit and a waste of time hehe...
anyway, a brief way of how I did it was load the stl into a Model class (granted i'm talking more java/c++ fashion here then straight c, not sure what you are programming in, doesn't really matter). Once all the polygons are loaded, you need a Ray intersection algorithm. Which I attempted for about an hour to do and mine only worked 99% of the time, which isn't enough for G-Code unless you want random deep spots poked into your model. So I downloaded a library that had ray->polygon intersection routines and just used that.
Of course it's all easy in theory, I had a gridsize setup, and basically the G-codes are done based upon that, then there's an optimization routine to remove G-code that is milling large 0 depth areas... also i wrotee a routine to optimize the G-code so there are way fewer lines, basically if you take 3 points of a g-code, detect the degree difference between them, if it's low enough remove the middle point, interating through the entire g-code file... LinuxEMC was having a fit with my enourmous g-code file and wasn't processing enough, so this helped a lot, and the model remains intact.
I'm not really sure i'm answering anything here, basically the key is a ray->polygon intersection routine, which you run against EVERY polygon, unless you want to get into BSP's and all sorts of complex routines, but lets keep it simple, just go through every polygon. and of course the ray start point is like +1000Z X Y (whereever you want to calculate) then it points straight down -1000Z same XY... and you loop through them and find the intersection height. then XY you move around in a grid fashion of course.
My advice is to find library's to help you out.. unless you want to learn 3d math, which is fun, but will take a while to learn. game developer sites is a great resource for this stuff though..
good luck to ya,
Ross