Jump to my Home Page Send me a message Check out stuff on GitHub Check out my photography on Instagram Check out my profile on LinkedIn Check me out on Facebook

Example UNIX Programs

Here you will find several examples of source code intended to illustrate concepts related to UNIX system programming -- in C, Perl, and Ruby. The makefile is here.

Security

suid0tmp.c
This is a little snippet of code I start with when making SUID0 programs
mjrrsh.pl
This little perl script demonstrates how to make perl do TCP/IP, how to make Perl timeout, and how rsh really works.
rssu.c
Demonstrates a very bad way to replace su.

Process Creation and Information

forknwait.c
How to fork and, more importantly, how to wait.
forkbunny.c
This tiny program demonstrates how some UNIX systems allow a process to have an unlimited number of children.

User and Password Information

passwd.c passwd.pl passwd.rb
Demonstrates how to traverse the password database in a portable way.
group.c group.pl group.rb
Demonstrates how to traverse the entire UNIX group database.
uideuid.c uideuid.pl uideuid.rb
Demonstrates how to query the current user ID.
gidegid.c gidegid.pl gidegid.rb
Demonstrates how to query the current group ID.
getgrXXX.c
Demonstrates how to look up group information by GID and/or gname (group name)
getpwXXX.c
Demonstrates how to look up password information by UID and/or uname (user name)
getgroups.c
Demonstrates how to find out what secondary groups the current user is in.
passwdshadow.c
Demonstrates how to obtain shadow password database information in a portable way.

Process Environment

env.c env.pl env.rb
This bit of code shows how to query and change environment variables.
environ.c
How to use the environ variable defined by ISO C
arguments.c arguments.pl arguments.rb
Demonstrates how to access the arguments given to a program.

Signals and Related Concepts

kill.c kill.pl kill.rb
Demonstrates how to send signals.
signal.c
Demonstrates several signal concepts using the BSD signal(3) API.
sleep.c sleep.pl
Demonstrates the correct way to get sleep to make your program suspend for a given amount of time.
sigaction.c
Demonstrate the sigaction system call.

Files and File I/O

IOerrors.c
Demonstrates the correct way to deal with errors from UNIX I/O functions.
fileIO.c
Demonstrates how to use the UNIX API to do file I/O.
sparse.c
This program demonstrates how to create a sparse file. Also called a "file with holes".
fileStuff.c
Several file operations are demonstrated
utime.c utime.pl utime.rb
Simple demo of the utime function (the API equivalent of the touch command)
fileData.c
Demonstrates the stat call to discover various bits of info about files.
select_poll.c
Demonstrates the select system call.
flock_ex.c
Demonstrate file locking with the flock system call.
mmap.c
How to use the mmap function (SysV & POSIX) on a file.

Directories and directory traversal

readdir.c readdir.pl readdir.rb
Demonstrates how read a UNIX directory(the file names in a subdirectory).
glob.pl glob.rb
Typical way to do a readdir-type operation in perl and ruby.
dirRec.c
Demonstrates the classical recursive algorithm to traverse a directory tree (lots of open file descriptors)
dirDepth.c dirDepthC.cpp
Demonstrates a method to do a depth first directory tree traversal (one open file descriptor and a linked list).
dirFastC.cpp
Like dirDepthC.cpp, but modified for a single ended container (no linked list) for performance
dirThreadCPP98.cpp
Demonstrates a threaded method (pthreads) for tree traversal
dirThreadCPP11.cpp
Demonstrates a threaded method (C++11 threads) for tree traversal

Sockets

unixSocC.c
Demonstrates how to write a UNIX domain socket client.
unixSocS.c
Demonstrates how to write a UNIX domain socket server.
inetSocC.c
Demonstrates how to write a Internet domain socket client.
inetSocS.c
Demonstrates how to write a Internet domain socket server.

POSIX Threads

ptCond.c
How to use condition variables
ptJoin.c
Demonstrates the pthread_join system call
ptMutex.c
How to use mutexes
mtUtils.c mtUtils.h
C, and C++, utilities for multi-threaded programs

Networking (IP)

hosts.c
Demonstrates how to query the current host name and how to look hosts up with the system's resolver.
fqdns.c fqdns.pl fqdns.rb fqdns.sh
How to get the FQDNS of a host -- this is tricky! The shell version is just silly, but it will give you some ideas regarding how to make this work.

Solaris

solaris_ps.c
Demonstrates how to use the Solaris procfs to access process information
rpacct.pl
Demonstrates how to parse Solaris process accounting records in perl.

Shared Memory (System V / POSIX XSI IPC)

shmMake.c
How to create a shared memory segment.
shmUse.c
How to attach to an existing memory segment, map it, and read data out of it.
shmRemove.c
How to remove a shared memory segment.

Shared Memory (POSIX real-time IPC)

shmMakeAndUseRT.c
How to create a new shared memory segment, and how to use an existing one.
shmRemoveRT.c
How to remove a shared memory segment.

Terminal Stuff

termSizeWatch.c
How to detect the current TTY's window size and monitor size changes

ncurses

curHello.c curHelloMV.c curHelloW.c curHelloMVW.c
Simple examples of minimal, "hello, world!" programs for curses
curColor.c
How to use color.
curDraw.c
Demonstrates several ncurses functions via an ASCII art drawing program
curInput.c
How to do non-line buffered input and identify input keys.

System Information

paths.c
How to use the paths.h include file available on many UNIX systems.
param.c
How to use the param.h include file available some UNIX systems.
sysconf.c
How to use the sysconf function.
isocLimits.c
How to discover various properties of C types.
posixLimits.c
Demonstrates several limits.h constants provided by POSIX
uname.c uname.sh
How to use the POSIX uname system call, and the /usr/bin/uname command.

Regular Expressions & String Token Extraction

regex_example.c
Simple example of standard UNIX regular expressions.
pcre_example.c
Simple example of the PCRE (Perl Compatible Regular Expressions) library
regexExample1.cpp
Trivial example of the boost regular expression library. The make file is here.
isoTokEx.c
An example of how to use strtok() and strtok_r().
tokenExample.cpp
Demonstrates how to make use of the boost string tokenizer. The make file is here.

Misc

NDBM/GDBM/etc
Example of basic use of the NDBM, GDBM, and Berkeley DB
popenDemo.c
An example of how to use the popen function.
getoptP.c
Minimal example of the getopt function call.
rlimit.c
Example of getrlimit -- of ulimit and limit fame.

Readline (GNU and others)

rlEX.c
Minimal example of readline use.
rlEXH.c rlEXC.c
Extends the minimal example with custom completions (GNU Readline).
© 2009 Mitch Richling