Here you will find several examples of source code intended to illustrate
concepts related to UNIX system programming. 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
- Demonstrates how to traverse the password database in a portable way.
- group.c group.pl
- Demonstrates how to traverse the entire UNIX group database.
- uideuid.c uideuid.pl
- Demonstrates how to query the current user ID.
- gidegid.c gidegid.pl
- 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
- 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
- Demonstrates how to access the arguments given to a program.
Signals and Related Concepts
- kill.c kill.pl
- 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
- 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
- Demonstrates how read a UNIX directory(the filenames in a subdirectory).
- glob.pl
- Typical way to do a readdir-type operation in Perl.
- dirRec.c
- Demonstrates the classical recursive algorithm to traverse a directory tree (lots of open file descriptors)
- dirDepth.c dirDepthC.cc
- Demonstrates a method to do a depth first directory tree traversal (one open file descriptor and a linked list).
- dirFastC.cc
- Like dirDepthC.cc, but modified for a single ended container (no linked list) for performance
- dirThread.cc
- Demonstrates a threaded method 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.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(2) system call, and the /usr/bin/uname(1) 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
- boostReEx1.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().
- boostTokEx.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).