DaemonForums  

Go Back   DaemonForums > Miscellaneous > Programming

Programming C, bash, Python, Perl, PHP, Java, you name it.

Reply
 
Thread Tools Display Modes
  #1   (View Single Post)  
Old 22nd January 2009
maxrussell maxrussell is offline
Package Pilot
 
Join Date: May 2008
Location: Montrose, Angus
Posts: 181
Thanked 1 Time in 1 Post
Question C project to study

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.
Reply With Quote
  #2   (View Single Post)  
Old 22nd January 2009
JMJ_coder JMJ_coder is offline
VPN Cryptographer
 
Join Date: May 2008
Posts: 464
Thanked 8 Times in 8 Posts
Default

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)
Reply With Quote
  #3   (View Single Post)  
Old 22nd January 2009
ephemera's Avatar
ephemera ephemera is offline
Knuth's homeboy
 
Join Date: Apr 2008
Posts: 537
Thanked 49 Times in 43 Posts
Default

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
Reply With Quote
  #4   (View Single Post)  
Old 22nd January 2009
maxrussell maxrussell is offline
Package Pilot
 
Join Date: May 2008
Location: Montrose, Angus
Posts: 181
Thanked 1 Time in 1 Post
Default

Quote:
Originally Posted by ephemera View Post
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
That'll be more of a long term task I feel - intially I want to read/dissect other code.
Reply With Quote
  #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
Thanked 112 Times in 104 Posts
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
  #6   (View Single Post)  
Old 22nd January 2009
JMJ_coder JMJ_coder is offline
VPN Cryptographer
 
Join Date: May 2008
Posts: 464
Thanked 8 Times in 8 Posts
Default

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)
Reply With Quote
  #7   (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
Thanked 112 Times in 104 Posts
Default

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''.
Reply With Quote
  #8   (View Single Post)  
Old 23rd January 2009
BSDfan666 BSDfan666 is offline
Real Name: N/A, this is the interweb.
Helpful companion
 
Join Date: Apr 2008
Location: Ontario, Canada
Posts: 2,223
Thanked 193 Times in 184 Posts
Default

K&R syntax is being phased out for the most part, the developers are slowly converting to ANSI/C99 compliant code AFAICT.
Reply With Quote
  #9   (View Single Post)  
Old 23rd January 2009
JMJ_coder JMJ_coder is offline
VPN Cryptographer
 
Join Date: May 2008
Posts: 464
Thanked 8 Times in 8 Posts
Default

Quote:
Originally Posted by BSDfan666 View Post
K&R syntax is being phased out for the most part, the developers are slowly converting to ANSI/C99 compliant code AFAICT.
Isn't the second edition of K&R 'ANSI C'? At least that's what it boasts about itself. Though, seeing how it was published in 1988, I don't see how they could guarantee it would be compliant with a standard years into the future.

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)
Reply With Quote
Old 23rd January 2009
ocicat ocicat is offline
Administrator
 
Join Date: Apr 2008
Posts: 2,888
Thanked 190 Times in 160 Posts
Default

Quote:
Originally Posted by JMJ_coder View Post
Isn't the second edition of K&R 'ANSI C'?
Yes.

http://www.amazon.com/Programming-La...2680933&sr=8-1
Reply With Quote
Old 23rd January 2009
maxrussell maxrussell is offline
Package Pilot
 
Join Date: May 2008
Location: Montrose, Angus
Posts: 181
Thanked 1 Time in 1 Post
Default

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.
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
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


All times are GMT. The time now is 10:45 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Content copyright © 2007-2010, the authors
Daemon image copyright ©1988, Marshall Kirk McKusick