Advanced UNIX Programming goes beyond the fundamentals of UNIX programming and presents information and techniques the readers needs to expand their knowledge base. Designed for professional UNIX programmers, this book builds on the skills and knowledge the reader already possesses. It includes coverage of internet processes, interprocess control, file system manipulation, synchronization, and much more.
I: FILES AND DIRECTORIES.1. Compiler Notes and Options.
Online Manual Pages.
Manual References Used in This Book. man(1) Resources on the Internet.
Example Code in This Book. Compiling C Programs.
The C Compile Command.
Managing Compiler Warnings.
Working with Compiler Warning Messages. Compiling to Standards. FreeBSD 3.4-Release Feature Tests. HPUX 10.2 Feature Tests. AIX 4.3 Feature Tests. SunOS 5.6 Feature Tests. Feature Test Summary.
Summary.2. UNIX File System Objects.
File System Objects.
Regular Files. Directories. Block Devices. Character Devices. Named Pipes (FIFOs). Sockets. Symbolic Links. Special Files.
Access of Regular Files. Access of Directories.
Working with Files Under UNIX.
Opening and Closing Files. Opening Special Files. Working with Sockets. Duplicating File Descriptors.
UNIX File I/O. Summary.3. Error Handling and Reporting.
Determining Success or Failure.
General Rules for Error Indication. Exceptions to the General Rule. Classifying Successful Return Values. Other Return Indications.
Determining the Reason for Failure. The Old errno Value.
Referencing Error Codes by Name. Applying errno Correctly.
The New errno Value.
Declaring the New errno Variable. Using the New errno Variable.
Reporting on errno Values.
Using the perror(3) Function. Using the sys_errlist[ ] Array. The strerror(3) Function.
Testing for Errors Using stdio(3).
Pitfalls of the ferror(3) Function. Avoiding the fclose(3) Pitfall. Delaying the Reporting of an Error.
Summary.4. UNIX Input and Output.
The umask(2) Function and umask Bits.
Permission Bits. Understanding the Need for umask. Understanding the Scope of umask. Using the umask(2) Function. Example Using the umask Value. The <F102MO>umask(2) Function. The creat(2) Function.
Reading and Writing.
Introducing <F102MO>read(2) and <F102MO>write(2). Applying UNIX I/O.
Seeking Within a File.
The truncate(2) Function. The ftruncate(2) Function.
Creating a Sparse File.
Forcing Data to Media.
The sync(2) Function. The Disadvantages of sync(2). The fsync(2) Function.
Scattered Reading and Writing.
The readv(2) and writev(2) Functions.
Determining Your tty Name. Summary.5. File Locking.
Understanding Lock Types. The Lock File Technique.
Limitations of the Lock File. Using an Advisory Lock on the Entire File. Locking with flock(2).
Locking with lockf(2). POSIX Locking with fcntl(2). Mandatory Locking. Enabling Mandatory Locking.
Summary.6. Managing Files and Their Properties.
Removing Files. Linking Files. Moving Files. Obtaining File System Information.
The stat(2) Function. The fstat(2) Function. Working with File Properties. Testing for File Type. Modification, Access, and Creation Times.
Testing Access to a File. Symbolic Links.
The symlink(2) Function. The lstat(2) Function. Reading the Contents of the Symbolic Link with readlink(2).
File Permissions and Ownership.
Changing Permissions. Changing Ownership.
Named Pipes (FIFOs). Obtaining Size and Configuration Information. Summary.7. Directory Management.
Obtaining the Working Directory.
Specifying a Null Buffer Argument.
Changing the Current Directory.
Saving a Working Directory. A Limitation of fchdir(2).
Making a New Directory. Removing a Directory. Opening a Directory for Searching. Closing a Directory. Searching a Directory. Rewinding to the Start of a Directory. Saving Position Within a Directory. Restoring Position Within a Directory. Scanning a Directory.
Declaring Your Own select Function for <F102MO>scandir(3). Declaring Your Own compar Function for scandir(3). SysV Variations. A scandir(3) Example.
Walking a Directory Structure. Changing Your Root Directory. Summary.8. Temporary Files and Process Cleanup.
Creating Temporary Files.
Using the tmpnam(3) Function. Using the mktemp(3) Function. Using the mkstemp(3) Function. Using the mkstemps(3) Function. Using the tmpfile(3) Function. Using the <F102MO>tempnam(3) Function.
Making Files Temporary.
Using unlink(2) to Make Files Temporary. Performing Exit Cleanup. Using the atexit(3) Function. Using C++ Destructors. Avoiding Cleanup with _exit(2).
II. LIBRARY FUNCTIONS.9. UNIX Command-Line Processing.
Using Multiple Options. Combining Multiple Options. Using Options with Arguments. Identifying Options or Arguments.
Arguments That Look Like Options. The getopt(3) Function.
The getopt(3) External Values. The getopt(3) Function Call. Defining the optstring Argument. Defining an Option-Processing Loop.
The getsubopt(3) Function.
Determining the End of Suboption Processing. A Full getsubopt(3) Example.
GNU Long Options Extension.
The GNU getopt_long(3) Function. Understanding the option Structure. Setting Up the option Structure. Using a Null option.flag Pointer. Using a Non-Null option.flag Pointer.
Summary.10. Conversion Functions.
Simple Conversion Functions.
Scrutinizing the Functions atoi(3) and atol(3). The atof(3) Function.
Using sscanf(3) for Conversion and Validation.
Applying sscanf(3) to Numeric Conversion. Testing Numeric Conversions Using sscanf(3). Improving the sscanf(3) Conversion. The Limitations of sscanf(3).
The strtol(3) and strtoul(3) Functions.
Using the strtol(3) Function. Testing for Errors. Testing the Conversion Pointer. Performing Multiple Conversions. Using the base Argument for Radix Conversions. Testing for Overflows and Underflows. Testing for strtoul(3) Overflows.
Large Integer Conversions. BSD strtoq(3) and strtouq(3) Functions. The strtod(3) Function.
Using the strtod(3) Function. Testing for Math Errors. Flowchart of Math Error Tests.
Summary.11. UNIX Date and Time Facilities.
Introducing World Time Standards.
Defining the Date and Time Data Type. Time Conversion Functions.
Converting Time to String Form Using ctime(3). The ctime_r(3) Function. The localtime(3) and gmtime(3) Functions. The Members of the struct tm. Conversion of Date/Time Components to Strings Using the asctime(3) Function. The tzset(3) Function. Creating Epoch Time Values with the mktime(3) Function.
Customizing Date and Time Formats with strftime(3).
The strftime(3) Format Specifiers. Implementing the <F102MO>DTime::strftime() Method. Testing Class DTime. Understanding the Effects of Locale.
Summary.12. User, Password, and Group Management.
Introduction to UNIX User Management.
Understanding Username and User ID Numbers. Understanding Username root. The Group Name and Group ID Numbers. Understanding gid Zero.
The getuid(2) and geteuid(2) Functions. The getgid(2) and getegid(2) Functions. Real, Effective, and Saved User ID.
The Effective User ID. The Real User ID. The Saved User ID. The Identification Role Summary.
Setting User ID. Setting Group ID. The FreeBSD Function issetugid(2). The <F102MO>/etc/passwd File.
The Comment Field. Using the & Feature of the Comment Field.
The Password Database Routines.
The passwd Structure. Error Handling for getpwent(3). The fgetpwent(3) Function. The putpwent(3) Function. The getpwuid(3) Function. The getpwnam(3) Function.
The Group Database.
The <F102MO>/etc/group File. Functions getgrent(3), <F102MO>setgrent(3), and endgrent(3). Understanding the group Structure. The fgetgrent(3) Function. The getgrgid(3) Function. The getgrnam(3) Function.
Related Re-entrant Functions. Supplementary Groups.
The getgroups(2) Function. Setting Groups with setgroups(2). Setting Groups for a Specific Username.
Summary.13. Static and Shared Libraries.
The Static Library.
Examining the Process Memory Image. Implementing a Static Library. Using the ar(1) Command to Create an Archive. Listing the Contents of an Archive. Obtaining a Verbose Listing of an Archive. Linking with Static Libraries.
The Shared Library.
Limitations of Static Libraries. Creating a Shared Library Linking with a Shared Library. Choosing Static or Dynamic Libraries. Listing Shared Library References. The Dynamic Loader. Position-Independent Code. Controlling What Is Shared.
Comparing Static and Shared Libraries.
The Benefits of Static Libraries. The Benefits of Shared Libraries.
Dynamic Library Loading.
Opening the Shared Library. Reporting Errors. Obtaining a Shared Reference Pointer. Closing a Shared Library. Initialization and Destruction. Applying Dynamic Loading. HPUX 10.2 Dynamic Library Loading.
Summary.14. Database Library Routines.
The NDBM Database.
Error Handling. Opening an NDBM Database. Closing an NDBM Database. Storing Information. Fetching Information. Deleting Information. Visiting All Keys. Deleting Keys with dbm_nextkey(3).
An NDBM Database Example.
Directory Software. The Dbm Class. The InoDb Class. The SnapShot Application. Running the SnapShot Application. Visiting All Keys and Deletion.
III. ADVANCED CONCEPTS.15. Signals.
Understanding UNIX Signals. Reliable and Unreliable Signals. The Unreliable signal(3) API. The Reliable Signal API.
Emptying a Signal Set. Filling a Signal Set. Adding Signals to a Signal Set. Removing Signals from a Signal Set. Testing for Signals in a Set. Setting Signal Actions. Signal Action Flags Applying Reliable Signals.
Blocking Signals. Obtaining Pending Signals. The sigsuspend(2) Function.
Applying the alarm(3) Function. Calling Functions from a Signal Handler.
Avoiding Re-entrant Code Issues. Re-entrancy Issues with errno in a Signal Handler.
Applying the EINTR Error Code. Raising Signals. Summary.16. Efficient I/O Scheduling.
Opening Files in Non-Blocking Mode. Setting Non-Blocking Mode. Performing Non-Blocking I/O. The Problem with Non-Blocking I/O.
I/O Scheduling Functions.
File Descriptor Sets and Their Macros. The timeval Structure. The select(2) Function. Using the select(2) Function.
Poll Events. Poll Priorities. A poll(2) Example.
The Sleep Functions.
The UNIX Implementation of sleep(3). Sleeping in Microsecond Units. Sleeping in Nanosecond Units.
Interval Timer Functions.
The Interval Timer API. Interval Timer Macros. Interval Timer Restrictions. Creating One-Shot Timers. Establishing Repeating Timers.
Summary.18. Pipes and Processes.
Creating UNIX Pipes. Opening Pipes to Other Processes. Reading from Pipes. Writing to Pipes. Closing a Pipe. Handling a Broken Pipe.
External Processes Without Pipes.
Interpreting system(3) Return Values. Invoking Commands. Scrutinizing the system(3) Function.
Summary.19. Forked Processes.
Overview of the UNIX Fork Process. The fork(2) Function.
Waiting for Process Completion.
Zombie Processes. The wait(2) Function. Interpreting the Exit Status. Other Wait System Calls.
Executing New Programs.
Other exec(2) Family Members.
Summary.20. Pattern Matching.
The * Meta-Character. The ? Meta-Character. The [ and ] Meta-Characters. The ! Meta-Character. Escaping Characters with \.
String Pattern Functions. The glob(3) Function.
Return Values for glob(3).
Summary.21. Regular Expressions.
Understanding Regular Expressions.
Anchors. Sets. Range. Character Classes. The . Meta-Character. Parenthesized Match Subexpression. Atoms. Piece. Branch. Expression Bounds. Quoted Characters.
The Regular Expression Library.
Compiling Regular Expressions. Reporting Errors. Freeing Regular Expressions. Matching Regular Expressions. Applying Regular Expressions.
Summary.22. Interprocess Communication Concepts.
Types of IPC. The Message Queue. Shared Memory. Semaphores. Referencing IPC Resources.
The IPC Key Value. Creating an IPC Resource. Accessing by IPC Key. Accessing by IPC ID.
Destroying IPC Resources. Summary.23. Message Queues.
Controlling a Message Queue.
Creating Message Queues. Accessing a Message Queue. Destroying a Message Queue. Obtaining Message Queue Information. Altering a Message Queue.
Sending and Receiving Messages.
Sending Messages. Receiving Messages.
Applying Message Queues. Summary.24. Semaphores.
Semaphore Utility Program. Creating and Accessing Semaphore Sets. Destroying Semaphore Sets. Controlling Semaphores.
Querying Semaphore Sets. Changing Semaphore Access. Querying the Value of a Semaphore. Query the Entire Semaphore Set of Values. Change the Value of a Semaphore. Change the Entire Semaphore Set of Values. Querying the Process ID for a Semaphore. Query the Number of Processes Waiting for Notifies. Query the Number of Processes Waiting for Zero.
Waiting on Semaphores. Notifying Semaphores. Waiting for Zero. Semaphore Undo Processing. The semop Utility Program.
Summary.25. Shared Memory.
The globvar Utility Program.
Creating Global Variable Pools. Destroying Global Variable Pools. The GLOBVAR Environment Variable. Creating Global Variables. Accessing Global Variables. Removing Global Variables. Clearing Global Variable Pools.
Shared Memory System Calls.
Creating and Accessing Shared Memory. Obtaining Information About Shared Memory. Changing Shared Memory Attributes. Attaching Shared Memory. Detaching Shared Memory. Destroying Shared Memory.
Using Shared Memory. Summary.26. Memory-Mapped Files.
Determining the Page Size. Creating Memory Mappings. Controlling Memory-Mapped Regions.
Changing the Access Protection. Advising the Kernel About Memory Use. Querying Pages in Memory. Synchronizing Changes.
Destroying Memory Mappings. Summary.27. X Window Programming.
An Event-Driven Model. Client/Server Processing. Software Layers.
An Xlib Client Program. Summary.Index.