Preface
PART I Files and Users
Chapter 1 Introduction
1.1 The Linux/Unix File Model
1.1.1 Files and Permissions
1.1.2 Directories and Filenames
1.1.3 Executable Files
1.1.4 Devices
1.2 The Linux/Unix Process Model
1.2.1 Pipes: Hooking Processes Together :
1.3 Standard C vs. Original C
1.4 Why GNU Programs Are Better
1.4.1 Program Design
1.4.2 Program Behavior
1.4.3 C Code Programming
1.4.4 Things That Make a GNU Program Better
1.4.5 Parting Thoughts about the "GNU Coding Standards".
1.5 Portability Revisited
1.6 Suggested Reading
1.7 Summary
Exercises
Chapter 2 Arguments, Options, and the Environment
2.1 Option and Argument Conventions
2.1.1 POSIX Conventions
2.1.2 GNU Long options
2.2 Basic Command-Line Processing
2.2.1 The V7 echo Program
2.3 Option Parsing: getopt ( ) and getopt_long ( )
2.3.1 Single-Letter Options
2.3.2 GNU getopt ( ) and Option Ordering
2.3.3 Long Options
2.3.3.1 Long Options Table
2.3.3.2 Long Options, POSIX Style
2.3.3.3 getopt_long ( ) Return Value Summary
2.3.3.4 GNU getopt ( ) or getopt long ( ) in User Programs
2.4 The Environment
2.4.1 Environment Management Functions
2.4.2 The Entire Environment: environ
2.4.3 GNU env
2.5 Summary
Exercises
Chapter 3 User-Level Memory Management
3.1 Linux/Unix Address Space
3.2 Memory Allocation
3.2.1 Library Calls: malloc (), calloc (), realloc (), free ( )
3.2.1.1 Examining C Language Details
3.2.1.2 Initially Allocating Memory: malloc ( )
3.2.1.3 Releasing Memory: free ( )
3.2.1.4 Changing Size: realloc ( )
3.2.1.5 Allocating and Zero-filling: calloc ( )
3.2.1.6 Summarizing from the GNU Coding Standards
3.2.1.7 Using Private Allocators
3.2.1.8 Example: Reading Arbitrarily Long Lines
3.2.1.9 GLIBC Only: Reading Entire Lines: getline ( ) and getdelim ( ) .
3.2.2 String Copying: strdup ( )
3.2.3 System Calls: brk ( ) and sbrk ( )
3.2.4 Lazy Programmer Calls: alloca ( )
3.2.5 Address Space Examination
3.3 Summary
Exercises
Chapter 4 Files and File I/O
4.1 Introducing the Linux/Unix I/O Model
4.2 Presenting a Basic Program Structure
4.3 Determining What Went Wrong
4.3.1 Values for errno
4.3.2 Error Message Style
4.4 Doing Input and Output
4.4.1 Understanding File Descriptors
4.4.2 Opening and Closing Files
4.4.2.1 Mapping FILE * Variables to File Descriptors
4.4.2.2 Closing All Open Files
4.4.3 Reading and Writing
4.4.4 Example: Unix cat
4.5 Random Access: Moving Around within a File
4.6 Creating Files
4.6.1 Specifying Initial File Permissions
4.6.2 Creating Files with ereat ( )
4.6.3 Revisiting open ( )
4.7 Forcing Data to Disk
4.8 Setting File Length
4.9 Summary
Exercises
Chapter 5 Directories and File Metaclata
5.1 Considering Directory Contents
5.1.1 Definitions
5.1.2 Directory Contents
5.1.3 Hard Links
5.1.3.1 The GNU link Program
5.1.3.2 Dot and Dot-Dot
5.1.4 File Renaming
5.1.5 File Removal
5.1.5.1 Removing Open Files
5.1.5.2 Using ISO C: remove ( )
5.1.6 Symbolic Links
5.2 Creating and Removing Directories
5.3 Reading Directories
5.3.1 Basic Directory Reading
5.3.1.1 Portability Considerations
5.3.1.2 Linux and BSD Directory Entries
5.3.2 BSD Directory Positioning Functions
5.4 Obtaining Information about Files
5.4.1 Linux File Types
5.4.2 Retrieving File Information
5.4.3 Linux Only: Specifying Higher-Precision File Times
5.4.4 Determining File Type
5.4.4.1 Device Information
5.4.4.2 The V7 cat Revisited
5.4.5 Working with Symbolic Links
5.5 Changing Ownership, Permission, and Modification Times
5.5.1 Changing File Ownership: chown (), fchown (), and ichown ( )
5.5.2 Changing Permissions: chmod ( ) and fchmod ( )
5.5.3 Changing Timestamps: utime ( )
5.5.3.1 Faking utime (file, NULL)
5.5.4 Using fchown ( ) and fchmod ( ) for Security
5.6 Summary
Exercises
Chapter 6 General Library Interfaces - Part 1
6.1 Times and Dates
6.1.1 Retrieving the Current Time: time ( ) and difftime ( )
6.1.2 Breaking Down Times: gmtime ( ) and localtime ( )
6.1.3 Formatting Dates and Times
6.1.3.1 Simple Time Formatting: asctime ( ) and ctime ( )
6.1.3.2 Complex Time Formatting: strftime ( )
6.1.4 Converting a Broken-Down Time to a t ime_t
6.1.5 Getting Time-Zone Information
6.1.5.1 BSD Systems Gotcha: timezone (), Not timezone
6.2 Sorting and Searching Functions
6.2.1 Sorting: qsort ( )
6.2.1.1 Example: Sorting Employees
6.2.1.2 Example: Sorting Directory Contents
6.2.2 Binary Searching: bsearch ( )
6.3 User and Group Names
6.3.1 User Database
6.3.2 Group Database
6.4 Terminals: isatty ( )
6.5 Suggested Reading
6.6 Summary
Exercises
Chapter 7 Putting It All Together: ls
7.1 V7 ls Options
7.2 V7 ls Code
7.3 Summary
Exercises
Chapter 8 Filesystems and Directory Walks
8.1 Mounting and Unmounting Filesystems
8.1.1 Reviewing the Background
8.1.2 Looking at Different Filesystem Types
8.1.3 Mounting Filesystems: mount
8.1.4 Unmounting Fil, esystems: uraount:
8.2 Files for Filesystem Administration
8.2.1 Using Mount Options
8.2.2 Working with Mounted Filesystems: getmntent ( )
8.3 Retrieving Per-Filesystem Information
8.3.1 POSIX Style: statvfs ( ) and fstatvfs ( )
8.3.2 Linux Style: statvfs ( ) and fstatvfs ( )
8.4 Moving Around in the File Hierarchy
8.4.1 Changing Directory: cbdir ( ) and fchdir ( )
8.4.2 Getting the Current Directory: getcwd ( )
8.4.3 Walking a Hierarchy: nftw ( )
8.4.3.1 The rift:w() Interface
8.4.3.2 The rill:w() Callback Function
8.5 Walking a File Tree: GNU clu
8.6 Changing the Root Directory: ~larool: ( )
8.7 Summary
Exercises
PART II Processes, IPC, and Internationalization
Chapter 9 Process Management and Pipes
9.1 Process Creation and Management
9.1.1 Creating a Process: fork( )
9.1.1.1 After the fork ( ) : Shared and Distinct Attributes
9.1.1.2 File Descriptor Sharing
9.1.1.3 File Descriptor Sharing and close ( )
9.1.2 Identifying a Process: getpid ( ) and getppid ( )
9.1.3 Setting Process Priority: nice ( )
9.1.3.1 POSIX vs. Reality
9.1.4 Starting New Programs: The exec ( ) Family
9.1.4.1 The execve ( ) System Call
9.1.4.2 Wrapper Functions: execl( ) et al
9.1.4.3 Program Names and argv [ 0 ]
9.1.4.4 Attributes Inherited across exec ( )
9.1.5 Terminating a Process
9.1.5.1 Defining Process Exit Status
9.1.5.2 Returning from main ( )
9.1.5.3 Exiting Functions
9.1.6 Recovering a Child's Exit Status
9.1.6.1 Using POSIX Functions: wait; ( ) and waitpid ( )
9.1.6.2 Using BSD Functions: wait3 ( ) and wait4 ()
9.2 Process Groups
9.2.1 Job Control Overview
9.2.2 Process Group Identification: getpgrp ( ) and getpgid ( )
9.2.3 Process Group Setting: setpgid ( ) and setpgrp ( )
9.3 Basic Interprocess Communication: Pipes and FIFOs
9.3.1 Pipes
9.3.1.1 Creating Pipes
9.3.1.2 Pipe Buffering
9.3.2 FIFOs
9.4 File Descriptor Management
9.4.1 Duplicating Open Files: dup ( ) and dup2 ( )
9.4.2 Creating Nonlinear Pipelines: /dev/fd/XX
9.4.3 Managing File Attributes: fcntl ( )
9.4.3.1 The Close-on-exec Flag
9.4.3.2 File Descriptor Duplication
9.4.3.3 Manipulation of File Status Flags and Access Modes
9.4.3.4 Nonblocking I/O for Pipes and FIFOs
9.4.3.5 fcntl ( ) Summary
9.5 Example: Two-Way Pipes in gawk
9.6 Suggested Reading
9.7 Summary
Exercises
Chapter 10 Signals
10.1 Introduction
10.2 Signal Actions
10.3 Standard C Signals: signaZ ( ) and raise ( )
10.3.1 The s~gnal () Function
10.3.2 Sending Signals Programmatically: raise ( )
10.4 Signal Handlers in Action
10.4.1 Traditional Systems
10.4.2 BSD and GNU/Linux
10.4.3 Ignoring Signals
10.4.4 Restartable System Calls
10.4.4.1 Example: GNU Coreutils safe_read( ) and safe_write()
10.4.4.2 GLIBC Only: TEMP_FAILURE_RETRY ( )
10.4.5 Race Conditions and sig_atomic_t (ISO C)
10.4.6 Additional Caveats
10.4.7 Out Story So Far, Episode I
10.5 The System V Release 3 Signal APIs: sigset ( ) et al
10.6 POSIX Signals
10.6.1 Uncovering the Problem
10.6.2 Signal Sets: sigset_t and Related Functions
10.6.3 Managing the Signal Mask: sigprocmask ( ) et al
10.6.4 Catching Signals: sigaction()
10.6.5 Retrieving Pending Signals: sigpending ( )
10.6.6 Making Functions Interruptible: siginterrupt ()
10.6.7 Sending Signals: kill ( ) and killpg ( )
10.6.8 Our Story So Far, Episode II
10.7 Signals for Interprocess Communication
10.8 Important Special-Purpose Signals
10.8.1 Alarm Clocks: sleep (), alarm (), and SIGALRM
10.8.1.1 Harder but with More Control: alarm( ) and SIGALRM
10.8.1.2 Simple and Easy: sleep ( )
10.8.2 Job Control Signals
10.8.3 Parental Supervision: Three Different Strategies
10.8.3.1 Poor Parenting: Ignoring Children Completely
10.8.3.2 Permissive Parenting: Supervising Minimally
10.8.3.3 Strict Parental Control
10.9 Signals Across fork ( ) and exec ( )
10.10 Summary
Exercises
Chapter 11 Permissions and User and Group ID Numbers
11.1 Checking Permissions
11.1.1 Real and Effective IDs
11.1.2 Setuid and Setgid Bits
11.2 Retrieving User and Group IDs
11.3 Checking As the Real User: access ( )
11.4 Checking as the Effective User: euidaccess ( ) (GLIBC)
11.5 Setting Extra Permission Bits for Directories
11.5.1 Default Group for New Files and Directories
11.5.2 Directories and the Sticky Bit
11.6 Setting Real and Effective IDs
11.6.1 Changing the Group Set
11.6.2 Changing the Real and Effective IDs
11.6.3 Using the Setuid and Setgid Bits
11.7 Working with All Three IDs: getresuid ( ) and setresuid ( ) (Linux)
11.8 Crossing a Security Minefield: Setuid root
11.9 Suggested Reading
11.10 Summary
Exercises
Chapter 12 General Library InterFaces -- Part 2
12.1 Assertion Statements: assert ()
12.2 Low-Level Memory: The memXXX ( ) Functions
12.2.1 Setting Memory: memset ()
12.2.2 Copying Memory: memcpy (), memmove (), and memccpy ( )
12.2.5 Comparing Memory Blocks: memcmp ( )
12.2,4 Searching for a Byte Value: memchr ( )
12.5 Temporary Files
12.3.1 Generating Temporary Filenames (Bad)
12.5.2 Creating and Opening Temporary Files (Good)
12.3.5 Using the TMPDIR Environment Variable
12.4 Committing Suicide: abort ( )
12.5 Nonlocal Gotos
12.5.1 Using Standard Functions: setjmp ( ) and longjmp ( )
12.5.2 Handling Signal Masks: sigsetjmp ( ) and siglongjmp ( )
12.5.3 Observing Important Caveats
12.6 Pseudorandom Numbers
12.6.1 Standard C: rand ( ) and srand ( )
12.6.2 POSIX Functions: random ( ) and srandom ( )
12.6.3 The/dev/random and/dev/urandom Special Files
12.7 Metacharacter Expansions
12.7.1 Simple Pattern Matching: fnmatch ( )
12.7.2 Filename Expansion: glob ( ) and globfree ( )
12.7.3 Shell Word Expansion: wordexp ( ) and wordfree ( )
12.8 Regular Expressions
12.9 Suggested Reading
12.10 Summary
Exercises
Chapter 13 Internationalization and Localization
13.1 Introduction
13.2 Locales and the C Library
13.2.1 Locale Categories and Environment Variables
13.2.2 Setting the Locale: setlocale ()
15.2.5 String Collation: strcoll ( ) and strxfrm( )
13.2.4 Low-Level Numeric and Monetary Formatting: localeconv ( )
13.2.5 High-Level Numeric and Monetary Formatting: strfmon ( )and printf ( )
13.2.6 Example: Formatting Numeric Values in gawk
15.2.7 Formatting Date and Time Values: ctime ( ) and strftime ( )
15.2.8 Other Locale Information: nl_langinfo ( )
13.3 Dynamic Translation of Program Messages
13.3.1 Setting the Text Domain: textdomain ( )
13.3.2 Translating Messages: gettext ( )
13.3.3 Working with Plurals: ngettext ( )
13.3.4 Making gettext ( ) Easy to Use
15.5.4.1 Portable Programs: "gettext.h"
13.3.4.2 GLIBC Only: <libintl.h>
13.3.5 Rearranging Word Order with printf ( )
15.5.6 Testing Translations in a Private Directory
15.5.7 Preparing Internationalized Programs
13.5.8 Creating Translations
15.4 Can You Spell That for Me, Please?
13.4.1 Wide Characters
13.4.2 Multibyte Character Encodings
13.4.3 Languages
13.4.4 Conclusion
13.5 Suggested Reading
13.6 Summary
Exercises
Chapter14 Extended Interfaces
14.1 Allocating Aligned Memory: posix_memalign ( ) and memalign ( )
14.2 Locking Files
14.2.1 File Locking Concepts
14.2.2 POSIX Locking: fcntl ( ) and lockf ( )
14.2.2.1 Describing a Lock
14.2.2.2 Obtaining and Releasing Locks
14.2.2.3 Observing Locking Caveats
14.2.3 BSD Locking: flock()
14.2.4 Mandatory Locking
14.3 More Precise Times
14.3.1 Microsecond Times: gettimeofday( )
14.3.2 Microsecond File Times: utimes ()
14.3.3 Interval Timers: setitimer ( ) and getitimer ( )
14.3.4 More Exact Pauses: nanosleep ( )
14.4 Advanced Searching with Binary Trees
14.4.1 Introduction to Binary Trees
14.4.2 Tree Management Functions
14.4.3 Tree Insertion: tsearch()
14.4.4 Tree Lookup and Use of A Returned Pointer: tfind ( ) andtsearch ( )
14.4.5 Tree Traversal: twalk ( )
14.4.6 Tree Node Removal and Tree Deletion: tdelete ( ) and tdestroy ( )
14.5 Summary
Exercises
PART III Debugging and Final Project
Chapter 15 Debugging
15.1 First Things First
15.2 Compilation for Debugging
15.3 GDB Basics
15.3.1 Running GDB
15.3.2 Setting Breakpoints, Single-Stepping, and Setting Watchpoints
15.4 Programming for Debugging
15.4.1 Compile-Time Debugging Code
15.4.1.1 Use Debugging Macros
15.4.1.2 Avoid Expression Macros If Possible
15.4.1.3 Reorder Code If Necessary
15.4.1.4 Use Debugging Helper Functions
15.4.1.5 Avoid Unions When Possible
15.4.2 Runtime Debugging Code
15.4.2.1 Add Debugging Options and Variables
15.4.2.2 Use Special Environment Variables
15.4.2.3 Add Logging Code
15.4.2.4 Runtime Debugging Files
15.4.2.5 Add Special Hooks for Breakpoints
15.5 Debugging Tools
15.5.1 The dbug Library -- A Sophisticated print f ( )
15.5.2 Memory Allocation Debuggers
15.5.2.1 GNU/Linux retrace
15.5.2.2 Electric Fence
15.5.2.3 Debugging Malloc: dmalloc
15.5.2.4 Valgrind: A Versatile Tool
15.5.2.5 Other Malloc Debuggers
15.5.3 A Modern lint
15.6 Software Testing
15.7 Debugging Rules
15.8 Suggested Reading
15.9 Summary
Exercises
Chapter 16 A Project That Ties Everything Together
16.1 Proiect Description
16.2 Suggested Reading
PART IV Appendixes
Appendix A Teach Yourself Programming in Ten Years
Appendix B Caldera Ancient UNIX License
Appendix C GNU General Public License
Index