![]() |
|
Programming C, bash, Python, Perl, PHP, Java, you name it. |
![]() |
|
Thread Tools | Display Modes |
|
|||
![]()
Can you please recommend an existing piece of software, with source, written in C that I could use to study?
I'm learning C and work well out of picking apart existing code (this worked previously with Ruby, Python). I'm also working through K&R and some other guides/manuals. I'm looking either for something included in the FreeBSD src that is relatively small ~1000-2000 lines or a project, perhaps on Sourceforge etc. Ideally, I want to look at something that uses the main features of the language. thanks. |
|
|||
![]()
Whatever *BSD you are running (it looks by your post that you are running FreeBSD), download its source from CVS and go through it. I would suggest starting with usr.bin -- there are a lot of good standard UNIX utilities that are a model of good programming.
__________________
And the WORD was made flesh, and dwelt among us. (John 1:14) |
|
||||
![]()
Ok, here's a small project you can try:
See if you can create a program to enumerate/print all the prime numbers upto 2^32 (~4 billion) as fast as possible. Sounds too simple? ... try it for yourself and give it your best shot. ![]() After you have written a program compare its performance with other peoples program on the www: http://primes.utm.edu/links/programs...C_source_code/ http://wwwhomes.uni-bielefeld.de/achim/prime_sieve.html |
|
|||
![]() Quote:
|
|
||||
![]()
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 |
|
|||
![]()
One of Terry's links is broken. Here is the OpenBSD src online and here is the NetBSD src online (since Terry didn't post it
![]() You'll also notice that if you read K&R, a lot of their examples are versions (usually simplified) of the standard UNIX utilities that you'll find in these three source trees.
__________________
And the WORD was made flesh, and dwelt among us. (John 1:14) |
|
||||
![]()
Link fixed, thanks :-)
Would've posted NetBSDs, but I only used NetBSD for about an hour or two.
__________________
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''. |
|
|||
![]()
K&R syntax is being phased out for the most part, the developers are slowly converting to ANSI/C99 compliant code AFAICT.
|
|
|||
![]() Quote:
![]() Do you have a source that shows the differences between K&R C and C99?
__________________
And the WORD was made flesh, and dwelt among us. (John 1:14) |
|
|||
![]() |
|
|||
![]()
I've got the second edition K&R, an early 90s SAMS 'Programmin ANSI C' and I use online resources from Universities - Strathclyde being one.
|
![]() |
Thread Tools | |
Display Modes | |
|
|
![]() |
||||
Thread | Thread Starter | Forum | Replies | Last Post |
Need suggestions on what to name this project | TerryP | Off-Topic | 10 | 6th November 2010 03:13 PM |
The PCC project seeks donations for ambitious 1.0 release. | BSDfan666 | General software and network | 34 | 24th January 2009 12:03 AM |
Project layout, any comments? | PatrickBaer | FreeBSD General | 1 | 12th October 2008 05:13 PM |
Suggestions for my honours degree project... | scotsman | FreeBSD General | 7 | 20th September 2008 01:38 PM |
C Programming Study Group on SDF | cajunman4life | Programming | 0 | 23rd August 2008 02:27 AM |