100% found this document useful (2 votes)
31 views

Linux Programming by Example The Fundamentals 1st Edition Robbins instant download

The document is a digital download link for 'Linux Programming by Example: The Fundamentals' by Arnold Robbins, which provides foundational knowledge for building Linux software. It includes an overview of the book's content, structure, and key topics covered, such as file management, memory management, and user-level programming. Additionally, it lists other related programming titles available for download.

Uploaded by

hansbyhernou
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
100% found this document useful (2 votes)
31 views

Linux Programming by Example The Fundamentals 1st Edition Robbins instant download

The document is a digital download link for 'Linux Programming by Example: The Fundamentals' by Arnold Robbins, which provides foundational knowledge for building Linux software. It includes an overview of the book's content, structure, and key topics covered, such as file management, memory management, and user-level programming. Additionally, it lists other related programming titles available for download.

Uploaded by

hansbyhernou
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 48

Linux Programming by Example The Fundamentals

1st Edition Robbins pdf download

https://ebookfinal.com/download/linux-programming-by-example-the-
fundamentals-1st-edition-robbins/

Explore and download more ebooks or textbooks


at ebookfinal.com
Here are some recommended products for you. Click the link to
download, or explore more at ebookfinal

Access 2002 Programming by Example 1st Edition Bob


Villareal

https://ebookfinal.com/download/access-2002-programming-by-
example-1st-edition-bob-villareal/

Your Wish is My Command Programming by Example 1st Edition


Henry Lieberman

https://ebookfinal.com/download/your-wish-is-my-command-programming-
by-example-1st-edition-henry-lieberman/

Professional Linux Programming 1st Edition Jon Masters

https://ebookfinal.com/download/professional-linux-programming-1st-
edition-jon-masters/

Linux System Programming 1st Edition Robert Love

https://ebookfinal.com/download/linux-system-programming-1st-edition-
robert-love/
Django By Example 1st Edition Antonio Melé

https://ebookfinal.com/download/django-by-example-1st-edition-antonio-
mele/

Version Control by Example 1st Edition Eric Sink

https://ebookfinal.com/download/version-control-by-example-1st-
edition-eric-sink/

Beginning Linux programming 4th Edition Neil Matthew

https://ebookfinal.com/download/beginning-linux-programming-4th-
edition-neil-matthew/

Beginning Linux programming 3rd Edition Neil Matthew

https://ebookfinal.com/download/beginning-linux-programming-3rd-
edition-neil-matthew/

SAS Functions by Example Second Edition Ron Cody

https://ebookfinal.com/download/sas-functions-by-example-second-
edition-ron-cody/
Linux Programming by Example The Fundamentals 1st
Edition Robbins Digital Instant Download
Author(s): Robbins, Arnold
ISBN(s): 9780131429642, 0131429647
Edition: 1
File Details: PDF, 3.31 MB
Year: 2004
Language: english
Linux Programming
by Example

Arnold Robbins

PRENTICE HALL
Professional Technical Reference
Upper Saddle River, NJ 07458
www.phptr.com
Prentice Hall
Open Source Software Development Series
Arnold Robbins, Series Editor

“Real world code from real world applications”

Open Source technology has revolutionized the computing world. Many large-scale projects are
in production use worldwide, such as Apache, MySQL, and Postgres, with programmers writing
applications in a variety of languages including Perl, Python, and PHP. These technologies are in
use on many different systems, ranging from proprietary systems, to Linux systems, to traditional
UNIX systems, to mainframes.
The Prentice Hall Open Source Software Development Series is designed to bring you the
best of these Open Source technologies. Not only will you learn how to use them for your
projects, but you will learn from them. By seeing real code from real applications, you will learn
the best practices of Open Source developers the world over.

Titles currently in the series include:


Linux® Debugging and Performance Tuning: Tips and Techniques
Steve Best
0131492470, Paper, 10/14/2005
The book is not only a high-level strategy guide but also a book that combines strategy with
hands-on debugging sessions and performance tuning tools and techniques.
Linux Programming by Example: The Fundamentals
Arnold Robbins
0131429647, Paper, 4/12/2004
Gradually, one step at a time, Robbins teaches both high level principles and “under the hood”
techniques. This book will help the reader master the fundamentals needed to build serious
Linux software.
The Linux® Kernel Primer: A Top-Down Approach for x86 and PowerPC Architectures
Claudia Salzberg, Gordon Fischer, Steven Smolski
0131181637, Paper, 9/21/2005
A comprehensive view of the Linux Kernel is presented in a top down approach—the big picture
first with a clear view of all components, how they interrelate, and where the hardware/software
separation exists. The coverage of both the x86 and the PowerPC is unique to this book.
© 2004 Pearson Education, Inc.
Publishing as Prentice Hall Professional Technical Reference
Upper Saddle River, New Jersey 07458

Prentice Hall PTR offers discounts on this book when ordered in quantity for bulk purchases or special sales. For
more information, please contact: U.S. Corporate and Government Sales, 1-800-382-3419,
[email protected]. For sales outside of the United States, please contact: International Sales,
1-317-581-3793, [email protected].

Portions of Chapter 1, Copyright © 1994 Arnold David Robbins, first appeared in an article in Issue 16 of Linux
Journal, reprinted by permission.
Portions of the documentation for Valgrind, Copyright © 2003 Julian Seward, reprinted by permission.
Portions of the documentation for the DBUG library, by Fred N. Fish, reprinted by permission.
The GNU programs in this book are Copyright © 1985-2003, Free Software Foundation, Inc.. The full list of files
and copyright dates is provided in the Preface. Each program is “free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by the Free Software Foundation; either version
2 of the License, or (at your option) any later version.” Appendix C of this book provides the text of the GNU
General Public License.
All V7 Unix code and documentation are Copyright © Caldera International Inc. 2001-2002. All rights reserved.
They are reprinted here under the terms of the Caldera Ancient UNIX License, which is reproduced in full in
Appendix B.
Cover image courtesy of Parks Sabers, Inc. The Arc-Wave(tm) saber is manufactured by Parks Sabers, Inc., Copyright
© 2001, www.parksabers.com. Parks Sabers is not associated with any Lucasfilm Ltd. property, film, or franchise.
The programs and applications presented in this book have been included for their instructional value. They have
been tested with care but are not guaranteed for any particular purpose. The publisher does not offer any warranties
or representations, nor does it accept any liabilities with respect to the programs or applications. UNIX is a registered
trademark of The Open Group in the United States and other countries.
Microsoft, MS, and MS-DOS are registered trademarks, and Windows is a trademark of Microsoft Corporation in
the United States and other countries. Linux is a registered trademark of Linux Torvalds.
All company and product names mentioned herein are the trademarks or registered trademarks of their respective
owners.
This material may be distributed only subject to the terms and conditions set forth in the Open Publication License,
v1.0 or later (the latest version is presently available at http://www.opencontent.org/openpub/), with License
Option B.
Printed in the United States of America
ISBN 0-13-142964-7
Text printed on recycled paper
First printing
Pearson Education LTD.
Pearson Education Australia PTY, Limited
Pearson Education Singapore, Pte. Ltd.
Pearson Education North Asia Ltd.
Pearson Education Canada, Ltd.
Pearson Educación de Mexico, S.A. de C.V.
Pearson Education—Japan
Pearson Education Malaysia, Ptd. Ltd.
To my wife Miriam,
and my children,
Chana, Rivka, Nachum, and Malka.
Contents

Preface ........................................................................................................... xvii

PART I Files and Users ............................................ 1


Chapter 1 Introduction .............................................................................. 3
1.1 The Linux/Unix File Model ............................................................................. 4
1.1.1 Files and Permissions ................................................................................ 4
1.1.2 Directories and Filenames ........................................................................ 6
1.1.3 Executable Files ........................................................................................ 7
1.1.4 Devices ..................................................................................................... 9
1.2 The Linux/Unix Process Model ........................................................................ 10
1.2.1 Pipes: Hooking Processes Together .......................................................... 12
1.3 Standard C vs. Original C ................................................................................ 12
1.4 Why GNU Programs Are Better ....................................................................... 14
1.4.1 Program Design ....................................................................................... 15
1.4.2 Program Behavior ..................................................................................... 16
1.4.3 C Code Programming .............................................................................. 16
1.4.4 Things That Make a GNU Program Better .............................................. 17
1.4.5 Parting Thoughts about the “GNU Coding Standards” ........................... 19
1.5 Portability Revisited ......................................................................................... 19
1.6 Suggested Reading ............................................................................................ 20
1.7 Summary .......................................................................................................... 21
Exercises ...................................................................................................................... 22
Chapter 2 Arguments, Options, and the Environment ................................ 23
2.1 Option and Argument Conventions ................................................................. 24
2.1.1 POSIX Conventions ................................................................................. 25
2.1.2 GNU Long Options ................................................................................. 27

v
vi Contents

2.2 Basic Command-Line Processing ...................................................................... 28


2.2.1 The V7 echo Program ............................................................................ 29
2.3 Option Parsing: getopt() and getopt_long() ....................................... 30
2.3.1 Single-Letter Options ............................................................................... 30
2.3.2 GNU getopt() and Option Ordering .................................................. 33
2.3.3 Long Options ........................................................................................... 34
2.3.3.1 Long Options Table ........................................................................... 34
2.3.3.2 Long Options, POSIX Style ............................................................... 37
2.3.3.3 getopt_long() Return Value Summary ........................................ 37
2.3.3.4 GNU getopt() or getopt_long() in User Programs ................ 39
2.4 The Environment ............................................................................................. 40
2.4.1 Environment Management Functions ...................................................... 41
2.4.2 The Entire Environment: environ ........................................................ 43
2.4.3 GNU env ................................................................................................ 44
2.5 Summary .......................................................................................................... 49
Exercises ...................................................................................................................... 50
Chapter 3 User-Level Memory Management ............................................... 51
3.1 Linux/Unix Address Space ................................................................................ 52
3.2 Memory Allocation .......................................................................................... 56
3.2.1 Library Calls: malloc(), calloc(), realloc(), free() .............. 56
3.2.1.1 Examining C Language Details ........................................................... 57
3.2.1.2 Initially Allocating Memory: malloc() ........................................... 58
3.2.1.3 Releasing Memory: free() .............................................................. 60
3.2.1.4 Changing Size: realloc() .............................................................. 62
3.2.1.5 Allocating and Zero-filling: calloc() .............................................. 65
3.2.1.6 Summarizing from the GNU Coding Standards .................................. 66
3.2.1.7 Using Private Allocators ...................................................................... 67
3.2.1.8 Example: Reading Arbitrarily Long Lines ........................................... 67
3.2.1.9 GLIBC Only: Reading Entire Lines: getline() and getdelim() . 73
3.2.2 String Copying: strdup() ..................................................................... 74
3.2.3 System Calls: brk() and sbrk() .......................................................... 75
3.2.4 Lazy Programmer Calls: alloca() ........................................................ 76
3.2.5 Address Space Examination ...................................................................... 78
3.3 Summary .......................................................................................................... 80
Exercises ...................................................................................................................... 81
Contents vii

Chapter 4 Files and File I/O ....................................................................... 83


4.1 Introducing the Linux/Unix I/O Model ........................................................... 84
4.2 Presenting a Basic Program Structure ............................................................... 84
4.3 Determining What Went Wrong ..................................................................... 86
4.3.1 Values for errno ..................................................................................... 87
4.3.2 Error Message Style .................................................................................. 90
4.4 Doing Input and Output ................................................................................. 91
4.4.1 Understanding File Descriptors ................................................................ 92
4.4.2 Opening and Closing Files ....................................................................... 93
4.4.2.1 Mapping FILE * Variables to File Descriptors ................................. 95
4.4.2.2 Closing All Open Files ........................................................................ 96
4.4.3 Reading and Writing ................................................................................ 96
4.4.4 Example: Unix cat .................................................................................. 99
4.5 Random Access: Moving Around within a File ................................................. 102
4.6 Creating Files ................................................................................................... 106
4.6.1 Specifying Initial File Permissions ............................................................ 106
4.6.2 Creating Files with creat() .................................................................. 109
4.6.3 Revisiting open() ................................................................................... 110
4.7 Forcing Data to Disk ........................................................................................ 113
4.8 Setting File Length ........................................................................................... 114
4.9 Summary .......................................................................................................... 115
Exercises ...................................................................................................................... 115
Chapter 5 Directories and File Metadata .................................................... 117
5.1 Considering Directory Contents ....................................................................... 118
5.1.1 Definitions ............................................................................................... 118
5.1.2 Directory Contents ................................................................................... 120
5.1.3 Hard Links ............................................................................................... 122
5.1.3.1 The GNU link Program .................................................................. 123
5.1.3.2 Dot and Dot-Dot ............................................................................... 125
5.1.4 File Renaming .......................................................................................... 125
5.1.5 File Removal ............................................................................................ 126
5.1.5.1 Removing Open Files ......................................................................... 127
5.1.5.2 Using ISO C: remove() .................................................................. 127
5.1.6 Symbolic Links ......................................................................................... 128
viii Contents

5.2 Creating and Removing Directories .................................................................. 130


5.3 Reading Directories .......................................................................................... 132
5.3.1 Basic Directory Reading ........................................................................... 133
5.3.1.1 Portability Considerations .................................................................. 136
5.3.1.2 Linux and BSD Directory Entries ....................................................... 137
5.3.2 BSD Directory Positioning Functions ...................................................... 138
5.4 Obtaining Information about Files ................................................................... 139
5.4.1 Linux File Types ....................................................................................... 139
5.4.2 Retrieving File Information ...................................................................... 141
5.4.3 Linux Only: Specifying Higher-Precision File Times ................................ 143
5.4.4 Determining File Type ............................................................................. 144
5.4.4.1 Device Information ............................................................................ 147
5.4.4.2 The V7 cat Revisited ........................................................................ 150
5.4.5 Working with Symbolic Links .................................................................. 151
5.5 Changing Ownership, Permission, and Modification Times ............................. 155
5.5.1 Changing File Ownership: chown(), fchown(), and lchown() ....... 155
5.5.2 Changing Permissions: chmod() and fchmod() .................................. 156
5.5.3 Changing Timestamps: utime() ............................................................ 157
5.5.3.1 Faking utime(file, NULL) ......................................................... 159
5.5.4 Using fchown() and fchmod() for Security ....................................... 161
5.6 Summary .......................................................................................................... 162
Exercises ...................................................................................................................... 163
Chapter 6 General Library Interfaces — Part 1 ............................................ 165
6.1 Times and Dates .............................................................................................. 166
6.1.1 Retrieving the Current Time: time() and difftime() ...................... 167
6.1.2 Breaking Down Times: gmtime() and localtime() ......................... 168
6.1.3 Formatting Dates and Times .................................................................... 170
6.1.3.1 Simple Time Formatting: asctime() and ctime() ...................... 170
6.1.3.2 Complex Time Formatting: strftime() ........................................ 171
6.1.4 Converting a Broken-Down Time to a time_t ...................................... 176
6.1.5 Getting Time-Zone Information .............................................................. 178
6.1.5.1 BSD Systems Gotcha: timezone(), Not timezone ...................... 179
6.2 Sorting and Searching Functions ...................................................................... 181
6.2.1 Sorting: qsort() ................................................................................... 181
Contents ix

6.2.1.1 Example: Sorting Employees .............................................................. 183


6.2.1.2 Example: Sorting Directory Contents ................................................. 188
6.2.2 Binary Searching: bsearch() ................................................................ 191
6.3 User and Group Names .................................................................................... 195
6.3.1 User Database .......................................................................................... 196
6.3.2 Group Database ....................................................................................... 199
6.4 Terminals: isatty() ..................................................................................... 202
6.5 Suggested Reading ............................................................................................ 203
6.6 Summary .......................................................................................................... 203
Exercises ...................................................................................................................... 205
Chapter 7 Putting It All Together: ls ......................................................... 207
7.1 V7 ls Options ................................................................................................. 208
7.2 V7 ls Code ..................................................................................................... 209
7.3 Summary .......................................................................................................... 225
Exercises ...................................................................................................................... 226
Chapter 8 Filesystems and Directory Walks ................................................ 227
8.1 Mounting and Unmounting Filesystems .......................................................... 228
8.1.1 Reviewing the Background ....................................................................... 228
8.1.2 Looking at Different Filesystem Types ..................................................... 232
8.1.3 Mounting Filesystems: mount ................................................................. 236
8.1.4 Unmounting Filesystems: umount .......................................................... 237
8.2 Files for Filesystem Administration ................................................................... 238
8.2.1 Using Mount Options .............................................................................. 239
8.2.2 Working with Mounted Filesystems: getmntent() .............................. 241
8.3 Retrieving Per-Filesystem Information ............................................................. 244
8.3.1 POSIX Style: statvfs() and fstatvfs() ........................................ 244
8.3.2 Linux Style: statfs() and fstatfs() ............................................... 252
8.4 Moving Around in the File Hierarchy .............................................................. 256
8.4.1 Changing Directory: chdir() and fchdir() ...................................... 256
8.4.2 Getting the Current Directory: getcwd() ............................................. 258
8.4.3 Walking a Hierarchy: nftw() ................................................................. 260
8.4.3.1 The nftw() Interface ....................................................................... 261
8.4.3.2 The nftw() Callback Function ........................................................ 263
x Contents

8.5 Walking a File Tree: GNU du ......................................................................... 269


8.6 Changing the Root Directory: chroot() ....................................................... 276
8.7 Summary .......................................................................................................... 277
Exercises ...................................................................................................................... 278

PART II Processes, IPC, and Internationalization ....... 281


Chapter 9 Process Management and Pipes ................................................. 283
9.1 Process Creation and Management ................................................................... 284
9.1.1 Creating a Process: fork() ..................................................................... 284
9.1.1.1 After the fork(): Shared and Distinct Attributes ............................. 285
9.1.1.2 File Descriptor Sharing ....................................................................... 286
9.1.1.3 File Descriptor Sharing and close() ............................................... 288
9.1.2 Identifying a Process: getpid() and getppid() ................................ 289
9.1.3 Setting Process Priority: nice() ............................................................. 291
9.1.3.1 POSIX vs. Reality ............................................................................... 293
9.1.4 Starting New Programs: The exec() Family .......................................... 293
9.1.4.1 The execve() System Call .............................................................. 294
9.1.4.2 Wrapper Functions: execl() et al. .................................................. 295
9.1.4.3 Program Names and argv[0] .......................................................... 297
9.1.4.4 Attributes Inherited across exec() ................................................... 298
9.1.5 Terminating a Process .............................................................................. 300
9.1.5.1 Defining Process Exit Status ............................................................... 300
9.1.5.2 Returning from main() .................................................................... 301
9.1.5.3 Exiting Functions ............................................................................... 302
9.1.6 Recovering a Child’s Exit Status ............................................................... 305
9.1.6.1 Using POSIX Functions: wait() and waitpid() ......................... 306
9.1.6.2 Using BSD Functions: wait3() and wait4() ............................... 310
9.2 Process Groups ................................................................................................. 312
9.2.1 Job Control Overview .............................................................................. 312
9.2.2 Process Group Identification: getpgrp() and getpgid() .................. 314
9.2.3 Process Group Setting: setpgid() and setpgrp() ............................ 314
9.3 Basic Interprocess Communication: Pipes and FIFOs ...................................... 315
9.3.1 Pipes ......................................................................................................... 315
9.3.1.1 Creating Pipes .................................................................................... 316
9.3.1.2 Pipe Buffering .................................................................................... 318
Contents xi

9.3.2 FIFOs ....................................................................................................... 319


9.4 File Descriptor Management ............................................................................ 320
9.4.1 Duplicating Open Files: dup() and dup2() ......................................... 321
9.4.2 Creating Nonlinear Pipelines: /dev/fd/XX ........................................... 326
9.4.3 Managing File Attributes: fcntl() ........................................................ 328
9.4.3.1 The Close-on-exec Flag ...................................................................... 329
9.4.3.2 File Descriptor Duplication ................................................................ 331
9.4.3.3 Manipulation of File Status Flags and Access Modes .......................... 332
9.4.3.4 Nonblocking I/O for Pipes and FIFOs ............................................... 333
9.4.3.5 fcntl() Summary ........................................................................... 336
9.5 Example: Two-Way Pipes in gawk .................................................................. 337
9.6 Suggested Reading ............................................................................................ 341
9.7 Summary .......................................................................................................... 342
Exercises ...................................................................................................................... 344
Chapter 10 Signals ....................................................................................... 347
10.1 Introduction ..................................................................................................... 348
10.2 Signal Actions ................................................................................................... 348
10.3 Standard C Signals: signal() and raise() ............................................... 349
10.3.1 The signal() Function ........................................................................ 349
10.3.2 Sending Signals Programmatically: raise() .......................................... 353
10.4 Signal Handlers in Action ................................................................................. 353
10.4.1 Traditional Systems .................................................................................. 353
10.4.2 BSD and GNU/Linux .............................................................................. 356
10.4.3 Ignoring Signals ....................................................................................... 356
10.4.4 Restartable System Calls ........................................................................... 357
10.4.4.1 Example: GNU Coreutils safe_read() and safe_write() ...... 359
10.4.4.2 GLIBC Only: TEMP_FAILURE_RETRY() ....................................... 360
10.4.5 Race Conditions and sig_atomic_t (ISO C) ...................................... 361
10.4.6 Additional Caveats ................................................................................... 363
10.4.7 Our Story So Far, Episode I ..................................................................... 363
10.5 The System V Release 3 Signal APIs: sigset() et al. .................................... 365
10.6 POSIX Signals .................................................................................................. 367
10.6.1 Uncovering the Problem .......................................................................... 367
10.6.2 Signal Sets: sigset_t and Related Functions ........................................ 368
xii Contents

10.6.3 Managing the Signal Mask: sigprocmask() et al. ............................... 369


10.6.4 Catching Signals: sigaction() ............................................................ 370
10.6.5 Retrieving Pending Signals: sigpending() .......................................... 375
10.6.6 Making Functions Interruptible: siginterrupt() ............................. 376
10.6.7 Sending Signals: kill() and killpg() ............................................... 376
10.6.8 Our Story So Far, Episode II .................................................................... 378
10.7 Signals for Interprocess Communication .......................................................... 379
10.8 Important Special-Purpose Signals ................................................................... 382
10.8.1 Alarm Clocks: sleep(), alarm(), and SIGALRM ............................... 382
10.8.1.1 Harder but with More Control: alarm() and SIGALRM ................. 382
10.8.1.2 Simple and Easy: sleep() ................................................................ 383
10.8.2 Job Control Signals .................................................................................. 383
10.8.3 Parental Supervision: Three Different Strategies ....................................... 385
10.8.3.1 Poor Parenting: Ignoring Children Completely .................................. 385
10.8.3.2 Permissive Parenting: Supervising Minimally .................................... 386
10.8.3.3 Strict Parental Control ........................................................................ 393
10.9 Signals Across fork() and exec() ............................................................... 398
10.10 Summary .......................................................................................................... 399
Exercises ...................................................................................................................... 401
Chapter 11 Permissions and User and Group ID Numbers ........................... 403
11.1 Checking Permissions ....................................................................................... 404
11.1.1 Real and Effective IDs .............................................................................. 405
11.1.2 Setuid and Setgid Bits .............................................................................. 406
11.2 Retrieving User and Group IDs ........................................................................ 407
11.3 Checking As the Real User: access() ........................................................... 410
11.4 Checking as the Effective User: euidaccess() (GLIBC) ............................. 412
11.5 Setting Extra Permission Bits for Directories .................................................... 412
11.5.1 Default Group for New Files and Directories ........................................... 412
11.5.2 Directories and the Sticky Bit ................................................................... 414
11.6 Setting Real and Effective IDs .......................................................................... 415
11.6.1 Changing the Group Set .......................................................................... 416
11.6.2 Changing the Real and Effective IDs ........................................................ 416
11.6.3 Using the Setuid and Setgid Bits .............................................................. 419
11.7 Working with All Three IDs: getresuid() and setresuid() (Linux) .... 421
Contents xiii

11.8 Crossing a Security Minefield: Setuid root ..................................................... 422


11.9 Suggested Reading ............................................................................................ 423
11.10 Summary .......................................................................................................... 424
Exercises ...................................................................................................................... 426
Chapter 12 General Library Interfaces — Part 2 ............................................ 427
12.1 Assertion Statements: assert() .................................................................... 428
12.2 Low-Level Memory: The memXXX() Functions .............................................. 432
12.2.1 Setting Memory: memset() ................................................................... 432
12.2.2 Copying Memory: memcpy(), memmove(), and memccpy() ............. 433
12.2.3 Comparing Memory Blocks: memcmp() ................................................. 434
12.2.4 Searching for a Byte Value: memchr() .................................................... 435
12.3 Temporary Files ............................................................................................... 436
12.3.1 Generating Temporary Filenames (Bad) ................................................... 437
12.3.2 Creating and Opening Temporary Files (Good) ....................................... 441
12.3.3 Using the TMPDIR Environment Variable ................................................ 443
12.4 Committing Suicide: abort() ....................................................................... 445
12.5 Nonlocal Gotos ................................................................................................ 446
12.5.1 Using Standard Functions: setjmp() and longjmp() ........................ 447
12.5.2 Handling Signal Masks: sigsetjmp() and siglongjmp() ............... 449
12.5.3 Observing Important Caveats ................................................................... 450
12.6 Pseudorandom Numbers .................................................................................. 454
12.6.1 Standard C: rand() and srand() ........................................................ 455
12.6.2 POSIX Functions: random() and srandom() ..................................... 457
12.6.3 The /dev/random and /dev/urandom Special Files .......................... 460
12.7 Metacharacter Expansions ................................................................................ 461
12.7.1 Simple Pattern Matching: fnmatch() ................................................... 462
12.7.2 Filename Expansion: glob() and globfree() ................................... 464
12.7.3 Shell Word Expansion: wordexp() and wordfree() ......................... 470
12.8 Regular Expressions .......................................................................................... 471
12.9 Suggested Reading ............................................................................................ 480
12.10 Summary .......................................................................................................... 481
Exercises ...................................................................................................................... 482
xiv Contents

Chapter 13 Internationalization and Localization .......................................... 485


13.1 Introduction ..................................................................................................... 486
13.2 Locales and the C Library ................................................................................. 487
13.2.1 Locale Categories and Environment Variables .......................................... 487
13.2.2 Setting the Locale: setlocale() .......................................................... 489
13.2.3 String Collation: strcoll() and strxfrm() ..................................... 490
13.2.4 Low-Level Numeric and Monetary Formatting: localeconv() ........... 494
13.2.5 High-Level Numeric and Monetary Formatting: strfmon()
and printf() ........................................................................................ 498
13.2.6 Example: Formatting Numeric Values in gawk ....................................... 501
13.2.7 Formatting Date and Time Values: ctime() and strftime() ........... 503
13.2.8 Other Locale Information: nl_langinfo() ......................................... 504
13.3 Dynamic Translation of Program Messages ...................................................... 507
13.3.1 Setting the Text Domain: textdomain() ............................................. 507
13.3.2 Translating Messages: gettext() ......................................................... 508
13.3.3 Working with Plurals: ngettext() ....................................................... 509
13.3.4 Making gettext() Easy to Use ............................................................ 510
13.3.4.1 Portable Programs: "gettext.h" ................................................... 511
13.3.4.2 GLIBC Only: <libintl.h> ........................................................... 513
13.3.5 Rearranging Word Order with printf() .............................................. 514
13.3.6 Testing Translations in a Private Directory .............................................. 515
13.3.7 Preparing Internationalized Programs ....................................................... 516
13.3.8 Creating Translations ............................................................................... 517
13.4 Can You Spell That for Me, Please? .................................................................. 521
13.4.1 Wide Characters ....................................................................................... 523
13.4.2 Multibyte Character Encodings ................................................................ 523
13.4.3 Languages ................................................................................................. 524
13.4.4 Conclusion ............................................................................................... 525
13.5 Suggested Reading ............................................................................................ 526
13.6 Summary .......................................................................................................... 526
Exercises ...................................................................................................................... 527
Chapter 14 Extended Interfaces ................................................................... 529
14.1 Allocating Aligned Memory: posix_memalign() and memalign() ......... 530
14.2 Locking Files .................................................................................................... 531
Contents xv

14.2.1 File Locking Concepts .............................................................................. 531


14.2.2 POSIX Locking: fcntl() and lockf() .............................................. 533
14.2.2.1 Describing a Lock ............................................................................... 533
14.2.2.2 Obtaining and Releasing Locks ........................................................... 536
14.2.2.3 Observing Locking Caveats ................................................................ 538
14.2.3 BSD Locking: flock() .......................................................................... 539
14.2.4 Mandatory Locking .................................................................................. 540
14.3 More Precise Times .......................................................................................... 543
14.3.1 Microsecond Times: gettimeofday() ................................................ 544
14.3.2 Microsecond File Times: utimes() ....................................................... 545
14.3.3 Interval Timers: setitimer() and getitimer() ............................. 546
14.3.4 More Exact Pauses: nanosleep() ......................................................... 550
14.4 Advanced Searching with Binary Trees ............................................................. 551
14.4.1 Introduction to Binary Trees .................................................................... 551
14.4.2 Tree Management Functions .................................................................... 554
14.4.3 Tree Insertion: tsearch() .................................................................... 554
14.4.4 Tree Lookup and Use of A Returned Pointer: tfind() and
tsearch() ............................................................................................ 555
14.4.5 Tree Traversal: twalk() ......................................................................... 557
14.4.6 Tree Node Removal and Tree Deletion: tdelete() and tdestroy() . 561
14.5 Summary .......................................................................................................... 562
Exercises ...................................................................................................................... 563

PART III Debugging and Final Project ........................ 565


Chapter 15 Debugging ................................................................................. 567
15.1 First Things First .............................................................................................. 568
15.2 Compilation for Debugging ............................................................................. 569
15.3 GDB Basics ...................................................................................................... 570
15.3.1 Running GDB ......................................................................................... 571
15.3.2 Setting Breakpoints, Single-Stepping, and Setting Watchpoints ............... 574
15.4 Programming for Debugging ............................................................................ 577
15.4.1 Compile-Time Debugging Code .............................................................. 577
15.4.1.1 Use Debugging Macros ...................................................................... 577
15.4.1.2 Avoid Expression Macros If Possible ................................................... 580
15.4.1.3 Reorder Code If Necessary ................................................................. 582
xvi Contents

15.4.1.4 Use Debugging Helper Functions ....................................................... 584


15.4.1.5 Avoid Unions When Possible ............................................................. 591
15.4.2 Runtime Debugging Code ....................................................................... 595
15.4.2.1 Add Debugging Options and Variables .............................................. 595
15.4.2.2 Use Special Environment Variables .................................................... 597
15.4.2.3 Add Logging Code ............................................................................. 601
15.4.2.4 Runtime Debugging Files ................................................................... 602
15.4.2.5 Add Special Hooks for Breakpoints .................................................... 603
15.5 Debugging Tools .............................................................................................. 605
15.5.1 The dbug Library — A Sophisticated printf() ................................... 606
15.5.2 Memory Allocation Debuggers ................................................................. 612
15.5.2.1 GNU/Linux mtrace ......................................................................... 613
15.5.2.2 Electric Fence ..................................................................................... 614
15.5.2.3 Debugging Malloc: dmalloc ............................................................ 619
15.5.2.4 Valgrind: A Versatile Tool .................................................................. 623
15.5.2.5 Other Malloc Debuggers .................................................................... 629
15.5.3 A Modern lint ...................................................................................... 631
15.6 Software Testing ............................................................................................... 632
15.7 Debugging Rules .............................................................................................. 633
15.8 Suggested Reading ............................................................................................ 637
15.9 Summary .......................................................................................................... 638
Exercises ...................................................................................................................... 639
Chapter 16 A Project That Ties Everything Together .................................... 641
16.1 Project Description ........................................................................................... 642
16.2 Suggested Reading ............................................................................................ 644

PART IV Appendixes ................................................. 647


Appendix A Teach Yourself Programming in Ten Years ................................. 649
Appendix B Caldera Ancient UNIX License .................................................... 655
Appendix C GNU General Public License ....................................................... 657
Index .............................................................................................................. 667
Preface

ne of the best ways to learn about programming is to read well-written pro-


O grams. This book teaches the fundamental Linux system call APIs—those
that form the core of any significant program—by presenting code from production
programs that you use every day.
By looking at concrete programs, you can not only see how to use the Linux APIs,
but you also can examine the real-world issues (performance, portability, robustness)
that arise in writing software.
While the book’s title is Linux Programming by Example, everything we cover, unless
otherwise noted, applies to modern Unix systems as well. In general we use “Linux”
to mean the Linux kernel, and “GNU/Linux” to mean the total system (kernel, li-
braries, tools). Also, we often say “Linux” when we mean all of Linux, GNU/Linux
and Unix; if something is specific to one system or the other, we mention it explicitly.

Audience
This book is intended for the person who understands programming and is familiar
with the basics of C, at least on the level of The C Programming Language by Kernighan
and Ritchie. (Java programmers wishing to read this book should understand C pointers,
since C code makes heavy use of them.) The examples use both the 1990 version of
Standard C and Original C.
In particular, you should be familiar with all C operators, control-flow structures,
variable and pointer declarations and use, the string management functions, the use of
exit(), and the <stdio.h> suite of functions for file input/output.
You should understand the basic concepts of standard input, standard output, and
standard error and the fact that all C programs receive an array of character strings
representing invocation options and arguments. You should also be familiar with the
fundamental command-line tools, such as cd, cp, date, ln, ls, man (and info if you

xvii
xviii Preface

have it), rmdir, and rm, the use of long and short command-line options, environment
variables, and I/O redirection, including pipes.
We assume that you want to write programs that work not just under GNU/Linux
but across the range of Unix systems. To that end, we mark each interface as to its
availability (GLIBC systems only, or defined by POSIX, and so on), and portability
advice is included as an integral part of the text.
The programming taught here may be at a lower level than you’re used to; that’s
OK. The system calls are the fundamental building blocks for higher operations and
are thus low-level by nature. This in turn dictates our use of C: The APIs were designed
for use from C, and code that interfaces them to higher-level languages, such as C++
and Java, will necessarily be lower level in nature, and most likely, written in C. It may
help to remember that “low level” doesn’t mean “bad,” it just means “more challenging.”

What You Will Learn


This book focuses on the basic APIs that form the core of Linux programming:
• Memory management
• File input/output
• File metadata
• Processes and signals
• Users and groups
• Programming support (sorting, argument parsing, and so on)
• Internationalization
• Debugging

We have purposely kept the list of topics short. We believe that it is intimidating to
try to learn “all there is to know” from a single book. Most readers prefer smaller, more
focused books, and the best Unix books are all written that way.
So, instead of a single giant tome, we plan several volumes: one on Interprocess
Communication (IPC) and networking, and another on software development and
code portability. We also have an eye toward possible additional volumes in a Linux
Preface xix

Programming by Example series that will cover topics such as thread programming and
GUI programming.
The APIs we cover include both system calls and library functions. Indeed, at the C
level, both appear as simple function calls. A system call is a direct request for system
services, such as reading or writing a file or creating a process. A library function, on the
other hand, runs at the user level, possibly never requesting any services from the oper-
ating system. System calls are documented in section 2 of the reference manual (viewable
online with the man command), and library functions are documented in section 3.
Our goal is to teach you the use of the Linux APIs by example: in particular, through
the use, wherever possible, of both original Unix source code and the GNU utilities.
Unfortunately, there aren’t as many self-contained examples as we thought there’d be.
Thus, we have written numerous small demonstration programs as well. We stress
programming principles: especially those aspects of GNU programming, such as “no
arbitrary limits,” that make the GNU utilities into exceptional programs.
The choice of everyday programs to study is deliberate. If you’ve been using
GNU/Linux for any length of time, you already understand what programs such as ls
and cp do; it then becomes easy to dive straight into how the programs work, without
having to spend a lot of time learning what they do.
Occasionally, we present both higher-level and lower-level ways of doing things.
Usually the higher-level standard interface is implemented in terms of the lower-level
interface or construct. We hope that such views of what’s “under the hood” will help
you understand how things work; for all the code you write, you should always use the
higher-level, standard interface.
Similarly, we sometimes introduce functions that provide certain functionality and
then recommend (with a provided reason) that these functions be avoided! The primary
reason for this approach is so that you’ll be able to recognize these functions when you
see them and thus understand the code using them. A well-rounded knowledge of a
topic requires understanding not just what you can do, but what you should and should
not do.
Finally, each chapter concludes with exercises. Some involve modifying or writing
code. Others are more in the category of “thought experiments” or “why do you
think …” We recommend that you do all of them—they will help cement your under-
standing of the material.
Discovering Diverse Content Through
Random Scribd Documents
INCLUDING BUT NOT LIMITED TO WARRANTIES OF
MERCHANTABILITY OR FITNESS FOR ANY PURPOSE.

1.F.5. Some states do not allow disclaimers of certain implied


warranties or the exclusion or limitation of certain types of damages.
If any disclaimer or limitation set forth in this agreement violates the
law of the state applicable to this agreement, the agreement shall be
interpreted to make the maximum disclaimer or limitation permitted
by the applicable state law. The invalidity or unenforceability of any
provision of this agreement shall not void the remaining provisions.

1.F.6. INDEMNITY - You agree to indemnify and hold the Foundation,


the trademark owner, any agent or employee of the Foundation,
anyone providing copies of Project Gutenberg™ electronic works in
accordance with this agreement, and any volunteers associated with
the production, promotion and distribution of Project Gutenberg™
electronic works, harmless from all liability, costs and expenses,
including legal fees, that arise directly or indirectly from any of the
following which you do or cause to occur: (a) distribution of this or
any Project Gutenberg™ work, (b) alteration, modification, or
additions or deletions to any Project Gutenberg™ work, and (c) any
Defect you cause.

Section 2. Information about the Mission


of Project Gutenberg™
Project Gutenberg™ is synonymous with the free distribution of
electronic works in formats readable by the widest variety of
computers including obsolete, old, middle-aged and new computers.
It exists because of the efforts of hundreds of volunteers and
donations from people in all walks of life.

Volunteers and financial support to provide volunteers with the


assistance they need are critical to reaching Project Gutenberg™’s
goals and ensuring that the Project Gutenberg™ collection will
remain freely available for generations to come. In 2001, the Project
Gutenberg Literary Archive Foundation was created to provide a
secure and permanent future for Project Gutenberg™ and future
generations. To learn more about the Project Gutenberg Literary
Archive Foundation and how your efforts and donations can help,
see Sections 3 and 4 and the Foundation information page at
www.gutenberg.org.

Section 3. Information about the Project


Gutenberg Literary Archive Foundation
The Project Gutenberg Literary Archive Foundation is a non-profit
501(c)(3) educational corporation organized under the laws of the
state of Mississippi and granted tax exempt status by the Internal
Revenue Service. The Foundation’s EIN or federal tax identification
number is 64-6221541. Contributions to the Project Gutenberg
Literary Archive Foundation are tax deductible to the full extent
permitted by U.S. federal laws and your state’s laws.

The Foundation’s business office is located at 809 North 1500 West,


Salt Lake City, UT 84116, (801) 596-1887. Email contact links and up
to date contact information can be found at the Foundation’s website
and official page at www.gutenberg.org/contact

Section 4. Information about Donations to


the Project Gutenberg Literary Archive
Foundation
Project Gutenberg™ depends upon and cannot survive without
widespread public support and donations to carry out its mission of
increasing the number of public domain and licensed works that can
be freely distributed in machine-readable form accessible by the
widest array of equipment including outdated equipment. Many
small donations ($1 to $5,000) are particularly important to
maintaining tax exempt status with the IRS.

The Foundation is committed to complying with the laws regulating


charities and charitable donations in all 50 states of the United
States. Compliance requirements are not uniform and it takes a
considerable effort, much paperwork and many fees to meet and
keep up with these requirements. We do not solicit donations in
locations where we have not received written confirmation of
compliance. To SEND DONATIONS or determine the status of
compliance for any particular state visit www.gutenberg.org/donate.

While we cannot and do not solicit contributions from states where


we have not met the solicitation requirements, we know of no
prohibition against accepting unsolicited donations from donors in
such states who approach us with offers to donate.

International donations are gratefully accepted, but we cannot make


any statements concerning tax treatment of donations received from
outside the United States. U.S. laws alone swamp our small staff.

Please check the Project Gutenberg web pages for current donation
methods and addresses. Donations are accepted in a number of
other ways including checks, online payments and credit card
donations. To donate, please visit: www.gutenberg.org/donate.

Section 5. General Information About


Project Gutenberg™ electronic works
Professor Michael S. Hart was the originator of the Project
Gutenberg™ concept of a library of electronic works that could be
freely shared with anyone. For forty years, he produced and
distributed Project Gutenberg™ eBooks with only a loose network of
volunteer support.
Project Gutenberg™ eBooks are often created from several printed
editions, all of which are confirmed as not protected by copyright in
the U.S. unless a copyright notice is included. Thus, we do not
necessarily keep eBooks in compliance with any particular paper
edition.

Most people start at our website which has the main PG search
facility: www.gutenberg.org.

This website includes information about Project Gutenberg™,


including how to make donations to the Project Gutenberg Literary
Archive Foundation, how to help produce our new eBooks, and how
to subscribe to our email newsletter to hear about new eBooks.
back
back
back
back
back
back
back
back
back
back
back
back
back
back
back
back
back
Welcome to our website – the ideal destination for book lovers and
knowledge seekers. With a mission to inspire endlessly, we offer a
vast collection of books, ranging from classic literary works to
specialized publications, self-development books, and children's
literature. Each book is a new journey of discovery, expanding
knowledge and enriching the soul of the reade

Our website is not just a platform for buying books, but a bridge
connecting readers to the timeless values of culture and wisdom. With
an elegant, user-friendly interface and an intelligent search system,
we are committed to providing a quick and convenient shopping
experience. Additionally, our special promotions and home delivery
services ensure that you save time and fully enjoy the joy of reading.

Let us accompany you on the journey of exploring knowledge and


personal growth!

ebookfinal.com

You might also like