View Single Post
  #5   (View Single Post)  
Old 22nd January 2009
TerryP's Avatar
TerryP TerryP is offline
Arp Constable
 
Join Date: May 2008
Location: USofA
Posts: 1,547
Default

I've spent a fair deal of time reading bits and pieces of FreeBSDs source tree. Did it when I was learning C, often when upgrading things, and fairly often when bored or curious.


You can install the source code for your system with csup or cvs. It's also possible to browse the repositories online for FreeBSD and OpenBSD quite easily. Look in 'src' in the webcvs, or /usr/src if you've got the system source installed (NB, /usr/src is the default place, not a required place).


The source tree is arranged mostly like the file system is, things in /bin can be found in src/bin/appname, things in /usr/bin can be found in src/usr.bin/appname, and so on. Kernel code is in src/sys and could use it's own road map. Things that were contributed or imported, generally reside in src/contrib. You can find an overview in the hier man page.


Try starting out with a simple program at first, src/usr.bin/head/head.c is probably a good starter. Utilities like src/usr.bin/wc/wc.c, src/bin/cat/cat.c, and src/usr.bin/tail/* (multiple files) are good next stops. Then try moving onto longer programs of interest; poking around the source tree for something to read, can actually be an interesting way to find programs you never knew existed before lol. It can also be interesting to look around the systems libc implementation (the C standard library). You can find out how things like the str*, exec*, warn*, err*, fork(), system(), and other routines do their work, what the fopen() function really does and how the f* functions work, etc. It might also be worth checking out src/lib/libc/stdlib/getopt.c IMHO. getopt is the usual function for handling command line options, like the -l in wc -l somefile; happy array dancing. For functions provided by the C standard library, Curses, Regular Expressions, Berkely Sockets (networking), and various common unix functions can be found in section 3 of the manual (e.g.$ man 3 strtonum). System calls are documented likewise in section 2; use '$ man N intro' to get an intro to what is in each section (N), there are 9 sections. If you wonder what the various foo.N files are, they are nroff sources for the manual pages; e.g. cat.c = cat source, cat.1 = cat man page source. Eventually you might want to track down how system calls work, but that should probably wait until the difference between standard libraries stream I/O functions work and system call I/O is second nature. If you have any interest in how unix works, digging into stuff like init, login, getty, login, mesg, talk, write, sh, ed, etc all work might be fun.


You can find programs from 10s of lines, to beyond 1000s of lines in the source tree. In most of the programs done in a more BSD coding style, you can often find the defition of a function by /^function_name in an editor like vi, which can be handy.
__________________
My Journal

Thou shalt check the array bounds of all strings (indeed, all arrays), for surely where thou typest ``foo'' someone someday shall type ``supercalifragilisticexpialidocious''.

Last edited by TerryP; 22nd January 2009 at 08:58 PM. Reason: Link fixed, thanks JMJ coder
Reply With Quote