Mitch Richling: Example UNIX Programs
| Author: | Mitch Richling |
| Updated: | 2024-04-22 |
Table of Contents
- 1. Introduction
- 2. Security
- 3. Process Creation and Information
- 4. User and Password Information
- 5. Process Environment
- 6. Signals and Related Concepts
- 7. Files and File I/O
- 8. Directories and directory traversal
- 9. Sockets
- 10. POSIX Threads
- 11. Networking (IP)
- 12. Solaris
- 13. Shared Memory (System V / POSIX XSI IPC)
- 14. Shared Memory (POSIX real-time IPC)
- 15. Terminal Stuff
- 16. ncurses
- 17. System Information
- 18. Regular Expressions & String Token Extraction
- 19. Misc
- 20. Readline (GNU and others)
- 21. GDBM (GNU DBM)
1. Introduction
Here you will find several examples of source code intended to illustrate concepts related to UNIX system programming – in C, Perl, and Ruby.
Get it all on github: https://github.com/richmit/ex-aupg/
2. Security
suid0tmp.c- This is a little snippet of code I start with when making SUID0 programs
mjrrsh.pl- This little
perlscript demonstrates how to makeperldo TCP/IP, how to make Perl timeout, and howrshreally works. rssu.c- Demonstrates a very bad way to replace su.
3. 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.
4. 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.
5. 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.
6. 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
sleepto make your program suspend for a given amount of time. sigaction.c- Demonstrate the
sigactionsystem call.
7. 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
touchcommand) fileData.c- Demonstrates the
statcall to discover various bits of info about files. select_poll.c- Demonstrates the
selectsystem call. flock_ex.c- Demonstrate file locking with the
flocksystem call. mmap.c- How to use the
mmapfunction (SysV & POSIX) on a file.
8. 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 inperlandruby. 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 (<span style='color: #f00;'>C++11</span> threads) for tree traversal
9. 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.
10. POSIX Threads
11. Networking (IP)
12. Solaris
solaris_ps.c- Demonstrates how to use the Solaris
procfsto access process information rpacct.pl- Demonstrates how to parse Solaris process accounting records in
perl.
13. 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.
14. 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.
15. Terminal Stuff
termSizeWatch.c- How to detect the current
TTY's window size and monitor size changes
16. 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
ncursesfunctions via an ASCII art drawing program curInput.c- How to do non-line buffered input and identify input keys.
17. System Information
paths.c- How to use the
paths.hinclude file available on many UNIX systems. param.c- How to use the
param.hinclude file available some UNIX systems. sysconf.c- How to use the
sysconffunction. isocLimits.c- How to discover various properties of C types.
posixLimits.c- Demonstrates several
limits.hconstants provided by POSIX uname.c&uname.sh- How to use the POSIX
unamesystem call, and the/usr/bin/unamecommand.
18. 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
isoTokEx.c- An example of how to use strtok() and strtok_r().
19. Misc
popenDemo.c- An example of how to use the
popenfunction. getoptP.c- Minimal example of the
getoptfunction call. rlimit.c- Example of
getrlimit– ofulimitandlimitfame.